Pledge.php 9.8 KB

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