Teac.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\api\logic\TeacLogin;
  5. use app\common\model\LedgerTeacChangeModel;
  6. use app\common\model\LedgerTokenChangeModel;
  7. use app\common\model\UserTeac;
  8. use app\common\model\LedgerWalletModel;
  9. use fast\Action;
  10. use fast\Asset;
  11. use Exception;
  12. use fast\RechargeStatus;
  13. use think\Db;
  14. use app\common\model\ProductTeac;
  15. //Teac交易
  16. class Teac extends Api
  17. {
  18. /*
  19. * 交易列表
  20. */
  21. public function getTeacList(ProductTeac $productTeac)
  22. {
  23. $type_id = $this->request->post('type_id/d', 0);
  24. if(!in_array($type_id, [ProductTeac::Buying, ProductTeac::Sell])) $this->error('类型错误');
  25. $list = $productTeac->with('users')
  26. ->where('product_teac.status', ProductTeac::Normal)->where('type_id', $type_id)
  27. ->order('create_time desc')
  28. ->paginate($this->pageSize);
  29. $this->success('ok', $list);
  30. }
  31. /*
  32. * 发布求购
  33. */
  34. public function setBuying(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
  35. {
  36. $params = $this->request->post();
  37. $validate = \think\Loader::validate('Teac');
  38. if(!$validate->scene('buying')->check($params)) $this->error($validate->getError());
  39. if(config('teac_trade.buy_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.buy_min_num'));
  40. if(config('teac_trade.buy_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.buy_min_price'));
  41. $chabao = bcmul($params['price'], $params['stock'], 2) ;
  42. if($ledgerWalletModel->getWalletChaBao($this->auth->id) < $chabao) $this->error('您的钱包茶宝余额不足');
  43. Db::startTrans();
  44. try{
  45. //新增求购信息
  46. $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock'], ProductTeac::Buying, $chabao);
  47. //冻结茶宝
  48. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$chabao, LedgerTokenChangeModel::Buying, $this->auth->id);
  49. Db::commit();
  50. }catch(Exception $e){
  51. Db::rollback();
  52. $this->error($e->getMessage());
  53. }
  54. $this->success('ok');
  55. }
  56. //发布出售
  57. public function setSell(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
  58. {
  59. $params = $this->request->post();
  60. $validate = \think\Loader::validate('Teac');
  61. if(!$validate->scene('sell')->check($params)) $this->error($validate->getError());
  62. if(config('teac_trade.sell_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.sell_min_num'));
  63. if(config('teac_trade.sell_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.sell_min_price'));
  64. if($ledgerWalletModel->getWalletTeac($this->auth->id) < $params['stock']) $this->error('您的钱包Teac余额不足');
  65. Db::startTrans();
  66. try{
  67. //新增求购信息
  68. $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock'], ProductTeac::Sell, $params['stock']);
  69. //冻结Teac
  70. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$params['stock'], LedgerTokenChangeModel::BuySellg, $this->auth->id);
  71. Db::commit();
  72. }catch(Exception $e){
  73. Db::rollback();
  74. $this->error($e->getMessage());
  75. }
  76. $this->success('ok');
  77. }
  78. /**
  79. * 出售购买
  80. */
  81. public function setSellOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac)
  82. {
  83. $params = $this->request->post();
  84. $validate = \think\Loader::validate('Teac');
  85. if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
  86. $row = $productTeac::where(['type_id'=> ProductTeac::Sell, 'id' => $params['teac_id']])->find();
  87. if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
  88. if($params['num'] < $row['stock'] - $row['num']) $this->error('库存不足');
  89. $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
  90. $tatal = bcmul($row['price'], $params['num'], 2);
  91. if($chabao < bcmul($row['price'], $tatal, 2)) $this->error('您的钱包茶宝余额不足');
  92. // 启动事务
  93. Db::startTrans();
  94. try {
  95. $fee = config('teac_trade.sell_fee');
  96. // 出售购买
  97. $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Sell, $params['num'], $row['price'], $fee);
  98. //添加扣除相应茶宝Teac
  99. $teacLogin::setCreateSellOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
  100. //修改状态
  101. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  102. $row->frozen -= $params['num'];
  103. $row->num += $params['num'];
  104. $row->save();
  105. // 提交事务
  106. Db::commit();
  107. } catch (Exception $e) {
  108. // 回滚事务
  109. Db::rollback();
  110. return $e->getMessage();
  111. }
  112. $this->success('ok');
  113. }
  114. /**
  115. * 求购出售
  116. */
  117. public function getBuyOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac)
  118. {
  119. $params = $this->request->post();
  120. $validate = \think\Loader::validate('Teac');
  121. if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
  122. $row = $productTeac::where(['type_id'=> ProductTeac::Buying, 'id' => $params['teac_id']])->find();
  123. if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
  124. if($params['num'] < $row['stock'] - $row['num']) $this->error('库存不足');
  125. $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
  126. $tatal = bcmul($row['price'], $params['num'], 2);
  127. if($chabao < bcmul($row['price'], $tatal, 2)) $this->error('您的钱包茶宝余额不足');
  128. // 启动事务
  129. Db::startTrans();
  130. try {
  131. $fee = config('teac_trade.sell_fee');
  132. $chabao = bcmul($row['price'], $params['num'], 2);
  133. // 出售购买
  134. $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Buying, $params['num'], $row['price'], $fee);
  135. //添加扣除相应茶宝Teac
  136. $teacLogin::setCreateBuyingOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
  137. //修改状态
  138. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  139. $row->frozen -= $chabao;
  140. $row->num += $params['num'];
  141. $row->save();
  142. // 提交事务
  143. Db::commit();
  144. } catch (Exception $e) {
  145. // 回滚事务
  146. Db::rollback();
  147. return $e->getMessage();
  148. }
  149. $this->success('ok');
  150. }
  151. /**
  152. * 获取配置
  153. */
  154. public function getConfig()
  155. {
  156. $this->success('ok', config('teac_trade'));
  157. }
  158. }