PledgeLogic.php 7.1 KB

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