PledgeLogic.php 8.9 KB

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