PledgeLogic.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\common\logic;
  3. use Exception;
  4. use think\Env;
  5. use think\Cache;
  6. use think\Loader ;
  7. use fast\Asset;
  8. use app\common\model\ProductOrder;
  9. use app\common\model\LedgerTeacChangeModel;
  10. use app\common\model\ProductPledges;
  11. class PledgeLogic
  12. {
  13. //获取产品信息
  14. public static function getByProductIdList(object $list, string $lan = 'zh')
  15. {
  16. $model = Loader::model('ProductLists');
  17. foreach ($list as &$item) {
  18. $item['product_list'] = $model->whereIn('id', $item->product_id)->column('id,thum,'.$lan.'_name as name');
  19. }
  20. return $list;
  21. }
  22. /*
  23. * 获取产品信息
  24. */
  25. public static function getHoldProductList($user_id, $product_id)
  26. {
  27. $model = Loader::model('ProductOrder');
  28. return $model::where('user_id', $user_id)->where('product_id', $product_id)->where('status', $model::Paid)->field('id,order_no')->select();
  29. }
  30. //添加质抵押订单
  31. public static function setPledgeOrder(object $pledge, array $order_no, int $user_id)
  32. {
  33. $model = Loader::model('ProductOrder');
  34. $product= $model::alias('a')
  35. ->join('product_list b', 'a.product_id = b.id', 'left')
  36. ->where('a.user_id', $user_id)
  37. ->where('a.status', $model::Paid)
  38. ->whereIn('a.order_no', $order_no)->field('b.id,b.zh_name,b.thum,a.order_no')->select();
  39. if(empty($product) || count($product) != count($order_no)) throw new Exception('订单不存在');
  40. $pledge_num = 1;
  41. if($pledge->type_id == ProductPledges::Single){
  42. $pledge_num = count($order_no);
  43. }
  44. //添加订单
  45. Loader::model('UserPledge')::setPledgeData($user_id, $pledge->id, $pledge->product_id,
  46. $product, //订单信息
  47. $pledge->day_num, $pledge_num);
  48. //修改状态
  49. return $model->whereIn('order_no', $order_no)->setField('status', $model::Freeze);
  50. }
  51. //获取质抵押订单列表
  52. //(60*60*24)*(当前时间-最后一次收取时间)
  53. public static function getPledgeOrderList(int $user_id)
  54. {
  55. $model = Loader::model('UserPledge');
  56. $list = $model::alias('a')
  57. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  58. ->where('a.user_id', $user_id)
  59. ->where('a.status', '>', $model::Close)
  60. ->field('a.*,b.title')
  61. ->select();
  62. if(empty($list)) throw new Exception('暂无质押订单');
  63. $day = 86400;
  64. $total = 0; //当前累计收益
  65. $growth= 0; //增长累计收益
  66. $time = time();
  67. foreach ($list as &$item) {
  68. if($item->status == $model::Remove) {
  69. $total += $item->total_self;
  70. unset($item);
  71. }else{
  72. $reta = bcdiv($item->day_num, $day, 6); //天
  73. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  74. $total += bcmul($reta, $inter, 6)* $item->num; //累计收益
  75. $growth += $reta* $item->num; //增长收益
  76. }
  77. }
  78. return ['total' => $total, 'growth' => $growth, 'list' => $list];
  79. }
  80. //解除质抵押订单
  81. public static function setPledgeRemove(int $pledge_id, int $user_id)
  82. {
  83. $model = Loader::model('UserPledge');
  84. $rows = $model::where('user_id', $user_id)->where('id', $pledge_id)->where('status', $model::Ongoing)->find();
  85. if(empty($rows)) throw new Exception('暂无质押订单');
  86. $day = 86400;
  87. $total = 0; //当前累计收益
  88. $time = time();
  89. $reta = bcdiv($rows->day_num, $day, 6); //天数
  90. $inter = ($rows->last_time == 0) ? $time - $rows->create_time: $time - $rows->last_time; //最后收取时间
  91. $total = bcmul($reta, $inter, 6) * $rows->num; //累计收益
  92. $rows->total_self= bcadd($total, $rows->total_self, 6);
  93. //修改状态
  94. $detail =json_decode($rows->details, true);
  95. Loader::model('ProductOrder')::whereIn('order_no', array_column($detail, 'order_no'))->setField('status', ProductOrder::Paid);
  96. $rows->status = $model::Remove;
  97. $rows->last_time = $time;
  98. return $rows->save();
  99. }
  100. //收取质抵押订单列表
  101. //(60*60*24)*(当前时间-最后一次收取时间)
  102. public static function getPledgeCollect(int $user_id)
  103. {
  104. $model = Loader::model('UserPledge');
  105. $list = $model::where('user_id', $user_id)->where('status', '>', $model::Close)->select();
  106. if(empty($list)) throw new Exception('暂无质押订单');
  107. $day = 86400;
  108. $total = 0; //当前累计收益
  109. $time = time();
  110. foreach ($list as $item)
  111. {
  112. //解冻
  113. if($item->status == $model::Remove) {
  114. $total += $item->total_self;
  115. $item->status = $model::Close; //关闭
  116. }else{
  117. $reta = bcdiv($item->day_num, $day, 6); //天数
  118. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  119. $total_inco = bcmul($reta, $inter, 6) * $item->num; //累计收益
  120. $item->last_time = $time; //收取时间
  121. $item->total_self= bcadd($total_inco, $item->total_self, 2); //累计收益
  122. $total += $total_inco; //累计收益
  123. }
  124. //更新领取状态
  125. $item->save();
  126. }
  127. //更新用户资产
  128. return Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
  129. }
  130. //判断请求限制
  131. public static function getCheckRequestApi(string $key, int $user_id, int $time = 300)
  132. {
  133. $timestampsKey = $key.'_'.$user_id;
  134. $currentTime = time();
  135. $timestamps = Cache::get($timestampsKey, []);
  136. if (count($timestamps) >= 5) {
  137. // 检查最早的记录是否超过5分钟前
  138. $oldestTime = min($timestamps);
  139. if ($currentTime - $oldestTime < $time) { // 5分钟内
  140. return false;
  141. } else {
  142. // 移除最早的记录并添加新的时间戳
  143. $timestamps = array_diff($timestamps, [$oldestTime]);
  144. $timestamps[] = $currentTime;
  145. }
  146. } else {
  147. // 添加新的时间戳
  148. $timestamps[] = $currentTime;
  149. }
  150. Cache::set($timestampsKey, $timestamps, $time); // 设置5分钟过期时间
  151. return true;
  152. }
  153. }