PledgeLogic.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\common\logic;
  3. use Exception;
  4. use think\Env;
  5. use think\Cache;
  6. use think\Loader ;
  7. use app\common\model\UserPledge;
  8. use app\common\model\ProductPledges;
  9. class PledgeLogic
  10. {
  11. //获取产品信息 user_pledge
  12. public static function getByProductIdList(object $list, string $lan = 'zh')
  13. {
  14. $model = Loader::model('ProductLists');
  15. foreach ($list as &$item) {
  16. $item['product_list'] = $model->whereIn('id', $item->product_id)->column('id,thum,'.$lan.'_name as name');
  17. }
  18. return $list;
  19. }
  20. /*
  21. * 获取产品信息
  22. */
  23. public static function getHoldProductList($user_id, $product_id)
  24. {
  25. $model = Loader::model('ProductOrder');
  26. return $model::where('user_id', $user_id)->where('product_id', $product_id)->where('status', $model::Paid)->field('id,order_no')->select();
  27. }
  28. //添加质抵押订单
  29. public static function setPledgeOrder(object $pledge, array $order_no, int $user_id)
  30. {
  31. $model = Loader::model('ProductOrder');
  32. $order = $model::where('user_id', $user_id)->whereIn('order_no', $order_no)->select();
  33. if(empty($order) || count($order) != count($order_no)) throw new Exception('订单不存在');
  34. $pledge_num = 1;
  35. if($pledge->type_id == ProductPledges::Single){
  36. $pledge_num = count($order);
  37. }
  38. //添加订单
  39. Loader::model('UserPledge')::setPledgeData($user_id, $pledge->id, $pledge->product_id, $pledge->day_num, $pledge_num);
  40. //修改状态
  41. return $model->whereIn('order_no', $order_no)->setField('status', $model::Freeze);
  42. }
  43. //获取质抵押订单列表
  44. //(60*60*24)*(当前时间-最后一次收取时间)
  45. public static function getPledgeOrderList(int $user_id)
  46. {
  47. $model = Loader::model('UserPledge');
  48. $list = $model::where('user_id', $user_id)->where('status', $model::Ongoing)->select();
  49. if(empty($list)) throw new Exception('暂无质押订单');
  50. $day = 86400;
  51. $total = 0; //当前累计收益
  52. $growth= 0; //增长累计收益
  53. $time = time();
  54. foreach ($list as $item) {
  55. $reta = bcdiv($item->day_num, $day, 2); //天数
  56. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  57. $total += bcmul($reta, $inter, 2) * $item->num; //累计收益
  58. $growth += $reta* $item->num;
  59. }
  60. return ['total' => $total, 'growth' => $growth, 'list' => $list];
  61. }
  62. //解除质抵押订单
  63. public static function setPledgeRemove(int $pledge_id,int $user_id)
  64. {
  65. $model = Loader::model('UserPledge');
  66. $rows = $model::where('user_id', $user_id)->where('id', $pledge_id)->where('status', $model::Ongoing)->find();
  67. if(empty($rows)) throw new Exception('暂无质押订单');
  68. $day = 86400;
  69. $total = 0; //当前累计收益
  70. $time = time();
  71. $reta = bcdiv($rows->day_num, $day, 2); //天数
  72. $inter = ($rows->last_time == 0) ? $time - $rows->create_time: $time - $rows->last_time; //最后收取时间
  73. $total = bcmul($reta, $inter, 2) * $rows->num; //累计收益
  74. $rows->total_self= bcadd($total, $rows->total_self, 2);
  75. $rows->status = $model::Remove;
  76. $rows->last_time = $time;
  77. return $rows->save();
  78. }
  79. //判断请求限制
  80. public static function getCheckRequestApi(string $key, int $user_id, int $time = 300)
  81. {
  82. $timestampsKey = $key.'_'.$user_id;
  83. $currentTime = time();
  84. $timestamps = Cache::get($timestampsKey, []);
  85. if (count($timestamps) >= 5) {
  86. // 检查最早的记录是否超过5分钟前
  87. $oldestTime = min($timestamps);
  88. if ($currentTime - $oldestTime < $time) { // 5分钟内
  89. return false;
  90. } else {
  91. // 移除最早的记录并添加新的时间戳
  92. $timestamps = array_diff($timestamps, [$oldestTime]);
  93. $timestamps[] = $currentTime;
  94. }
  95. } else {
  96. // 添加新的时间戳
  97. $timestamps[] = $currentTime;
  98. }
  99. Cache::set($timestampsKey, $timestamps, $time); // 设置5分钟过期时间
  100. return true;
  101. }
  102. }