PledgeLogic.php 8.7 KB

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