Teac.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. $teamLeavel = $userModel::getTeamLevelId($row['user_id']);
  108. if($teamLeavel > 0) CommonLogic::setTeamLevelIncome($row['user_id'], $fees, $teamLeavel, Asset::TEAC, LedgerTeacChangeModel::TeamLevel);
  109. //修改状态
  110. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  111. $row->frozen -= $params['num'];
  112. $row->num += $params['num'];
  113. $row->save();
  114. // 提交事务
  115. Db::commit();
  116. } catch (Exception $e) {
  117. // 回滚事务
  118. Db::rollback();
  119. return $e->getMessage();
  120. }
  121. $this->success('ok');
  122. }
  123. // 求购出售
  124. public function setBuyOrder(ProductTeac $productTeac, TeacLogin $teacLogin, UserTeac $userTeac)
  125. {
  126. $params = $this->request->post();
  127. $validate = \think\Loader::validate('Teac');
  128. if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
  129. $row = $productTeac::where(['type_id'=> ProductTeac::Buying, 'id' => $params['teac_id']])->find();
  130. if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
  131. if($this->auth->id == $row['user_id']) $this->error('不能购买自己的订单');
  132. if($params['num'] > $row['stock'] - $row['num']) $this->error('库存不足');
  133. // 启动事务
  134. Db::startTrans();
  135. try {
  136. $fee = config('teac_trade.buy_serve_fee');
  137. $chabao = bcmul($row['price'], $params['num'], 4);
  138. // 出售购买
  139. $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Buying, $params['num'], $row['price'], $fee);
  140. //添加扣除相应茶宝Teac
  141. $teacLogin::setCreateBuyingOrder($this->auth->id, $this->auth->team_level_id, $row['user_id'], $row['price'], $params['num'], $fee);
  142. //修改状态
  143. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  144. $row->frozen -= $chabao;
  145. $row->num += $params['num'];
  146. $row->save();
  147. // 提交事务
  148. Db::commit();
  149. } catch (Exception $e) {
  150. // 回滚事务
  151. Db::rollback();
  152. return $e->getMessage();
  153. }
  154. $this->success('ok');
  155. }
  156. // 我的交易列表
  157. public function getUserTeacList(ProductTeac $productTeac)
  158. {
  159. $list = $productTeac::where('user_id', $this->auth->id)
  160. ->order('create_time desc')
  161. ->paginate($this->pageSize);
  162. $this->success('ok', $list);
  163. }
  164. //取消交易
  165. public function cancelTrade(ProductTeac $productTeac, TeacLogin $teacLogin)
  166. {
  167. $id = $this->request->post('teac_id/d', 0);
  168. $row = $productTeac::where(['id' => $id, 'user_id' => $this->auth->id])->find();
  169. if(empty($row)) $this->error('订单不存在');
  170. if($row['status'] != ProductTeac::Normal) $this->error('订单已完成');
  171. Db::startTrans();
  172. try {
  173. //返回相应茶宝Teac
  174. $teacLogin::setUserReturnOrder($this->auth->id, $row['type_id'], $row['frozen']);
  175. $row->frozen = 0;
  176. $row->status = ProductTeac::Closure;
  177. $row->save();
  178. // 提交事务
  179. Db::commit();
  180. } catch (Exception $e) {
  181. // 回滚事务
  182. Db::rollback();
  183. return $e->getMessage();
  184. }
  185. $this->success('ok');
  186. }
  187. /**
  188. * 获取配置
  189. */
  190. public function getConfig()
  191. {
  192. $this->success('ok', config('teac_trade'));
  193. }
  194. }