Market.php 8.9 KB

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