Test.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\logic\BscApi;
  4. use app\common\logic\MyBscApi;
  5. use app\common\logic\TeamRewards;
  6. use app\common\model\AnnouncementModel;
  7. use app\common\model\LedgerPowerChangeModel;
  8. use app\common\model\LedgerSmhChangeModel;
  9. use app\common\model\LedgerUsdtChangeModel;
  10. use app\common\model\OfflineRechargeRecordModel;
  11. use app\common\model\OfflineWithdrawRecordModel;
  12. use app\common\model\ParametersModel;
  13. use app\common\model\ProductOrder;
  14. use app\common\model\UserModel;
  15. use app\common\model\UserPathModel;
  16. use app\common\model\LedgerWalletModel;
  17. use fast\Action;
  18. use fast\Asset;
  19. use fast\Http;
  20. use fast\RechargeStatus;
  21. use think\Db;
  22. use think\Log;
  23. use think\Model;
  24. class Test
  25. {
  26. public function copy_user()
  27. {
  28. //从主表复制到备份表
  29. //$user_list = Db::query("SELECT * FROM `user` where id in (SELECT user_id from user_path where parent_id = 100098) or id = 100098");
  30. $user_list = Db::name('user')
  31. ->fetchSql(false)
  32. ->field('u.*,p.distance')
  33. ->alias('u')
  34. ->join('user_path p', 'u.id = p.user_id')
  35. //->where('p.parent_id', 100098)
  36. ->where('p.parent_id', 100115)
  37. ->select();
  38. $i = 0;
  39. foreach ($user_list as $user){
  40. $is = Db::table('user_1')
  41. ->insert($user);
  42. $i++;
  43. }
  44. halt($i);
  45. $user_list = Db::name('user_1')
  46. ->whereNull('address')
  47. ->select();
  48. $i = 0;
  49. foreach ($user_list as $user){
  50. $parent_info = (new UserModel())
  51. ->where('id', $user['parent_id'])
  52. ->find();
  53. if(!empty($parent_info)){
  54. $is = Db::table('user_1')
  55. ->where('id', $user['id'])
  56. ->update([
  57. 'parent_address' => $parent_info['address']
  58. ]);
  59. $i++;
  60. }
  61. }
  62. halt($i);
  63. // //从备份表中除掉不要的数据
  64. // $user_list = Db::query("SELECT * FROM `user_1` where id in (SELECT user_id from user_path where parent_id = 100277) or id = 100277");
  65. // $i = 0;
  66. // foreach ($user_list as $user){
  67. // $is = Db::table('user_1')
  68. // ->where('id', $user['id'])
  69. // ->delete();
  70. // $i++;
  71. // }
  72. // halt($i);
  73. //从备份表中根据上级ID,更新每个用户的上级钱包地址和钱包地址
  74. $user_list = Db::table('user_1')
  75. ->select();
  76. $i = 0;
  77. $is = Db::table('ledger_wallet_1')
  78. ->where('user_id', '>', 0)
  79. ->update([
  80. 'address' => null
  81. ]);
  82. $parent_arr[100115] = '0xc3e8f8179fad209304ada21708acdcf3834c448c';
  83. foreach ($user_list as $user){
  84. $parent_arr[$user['id']] = $user['address'];
  85. }
  86. foreach ($user_list as $user){
  87. $is = Db::table('ledger_wallet_1')
  88. ->where('user_id', $user['id'])
  89. ->update([
  90. 'address' => $user['address']
  91. ]);
  92. if(isset($parent_arr[$user['id']])){
  93. $is = Db::table('user_1')
  94. ->where('id', $user['id'])
  95. ->update([
  96. 'parent_address' => $parent_arr[$user['parent_id']]
  97. ]);
  98. $i++;
  99. }else{
  100. dump($user['id'] . '--无上级');
  101. }
  102. }
  103. halt($i);
  104. }
  105. /*
  106. * 更新上级
  107. */
  108. public function change_parent()
  109. {
  110. //1.从主表复制到备份表,需手动插入上级信息
  111. // dump('从主表复制到备份表');
  112. // $user_list = Db::name('user')
  113. // ->fetchSql(false)
  114. // ->field('u.*,p.distance')
  115. // ->alias('u')
  116. // ->join('user_path p', 'u.id = p.user_id')
  117. // ->where('p.parent_id', 101985)
  118. // //->where('u.id','>', 103931)
  119. // ->select();
  120. //
  121. // $i = 0;
  122. // foreach ($user_list as $user){
  123. // $is = Db::table('user_1')
  124. // ->insert($user);
  125. // $i++;
  126. // }
  127. // halt($i);
  128. //*2.更新备份表上级地址
  129. // dump('更新上级地址');
  130. // $user_list = Db::name('user_1')
  131. // ->whereNull('parent_address')
  132. // ->select();
  133. // $i = 0;
  134. // foreach ($user_list as $user){
  135. // $parent_info = (new UserModel())
  136. // ->where('id', $user['parent_id'])
  137. // ->find();
  138. // if(!empty($parent_info)){
  139. // $is = Db::table('user_1')
  140. // ->where('id', $user['id'])
  141. // ->update([
  142. // 'parent_address' => $parent_info['address']
  143. // ]);
  144. // $i++;
  145. // }
  146. // }
  147. // halt($i);
  148. //3.更新主表会员上级
  149. dump('重建网体');
  150. $user_list = Db::table('user_1')
  151. ->order('distance')
  152. ->select();
  153. $i = 0;
  154. foreach ($user_list as $user){
  155. $parent_info = (new UserModel())
  156. ->where('address', $user['parent_address'])
  157. ->find();
  158. if(!empty($parent_info)){
  159. $is = Db::table('user')
  160. ->where('id', $user['id'])
  161. ->update([
  162. 'parent_id' => $parent_info['id']
  163. ]);
  164. //删除网体
  165. (new UserPathModel())
  166. ->where('user_id', $user['id'])
  167. ->delete();
  168. // 创建网体
  169. (new UserPathModel())->createPath($user['id'], $parent_info['id']);
  170. $i++;
  171. } else{
  172. dump($user['id'] . '--无上级');
  173. }
  174. }
  175. halt($i);
  176. }
  177. /*
  178. * 更新上级
  179. *
  180. * 更改给定ID的上级,并把它的直推一并更改
  181. */
  182. public function resetParent()
  183. {
  184. //3.更新主表会员上级
  185. dump('重建网体');
  186. $parent_id = 100100;
  187. $user_id = 101134;
  188. $user = (new UserModel())
  189. ->fetchSql(false)
  190. ->where('id', $user_id)
  191. ->update([
  192. 'parent_id' => $parent_id
  193. ]);
  194. dump($user, true, '更新状态');
  195. //删除网体
  196. (new UserPathModel())
  197. ->where('user_id', $user_id)
  198. ->delete();
  199. // 创建网体
  200. (new UserPathModel())->createPath($user_id, $parent_id);
  201. $user_list = (new UserModel())
  202. ->where('parent_id', $user_id)
  203. ->select();
  204. $i = 0;
  205. foreach ($user_list as $user){
  206. //删除网体
  207. (new UserPathModel())
  208. ->where('user_id', $user['id'])
  209. ->delete();
  210. // 创建网体
  211. (new UserPathModel())->createPath($user['id'], $user['parent_id']);
  212. $i++;
  213. }
  214. halt($i);
  215. }
  216. /**
  217. 导入数据表生成 sql
  218. CREATE TABLE dao_ru AS SELECT
  219. u.id,
  220. u.parent_id,
  221. u.address,
  222. u.team_power,
  223. u.team_num,
  224. w.power,
  225. w.rental_power,
  226. w.usdt
  227. FROM
  228. `user` u
  229. LEFT JOIN ledger_wallet w ON u.id = w.user_id
  230. WHERE
  231. id IN ( SELECT user_id FROM `user_path` WHERE parent_id = 1012 )
  232. OR id = 1012
  233. ORDER BY
  234. id
  235. */
  236. public function daoru()
  237. {
  238. die;
  239. $daoru_list = Db::table('user_base')
  240. //->where('is_dao', 1)
  241. ->select();
  242. $i = 0;
  243. $j = 0;
  244. //重建钱包
  245. foreach ($daoru_list as $key => $info) {
  246. $user = (new UserModel())->getByAddress($info['address']);
  247. if (!empty($user)) { // 已存在
  248. // 更新总算力和账变
  249. (new LedgerWalletModel)->changeWalletAccount($user['id'], Asset::POWER, $info['power'], Action::Reversal, 0);
  250. // 更新自己(有效会员时间)和所有上级的信息(有效直推人数和团队总算力)
  251. (new UserModel())->updateForRental($user['id'], $info['power']);
  252. $j++;
  253. continue;
  254. }
  255. }
  256. //重建用户
  257. // foreach ($daoru_list as $key => $info) {
  258. //
  259. // $parent_user = (new UserModel())->getByAddress($info['parent_address']);
  260. // if(empty($parent_user)){
  261. // dump('没有上级');
  262. // dump($info);
  263. // continue;
  264. // }
  265. //
  266. // $user = (new UserModel())->getByAddress($info['address']);
  267. // if (!empty($user)) { // 已存在
  268. // dump($info['id'] . ' - ' . $info['address'] . ' - 已存在');
  269. //
  270. // $is = Db::table('user')
  271. // ->where('id', $user['id'])
  272. // ->update([
  273. // 'parent_id' => $parent_user['id']
  274. // ]);
  275. // $j++;
  276. // continue;
  277. // }
  278. //
  279. // unset($info['id']);
  280. // unset($info['parent_address']);
  281. // unset($info['is_dao']);
  282. // $info['parent_id'] = $parent_user['id'];
  283. // // 创建用户
  284. // $newUserID = (new UserModel())->insertGetId($info);
  285. // // 创建钱包
  286. // (new LedgerWalletModel())->insertGetId([
  287. // 'user_id' => $newUserID,
  288. // ]);
  289. //
  290. // // 创建网体
  291. // (new UserPathModel())->createPath($newUserID, $parent_user['id']);
  292. //
  293. // $i++;
  294. //
  295. // }
  296. dump($i);
  297. dump($j);
  298. }
  299. public function updaetWallet()
  300. {
  301. $daoru_list = Db::table('ledger_wallet_1')
  302. ->whereNotNull('address')
  303. ->select();
  304. $i = 0;
  305. foreach ($daoru_list as $key => $info) {
  306. dump($info);
  307. $user = (new UserModel())->getByAddress($info['address']);
  308. if (empty($user)) {
  309. dump($info['user_id'] . ' - ' . $info['address'] . ' - 不存在');
  310. continue;
  311. }
  312. unset($info['user_id']);
  313. unset($info['address']);
  314. //更新钱包
  315. $is_up = (new LedgerWalletModel())
  316. ->where('user_id', $user['id'])
  317. ->update($info);
  318. dump($user['id'] . '更新' . $is_up);
  319. $i++;
  320. }
  321. dump($i);
  322. }
  323. /**
  324. * 手段向某会员报单算力
  325. * @return void
  326. * @throws \think\db\exception\DataNotFoundException
  327. * @throws \think\db\exception\ModelNotFoundException
  328. * @throws \think\exception\DbException
  329. */
  330. public function updateOrder()
  331. {
  332. //算力租赁订单处理
  333. // 查询兑换比例
  334. $usdtToPower = (new ParametersModel)->getValue('usdtToPowerRate');
  335. $usdtToPowerFloat = floatval($usdtToPower);
  336. if (is_null($usdtToPower) || $usdtToPowerFloat <= 0) {
  337. return '获取USDT兑换算力的比例失败';
  338. }
  339. $orderInfo = (new OfflineRechargeRecordModel())
  340. ->where('id', 4)
  341. ->find();
  342. if(empty($orderInfo)){
  343. halt('订单信息不存在');
  344. }
  345. $uid = $orderInfo['user_id'];
  346. $fee = $orderInfo['amount'];
  347. $power = bcmul($fee, $usdtToPowerFloat, 6); // 该用户兑得的算力
  348. // 启动事务
  349. Db::startTrans();
  350. try {
  351. // 更新总算力和账变
  352. (new LedgerWalletModel)->changeWalletAccount($uid, Asset::POWER, $power, Action::PowerRentalPower, $orderInfo['id']);
  353. // 更新服务器算力,不账变
  354. (new LedgerWalletModel)->changeWalletOnly($uid, Asset::RENTAL_POWER, $power);
  355. // 更新自己(有效会员时间)和所有上级的信息(有效直推人数和团队总算力)
  356. (new UserModel())->updateForRental($uid, $power);
  357. // 发放直推USDT收益
  358. (new LedgerWalletModel)->sendUsdtProfit($uid, $fee);
  359. // 发放直推算力收益
  360. (new LedgerWalletModel)->sendDirectProfit($uid, $power);
  361. // 发代数收益
  362. (new LedgerWalletModel)->sendGenerateProfit($uid, $fee);
  363. // 发放见点奖
  364. (new LedgerWalletModel)->sendRegBonus($uid, $fee);
  365. // 更新购买(充值)记录
  366. (new OfflineRechargeRecordModel())->updateOrderStatus($orderInfo['id'], RechargeStatus::StatusAuthSuccess, 0, $power);
  367. // 提交事务
  368. Db::commit();
  369. } catch (Exception $e) {
  370. // 回滚事务
  371. Db::rollback();
  372. return $e->getMessage();
  373. }
  374. }
  375. //重算团队业绩和直推人数
  376. public function reset_team()
  377. {
  378. set_time_limit(0);
  379. //先清空
  380. $update = Db::table('user')
  381. ->where('id', '>', 0)
  382. ->update([
  383. 'team_power' => 0,
  384. 'team_num' => 0,
  385. 'direct_num' => 0
  386. ]);
  387. $up_power = Db::table('ledger_wallet')
  388. ->field('u.id')
  389. ->alias('w')
  390. ->join('user u', 'w.user_id = u.id')
  391. ->where('u.effective_time','>',0)
  392. ->where('w.rental_power', 0)
  393. ->select();
  394. foreach ($up_power as $item){
  395. $update = Db::table('ledger_wallet')
  396. ->where('user_id', $item['id'])
  397. ->update([
  398. 'rental_power' => 0.01
  399. ]);
  400. }
  401. $power_list = Db::table('ledger_wallet')
  402. ->where('rental_power', '>', 0)
  403. ->select();
  404. $info_list = 0;
  405. $bad_list = [];
  406. foreach ($power_list as $key => $info) {
  407. dump('正在处理ID:' . $info['user_id']);
  408. $parent_ids = (new UserPathModel())
  409. ->where('user_id',$info['user_id'])
  410. ->order('distance')
  411. ->select();
  412. foreach ($parent_ids as $item) {
  413. $data = [
  414. 'team_power' => Db::raw('team_power + ' . $info['rental_power']),
  415. 'team_num' => Db::raw('team_num + 1'),
  416. ];
  417. if ($item['distance'] == 1) {
  418. $data['direct_num'] = Db::raw('direct_num+1');
  419. }
  420. $update = Db::table('user')
  421. ->where('id', $item['parent_id'])
  422. ->update($data);
  423. if (!$update) {
  424. $bad_list[] = $info;
  425. } else {
  426. $info_list++;
  427. }
  428. }
  429. }
  430. dump('成功数量');
  431. dump($info_list);
  432. dump('失败数量');
  433. dump($bad_list);
  434. }
  435. //累加新增团队业绩
  436. public function addTeamPower()
  437. {
  438. set_time_limit(0);
  439. $uid = 100702;
  440. $num = 1429;
  441. $parentIDs = (new UserPathModel())->getAllParentIDs($uid);
  442. $parentIDs[] = $uid;
  443. dump($parentIDs, true, 'ID列表');
  444. $update = (new UserModel())
  445. ->where('id', 'in', $parentIDs)
  446. ->update([
  447. 'team_power' => Db::raw('team_power + ' . $num)
  448. ]);
  449. dump($update, true, '影响行数');
  450. }
  451. public function resetUserPath()
  452. {
  453. $ratio = 0.05;//直推奖励
  454. $info_list = (new OfflineRechargeRecordModel())
  455. ->where('status', OfflineRechargeRecordModel::StatusFail)
  456. ->order('id', 'ASC')
  457. ->select();
  458. dump('本次数据共有:' . count($info_list));
  459. foreach ($info_list as $info) {
  460. $user = (new UserModel())->get($info['user_id']);
  461. if (empty($user) || $user['parent_id'] == 0) {
  462. continue;
  463. }
  464. $check = (new LedgerUsdtChangeModel())
  465. ->where('user_id', $user['parent_id'])
  466. ->where('action', Action::UsdtShareBonus)
  467. ->where('from_id', $info['user_id'])
  468. ->count();
  469. if($check == 0){
  470. (new LedgerWalletModel())->changeWalletAccount($user['parent_id'], Asset::USDT, $info['amount'] * $ratio, Action::UsdtShareBonus, $info['user_id']);
  471. dump('订单ID:' . $info['id'] . '处理成功,奖金:' . $info['amount'] * $ratio);
  472. }else{
  473. dump('订单ID:' . $info['id'] . '处理失败,奖金已存在');
  474. }
  475. }
  476. }
  477. /**
  478. * 补发直推奖
  479. * @return void
  480. */
  481. public function resetShareBonus()
  482. {
  483. $users = (new UserModel())->order('id', 'ASC')->select();
  484. foreach ($users as $v) {
  485. (new UserPathModel())->createPath($v['id'], $v['parent_id']);
  486. }
  487. }
  488. /**
  489. * @return string
  490. *
  491. * 返回结果
  492. * {
  493. "status": "1",
  494. "message": "OK",
  495. "result": {
  496. "status": "1"
  497. }
  498. }
  499. */
  500. public function getStatusByHaxh()
  501. {
  502. $url = "https://api.bscscan.com/api?module=transaction&action=gettxreceiptstatus&txhash=0x30f4190a8237c7744a0981a2895728bab93acf4cb78b03c192ed5ac387405c54&apikey=VTCKIP346DCRWB6JNS4KDANUJJEQN9VAKW";
  503. $body = Http::get($url);
  504. if (empty($body)) {
  505. return "api返回内容为空";
  506. }
  507. dump($body);
  508. // 转成数组
  509. $rsArr = json_decode($body, true);
  510. dump($rsArr);
  511. if (empty($rsArr) || !is_array($rsArr)) {
  512. return "状态api返回数据异常";
  513. }
  514. if ($rsArr['status'] != '1') {
  515. return '状态api返回status不为1,错误信息:' . $rsArr['message'];
  516. }
  517. if ($rsArr['result']['status'] != 1) {
  518. return '状态api返回result中的status不为1,错误信息:' . $rsArr['message'];
  519. }
  520. }
  521. public function AllocateEtc()
  522. {
  523. $url = "https://www.binance.com/api/v1/klines?symbol=ETCUSDT&limit=1&interval=3m";
  524. $body = Http::get($url);
  525. if (empty($body)) {
  526. return "api返回内容为空";
  527. }
  528. dump($body);
  529. // 转成数组
  530. $rsArr = json_decode($body, true);
  531. dump($rsArr[0][2]);
  532. $body = Http::get($url);
  533. if (empty($body)) {
  534. return "api返回内容为空";
  535. }
  536. dump($body);
  537. // 转成数组
  538. $rsArr = json_decode($body, true);
  539. dump($rsArr);
  540. if (empty($rsArr) || !is_array($rsArr)) {
  541. return "状态api返回数据异常";
  542. }
  543. if ($rsArr['status'] != '1') {
  544. return '状态api返回status不为1,错误信息:' . $rsArr['message'];
  545. }
  546. if ($rsArr['result']['status'] != 1) {
  547. return '状态api返回result中的status不为1,错误信息:' . $rsArr['message'];
  548. }
  549. }
  550. public function debug()
  551. {
  552. $num =rand(9000,10000);
  553. dump($num/10000/1000);
  554. $body = (new MyBscApi())->getInfoByTransactionHash('0xc3fc59faa9a1a9ff4fa1516b7df10570876a87993c397f6cd33ce3446641b5b0');
  555. dump($body);
  556. }
  557. public function resetAirdrop()
  558. {
  559. $order_list = (new ProductOrder())
  560. ->where('type_id', 3)
  561. ->group('user_id')
  562. ->column('user_id');
  563. dump($order_list);
  564. $order_list_send = (new ProductOrder())
  565. ->where('type_id', 4)
  566. ->group('user_id')
  567. ->column('user_id');
  568. dump($order_list_send);
  569. $order_list = array_diff($order_list, $order_list_send);
  570. dump($order_list);
  571. $rs = (new ProductOrder())
  572. ->where('type_id', 3)
  573. ->where('user_id', 'in', $order_list)
  574. ->update([
  575. 'create_time' => 1744819200
  576. ]);
  577. dump($rs);
  578. }
  579. public function updateNews()
  580. {
  581. $en_news = DB::name('announcement_cope')->select();
  582. foreach ($en_news as $info) {
  583. if(strlen($info['en_title']) > 3){
  584. $rs = (new AnnouncementModel())
  585. ->save([
  586. 'type_id' => $info['type_id'],
  587. 'title' => $info['en_body'],
  588. 'introduction' => $info['en_introduction'],
  589. 'body' => $info['en_body'],
  590. 'img_url' => $info['img_url'],
  591. 'to_lang' => 1,
  592. 'status' => $info['status'],
  593. 'created_by' => $info['created_by'],
  594. 'createtime' => $info['createtime'],
  595. ]);
  596. }
  597. }
  598. dump($rs);
  599. }
  600. }