PledgeLogic.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, 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. $day = 86400;
  101. $total = 0; //当前累计收益
  102. $reta = bcdiv($rows->day_num, $day, 6); //天数
  103. $inter = ($rows->last_time == 0) ? $time - $rows->create_time: $time - $rows->last_time; //最后收取时间
  104. $total = bcmul($reta, $inter, 6) * $rows->num; //累计收益
  105. $rows->total_self= bcadd($total, $rows->total_self, 6);
  106. //修改状态
  107. $detail =json_decode($rows->details, true);
  108. Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('id', array_column($detail, 'id'))->setField('status', ProductOrder::Paid);
  109. $rows->status = $model::Remove;
  110. $rows->last_time = $time;
  111. return $rows->save();
  112. }
  113. //收取质抵押订单列表
  114. //(60*60*24)*(当前时间-最后一次收取时间)
  115. public static function getPledgeCollect(int $user_id)
  116. {
  117. $model = Loader::model('UserPledge');
  118. $time = time();
  119. $list = $model::alias('a')
  120. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  121. ->where('a.user_id', $user_id)
  122. ->where('a.status', '>', $model::Close)
  123. ->where('a.end_time', '<', $time)
  124. ->field('a.*,b.day_num')
  125. ->select();
  126. if(empty($list)) throw new Exception('暂无质押订单');
  127. $day = 86400;
  128. $total = 0; //当前累计收益
  129. foreach ($list as $item)
  130. {
  131. //解冻
  132. if($item->status == $model::Remove) {
  133. $total += $item->total_self;
  134. $item->status = $model::Close; //关闭
  135. }else{
  136. $reta = bcdiv($item->day_num, $day, 6); //天数
  137. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  138. $total_inco = bcmul($reta, $inter, 6) * $item->num; //累计收益
  139. $item->last_time = $time; //收取时间
  140. $item->total_self= bcadd($total_inco, $item->total_self, 2); //累计收益
  141. $total += $total_inco; //累计收益
  142. }
  143. //更新领取状态
  144. $item->save();
  145. }
  146. //更新用户资产
  147. return Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
  148. }
  149. }