Teac.php 8.8 KB

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