Market.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\AnnouncementModel;
  5. use app\common\model\UserCollect;
  6. use app\common\model\ProductBuying;
  7. use app\common\model\ParametersModel;
  8. use app\common\model\ProductOrder;
  9. use app\common\model\UserModel;
  10. use app\common\model\LedgerWalletModel;
  11. use app\common\model\ProductTransfer;
  12. use app\api\logic\MarketLogic;
  13. use fast\Action;
  14. use fast\Asset;
  15. use fast\Http;
  16. use fast\RechargeStatus;
  17. use think\Db;
  18. use think\Exception;
  19. class Market extends Api
  20. {
  21. //用户收藏
  22. public function collect(UserCollect $userCollect)
  23. {
  24. $params = $this->request->post();
  25. $validate = \think\Loader::validate('Market');
  26. if(!$validate->scene('collect')->check($params)) $this->error($validate->getError());
  27. $userCollect::setUserCollect($this->auth->id, $params['market_id']);
  28. $this->success("ok");
  29. }
  30. /*
  31. * 相关公告
  32. */
  33. public function announcement(AnnouncementModel $announcementModel)
  34. {
  35. $params = $this->request->post();
  36. $validate = \think\Loader::validate('Market');
  37. if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
  38. $announcement = $announcementModel
  39. ->where('find_in_set(:id,product_id)',['id'=>$params['product_id']])
  40. ->where('status', $announcementModel::Normal)
  41. ->where('to_lang', $this->request->getLan())
  42. ->order('id desc')
  43. ->paginate($this->pageSize);
  44. $this->success('ok', $announcement);
  45. }
  46. //获取产品最大求购价格
  47. public function getBuyingMaxPrice(ProductBuying $productBuying)
  48. {
  49. $params = $this->request->post();
  50. $validate = \think\Loader::validate('Market');
  51. if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
  52. $this->success('ok', $productBuying::getProductBuyingMaxPrice($params['product_id']));
  53. }
  54. //锁定寄售
  55. public function setTransferLock(ProductTransfer $productTransfer, MarketLogic $marketLogic)
  56. {
  57. $params = $this->request->post();
  58. $validate = \think\Loader::validate('Market');
  59. if(!$validate->scene('transferlock')->check($params)) $this->error($validate->getError());
  60. Db::startTrans();
  61. try {
  62. $marketLogic::setTransferLock($productTransfer, config('market_transfer.lock_time'), $params['transfer_id']);
  63. // 提交事务
  64. Db::commit();
  65. } catch (Exception $e) {
  66. // 回滚事务
  67. Db::rollback();
  68. $this->error($e->getMessage(), null, $e->getCode());
  69. }
  70. $this->success('ok');
  71. }
  72. //求购
  73. public function buying()
  74. {
  75. $params = $this->request->post();
  76. $validate = \think\Loader::validate('Market');
  77. if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
  78. }
  79. //求购列表
  80. public function buyingList()
  81. {
  82. }
  83. /**
  84. * 手段向某会员报单算力
  85. * @return void
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. */
  90. public function updateOrder()
  91. {
  92. //算力租赁订单处理
  93. // 查询兑换比例
  94. $usdtToPower = (new ParametersModel)->getValue('usdtToPowerRate');
  95. $usdtToPowerFloat = floatval($usdtToPower);
  96. if (is_null($usdtToPower) || $usdtToPowerFloat <= 0) {
  97. return '获取USDT兑换算力的比例失败';
  98. }
  99. $orderInfo = (new OfflineRechargeRecordModel())
  100. ->where('id', 4)
  101. ->find();
  102. if(empty($orderInfo)){
  103. halt('订单信息不存在');
  104. }
  105. $uid = $orderInfo['user_id'];
  106. $fee = $orderInfo['amount'];
  107. $power = bcmul($fee, $usdtToPowerFloat, 6); // 该用户兑得的算力
  108. // 启动事务
  109. Db::startTrans();
  110. try {
  111. // 更新总算力和账变
  112. (new LedgerWalletModel)->changeWalletAccount($uid, Asset::POWER, $power, Action::PowerRentalPower, $orderInfo['id']);
  113. // 更新服务器算力,不账变
  114. (new LedgerWalletModel)->changeWalletOnly($uid, Asset::RENTAL_POWER, $power);
  115. // 更新自己(有效会员时间)和所有上级的信息(有效直推人数和团队总算力)
  116. (new UserModel())->updateForRental($uid, $power);
  117. // 发放直推USDT收益
  118. (new LedgerWalletModel)->sendUsdtProfit($uid, $fee);
  119. // 发放直推算力收益
  120. (new LedgerWalletModel)->sendDirectProfit($uid, $power);
  121. // 发代数收益
  122. (new LedgerWalletModel)->sendGenerateProfit($uid, $fee);
  123. // 发放见点奖
  124. (new LedgerWalletModel)->sendRegBonus($uid, $fee);
  125. // 更新购买(充值)记录
  126. (new OfflineRechargeRecordModel())->updateOrderStatus($orderInfo['id'], RechargeStatus::StatusAuthSuccess, 0, $power);
  127. // 提交事务
  128. Db::commit();
  129. } catch (Exception $e) {
  130. // 回滚事务
  131. Db::rollback();
  132. return $e->getMessage();
  133. }
  134. }
  135. //获取配置信息
  136. public function getMarketConfig()
  137. {
  138. $this->success('ok', config('market_transfer'));
  139. }
  140. }