PledgeLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace app\api\logic;
  3. use Exception;
  4. use think\Loader ;
  5. use fast\Asset;
  6. use app\common\model\LedgerFrozenChangeModel;
  7. use app\common\model\LedgerTokenChangeModel;
  8. use app\common\model\ProductLists;
  9. use app\common\model\ProductOrder;
  10. use app\common\model\LedgerTeacChangeModel;
  11. use app\common\model\ProductPledges;
  12. //质押抵扣
  13. class PledgeLogic
  14. {
  15. //获取产品信息
  16. public static function getByProductIdList(object $list, string $lan = 'zh')
  17. {
  18. $model = Loader::model('ProductLists');
  19. foreach ($list as &$item) {
  20. $item['product_list'] = $model->whereIn('id', $item->product_id);
  21. if($item->type_id == ProductPledges::Combin) $item['product_list'] = $model->orderRaw('field(id,'. $item->product_id.')');
  22. $item['product_list'] = $model->field('id,thum,'.$lan.'_name as name')->select();
  23. $item['day_num'] = round($item['day_num'], 2);
  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::alias('a')
  34. ->join('product_list b', 'a.product_id = b.id', 'left')
  35. ->where('b.is_show', ProductLists::Normal)
  36. ->where('a.user_id', $user_id)
  37. ->where('a.product_id', $product_id)
  38. ->where('a.status', $model::Paid)
  39. ->field('a.id,a.order_no')->select();
  40. }
  41. //添加质抵押订单
  42. public static function setPledgeOrder(object $pledge, array $order_id, int $user_id, int $count, string $pay_type, float $price)
  43. {
  44. $product= self::getOrderProductList($user_id, $order_id, $count);
  45. if(empty($product) || count($product) < $count) throw new Exception('订单不存在');
  46. //数量
  47. $pledge_num = ($pledge->type_id == ProductPledges::Single)? $count: 1;
  48. //添加订单
  49. Loader::model('UserPledge')::setPledgeData($user_id, $pledge->id, $pledge->product_id,
  50. $product, //订单信息
  51. $pledge->day_num, $pledge_num);
  52. self::setPledgeFee($user_id, $pay_type, $price);
  53. //修改状态
  54. return Loader::model('ProductOrder')->whereIn('id', $order_id)->setField('status', ProductOrder::Freeze);
  55. }
  56. //获取质抵押订单列表
  57. public static function getPledgeOrderList(int $user_id)//: array
  58. {
  59. $model = Loader::model('UserPledge');
  60. $time = time();
  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. $result = [];
  72. foreach ($list as $item) {
  73. if($item->end_time > $time){
  74. if($item->status == $model::Remove) {
  75. $total += $item->total_self;
  76. }else{
  77. $reta = bcdiv($item->day_num, $day, 8); //天
  78. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  79. $total += bcmul($reta, $inter, 6)* $item->num; //累计收益
  80. $growth = bcadd($growth, bcmul($reta, $item->num, 6), 6); //增长收益
  81. }
  82. }
  83. if($item->status != $model::Remove) $result[] = $item;
  84. }
  85. return ['total' => $total, 'growth' => $growth, 'list' => $result];
  86. }
  87. //解除质抵押订单
  88. public static function setPledgeRemove(int $pledge_id, string $order_id, int $user_id, $team_level_id, $address_level)
  89. {
  90. $model = Loader::model('UserPledge');
  91. $time = time();
  92. $rows = $model::alias('a')
  93. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  94. ->where('a.user_id', $user_id)
  95. ->where('a.id', $pledge_id)
  96. ->where('a.status', $model::Ongoing)
  97. ->field('a.*,b.day_num,b.type_id')
  98. ->find();
  99. if(empty($rows)) throw new Exception('暂无质押订单');
  100. $orderId = explode(',', $order_id);
  101. $detail = json_decode($rows->details, true);
  102. $count = count($orderId);
  103. $num = $rows->num;
  104. if(count($orderId) == count($detail)) {
  105. $rows->status = $model::Remove;
  106. }else{
  107. $result = [];
  108. foreach ($detail as &$item)
  109. if(!in_array($item['id'], $orderId)) $result[] = $item;
  110. $num = $count;
  111. $rows->num = count($result);
  112. $rows->details = json_encode($result, JSON_UNESCAPED_UNICODE);
  113. }
  114. $day = 86400;
  115. $reta = bcdiv($rows->day_num, $day, 8); //天数
  116. $total = 0;
  117. //到期解除收取
  118. if($time > $rows->end_time){
  119. $inter = ($rows->last_time == 0) ? $rows->end_time - $rows->create_time: $rows->end_time - $rows->last_time; //最后收取时间
  120. $rows->total_self= bcmul($reta, $inter, 6) * $num; //累计收益
  121. $rows->status = $model::Close; //更新领取状态
  122. Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, Asset::TEAC, $rows->total_self, LedgerTeacChangeModel::Pledge, 0);
  123. }else{
  124. $inter = ($rows->last_time == 0) ? $time - $rows->create_time: $time - $rows->last_time; //最后收取时间
  125. $total = bcmul($reta, $inter, 6) * $num; //累计收益
  126. $rows->total_self= ($rows->status == $model::Remove)? $total: bcadd($total, $rows->total_self, 6);
  127. }
  128. //修改状态
  129. Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('id', $orderId)->setField('status', ProductOrder::Paid);
  130. //等级
  131. if($rows->type_id == 2) CommonLogic::setIsOuLevelSave($user_id, $team_level_id);
  132. $rows->last_time = $time;
  133. return $rows->save();
  134. }
  135. //收取质抵押订单列表:(60*60*24)*(当前时间-最后一次收取时间)
  136. public static function getPledgeCollect(int $user_id)
  137. {
  138. $model = Loader::model('UserPledge');
  139. $time = time();
  140. $list = $model::alias('a')
  141. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  142. ->where('a.user_id', $user_id)
  143. ->where('a.status', '>', $model::Close)
  144. ->where('a.end_time', '>', $time)
  145. ->field('a.*,b.day_num')
  146. ->select();
  147. if(empty($list)) throw new Exception('暂无质押订单');
  148. $day = 86400;
  149. $total = 0; //当前累计收益
  150. foreach ($list as $item) {
  151. //解冻
  152. if($item->status == $model::Remove) {
  153. $total += $item->total_self;
  154. $item->status = $model::Close; //关闭
  155. }else{
  156. $reta = bcdiv($item->day_num, $day, 8); //天数
  157. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  158. $total_inco = bcmul($reta, $inter, 6) * $item->num; //累计收益
  159. $item->last_time = $time; //收取时间
  160. $item->total_self= bcadd($total_inco, $item->total_self, 2); //累计收益
  161. $total += $total_inco; //累计收益
  162. }
  163. //更新领取状态
  164. $item->save();
  165. }
  166. //等级分佣
  167. CommonLogic::setTeamLevelIncome($user_id, $total, Asset::TEAC, LedgerTeacChangeModel::TeamLevel);
  168. //更新用户资产
  169. return Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
  170. }
  171. //存储订单续费
  172. public static function setPledgeOrderRenew(object $pledge, int $user_id, string $pay_type)
  173. {
  174. //扣除支付金额
  175. self::setPledgeFee($user_id, $pay_type, $pledge[$pay_type]);
  176. $pledge->end_time = $pledge->end_time + config('pledge_end_time');
  177. return $pledge->save();
  178. }
  179. //添加用户存储产品
  180. public static function setPledgeProductAdd(object $pledge, int $user_id, int $count, array $order_id)
  181. {
  182. $product = self::getOrderProductList($user_id, $order_id, $count);
  183. if(empty($product) || count($product) < $count) throw new Exception('订单不存在');
  184. //修改订单状态
  185. Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('id', $order_id)->setField('status', ProductOrder::Freeze);
  186. //添加产品
  187. $detail = json_decode($pledge->details, true);
  188. $pledge->details = json_encode(array_merge($detail, $product), JSON_UNESCAPED_UNICODE);
  189. $pledge->num = $pledge->num + $count ;
  190. return $pledge->save();
  191. }
  192. //判断用户余额
  193. public static function isUserBalance(object $user, int $pay_type, string $pay)
  194. {
  195. $result = true;
  196. if ($pay_type == 1 && bcadd($user['token'], $user['frozen'], 6) < $pay) $result = false;
  197. if ($pay_type == 2 && $user['teac'] < $pay) $result = false;
  198. return $result;
  199. }
  200. //扣除存储手续费
  201. private static function setPledgeFee(int $user_id, string $pay_type, float $price)
  202. {
  203. if( empty($price)) return true;
  204. //扣除支付金额
  205. if ($pay_type == Asset::TEAC) {
  206. Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, $pay_type, -$price, LedgerTeacChangeModel::PledgeFee, 0);
  207. }else{
  208. Loader::model('LedgerWalletModel')->setChangeFrozen($user_id, $price, LedgerFrozenChangeModel::Pledge, LedgerTokenChangeModel::Pledge, '-');
  209. }
  210. }
  211. //获取订单产品
  212. private static function getOrderProductList(int $user_id, array $order_id, int $count)
  213. {
  214. return Loader::model('ProductOrder')::alias('a')
  215. ->join('product_list b', 'a.product_id = b.id', 'left')
  216. ->where('a.user_id', $user_id)
  217. ->where('a.status', ProductOrder::Paid)
  218. ->whereIn('a.id', $order_id)->field('a.id,b.zh_name,b.thum,a.order_no')->select();
  219. }
  220. }