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