PledgeLogic.php 8.7 KB

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