PledgeLogic.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 $key=>$item) {
  74. if($item->end_time > $time){
  75. if($item->status == $model::Remove) {
  76. $total += $item->total_self;
  77. unset($list[$key]);
  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 = bcadd($growth, bcmul($reta, $item->num, 6), 6); //增长收益
  83. }
  84. }
  85. }
  86. return ['total' => $total, 'growth' => $growth, 'list' => $list];
  87. }
  88. //解除质抵押订单
  89. public static function setPledgeRemove(int $pledge_id, string $order_id, int $user_id, $team_level_id, $address_level)
  90. {
  91. $model = Loader::model('UserPledge');
  92. $time = time();
  93. $rows = $model::alias('a')
  94. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  95. ->where('a.user_id', $user_id)
  96. ->where('a.id', $pledge_id)
  97. ->where('a.status', $model::Ongoing)
  98. ->where('a.end_time', '>', $time)
  99. ->field('a.*,b.day_num,b.type_id')
  100. ->find();
  101. if(empty($rows)) throw new Exception('暂无质押订单');
  102. $orderId = explode(',', $order_id);
  103. $detail = json_decode($rows->details, true);
  104. $count = count($orderId);
  105. $num = $rows->num;
  106. if(count($orderId) == count($detail)) {
  107. $rows->status = $model::Remove;
  108. }else{
  109. $result = [];
  110. foreach ($detail as &$item)
  111. if(!in_array($item['id'], $orderId)) $result[] = $item;
  112. $num = $count;
  113. $rows->num = count($result);
  114. $rows->details = json_encode($result, JSON_UNESCAPED_UNICODE);
  115. }
  116. $day = 86400;
  117. $reta = bcdiv($rows->day_num, $day, 6); //天数
  118. $inter = ($rows->last_time == 0) ? $time - $rows->create_time: $time - $rows->last_time; //最后收取时间
  119. $rows->total_self= bcmul($reta, $inter, 6) * $num; //累计收益
  120. //修改状态
  121. Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('id', $orderId)->setField('status', ProductOrder::Paid);
  122. //等级
  123. if($rows->type_id == 2) CommonLogic::setIsOuLevelSave($user_id, $team_level_id, $address_level);
  124. //更新领取状态
  125. $rows->last_time = $time;
  126. return $rows->save();
  127. }
  128. //收取质抵押订单列表:(60*60*24)*(当前时间-最后一次收取时间)
  129. public static function getPledgeCollect(int $user_id)
  130. {
  131. $model = Loader::model('UserPledge');
  132. $time = time();
  133. $list = $model::alias('a')
  134. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  135. ->where('a.user_id', $user_id)
  136. ->where('a.status', '>', $model::Close)
  137. ->where('a.end_time', '>', $time)
  138. ->field('a.*,b.day_num')
  139. ->select();
  140. if(empty($list)) throw new Exception('暂无质押订单');
  141. $day = 86400;
  142. $total = 0; //当前累计收益
  143. foreach ($list as $item) {
  144. //解冻
  145. if($item->status == $model::Remove) {
  146. $total += $item->total_self;
  147. $item->status = $model::Close; //关闭
  148. }else{
  149. $reta = bcdiv($item->day_num, $day, 6); //天数
  150. $inter = ($item->last_time == 0) ? $time - $item->create_time: $time - $item->last_time; //最后收取时间
  151. $total_inco = bcmul($reta, $inter, 6) * $item->num; //累计收益
  152. $item->last_time = $time; //收取时间
  153. $item->total_self= bcadd($total_inco, $item->total_self, 2); //累计收益
  154. $total += $total_inco; //累计收益
  155. }
  156. //更新领取状态
  157. $item->save();
  158. }
  159. //等级分佣
  160. CommonLogic::setTeamLevelIncome($user_id, $total, Asset::TEAC, LedgerTeacChangeModel::TeamLevel);
  161. //更新用户资产
  162. return Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
  163. }
  164. //存储订单续费
  165. public static function setPledgeOrderRenew(object $pledge, int $user_id, string $pay_type)
  166. {
  167. //扣除支付金额
  168. self::setPledgeFee($user_id, $pay_type, $pledge[$pay_type]);
  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. public static function isUserBalance(object $user, int $pay_type, string $pay)
  187. {
  188. $result = true;
  189. if ($pay_type == 1 && bcadd($user['token'], $user['frozen'], 6) < $pay) $result = false;
  190. if ($pay_type == 2 && $user['teac'] < $pay) $result = false;
  191. return $result;
  192. }
  193. //扣除存储手续费
  194. private static function setPledgeFee(int $user_id, string $pay_type, float $price)
  195. {
  196. if( empty($price)) return true;
  197. //扣除支付金额
  198. if ($pay_type == Asset::TEAC) {
  199. Loader::model('LedgerWalletModel')->changeWalletAccount($user_id, $pay_type, -$price, LedgerTeacChangeModel::PledgeFee, 0);
  200. }else{
  201. Loader::model('LedgerWalletModel')->setChangeFrozen($user_id, $price, LedgerFrozenChangeModel::Pledge, LedgerTokenChangeModel::Pledge, '-');
  202. }
  203. }
  204. //获取订单产品
  205. private static function getOrderProductList(int $user_id, array $order_id, int $count)
  206. {
  207. return Loader::model('ProductOrder')::alias('a')
  208. ->join('product_list b', 'a.product_id = b.id', 'left')
  209. ->where('a.user_id', $user_id)
  210. ->where('a.status', ProductOrder::Paid)
  211. ->whereIn('a.id', $order_id)->field('a.id,b.zh_name,b.thum,a.order_no')->select();
  212. }
  213. }