PledgeLogic.php 8.8 KB

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