Market.php 5.3 KB

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