Teac.php 8.6 KB

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