Teac.php 7.3 KB

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