PledgeLogic.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\api\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. //质押抵扣
  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);
  20. if($item->type_id == ProductPledges::Combin) $item['product_list'] = $model->orderRaw('field(id,'. $item->product_id.')');
  21. $item['product_list'] = $model->field('id,thum,'.$lan.'_name as name')->select();
  22. }
  23. return $list;
  24. }
  25. /*
  26. * 获取产品信息
  27. */
  28. public static function getHoldProductList($user_id, $product_id)
  29. {
  30. $model = Loader::model('ProductOrder');
  31. return $model::where('user_id', $user_id)->where('product_id', $product_id)->where('status', $model::Paid)->field('id,order_no')->select();
  32. }
  33. //添加质抵押订单
  34. public static function setPledgeOrder(object $pledge, array $order_no, int $user_id)
  35. {
  36. $model = Loader::model('ProductOrder');
  37. $count = count($order_no);
  38. $product= $model::alias('a')
  39. ->join('product_list b', 'a.product_id = b.id', 'left')
  40. ->where('a.user_id', $user_id)
  41. ->where('a.status', $model::Paid)
  42. ->whereIn('a.order_no', $order_no)->field('a.id,b.zh_name,b.thum,a.order_no')->limit($count)->select();
  43. if(empty($product) || count($product) < $count) throw new Exception('订单不存在');
  44. $pledge_num = 1;
  45. if($pledge->type_id == ProductPledges::Single){
  46. $pledge_num = $count;
  47. }
  48. //添加订单
  49. Loader::model('UserPledge')::setPledgeData($user_id, $pledge->id, $pledge->product_id,
  50. $product, //订单信息
  51. $pledge->day_num, $pledge_num);
  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,b.day_num')
  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::alias('a')
  90. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  91. ->where('a.user_id', $user_id)
  92. ->where('a.id', $pledge_id)->where('a.status', $model::Ongoing)
  93. ->field('a.*,b.day_num')
  94. ->find();
  95. if(empty($rows)) throw new Exception('暂无质押订单');
  96. $day = 86400;
  97. $total = 0; //当前累计收益
  98. $time = time();
  99. $reta = bcdiv($rows->day_num, $day, 6); //天数
  100. $inter = ($rows->last_time == 0) ? $time - $rows->create_time: $time - $rows->last_time; //最后收取时间
  101. $total = bcmul($reta, $inter, 6) * $rows->num; //累计收益
  102. $rows->total_self= bcadd($total, $rows->total_self, 6);
  103. //修改状态
  104. $detail =json_decode($rows->details, true);
  105. Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('id', array_column($detail, 'id'))->setField('status', ProductOrder::Paid);
  106. $rows->status = $model::Remove;
  107. $rows->last_time = $time;
  108. return $rows->save();
  109. }
  110. //收取质抵押订单列表
  111. //(60*60*24)*(当前时间-最后一次收取时间)
  112. public static function getPledgeCollect(int $user_id)
  113. {
  114. $model = Loader::model('UserPledge');
  115. $list = $model::alias('a')
  116. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  117. ->where('a.user_id', $user_id)
  118. ->where('a.status', '>', $model::Close)
  119. ->field('a.*,b.day_num')
  120. ->select();
  121. if(empty($list)) throw new Exception('暂无质押订单');
  122. $day = 86400;
  123. $total = 0; //当前累计收益
  124. $time = time();
  125. foreach ($list as $item)
  126. {
  127. //解冻
  128. if($item->status == $model::Remove) {
  129. $total += $item->total_self;
  130. $item->status = $model::Close; //关闭
  131. }else{
  132. $reta = bcdiv($item->day_num, $day, 6); //天数
  133. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  134. $total_inco = bcmul($reta, $inter, 6) * $item->num; //累计收益
  135. $item->last_time = $time; //收取时间
  136. $item->total_self= bcadd($total_inco, $item->total_self, 2); //累计收益
  137. $total += $total_inco; //累计收益
  138. }
  139. //更新领取状态
  140. $item->save();
  141. }
  142. //更新用户资产
  143. return Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
  144. }
  145. }