Pledge.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. //dump($pledges);die;
  61. $num = $pledges->type_id == $productPledges::Single? 3 : 1; //单品最大三个限制
  62. $pledges['day_num'] = bcmul($pledges['day_num'] *$num, 30, 2);
  63. $pledges['announcement_id'] = ['zh'=>339, 'en'=>340]; //协议ID
  64. $this->success('', $pledges);
  65. }
  66. /*
  67. * 持有商品列表
  68. */
  69. public function holdProductList(PledgeLogic $pledgeLogic)
  70. {
  71. $product_id = $this->request->param('product_id', 0, 'intval');
  72. if(empty($product_id)) $this->error(__("参数有误,无可用产品"));
  73. $pledges = $pledgeLogic::getHoldProductList($this->auth->id, $product_id);
  74. $this->success('', $pledges);
  75. }
  76. /*
  77. * 质押存储
  78. */
  79. public function create(ProductPledges $productPledges, PledgeLogic $pledgeLogic, LedgerWalletModel $ledgerWalletModel)
  80. {
  81. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  82. $order_id = $this->request->post('order_id/a', '');
  83. $pay_type = $this->request->post('pay_type/d', 1);//支付方式 1 茶宝 2 Teac
  84. if(empty($pledge_id) || empty($order_id) || in_array($pay_type, [1,2]) == false) $this->error(__("参数有误,无可用产品"));
  85. $pledge = $productPledges::get($pledge_id);
  86. $count = count($order_id);//选择商品数量
  87. if($pledge->type_id == $productPledges::Combin && count(explode(',', $pledge->product_id)) != $count) $this->error(__("质押商品数量与订单数量不匹配"));
  88. //单品最大三个限制
  89. if($pledge->type_id == $productPledges::Single && $count > config('pledge_single_max')) $this->error(__("单品最大三个限制"));
  90. if (empty($pledge)) $this->error(__("质抵活动不存在"));
  91. if (empty($pledge->status))$this->error(__("质抵活动已结束"));
  92. //余额判断
  93. if(empty($pledge[$this->pay[$pay_type]])) $this->error(__("无法使用此服务费方式支付"));
  94. $user = $ledgerWalletModel::getWallet($this->auth->id);
  95. //判断余额是否足够
  96. if($pledgeLogic::isUserBalance($user, $pay_type, $pledge[$this->pay[$pay_type]]) == false) $this->error(__("余额不足"));
  97. Db::startTrans();
  98. try {
  99. // 质抵押订单
  100. $res = $pledgeLogic::setPledgeOrder($pledge, $order_id, $this->auth->id, $count, $this->pay[$pay_type], $pledge[$this->pay[$pay_type]]);
  101. //组合质押
  102. if($pledge->type_id == 2) CommonLogic::setIsUpLevel($this->auth->id, $this->auth->team_level_id);
  103. // 提交事务
  104. Db::commit();
  105. } catch (Exception $e) {
  106. // 回滚事务
  107. Db::rollback();
  108. $this->error($e->getMessage(), null, $e->getCode());
  109. }
  110. if ($res === false) {
  111. $this->error(__("订单创建失败"));
  112. }
  113. $this->success('ok');
  114. }
  115. /*
  116. * 我的茶矿
  117. */
  118. public function teamine(PledgeLogic $pledgeLogic)
  119. {
  120. try {
  121. // 质抵押订单
  122. $res = $pledgeLogic::getPledgeOrderList($this->auth->id);
  123. } catch (Exception $e) {
  124. $this->error($e->getMessage(), null, $e->getCode());
  125. }
  126. $this->success('ok', $res);
  127. }
  128. /*
  129. * 收取茶矿
  130. */
  131. public function collect(PledgeLogic $pledgeLogic)
  132. {
  133. if(cache('collect_'.$this->auth->id)) $this->error(__("正在挖矿中,请稍后再试"));
  134. Db::startTrans();
  135. try {
  136. // 质抵押订单
  137. $res = $pledgeLogic::getPledgeCollect($this->auth->id);
  138. //请求限制
  139. cache('collect_'.$this->auth->id, time(), 300);
  140. // 提交事务
  141. Db::commit();
  142. } catch (Exception $e) {
  143. // 回滚事务
  144. Db::rollback();
  145. $this->error($e->getMessage(), null, $e->getCode());
  146. }
  147. $this->success('ok', $res);
  148. }
  149. /*
  150. * 解除质押
  151. */
  152. public function remove(PledgeLogic $pledgeLogic)
  153. {
  154. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  155. $order_id = $this->request->post('order_id/s', '');
  156. if(empty($pledge_id) || empty($order_id)) $this->error(__("参数有误,无可用产品"));
  157. Db::startTrans();
  158. try {
  159. // 质抵押订单
  160. $res = $pledgeLogic::setPledgeRemove($pledge_id, $order_id, $this->auth->id, $this->auth->team_level_id, $this->auth->address_level);
  161. // 提交事务
  162. Db::commit();
  163. } catch (Exception $e) {
  164. // 回滚事务
  165. Db::rollback();
  166. $this->error($e->getMessage(), null, $e->getCode());
  167. }
  168. $this->success('ok', $res);
  169. }
  170. //存储续费
  171. public function setRenew(UserPledge $userPledge, PledgeLogic $pledgeLogic, LedgerWalletModel $ledgerWalletModel)
  172. {
  173. $pledge_id = $this->request->post('pledge_id', 0, 'intval');
  174. $pay_type = $this->request->post('pay_type/d', 1);//支付方式 1 茶宝 2 Teac
  175. if(empty($pledge_id) || in_array($pay_type, [1,2]) == false) $this->error(__("参数有误,无可用产品"));
  176. //记录
  177. $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();
  178. if (empty($pledge)) $this->error(__("质抵活动不存在"));
  179. if ($pledge->status != $userPledge::Ongoing || empty($pledge->is_renew) || empty($pledge->p_status))$this->error(__("质抵活动已结束"));
  180. //余额判断
  181. if($pledge[$this->pay[$pay_type]] > 0){
  182. $user = $ledgerWalletModel::getWallet($this->auth->id);
  183. //判断余额是否足够
  184. if($pledgeLogic::isUserBalance($user, $pay_type, $pledge[$this->pay[$pay_type]]) == false) $this->error(__("余额不足"));
  185. }
  186. Db::startTrans();
  187. try {
  188. // 质抵押订单
  189. $pledgeLogic::setPledgeOrderRenew($pledge, $this->auth->id, $this->pay[$pay_type]);
  190. // 提交事务
  191. Db::commit();
  192. } catch (Exception $e) {
  193. // 回滚事务
  194. Db::rollback();
  195. $this->error($e->getMessage(), null, $e->getCode());
  196. }
  197. $this->success('ok');
  198. }
  199. //添加用户存储产品
  200. public function addPledgeProduct(UserPledge $userPledge, PledgeLogic $pledgeLogic, ProductPledges $productPledges)
  201. {
  202. $order_id = $this->request->post('order_id/s', '');
  203. $pledge_id = $this->request->post('pledge_id/d', 0);//存储Id
  204. if(empty($order_id) || empty($pledge_id)) $this->error(__("参数有误,无可用产品"));
  205. $order_arr= explode(',', $order_id);
  206. $count = count($order_arr);
  207. $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();
  208. if (empty($pledge) || $pledge->status != $userPledge::Ongoing || empty($pledge->p_status) || $pledge->type_id != $productPledges::Single) $this->error(__("产品质抵活动不存在不存在"));
  209. if(($count + $pledge->num) > config('pledge_single_max')) $this->error(__("产品数量超出限制"));
  210. Db::startTrans();
  211. try {
  212. // 质抵押订单
  213. $pledgeLogic::setPledgeProductAdd($pledge, $this->auth->id, $count, $order_arr);
  214. // 提交事务
  215. Db::commit();
  216. } catch (Exception $e) {
  217. // 回滚事务
  218. Db::rollback();
  219. $this->error($e->getMessage(), null, $e->getCode());
  220. }
  221. $this->success('ok');
  222. }
  223. }