PledgeLogic.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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::Ongoing)
  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. $reta = bcdiv($item->day_num, $day, 6); //天
  69. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  70. $total += bcmul($reta, $inter, 6)* $item->num; //累计收益
  71. $growth += $reta* $item->num;
  72. }
  73. return ['total' => $total, 'growth' => $growth, 'list' => $list];
  74. }
  75. //解除质抵押订单
  76. public static function setPledgeRemove(int $pledge_id, int $user_id)
  77. {
  78. $model = Loader::model('UserPledge');
  79. $rows = $model::where('user_id', $user_id)->where('id', $pledge_id)->where('status', $model::Ongoing)->find();
  80. if(empty($rows)) throw new Exception('暂无质押订单');
  81. $day = 86400;
  82. $total = 0; //当前累计收益
  83. $time = time();
  84. $reta = bcdiv($rows->day_num, $day, 6); //天数
  85. $inter = ($rows->last_time == 0) ? $time - $rows->create_time: $time - $rows->last_time; //最后收取时间
  86. $total = bcmul($reta, $inter, 6) * $rows->num; //累计收益
  87. $rows->total_self= bcadd($total, $rows->total_self, 6);
  88. //修改状态
  89. $detail =json_decode($rows->details, true);
  90. Loader::model('ProductOrder')::whereIn('order_no', array_column($detail, 'order_no'))->setField('status', ProductOrder::Paid);
  91. $rows->status = $model::Remove;
  92. $rows->last_time = $time;
  93. return $rows->save();
  94. }
  95. //收取质抵押订单列表
  96. //(60*60*24)*(当前时间-最后一次收取时间)
  97. public static function getPledgeCollect(int $user_id)
  98. {
  99. $model = Loader::model('UserPledge');
  100. $list = $model::where('user_id', $user_id)->where('status', '>', $model::Close)->select();
  101. if(empty($list)) throw new Exception('暂无质押订单');
  102. $day = 86400;
  103. $total = 0; //总前累计收益
  104. $time = time();
  105. foreach ($list as $item)
  106. {
  107. //解冻
  108. if($item->status == $model::Remove) {
  109. $total += $item->total_self;
  110. $item->status = $model::Close; //关闭
  111. }else{
  112. $reta = bcdiv($item->day_num, $day, 6); //天数
  113. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  114. $total_inco = bcmul($reta, $inter, 6) * $item->num; //累计收益
  115. $item->last_time = $time; //收取时间
  116. $item->total_self= bcadd($total_inco, $item->total_self, 2); //累计收益
  117. $total += $total_inco; //累计收益
  118. }
  119. //更新领取状态
  120. $item->save();
  121. }
  122. //更新用户资产
  123. return Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
  124. }
  125. //判断请求限制
  126. public static function getCheckRequestApi(string $key, int $user_id, int $time = 300)
  127. {
  128. $timestampsKey = $key.'_'.$user_id;
  129. $currentTime = time();
  130. $timestamps = Cache::get($timestampsKey, []);
  131. if (count($timestamps) >= 5) {
  132. // 检查最早的记录是否超过5分钟前
  133. $oldestTime = min($timestamps);
  134. if ($currentTime - $oldestTime < $time) { // 5分钟内
  135. return false;
  136. } else {
  137. // 移除最早的记录并添加新的时间戳
  138. $timestamps = array_diff($timestamps, [$oldestTime]);
  139. $timestamps[] = $currentTime;
  140. }
  141. } else {
  142. // 添加新的时间戳
  143. $timestamps[] = $currentTime;
  144. }
  145. Cache::set($timestampsKey, $timestamps, $time); // 设置5分钟过期时间
  146. return true;
  147. }
  148. }