Market.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\LedgerTokenChangeModel;
  4. use app\common\controller\Api;
  5. use app\common\model\AnnouncementModel;
  6. use app\common\model\UserCollect;
  7. use app\common\model\ProductBuying;
  8. use app\common\model\UserModel;
  9. use app\common\model\LedgerWalletModel;
  10. use app\common\model\ProductTransfer;
  11. use app\api\logic\MarketLogic;
  12. use app\common\logic\BscApi;
  13. use app\common\model\ProductOrder;
  14. use app\common\model\UserBuying;
  15. use fast\Action;
  16. use fast\Asset;
  17. use think\Db;
  18. use Exception;
  19. class Market extends Api
  20. {
  21. //用户收藏
  22. public function collect(UserCollect $userCollect)
  23. {
  24. $params = $this->request->post();
  25. $validate = \think\Loader::validate('Market');
  26. if(!$validate->scene('collect')->check($params)) $this->error($validate->getError());
  27. $userCollect::setUserCollect($this->auth->id, $params['market_id']);
  28. $this->success("ok");
  29. }
  30. /*
  31. * 相关公告
  32. */
  33. public function announcement(AnnouncementModel $announcementModel)
  34. {
  35. $params = $this->request->post();
  36. $validate = \think\Loader::validate('Market');
  37. if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
  38. $announcement = $announcementModel
  39. ->where('find_in_set(:id,product_id)',['id'=>$params['product_id']])
  40. ->where('status', $announcementModel::Normal)
  41. ->where('to_lang', $this->request->getLan())
  42. ->order('id desc')
  43. ->paginate($this->pageSize);
  44. $this->success('ok', $announcement);
  45. }
  46. //获取产品最大求购价格
  47. public function getBuyingMaxPrice(ProductBuying $productBuying)
  48. {
  49. $params = $this->request->post();
  50. $validate = \think\Loader::validate('Market');
  51. if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
  52. $this->success('ok', $productBuying::getProductBuyingMaxPrice($params['product_id']));
  53. }
  54. //锁定寄售
  55. public function setTransferLock(ProductTransfer $productTransfer, MarketLogic $marketLogic)
  56. {
  57. $params = $this->request->post();
  58. $validate = \think\Loader::validate('Market');
  59. if(!$validate->scene('transferlock')->check($params)) $this->error($validate->getError());
  60. try {
  61. $marketLogic::setTransferLock($productTransfer, config('market_transfer.lock_time'), $this->auth->id, $params['transfer_id']);
  62. } catch (Exception $e) {
  63. // 回滚事务
  64. $this->error($e->getMessage());
  65. }
  66. $this->success('ok');
  67. }
  68. //求购列表
  69. public function buyingList(ProductBuying $productBuying)
  70. {
  71. $params = $this->request->post();
  72. $validate = \think\Loader::validate('Market');
  73. if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
  74. $this->success('ok', $productBuying::getBuyingList($params['product_id']));
  75. }
  76. /**
  77. * 发起求购
  78. * @return void
  79. */
  80. public function setBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying)
  81. {
  82. $params = $this->request->post();
  83. $validate = \think\Loader::validate('Market');
  84. if(!$validate->scene('buying')->check($params)) $this->error($validate->getError());
  85. if($productBuying::getProductBuyingCount($this->auth->id, $params['product_id']) > config('market_transfer.max_buying_count')) $this->error('您的求购次数已达上限');
  86. $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
  87. $total = bcmul($params['min_price'], $params['stock'], 6); // 所需茶宝
  88. if($chabao < $total) $this->error('您的茶币不足');
  89. // 启动事务
  90. Db::startTrans();
  91. try {
  92. // 记录订单
  93. $productBuying::setCreateBuying($this->auth->id, $params['product_id'], $params['stock'], $params['min_price'], $total);
  94. // 扣除茶币
  95. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$total, LedgerTokenChangeModel::Buying, $this->auth->id);
  96. $ledgerWalletModel->changeWalletOnly($this->auth->id, Asset::BUYING, $total); //冻结金额
  97. // 提交事务
  98. Db::commit();
  99. } catch (Exception $e) {
  100. // 回滚事务
  101. Db::rollback();
  102. return $this->error($e->getMessage());
  103. }
  104. $this->success('ok');
  105. }
  106. /**
  107. * 求购详情
  108. * @return void
  109. */
  110. public function getBuyingDetail( ProductBuying $productBuying)
  111. {
  112. $params = $this->request->post();
  113. $validate = \think\Loader::validate('Market');
  114. if(!$validate->scene('buying_info')->check($params)) $this->error($validate->getError());
  115. $resq = $productBuying::getProductBuyingDetail($params['buying_id'], $this->request->getLan());
  116. $this->success('ok', $resq);
  117. }
  118. //出售求购
  119. public function sellBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying, UserBuying $userBuying, ProductOrder $productOrder)
  120. {
  121. $params = $this->request->post();
  122. $validate = \think\Loader::validate('Market');
  123. if(!$validate->scene('sellbuying')->check($params)) $this->error($validate->getError());
  124. $buying = $productBuying::get($params['buying_id']);
  125. if(empty($buying)) $this->error('订单不存在');
  126. if($buying->user_id == $this->auth->id) $this->error('无权操作');
  127. if($buying->status != ProductBuying::Normal) $this->error('订单已完成');
  128. // 启动事务
  129. Db::startTrans();
  130. try {
  131. // 记录出售订单
  132. $chabao =$userBuying::getCreateUserBuying($this->auth->id, $params['buying_id'], $buying->user_id, $buying->min_price);
  133. // 添加扣除茶币
  134. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, $chabao, LedgerTokenChangeModel::BuySellg, $buying->user_id);
  135. // 扣除冻结金额
  136. $ledgerWalletModel->changeWalletOnly($buying->user_id, Asset::BUYING, -$buying->min_price); //解冻金额
  137. //关闭订单
  138. $productOrder::where('id', $params['order_id'])->where('user_id', $this->auth->id)->setField(['status' => ProductOrder::Closure]);
  139. if(($buying->stock - $buying->num) == 1) $buying->status = ProductBuying::SaleOut;
  140. $buying->num += 1;
  141. $buying->save();
  142. // 提交事务
  143. Db::commit();
  144. } catch (Exception $e) {
  145. // 回滚事务
  146. Db::rollback();
  147. return $this->error($e->getMessage());
  148. }
  149. $this->success('ok');
  150. }
  151. //取消求购
  152. public function cancelBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying)
  153. {
  154. $params = $this->request->post();
  155. $validate = \think\Loader::validate('Market');
  156. if(!$validate->scene('cancelbuying')->check($params)) $this->error($validate->getError());
  157. $buying = $productBuying::get($params['buying_id']);
  158. if(empty($buying)) $this->error('订单不存在');
  159. if($buying->user_id!= $this->auth->id) $this->error('无权操作');
  160. if($buying->status != ProductBuying::Normal) $this->error('订单已完成');
  161. Db::startTrans();
  162. try {
  163. //退回金额
  164. $amount = bcmul($buying->min_price, ($buying->stock - $buying->num), 6);
  165. // 添加扣除茶币
  166. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, $amount, LedgerTokenChangeModel::BuyCancel, $buying->user_id);
  167. // 扣除冻结金额
  168. $ledgerWalletModel->changeWalletOnly($buying->user_id, Asset::BUYING, -$amount); //解冻金额
  169. $buying->status = ProductBuying::Close;
  170. $buying->save();
  171. // 提交事务
  172. Db::commit();
  173. } catch (Exception $e) {
  174. // 回滚事务
  175. Db::rollback();
  176. return $this->error($e->getMessage());
  177. }
  178. $this->success('ok');
  179. }
  180. //获取配置信息
  181. public function getMarketConfig()
  182. {
  183. $this->success('ok', config('market_transfer'));
  184. }
  185. }