PledgeLogic.php 6.6 KB

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