PledgeLogic.php 10 KB

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