PledgeLogic.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. $result = [];
  74. foreach ($list as $item) {
  75. if($item->end_time > $time){
  76. if($item->status == $model::Remove) {
  77. $total += $item->total_self;
  78. }else{
  79. $reta = bcdiv($item->day_num, $day, 6); //天
  80. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  81. $total += bcmul($reta, $inter, 6)* $item->num; //累计收益
  82. $growth += $reta* $item->num; //增长收益
  83. $result[] = $item;
  84. }
  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, $team_level_id, $address_level)
  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,b.type_id')
  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. if($rows->type_id == 2) CommonLogic::setIsOuLevelSave($user_id, $team_level_id, $address_level);
  127. //更新领取状态
  128. $rows->last_time = $time;
  129. return $rows->save();
  130. }
  131. //收取质抵押订单列表:(60*60*24)*(当前时间-最后一次收取时间)
  132. public static function getPledgeCollect(int $user_id)
  133. {
  134. $model = Loader::model('UserPledge');
  135. $time = time();
  136. $list = $model::alias('a')
  137. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  138. ->where('a.user_id', $user_id)
  139. ->where('a.status', '>', $model::Close)
  140. ->where('a.end_time', '>', $time)
  141. ->field('a.*,b.day_num')
  142. ->select();
  143. if(empty($list)) throw new Exception('暂无质押订单');
  144. $day = 86400;
  145. $total = 0; //当前累计收益
  146. foreach ($list as $item) {
  147. //解冻
  148. if($item->status == $model::Remove) {
  149. $total += $item->total_self;
  150. $item->status = $model::Close; //关闭
  151. }else{
  152. $reta = bcdiv($item->day_num, $day, 6); //天数
  153. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  154. $total_inco = bcmul($reta, $inter, 6) * $item->num; //累计收益
  155. $item->last_time = $time; //收取时间
  156. $item->total_self= bcadd($total_inco, $item->total_self, 2); //累计收益
  157. $total += $total_inco; //累计收益
  158. }
  159. //更新领取状态
  160. $item->save();
  161. }
  162. //等级分佣
  163. CommonLogic::setTeamLevelIncome($user_id, $total, Asset::TEAC, LedgerTeacChangeModel::TeamLevel);
  164. //更新用户资产
  165. return Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
  166. }
  167. //存储订单续费
  168. public static function setPledgeOrderRenew(object $pledge, int $user_id, string $pay_type)
  169. {
  170. //扣除支付金额
  171. self::setPledgeFee($user_id, $pay_type, $pledge[$pay_type]);
  172. $pledge->end_time = $pledge->end_time + config('pledge_end_time');
  173. return $pledge->save();
  174. }
  175. //添加用户存储产品
  176. public static function setPledgeProductAdd(object $pledge, int $user_id, int $count, array $order_id)
  177. {
  178. $product = self::getOrderProductList($user_id, $order_id, $count);
  179. if(empty($product) || count($product) < $count) throw new Exception('订单不存在');
  180. //修改订单状态
  181. Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('id', $order_id)->setField('status', ProductOrder::Freeze);
  182. //添加产品
  183. $detail = json_decode($pledge->details, true);
  184. $pledge->details = json_encode(array_merge($detail, $product), JSON_UNESCAPED_UNICODE);
  185. $pledge->num = $pledge->num + $count ;
  186. return $pledge->save();
  187. }
  188. //判断用户余额
  189. public static function isUserBalance(object $user, int $pay_type, string $pay)
  190. {
  191. $result = true;
  192. if ($pay_type == 1 && bcadd($user['token'], $user['frozen'], 6) < $pay) $result = false;
  193. if ($pay_type == 2 && $user['teac'] < $pay) $result = false;
  194. return $result;
  195. }
  196. //扣除存储手续费
  197. private static function setPledgeFee(int $user_id, string $pay_type, float $price)
  198. {
  199. if( empty($price)) return true;
  200. //扣除支付金额
  201. if ($pay_type == Asset::TEAC) {
  202. Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, $pay_type, -$price, LedgerTeacChangeModel::PledgeFee, 0);
  203. }else{
  204. Loader::model('LedgerWalletModel')->setChangeFrozen($user_id, $price, LedgerFrozenChangeModel::Pledge, LedgerTokenChangeModel::Pledge, '-');
  205. }
  206. }
  207. //获取订单产品
  208. private static function getOrderProductList(int $user_id, array $order_id, int $count)
  209. {
  210. return Loader::model('ProductOrder')::alias('a')
  211. ->join('product_list b', 'a.product_id = b.id', 'left')
  212. ->where('a.user_id', $user_id)
  213. ->where('a.status', ProductOrder::Paid)
  214. ->whereIn('a.id', $order_id)->field('a.id,b.zh_name,b.thum,a.order_no')->select();
  215. }
  216. }