Test.php 18 KB

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