Market.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\logic\BscApi;
  4. use app\common\logic\MyBscApi;
  5. use app\common\controller\Api;
  6. use app\common\model\AnnouncementModel;
  7. use app\common\model\UserCollect;
  8. use app\common\model\LedgerUsdtChangeModel;
  9. use app\common\model\ProductBuying;
  10. use app\common\model\ParametersModel;
  11. use app\common\model\ProductOrder;
  12. use app\common\model\UserModel;
  13. use app\common\model\UserPathModel;
  14. use app\common\model\LedgerWalletModel;
  15. use fast\Action;
  16. use fast\Asset;
  17. use fast\Http;
  18. use fast\RechargeStatus;
  19. use think\Db;
  20. use think\Log;
  21. use think\Model;
  22. class Market extends Api
  23. {
  24. //用户收藏
  25. public function collect(UserCollect $userCollect)
  26. {
  27. $params = $this->request->post();
  28. $validate = \think\Loader::validate('Market');
  29. if(!$validate->scene('collect')->check($params)) $this->error($validate->getError());
  30. $userCollect::setUserCollect($this->auth->id, $params['market_id']);
  31. $this->success("ok");
  32. }
  33. /*
  34. * 相关公告
  35. */
  36. public function announcement(AnnouncementModel $announcementModel)
  37. {
  38. $params = $this->request->post();
  39. $validate = \think\Loader::validate('Market');
  40. if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
  41. $announcement = $announcementModel
  42. ->where('find_in_set(:id,product_id)',['id'=>$params['product_id']])
  43. ->where('status', $announcementModel::Normal)
  44. ->where('to_lang', $this->request->getLan())
  45. ->order('id desc')
  46. ->paginate($this->pageSize);
  47. $this->success('ok', $announcement);
  48. }
  49. //获取产品最大求购价格
  50. public function getBuyingMaxPrice(ProductBuying $productBuying)
  51. {
  52. $params = $this->request->post();
  53. $validate = \think\Loader::validate('Market');
  54. if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
  55. $this->success('ok', $productBuying::getProductBuyingMaxPrice($params['product_id']));
  56. }
  57. //求购
  58. public function buying()
  59. {
  60. $params = $this->request->post();
  61. $validate = \think\Loader::validate('Market');
  62. if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
  63. }
  64. //求购列表
  65. public function buyingList()
  66. {
  67. }
  68. /**
  69. * 手段向某会员报单算力
  70. * @return void
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. * @throws \think\exception\DbException
  74. */
  75. public function updateOrder()
  76. {
  77. //算力租赁订单处理
  78. // 查询兑换比例
  79. $usdtToPower = (new ParametersModel)->getValue('usdtToPowerRate');
  80. $usdtToPowerFloat = floatval($usdtToPower);
  81. if (is_null($usdtToPower) || $usdtToPowerFloat <= 0) {
  82. return '获取USDT兑换算力的比例失败';
  83. }
  84. $orderInfo = (new OfflineRechargeRecordModel())
  85. ->where('id', 4)
  86. ->find();
  87. if(empty($orderInfo)){
  88. halt('订单信息不存在');
  89. }
  90. $uid = $orderInfo['user_id'];
  91. $fee = $orderInfo['amount'];
  92. $power = bcmul($fee, $usdtToPowerFloat, 6); // 该用户兑得的算力
  93. // 启动事务
  94. Db::startTrans();
  95. try {
  96. // 更新总算力和账变
  97. (new LedgerWalletModel)->changeWalletAccount($uid, Asset::POWER, $power, Action::PowerRentalPower, $orderInfo['id']);
  98. // 更新服务器算力,不账变
  99. (new LedgerWalletModel)->changeWalletOnly($uid, Asset::RENTAL_POWER, $power);
  100. // 更新自己(有效会员时间)和所有上级的信息(有效直推人数和团队总算力)
  101. (new UserModel())->updateForRental($uid, $power);
  102. // 发放直推USDT收益
  103. (new LedgerWalletModel)->sendUsdtProfit($uid, $fee);
  104. // 发放直推算力收益
  105. (new LedgerWalletModel)->sendDirectProfit($uid, $power);
  106. // 发代数收益
  107. (new LedgerWalletModel)->sendGenerateProfit($uid, $fee);
  108. // 发放见点奖
  109. (new LedgerWalletModel)->sendRegBonus($uid, $fee);
  110. // 更新购买(充值)记录
  111. (new OfflineRechargeRecordModel())->updateOrderStatus($orderInfo['id'], RechargeStatus::StatusAuthSuccess, 0, $power);
  112. // 提交事务
  113. Db::commit();
  114. } catch (Exception $e) {
  115. // 回滚事务
  116. Db::rollback();
  117. return $e->getMessage();
  118. }
  119. }
  120. //重算团队业绩和直推人数
  121. public function reset_team()
  122. {
  123. set_time_limit(0);
  124. //先清空
  125. $update = Db::table('user')
  126. ->where('id', '>', 0)
  127. ->update([
  128. 'team_power' => 0,
  129. 'team_num' => 0,
  130. 'direct_num' => 0
  131. ]);
  132. $up_power = Db::table('ledger_wallet')
  133. ->field('u.id')
  134. ->alias('w')
  135. ->join('user u', 'w.user_id = u.id')
  136. ->where('u.effective_time','>',0)
  137. ->where('w.rental_power', 0)
  138. ->select();
  139. foreach ($up_power as $item){
  140. $update = Db::table('ledger_wallet')
  141. ->where('user_id', $item['id'])
  142. ->update([
  143. 'rental_power' => 0.01
  144. ]);
  145. }
  146. $power_list = Db::table('ledger_wallet')
  147. ->where('rental_power', '>', 0)
  148. ->select();
  149. $info_list = 0;
  150. $bad_list = [];
  151. foreach ($power_list as $key => $info) {
  152. dump('正在处理ID:' . $info['user_id']);
  153. $parent_ids = (new UserPathModel())
  154. ->where('user_id',$info['user_id'])
  155. ->order('distance')
  156. ->select();
  157. foreach ($parent_ids as $item) {
  158. $data = [
  159. 'team_power' => Db::raw('team_power + ' . $info['rental_power']),
  160. 'team_num' => Db::raw('team_num + 1'),
  161. ];
  162. if ($item['distance'] == 1) {
  163. $data['direct_num'] = Db::raw('direct_num+1');
  164. }
  165. $update = Db::table('user')
  166. ->where('id', $item['parent_id'])
  167. ->update($data);
  168. if (!$update) {
  169. $bad_list[] = $info;
  170. } else {
  171. $info_list++;
  172. }
  173. }
  174. }
  175. dump('成功数量');
  176. dump($info_list);
  177. dump('失败数量');
  178. dump($bad_list);
  179. }
  180. //累加新增团队业绩
  181. public function addTeamPower()
  182. {
  183. set_time_limit(0);
  184. $uid = 100702;
  185. $num = 1429;
  186. $parentIDs = (new UserPathModel())->getAllParentIDs($uid);
  187. $parentIDs[] = $uid;
  188. dump($parentIDs, true, 'ID列表');
  189. $update = (new UserModel())
  190. ->where('id', 'in', $parentIDs)
  191. ->update([
  192. 'team_power' => Db::raw('team_power + ' . $num)
  193. ]);
  194. dump($update, true, '影响行数');
  195. }
  196. public function resetUserPath()
  197. {
  198. $ratio = 0.05;//直推奖励
  199. $info_list = (new OfflineRechargeRecordModel())
  200. ->where('status', OfflineRechargeRecordModel::StatusFail)
  201. ->order('id', 'ASC')
  202. ->select();
  203. dump('本次数据共有:' . count($info_list));
  204. foreach ($info_list as $info) {
  205. $user = (new UserModel())->get($info['user_id']);
  206. if (empty($user) || $user['parent_id'] == 0) {
  207. continue;
  208. }
  209. $check = (new LedgerUsdtChangeModel())
  210. ->where('user_id', $user['parent_id'])
  211. ->where('action', Action::UsdtShareBonus)
  212. ->where('from_id', $info['user_id'])
  213. ->count();
  214. if($check == 0){
  215. (new LedgerWalletModel())->changeWalletAccount($user['parent_id'], Asset::USDT, $info['amount'] * $ratio, Action::UsdtShareBonus, $info['user_id']);
  216. dump('订单ID:' . $info['id'] . '处理成功,奖金:' . $info['amount'] * $ratio);
  217. }else{
  218. dump('订单ID:' . $info['id'] . '处理失败,奖金已存在');
  219. }
  220. }
  221. }
  222. /**
  223. * 补发直推奖
  224. * @return void
  225. */
  226. public function resetShareBonus()
  227. {
  228. $users = (new UserModel())->order('id', 'ASC')->select();
  229. foreach ($users as $v) {
  230. (new UserPathModel())->createPath($v['id'], $v['parent_id']);
  231. }
  232. }
  233. /**
  234. * @return string
  235. *
  236. * 返回结果
  237. * {
  238. "status": "1",
  239. "message": "OK",
  240. "result": {
  241. "status": "1"
  242. }
  243. }
  244. */
  245. public function getStatusByHaxh()
  246. {
  247. $url = "https://api.bscscan.com/api?module=transaction&action=gettxreceiptstatus&txhash=0x30f4190a8237c7744a0981a2895728bab93acf4cb78b03c192ed5ac387405c54&apikey=VTCKIP346DCRWB6JNS4KDANUJJEQN9VAKW";
  248. $body = Http::get($url);
  249. if (empty($body)) {
  250. return "api返回内容为空";
  251. }
  252. dump($body);
  253. // 转成数组
  254. $rsArr = json_decode($body, true);
  255. dump($rsArr);
  256. if (empty($rsArr) || !is_array($rsArr)) {
  257. return "状态api返回数据异常";
  258. }
  259. if ($rsArr['status'] != '1') {
  260. return '状态api返回status不为1,错误信息:' . $rsArr['message'];
  261. }
  262. if ($rsArr['result']['status'] != 1) {
  263. return '状态api返回result中的status不为1,错误信息:' . $rsArr['message'];
  264. }
  265. }
  266. public function AllocateEtc()
  267. {
  268. $url = "https://www.binance.com/api/v1/klines?symbol=ETCUSDT&limit=1&interval=3m";
  269. $body = Http::get($url);
  270. if (empty($body)) {
  271. return "api返回内容为空";
  272. }
  273. dump($body);
  274. // 转成数组
  275. $rsArr = json_decode($body, true);
  276. dump($rsArr[0][2]);
  277. $body = Http::get($url);
  278. if (empty($body)) {
  279. return "api返回内容为空";
  280. }
  281. dump($body);
  282. // 转成数组
  283. $rsArr = json_decode($body, true);
  284. dump($rsArr);
  285. if (empty($rsArr) || !is_array($rsArr)) {
  286. return "状态api返回数据异常";
  287. }
  288. if ($rsArr['status'] != '1') {
  289. return '状态api返回status不为1,错误信息:' . $rsArr['message'];
  290. }
  291. if ($rsArr['result']['status'] != 1) {
  292. return '状态api返回result中的status不为1,错误信息:' . $rsArr['message'];
  293. }
  294. }
  295. public function debug()
  296. {
  297. $num =rand(9000,10000);
  298. dump($num/10000/1000);
  299. $body = (new MyBscApi())->getInfoByTransactionHash('0xc3fc59faa9a1a9ff4fa1516b7df10570876a87993c397f6cd33ce3446641b5b0');
  300. dump($body);
  301. }
  302. public function resetAirdrop()
  303. {
  304. $order_list = (new ProductOrder())
  305. ->where('type_id', 3)
  306. ->group('user_id')
  307. ->column('user_id');
  308. dump($order_list);
  309. $order_list_send = (new ProductOrder())
  310. ->where('type_id', 4)
  311. ->group('user_id')
  312. ->column('user_id');
  313. dump($order_list_send);
  314. $order_list = array_diff($order_list, $order_list_send);
  315. dump($order_list);
  316. $rs = (new ProductOrder())
  317. ->where('type_id', 3)
  318. ->where('user_id', 'in', $order_list)
  319. ->update([
  320. 'create_time' => 1745942400
  321. ]);
  322. dump($rs);
  323. }
  324. public function updateNews()
  325. {
  326. $en_news = DB::name('announcement_cope')->select();
  327. foreach ($en_news as $info) {
  328. if(strlen($info['en_title']) > 3){
  329. $rs = (new AnnouncementModel())
  330. ->save([
  331. 'type_id' => $info['type_id'],
  332. 'title' => $info['en_body'],
  333. 'introduction' => $info['en_introduction'],
  334. 'body' => $info['en_body'],
  335. 'img_url' => $info['img_url'],
  336. 'to_lang' => 1,
  337. 'status' => $info['status'],
  338. 'created_by' => $info['created_by'],
  339. 'createtime' => $info['createtime'],
  340. ]);
  341. }
  342. }
  343. dump($rs);
  344. }
  345. }