Teac.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. // 最小最大价格
  41. if(config('teac_trade.buy_min_price') > $params['price'] || config('teac_trade.buy_max_price') < $params['price']) $this->error('价格不在'.config('teac_trade.buy_min_price').'~'.config('teac_trade.buy_max_price').'之间');
  42. $chabao = bcmul($params['price'], $params['stock'], 4) ;
  43. if($ledgerWalletModel->getWalletChaBao($this->auth->id) < $chabao) $this->error('您的钱包茶宝余额不足');
  44. Db::startTrans();
  45. try{
  46. //新增求购信息
  47. $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock'], ProductTeac::Buying, $chabao);
  48. //冻结茶宝
  49. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$chabao, LedgerTokenChangeModel::Buying, $this->auth->id);
  50. Db::commit();
  51. }catch(Exception $e){
  52. Db::rollback();
  53. $this->error($e->getMessage());
  54. }
  55. $this->success('ok');
  56. }
  57. //发布出售
  58. public function setSell(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
  59. {
  60. $params = $this->request->post();
  61. $validate = \think\Loader::validate('Teac');
  62. if(!$validate->scene('sell')->check($params)) $this->error($validate->getError());
  63. if(config('teac_trade.sell_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.sell_min_num'));
  64. if(config('teac_trade.sell_min_price') > $params['price'] || config('teac_trade.sell_max_price') < $params['price']) $this->error('价格不在'.config('teac_trade.sell_min_price').'~'.config('teac_trade.sell_max_price').'之间');
  65. if($ledgerWalletModel->getWalletTeac($this->auth->id) < $params['stock']) $this->error('您的钱包Teac余额不足');
  66. Db::startTrans();
  67. try{
  68. //新增求购信息
  69. $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock'], ProductTeac::Sell, $params['stock']);
  70. //冻结Teac
  71. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$params['stock'], LedgerTeacChangeModel::Sell, $this->auth->id);
  72. Db::commit();
  73. }catch(Exception $e){
  74. Db::rollback();
  75. $this->error($e->getMessage());
  76. }
  77. $this->success('ok');
  78. }
  79. /**
  80. * 出售购买
  81. */
  82. public function setSellOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac)
  83. {
  84. $params = $this->request->post();
  85. $validate = \think\Loader::validate('Teac');
  86. if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
  87. $row = $productTeac::where(['type_id'=> ProductTeac::Sell, 'id' => $params['teac_id']])->find();
  88. if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
  89. if($this->auth->id == $row['user_id']) $this->error('不能购买自己的订单');
  90. if($params['num'] > $row['stock'] - $row['num']) $this->error('库存不足');
  91. $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
  92. $tatal = bcmul($row['price'], $params['num'], 4);
  93. if($chabao < $tatal) $this->error('您的钱包茶宝余额不足');
  94. // 启动事务
  95. Db::startTrans();
  96. try {
  97. $fee = config('teac_trade.sell_serve_fee');
  98. // 出售购买
  99. $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Sell, $params['num'], $row['price'], $fee);
  100. //添加扣除相应茶宝Teac
  101. $teacLogin::setCreateSellOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
  102. //修改状态
  103. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  104. $row->frozen -= $params['num'];
  105. $row->num += $params['num'];
  106. $row->save();
  107. // 提交事务
  108. Db::commit();
  109. } catch (Exception $e) {
  110. // 回滚事务
  111. Db::rollback();
  112. return $e->getMessage();
  113. }
  114. $this->success('ok');
  115. }
  116. // 求购出售
  117. public function setBuyOrder(ProductTeac $productTeac, 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($this->auth->id == $row['user_id']) $this->error('不能购买自己的订单');
  125. if($params['num'] > $row['stock'] - $row['num']) $this->error('库存不足');
  126. // 启动事务
  127. Db::startTrans();
  128. try {
  129. $fee = config('teac_trade.buy_serve_fee');
  130. $chabao = bcmul($row['price'], $params['num'], 4);
  131. // 出售购买
  132. $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Buying, $params['num'], $row['price'], $fee);
  133. //添加扣除相应茶宝Teac
  134. $teacLogin::setCreateBuyingOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
  135. //修改状态
  136. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  137. $row->frozen -= $chabao;
  138. $row->num += $params['num'];
  139. $row->save();
  140. // 提交事务
  141. Db::commit();
  142. } catch (Exception $e) {
  143. // 回滚事务
  144. Db::rollback();
  145. return $e->getMessage();
  146. }
  147. $this->success('ok');
  148. }
  149. // 我的交易列表
  150. public function getUserTeacList(ProductTeac $productTeac)
  151. {
  152. $list = $productTeac::where('user_id', $this->auth->id)
  153. ->order('create_time desc')
  154. ->paginate($this->pageSize);
  155. $this->success('ok', $list);
  156. }
  157. //取消交易
  158. public function cancelTrade(ProductTeac $productTeac, TeacLogin $teacLogin)
  159. {
  160. $id = $this->request->post('teac_id/d', 0);
  161. $row = $productTeac::where(['id' => $id, 'user_id' => $this->auth->id])->find();
  162. if(empty($row)) $this->error('订单不存在');
  163. if($row['status'] != ProductTeac::Normal) $this->error('订单已完成');
  164. Db::startTrans();
  165. try {
  166. //返回相应茶宝Teac
  167. $teacLogin::setUserReturnOrder($this->auth->id, $row['type_id'], $row['frozen']);
  168. $row->frozen = 0;
  169. $row->status = ProductTeac::Closure;
  170. $row->save();
  171. // 提交事务
  172. Db::commit();
  173. } catch (Exception $e) {
  174. // 回滚事务
  175. Db::rollback();
  176. return $e->getMessage();
  177. }
  178. $this->success('ok');
  179. }
  180. /**
  181. * 获取配置
  182. */
  183. public function getConfig()
  184. {
  185. $this->success('ok', config('teac_trade'));
  186. }
  187. }