Teac.php 18 KB

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