Pledge.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\logic\CommonLogic;
  4. use app\common\controller\Api;
  5. use app\common\model\ProductLists;
  6. use app\common\model\LedgerWalletModel;
  7. use app\common\model\ProductPledges;
  8. use Exception;
  9. use app\common\model\UserModel;
  10. use think\Db;
  11. use app\api\logic\PledgeLogic;
  12. use app\common\model\UserPledge;
  13. //质押抵扣
  14. class Pledge extends Api
  15. {
  16. protected string $lan = '';
  17. protected $pay = [1=>'token', 2=>'teac'];
  18. protected $order = [1=>'desc', 2=>'asc'];
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->lan = $this->request->getLan();
  23. }
  24. //质押列表
  25. public function list(ProductPledges $productPledges, PledgeLogic $pledgeLogic)
  26. {
  27. $type_id = $this->request->param('type_id', 0, 'intval');
  28. if(empty($type_id)) $this->error(__("参数有误,无可用产品"));
  29. $title = $this->request->param('title/s', '');
  30. $day_num = $this->request->param('day_num/d', 0);
  31. $income_reta = $this->request->param('income_reta/d', 0);
  32. $map = [];
  33. $order = 'weigh desc,id desc';
  34. if(!empty($title)) $map['title'] = [ 'LIKE', '%'.$title.'%'];
  35. if ($day_num > 0) $order = 'day_num '.$this->order[$day_num];
  36. if ($income_reta > 0) $order = 'income_reta '.$this->order[$income_reta];
  37. if ($day_num > 0 && $income_reta > 0) $order = 'day_num '.$this->order[$day_num].',income_reta '.$this->order[$income_reta];
  38. $list = $productPledges
  39. ->where('status', $productPledges::Normal)
  40. ->where('to_lang', $this->lan)->where('type_id', $type_id)
  41. ->where($map)
  42. ->field('id,title,day_num,income_reta,product_id,type_id')
  43. ->order($order)
  44. ->paginate($this->pageSize);
  45. $list = $pledgeLogic::getByProductIdList($list, $this->lan);
  46. $this->success('', $list);
  47. }
  48. /*
  49. * 质押详情
  50. */
  51. public function detail(ProductPledges $productPledges, ProductLists $productList)
  52. {
  53. $id = $this->request->param('id', 0, 'intval');
  54. if(empty($id)) $this->error(__("参数有误,无可用产品"));
  55. //质押详情
  56. $pledges = $productPledges::get($id);
  57. if (!empty($pledges)) {
  58. $pledges->product_list = $productList::getBySynthesisProduct($pledges->product_id, $pledges->type_id, $this->lan);
  59. }
  60. $num = $pledges->type_id == $productPledges::Single? 3 : 1; //单品最大三个限制
  61. $pledges['day_num'] = bcmul($pledges['day_num'] *$num, 30, 2);
  62. $pledges['announcement_id'] = ['zh'=>339, 'en'=>340]; //协议ID
  63. $this->success('', $pledges);
  64. }
  65. /*
  66. * 持有商品列表
  67. */
  68. public function holdProductList(PledgeLogic $pledgeLogic)
  69. {
  70. $product_id = $this->request->param('product_id', 0, 'intval');
  71. if(empty($product_id)) $this->error(__("参数有误,无可用产品"));
  72. $pledges = $pledgeLogic::getHoldProductList($this->auth->id, $product_id);
  73. $this->success('', $pledges);
  74. }
  75. /*
  76. * 质押存储
  77. */
  78. public function create(ProductPledges $productPledges, PledgeLogic $pledgeLogic, LedgerWalletModel $ledgerWalletModel)
  79. {
  80. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  81. $order_id = $this->request->post('order_id/a', '');
  82. $pay_type = $this->request->post('pay_type/d', 1);//支付方式 1 茶宝 2 Teac
  83. if(empty($pledge_id) || empty($order_id) || in_array($pay_type, [1,2]) == false) $this->error(__("参数有误,无可用产品"));
  84. $pledge = $productPledges::get($pledge_id);
  85. $count = count($order_id);//选择商品数量
  86. if($pledge->type_id == $productPledges::Combin && count(explode(',', $pledge->product_id)) != $count) $this->error(__("质押商品数量与订单数量不匹配"));
  87. //单品最大三个限制
  88. if($pledge->type_id == $productPledges::Single && $count > config('pledge_single_max')) $this->error(__("单品最大三个限制"));
  89. if (empty($pledge)) $this->error(__("质抵活动不存在"));
  90. if (empty($pledge->status))$this->error(__("质抵活动已结束"));
  91. //余额判断
  92. if(empty($pledge[$this->pay[$pay_type]])) $this->error(__("无法使用此服务费方式支付"));
  93. $user = $ledgerWalletModel::getWallet($this->auth->id);
  94. //判断余额是否足够
  95. if($pledgeLogic::isUserBalance($user, $pay_type, $pledge[$this->pay[$pay_type]]) == false) $this->error(__("余额不足"));
  96. Db::startTrans();
  97. try {
  98. // 质抵押订单
  99. $res = $pledgeLogic::setPledgeOrder($pledge, $order_id, $this->auth->id, $count, $this->pay[$pay_type], $pledge[$this->pay[$pay_type]]);
  100. //组合质押
  101. if($pledge->type_id == 2) CommonLogic::setIsUpLevel($this->auth->id, $this->auth->team_level_id);
  102. // 提交事务
  103. Db::commit();
  104. } catch (Exception $e) {
  105. // 回滚事务
  106. Db::rollback();
  107. $this->error($e->getMessage(), null, $e->getCode());
  108. }
  109. if ($res === false) {
  110. $this->error(__("订单创建失败"));
  111. }
  112. $this->success('ok');
  113. }
  114. /*
  115. * 我的茶矿
  116. */
  117. public function teamine(PledgeLogic $pledgeLogic)
  118. {
  119. try {
  120. // 质抵押订单
  121. $res = $pledgeLogic::getPledgeOrderList($this->auth->id);
  122. } catch (Exception $e) {
  123. $this->error($e->getMessage(), null, $e->getCode());
  124. }
  125. $this->success('ok', $res);
  126. }
  127. /*
  128. * 收取茶矿
  129. */
  130. public function collect(PledgeLogic $pledgeLogic)
  131. {
  132. if(cache('collect_'.$this->auth->id)) $this->error(__("正在挖矿中,请稍后再试"));
  133. Db::startTrans();
  134. try {
  135. // 质抵押订单
  136. $res = $pledgeLogic::getPledgeCollect($this->auth->id);
  137. //请求限制
  138. cache('collect_'.$this->auth->id, time(), 300);
  139. // 提交事务
  140. Db::commit();
  141. } catch (Exception $e) {
  142. // 回滚事务
  143. Db::rollback();
  144. $this->error($e->getMessage(), null, $e->getCode());
  145. }
  146. $this->success('ok', $res);
  147. }
  148. /*
  149. * 解除质押
  150. */
  151. public function remove(PledgeLogic $pledgeLogic)
  152. {
  153. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  154. $order_id = $this->request->post('order_id/s', '');
  155. if(empty($pledge_id) || empty($order_id)) $this->error(__("参数有误,无可用产品"));
  156. Db::startTrans();
  157. try {
  158. // 质抵押订单
  159. $res = $pledgeLogic::setPledgeRemove($pledge_id, $order_id, $this->auth->id, $this->auth->team_level_id, $this->auth->address_level);
  160. // 提交事务
  161. Db::commit();
  162. } catch (Exception $e) {
  163. // 回滚事务
  164. Db::rollback();
  165. $this->error($e->getMessage(), null, $e->getCode());
  166. }
  167. $this->success('ok', $res);
  168. }
  169. //存储续费
  170. public function setRenew(UserPledge $userPledge, PledgeLogic $pledgeLogic, LedgerWalletModel $ledgerWalletModel)
  171. {
  172. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  173. $pay_type = $this->request->post('pay_type/d', 1);//支付方式 1 茶宝 2 Teac
  174. if(empty($pledge_id) || in_array($pay_type, [1,2]) == false) $this->error(__("参数有误,无可用产品"));
  175. //记录
  176. $pledge = $userPledge::alias('a')->where('a.id', $pledge_id)->join('product_pledge p', 'p.id = a.pledge_id')->field('a.*,p.token,p.teac,p.type_id,p.is_renew,p.status as p_status')->find();
  177. if (empty($pledge)) $this->error(__("质抵活动不存在"));
  178. if ($pledge->status != $userPledge::Ongoing || empty($pledge->is_renew) || empty($pledge->p_status))$this->error(__("质抵活动已结束"));
  179. //余额判断
  180. if($pledge[$this->pay[$pay_type]] > 0){
  181. $user = $ledgerWalletModel::getWallet($this->auth->id);
  182. //判断余额是否足够
  183. if($pledgeLogic::isUserBalance($user, $pay_type, $pledge[$this->pay[$pay_type]]) == false) $this->error(__("余额不足"));
  184. }
  185. Db::startTrans();
  186. try {
  187. // 质抵押订单
  188. $pledgeLogic::setPledgeOrderRenew($pledge, $this->auth->id, $this->pay[$pay_type]);
  189. // 提交事务
  190. Db::commit();
  191. } catch (Exception $e) {
  192. // 回滚事务
  193. Db::rollback();
  194. $this->error($e->getMessage(), null, $e->getCode());
  195. }
  196. $this->success('ok');
  197. }
  198. //添加用户存储产品
  199. public function addPledgeProduct(UserPledge $userPledge, PledgeLogic $pledgeLogic, ProductPledges $productPledges)
  200. {
  201. $order_id = $this->request->post('order_id/s', '');
  202. $pledge_id = $this->request->post('pledge_id/d', 0);//存储Id
  203. if(empty($order_id) || empty($pledge_id)) $this->error(__("参数有误,无可用产品"));
  204. $order_arr= explode(',', $order_id);
  205. $count = count($order_arr);
  206. $pledge = $userPledge::alias('a')->where('a.id', $pledge_id)->join('product_pledge p', 'p.id = a.pledge_id')->field('a.*,p.type_id,p.status as p_status')->find();
  207. if (empty($pledge) || $pledge->status != $userPledge::Ongoing || empty($pledge->p_status) || $pledge->type_id != $productPledges::Single) $this->error(__("产品质抵活动不存在不存在"));
  208. if(($count + $pledge->num) > config('pledge_single_max')) $this->error(__("产品数量超出限制"));
  209. Db::startTrans();
  210. try {
  211. // 质抵押订单
  212. $pledgeLogic::setPledgeProductAdd($pledge, $this->auth->id, $count, $order_arr);
  213. // 提交事务
  214. Db::commit();
  215. } catch (Exception $e) {
  216. // 回滚事务
  217. Db::rollback();
  218. $this->error($e->getMessage(), null, $e->getCode());
  219. }
  220. $this->success('ok');
  221. }
  222. }