Market.php 8.2 KB

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