PledgeLogic.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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\LedgerFrozenChangeModel;
  9. use app\common\model\LedgerTokenChangeModel;
  10. use app\common\model\ProductLists;
  11. use app\common\model\ProductOrder;
  12. use app\common\model\LedgerTeacChangeModel;
  13. use app\common\model\ProductPledges;
  14. //质押抵扣
  15. class PledgeLogic
  16. {
  17. //获取产品信息
  18. public static function getByProductIdList(object $list, string $lan = 'zh')
  19. {
  20. $model = Loader::model('ProductLists');
  21. foreach ($list as &$item) {
  22. $item['product_list'] = $model->whereIn('id', $item->product_id);
  23. if($item->type_id == ProductPledges::Combin) $item['product_list'] = $model->orderRaw('field(id,'. $item->product_id.')');
  24. $item['product_list'] = $model->field('id,thum,'.$lan.'_name as name')->select();
  25. $item['day_num'] = round($item['day_num'], 2);
  26. }
  27. return $list;
  28. }
  29. /*
  30. * 获取产品信息
  31. */
  32. public static function getHoldProductList($user_id, $product_id)
  33. {
  34. $model = Loader::model('ProductOrder');
  35. return $model::alias('a')
  36. ->join('product_list b', 'a.product_id = b.id', 'left')
  37. ->where('b.is_show', ProductLists::Normal)
  38. ->where('a.user_id', $user_id)
  39. ->where('a.product_id', $product_id)
  40. ->where('a.status', $model::Paid)
  41. ->field('a.id,a.order_no')->select();
  42. }
  43. //添加质抵押订单
  44. public static function setPledgeOrder(object $pledge, array $order_id, int $user_id, int $count, string $pay_type, float $price)
  45. {
  46. $product= self::getOrderProductList($user_id, $order_id, $count);
  47. if(empty($product) || count($product) < $count) throw new Exception('订单不存在');
  48. //数量
  49. $pledge_num = ($pledge->type_id == ProductPledges::Single)? $count: 1;
  50. //添加订单
  51. Loader::model('UserPledge')::setPledgeData($user_id, $pledge->id, $pledge->product_id,
  52. $product, //订单信息
  53. $pledge->day_num, $pledge_num);
  54. self::setPledgeFee($user_id, $pay_type, $price);
  55. //修改状态
  56. return Loader::model('ProductOrder')->whereIn('id', $order_id)->setField('status', ProductOrder::Freeze);
  57. }
  58. //获取质抵押订单列表
  59. public static function getPledgeOrderList(int $user_id)//: array
  60. {
  61. $model = Loader::model('UserPledge');
  62. $time = time();
  63. $list = $model::alias('a')
  64. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  65. ->where('a.user_id', $user_id)
  66. ->where('a.status', '>', $model::Close)
  67. ->field('a.*,b.title,b.type_id,b.day_num,b.token,b.teac,b.is_renew')
  68. ->select();
  69. if(empty($list)) throw new Exception('暂无存储订单');
  70. $day = 86400;
  71. $total = 0; //当前累计收益
  72. $growth= 0; //增长累计收益
  73. foreach ($list as $item) {
  74. if($item->end_time > $time){
  75. if($item->status == $model::Remove) {
  76. $total += $item->total_self;
  77. }else{
  78. $reta = bcdiv($item->day_num, $day, 6); //天
  79. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  80. $total += bcmul($reta, $inter, 6)* $item->num; //累计收益
  81. $growth += $reta* $item->num; //增长收益
  82. }
  83. }
  84. }
  85. return ['total' => $total, 'growth' => $growth, 'list' => $list];
  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. ->where('a.end_time', '>', $time)
  98. ->field('a.*,b.day_num,b.type_id')
  99. ->find();
  100. if(empty($rows)) throw new Exception('暂无质押订单');
  101. $orderId = explode(',', $order_id);
  102. $detail = json_decode($rows->details, true);
  103. $count = count($orderId);
  104. $num = $rows->num;
  105. if(count($orderId) == count($detail)) {
  106. $rows->status = $model::Remove;
  107. }else{
  108. $result = [];
  109. foreach ($detail as &$item)
  110. if(!in_array($item['id'], $orderId)) $result[] = $item;
  111. $num = $count;
  112. $rows->num = count($result);
  113. $rows->details = json_encode($result, JSON_UNESCAPED_UNICODE);
  114. }
  115. $day = 86400;
  116. $total = 0; //当前累计收益
  117. $reta = bcdiv($rows->day_num, $day, 6); //天数
  118. $inter = ($rows->last_time == 0) ? $time - $rows->create_time: $time - $rows->last_time; //最后收取时间
  119. $total = bcmul($reta, $inter, 6) * $num; //累计收益
  120. $rows->total_self= bcadd($total, $rows->total_self, 6);
  121. //修改状态
  122. Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('id', $orderId)->setField('status', ProductOrder::Paid);
  123. //等级
  124. if($rows->type_id == 2) CommonLogic::setIsOuLevelSave($user_id, $team_level_id, $address_level);
  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. CommonLogic::setTeamLevelIncome($user_id, $total, Asset::TEAC, LedgerTeacChangeModel::TeamLevel);
  162. //更新用户资产
  163. return Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
  164. }
  165. //存储订单续费
  166. public static function setPledgeOrderRenew(object $pledge, int $user_id, string $pay_type)
  167. {
  168. //扣除支付金额
  169. self::setPledgeFee($user_id, $pay_type, $pledge[$pay_type]);
  170. $pledge->end_time = $pledge->end_time + config('pledge_end_time');
  171. return $pledge->save();
  172. }
  173. //添加用户存储产品
  174. public static function setPledgeProductAdd(object $pledge, int $user_id, int $count, array $order_id)
  175. {
  176. $product = self::getOrderProductList($user_id, $order_id, $count);
  177. if(empty($product) || count($product) < $count) throw new Exception('订单不存在');
  178. //修改订单状态
  179. Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('id', $order_id)->setField('status', ProductOrder::Freeze);
  180. //添加产品
  181. $detail = json_decode($pledge->details, true);
  182. $pledge->details = json_encode(array_merge($detail, $product), JSON_UNESCAPED_UNICODE);
  183. $pledge->num = $pledge->num + $count ;
  184. return $pledge->save();
  185. }
  186. //判断用户余额
  187. public static function isUserBalance(object $user, int $pay_type, string $pay)
  188. {
  189. $result = true;
  190. if ($pay_type == 1 && bcadd($user['token'], $user['frozen'], 6) < $pay) $result = false;
  191. if ($pay_type == 2 && $user['teac'] < $pay) $result = false;
  192. return $result;
  193. }
  194. //扣除存储手续费
  195. private static function setPledgeFee(int $user_id, string $pay_type, float $price)
  196. {
  197. if( empty($price)) return true;
  198. //扣除支付金额
  199. if ($pay_type == Asset::TEAC) {
  200. Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, $pay_type, -$price, LedgerTeacChangeModel::PledgeFee, 0);
  201. }else{
  202. Loader::model('LedgerWalletModel')->setChangeFrozen($user_id, $price, LedgerFrozenChangeModel::Pledge, LedgerTokenChangeModel::Pledge, '-');
  203. }
  204. }
  205. //获取订单产品
  206. private static function getOrderProductList(int $user_id, array $order_id, int $count)
  207. {
  208. return Loader::model('ProductOrder')::alias('a')
  209. ->join('product_list b', 'a.product_id = b.id', 'left')
  210. ->where('a.user_id', $user_id)
  211. ->where('a.status', ProductOrder::Paid)
  212. ->whereIn('a.id', $order_id)->field('a.id,b.zh_name,b.thum,a.order_no')->select();
  213. }
  214. }