PledgeLogic.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\LedgerTokenChangeModel;
  9. use app\common\model\LedgerWalletModel;
  10. use app\common\model\ProductOrder;
  11. use app\common\model\LedgerTeacChangeModel;
  12. use app\common\model\ProductPledges;
  13. //质押抵扣
  14. class PledgeLogic
  15. {
  16. //获取产品信息
  17. public static function getByProductIdList(object $list, string $lan = 'zh')
  18. {
  19. $model = Loader::model('ProductLists');
  20. foreach ($list as &$item) {
  21. $item['product_list'] = $model->whereIn('id', $item->product_id);
  22. if($item->type_id == ProductPledges::Combin) $item['product_list'] = $model->orderRaw('field(id,'. $item->product_id.')');
  23. $item['product_list'] = $model->field('id,thum,'.$lan.'_name as name')->select();
  24. }
  25. return $list;
  26. }
  27. /*
  28. * 获取产品信息
  29. */
  30. public static function getHoldProductList($user_id, $product_id)
  31. {
  32. $model = Loader::model('ProductOrder');
  33. return $model::where('user_id', $user_id)->where('product_id', $product_id)->where('status', $model::Paid)->field('id,order_no')->select();
  34. }
  35. //添加质抵押订单
  36. public static function setPledgeOrder(object $pledge, array $order_no, int $user_id, int $count, string $pay_type, float $price)
  37. {
  38. $model = Loader::model('ProductOrder');
  39. $product= $model::alias('a')
  40. ->join('product_list b', 'a.product_id = b.id', 'left')
  41. ->where('a.user_id', $user_id)
  42. ->where('a.status', $model::Paid)
  43. ->whereIn('a.order_no', $order_no)->field('a.id,b.zh_name,b.thum,a.order_no')->limit($count)->select();
  44. if(empty($product) || count($product) < $count) throw new Exception('订单不存在');
  45. $pledge_num = 1;
  46. if($pledge->type_id == ProductPledges::Single) $pledge_num = $count;
  47. //添加订单
  48. Loader::model('UserPledge')::setPledgeData($user_id, $pledge->id, $pledge->product_id,
  49. $product, //订单信息
  50. $pledge->day_num, $pledge_num);
  51. //扣除支付金额
  52. $action = ($pay_type == Asset::TEAC)? LedgerTeacChangeModel::PledgeFee: LedgerTokenChangeModel::Pledge; //类型
  53. (new LedgerWalletModel())->changeWalletAccount($user_id, $pay_type, -$price, $action, 0);
  54. //修改状态
  55. return $model->whereIn('id', array_column($product, 'id'))->setField('status', $model::Freeze);
  56. }
  57. //获取质抵押订单列表
  58. public static function getPledgeOrderList(int $user_id)//: array
  59. {
  60. $model = Loader::model('UserPledge');
  61. $list = $model::alias('a')
  62. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  63. ->where('a.user_id', $user_id)
  64. ->where('a.status', '>', $model::Close)
  65. ->field('a.*,b.title,b.type_id,b.day_num,b.token,b.teac,b.is_renew')
  66. ->select();
  67. if(empty($list)) throw new Exception('暂无质押订单');
  68. $day = 86400;
  69. $total = 0; //当前累计收益
  70. $growth= 0; //增长累计收益
  71. $time = time();
  72. $result = [];
  73. foreach ($list as $item) {
  74. if($item->status == $model::Remove) {
  75. $total += $item->total_self;
  76. }else{
  77. $reta = bcdiv($item->day_num, $day, 6); //天
  78. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  79. $total += bcmul($reta, $inter, 6)* $item->num; //累计收益
  80. $growth += $reta* $item->num; //增长收益
  81. $result[] = $item;
  82. }
  83. }
  84. return ['total' => $total, 'growth' => $growth, 'list' => $result];
  85. }
  86. //解除质抵押订单
  87. public static function setPledgeRemove(int $pledge_id, string $order_id, int $user_id)
  88. {
  89. $model = Loader::model('UserPledge');
  90. $time = time();
  91. $rows = $model::alias('a')
  92. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  93. ->where('a.user_id', $user_id)
  94. ->where('a.id', $pledge_id)
  95. ->where('a.status', $model::Ongoing)
  96. ->where('a.end_time', '<', $time)
  97. ->field('a.*,b.day_num')
  98. ->find();
  99. if(empty($rows)) throw new Exception('暂无质押订单');
  100. //修改状态
  101. $orderId = explode(',', $order_id);
  102. $detail = array_column(json_decode($rows->details, true), 'id');
  103. $diff = array_diff($detail, $orderId);
  104. $num = $rows->num;
  105. if(empty($diff)) {
  106. $rows->status = $model::Remove;
  107. }else{
  108. $rows->num = count($diff);
  109. $num = count($orderId);
  110. }
  111. $day = 86400;
  112. $total = 0; //当前累计收益
  113. $reta = bcdiv($rows->day_num, $day, 6); //天数
  114. $inter = ($rows->last_time == 0) ? $time - $rows->create_time: $time - $rows->last_time; //最后收取时间
  115. $total = bcmul($reta, $inter, 6) * $num; //累计收益
  116. $rows->total_self= bcadd($total, $rows->total_self, 6);
  117. //修改状态
  118. Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('id', $orderId)->setField('status', ProductOrder::Paid);
  119. //更新领取状态
  120. $rows->last_time = $time;
  121. return $rows->save();
  122. }
  123. //收取质抵押订单列表
  124. //(60*60*24)*(当前时间-最后一次收取时间)
  125. public static function getPledgeCollect(int $user_id)
  126. {
  127. $model = Loader::model('UserPledge');
  128. $time = time();
  129. $list = $model::alias('a')
  130. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  131. ->where('a.user_id', $user_id)
  132. ->where('a.status', '>', $model::Close)
  133. ->where('a.end_time', '<', $time)
  134. ->field('a.*,b.day_num')
  135. ->select();
  136. if(empty($list)) throw new Exception('暂无质押订单');
  137. $day = 86400;
  138. $total = 0; //当前累计收益
  139. foreach ($list as $item)
  140. {
  141. //解冻
  142. if($item->status == $model::Remove) {
  143. $total += $item->total_self;
  144. $item->status = $model::Close; //关闭
  145. }else{
  146. $reta = bcdiv($item->day_num, $day, 6); //天数
  147. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  148. $total_inco = bcmul($reta, $inter, 6) * $item->num; //累计收益
  149. $item->last_time = $time; //收取时间
  150. $item->total_self= bcadd($total_inco, $item->total_self, 2); //累计收益
  151. $total += $total_inco; //累计收益
  152. }
  153. //更新领取状态
  154. $item->save();
  155. }
  156. //更新用户资产
  157. return Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
  158. }
  159. //存储订单续费
  160. public static function setPledgeOrderRenew(object $pledge, int $user_id, string $pay_type)
  161. {
  162. //扣除支付金额
  163. $action = ($pay_type == Asset::TEAC)? LedgerTeacChangeModel::PledgeFee: LedgerTokenChangeModel::Pledge; //类型
  164. (new LedgerWalletModel())->changeWalletAccount($user_id, $pay_type, -$pledge[$pay_type], $action, 0);
  165. $pledge->end_time = $pledge->end_time + config('pledge_end_time');
  166. return $pledge->save();
  167. }
  168. }