Teac.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\api\logic\TeacLogin;
  5. use app\common\model\ParametersModel;
  6. use app\common\model\LedgerTokenChangeModel;
  7. use app\common\model\UserModel;
  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. //Teac交易
  15. class Teac extends Api
  16. {
  17. //出售列表
  18. public function getSell()
  19. {
  20. //从主表复制到备份表
  21. //$user_list = Db::query("SELECT * FROM `user` where id in (SELECT user_id from user_path where parent_id = 100098) or id = 100098");
  22. $user_list = Db::name('user')
  23. ->fetchSql(false)
  24. ->field('u.*,p.distance')
  25. ->alias('u')
  26. ->join('user_path p', 'u.id = p.user_id')
  27. //->where('p.parent_id', 100098)
  28. ->where('p.parent_id', 100115)
  29. ->select();
  30. halt($i);
  31. }
  32. /*
  33. * 求购列表
  34. */
  35. public function getBuyingList()
  36. {
  37. $params = $this->request->post();
  38. $validate = \think\Loader::validate('Teac');
  39. if(!$validate->scene('buying')->check($params)) $this->error($validate->getError());
  40. halt(11);
  41. }
  42. /*
  43. * 发布求购
  44. */
  45. public function setBuying(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
  46. {
  47. $params = $this->request->post();
  48. $validate = \think\Loader::validate('Teac');
  49. if(!$validate->scene('buying')->check($params)) $this->error($validate->getError());
  50. if(config('teac_trade.sell_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.sell_min_price'));
  51. $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
  52. if($chabao < bcmul($params['price'], $params['stock'], 2)) $this->error('您的钱包茶宝余额不足');
  53. Db::startTrans();
  54. try{
  55. //新增求购信息
  56. $order_price = $teacLogin::setCreateOrder($this->auth->id, $params['price'], $params['stock']);
  57. //冻结资产
  58. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_price, LedgerTokenChangeModel::Buying, $this->auth->id);
  59. Db::commit();
  60. $this->success('订单创建成功');
  61. }catch(Exception $e){
  62. Db::rollback();
  63. $this->error($e->getMessage());
  64. }
  65. }
  66. //发布出售
  67. public function setSell()
  68. {
  69. $validate = \think\Loader::validate('Teac');
  70. if(!$validate->scene('sell')->check($params)) $this->error($validate->getError());
  71. if(config('teac_trade.sell_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.sell_min_price'));
  72. $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
  73. if($chabao < bcmul($params['price'], $params['stock'], 2)) $this->error('您的钱包茶宝余额不足');
  74. Db::startTrans();
  75. try{
  76. //新增求购信息
  77. $order_price = $teacLogin::setCreateOrder($this->auth->id, $params['price'], $params['stock']);
  78. //冻结资产
  79. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_price, LedgerTokenChangeModel::Buying, $this->auth->id);
  80. Db::commit();
  81. $this->success('订单创建成功');
  82. }catch(Exception $e){
  83. Db::rollback();
  84. $this->error($e->getMessage());
  85. }
  86. }
  87. /**
  88. * 手段向某会员报单算力
  89. * @return void
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. * @throws \think\exception\DbException
  93. */
  94. public function updateOrder()
  95. {
  96. //算力租赁订单处理
  97. // 查询兑换比例
  98. $usdtToPower = (new ParametersModel)->getValue('usdtToPowerRate');
  99. $usdtToPowerFloat = floatval($usdtToPower);
  100. if (is_null($usdtToPower) || $usdtToPowerFloat <= 0) {
  101. return '获取USDT兑换算力的比例失败';
  102. }
  103. $orderInfo = (new OfflineRechargeRecordModel())
  104. ->where('id', 4)
  105. ->find();
  106. if(empty($orderInfo)){
  107. halt('订单信息不存在');
  108. }
  109. $uid = $orderInfo['user_id'];
  110. $fee = $orderInfo['amount'];
  111. $power = bcmul($fee, $usdtToPowerFloat, 6); // 该用户兑得的算力
  112. // 启动事务
  113. Db::startTrans();
  114. try {
  115. // 更新总算力和账变
  116. (new LedgerWalletModel)->changeWalletAccount($uid, Asset::POWER, $power, Action::PowerRentalPower, $orderInfo['id']);
  117. // 更新服务器算力,不账变
  118. (new LedgerWalletModel)->changeWalletOnly($uid, Asset::RENTAL_POWER, $power);
  119. // 更新自己(有效会员时间)和所有上级的信息(有效直推人数和团队总算力)
  120. (new UserModel())->updateForRental($uid, $power);
  121. // 发放直推USDT收益
  122. (new LedgerWalletModel)->sendUsdtProfit($uid, $fee);
  123. // 发放直推算力收益
  124. (new LedgerWalletModel)->sendDirectProfit($uid, $power);
  125. // 发代数收益
  126. (new LedgerWalletModel)->sendGenerateProfit($uid, $fee);
  127. // 发放见点奖
  128. (new LedgerWalletModel)->sendRegBonus($uid, $fee);
  129. // 更新购买(充值)记录
  130. (new OfflineRechargeRecordModel())->updateOrderStatus($orderInfo['id'], RechargeStatus::StatusAuthSuccess, 0, $power);
  131. // 提交事务
  132. Db::commit();
  133. } catch (Exception $e) {
  134. // 回滚事务
  135. Db::rollback();
  136. return $e->getMessage();
  137. }
  138. }
  139. /**
  140. *获取配置
  141. */
  142. public function getConfig()
  143. {
  144. $this->success('ok', config('teac_trade'));
  145. }
  146. }