PledgeLogic.php 6.1 KB

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