Teac.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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'], LedgerTeacChangeModel::Sell, $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($this->auth->id == $row['user_id']) $this->error('不能购买自己的订单');
  89. if($params['num'] > $row['stock'] - $row['num']) $this->error('库存不足');
  90. $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
  91. $tatal = bcmul($row['price'], $params['num'], 2);
  92. if($chabao < $tatal) $this->error('您的钱包茶宝余额不足');
  93. // 启动事务
  94. Db::startTrans();
  95. try {
  96. $fee = config('teac_trade.sell_serve_fee');
  97. // 出售购买
  98. $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Sell, $params['num'], $row['price'], $fee);
  99. //添加扣除相应茶宝Teac
  100. $teacLogin::setCreateSellOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
  101. //修改状态
  102. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  103. $row->frozen -= $params['num'];
  104. $row->num += $params['num'];
  105. $row->save();
  106. // 提交事务
  107. Db::commit();
  108. } catch (Exception $e) {
  109. // 回滚事务
  110. Db::rollback();
  111. return $e->getMessage();
  112. }
  113. $this->success('ok');
  114. }
  115. // 求购出售
  116. public function setBuyOrder(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($this->auth->id == $row['user_id']) $this->error('不能购买自己的订单');
  124. if($params['num'] > $row['stock'] - $row['num']) $this->error('库存不足');
  125. // 启动事务
  126. Db::startTrans();
  127. try {
  128. $fee = config('teac_trade.buy_serve_fee');
  129. $chabao = bcmul($row['price'], $params['num'], 2);
  130. // 出售购买
  131. $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Buying, $params['num'], $row['price'], $fee);
  132. //添加扣除相应茶宝Teac
  133. $teacLogin::setCreateBuyingOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
  134. //修改状态
  135. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  136. $row->frozen -= $chabao;
  137. $row->num += $params['num'];
  138. $row->save();
  139. // 提交事务
  140. Db::commit();
  141. } catch (Exception $e) {
  142. // 回滚事务
  143. Db::rollback();
  144. return $e->getMessage();
  145. }
  146. $this->success('ok');
  147. }
  148. // 我的交易列表
  149. public function getUserTeacList(ProductTeac $productTeac)
  150. {
  151. $list = $productTeac::where('user_id', $this->auth->id)
  152. ->order('create_time desc')
  153. ->paginate($this->pageSize);
  154. $this->success('ok', $list);
  155. }
  156. //取消交易
  157. public function cancelTrade(ProductTeac $productTeac, TeacLogin $teacLogin)
  158. {
  159. $id = $this->request->post('teac_id/d', 0);
  160. $row = $productTeac::where(['id' => $id, 'user_id' => $this->auth->id])->find();
  161. if(empty($row)) $this->error('订单不存在');
  162. if($row['status'] != ProductTeac::Normal) $this->error('订单已完成');
  163. Db::startTrans();
  164. try {
  165. //返回相应茶宝Teac
  166. $teacLogin::setUserReturnOrder($this->auth->id, $row['type_id'], $row['frozen']);
  167. $row->status = ProductTeac::Closure;
  168. $row->save();
  169. // 提交事务
  170. Db::commit();
  171. } catch (Exception $e) {
  172. // 回滚事务
  173. Db::rollback();
  174. return $e->getMessage();
  175. }
  176. $this->success('ok');
  177. }
  178. /**
  179. * 获取配置
  180. */
  181. public function getConfig()
  182. {
  183. $this->success('ok', config('teac_trade'));
  184. }
  185. }