V2173.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. namespace App\Console\Commands\Upgrade;
  3. use App\Models\Deed\EachLikeModel;
  4. use App\Models\Deed\FriendsLogModel;
  5. use App\Models\Deed\InvitationCardModel;
  6. use App\Models\Fpdx\RoomModel;
  7. use App\Models\Friends\FriendApplyModel;
  8. use App\Models\Friends\FriendModel;
  9. use App\Models\Goodnight\RoomMemberModel;
  10. use Illuminate\Console\Command;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Redis;
  13. class V2173 extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'upgrade:V2173 {task} {--d}';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = '好友列表优化批量处理数据脚本:提取旧的数据[晚安匹配|72h匹配|开黑匹配|好友申请|相互心动|取消心动]的数据按照新的数据格式存储';
  27. protected $take = 50000;
  28. /**
  29. * Create a new command instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. }
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return mixed
  41. */
  42. public function handle()
  43. {
  44. $deamon = $this->option('d');
  45. $task = $this->argument('task');
  46. switch ($task) {
  47. case "oldInvite":
  48. $this->oldInvite($deamon);
  49. break;
  50. case 'newInvite':
  51. $this->newInvite($deamon);
  52. break;
  53. case 'agree':
  54. $this->agree($deamon);
  55. break;
  56. case 'pair':
  57. $this->pair($deamon);
  58. break;
  59. case 'gteam':
  60. $this->gteam($deamon);
  61. break;
  62. case 'goodnight':
  63. $this->goodnight($deamon);
  64. break;
  65. case 'eachlike':
  66. $this->eachlike($deamon);
  67. break;
  68. case 'uneachlike':
  69. $this->unEachlike($deamon);
  70. break;
  71. case 'hidefriend':
  72. $this->hidefriend($deamon);
  73. break;
  74. }
  75. }
  76. // 相互心动
  77. public function eachlike($deamon = false)
  78. {
  79. $bar = null;
  80. if (!$deamon) {
  81. $last_id = Redis::hget("v2173", "eachlike") ?? 0;
  82. $bar = $this->output->createProgressBar(EachLikeModel::where('id', '>', $last_id)->count());
  83. $bar->start();
  84. }
  85. do {
  86. $last_id = Redis::hget("v2173", "eachlike") ?? 0;
  87. $eachlikes = EachLikeModel::where('id', '>', $last_id)->take($this->take)->get();
  88. foreach ($eachlikes as $eachlike) {
  89. /** @var EachLikeModel $eachlike */
  90. DB::table('kdgx_fpdx_friends_log_tmp')->insert([
  91. 'uid' => $eachlike->uid,
  92. 'tuid' => $eachlike->tuid,
  93. 'created_at' => $eachlike->created_at,
  94. 'do' => 0,
  95. 'attach' => ""
  96. ]);
  97. Redis::hset("v2173", "eachlike", $eachlike->id);
  98. }
  99. if (!$deamon) {
  100. $bar->advance($this->take);
  101. }
  102. } while ($eachlikes->isNotEmpty());
  103. if (!$deamon) {
  104. $bar->finish();
  105. }
  106. }
  107. // 取消心动
  108. public function unEachlike($deamon = false)
  109. {
  110. $bar = null;
  111. if (!$deamon) {
  112. $last_id = Redis::hget("v2173", "uneachlike") ?? 0;
  113. $bar = $this->output->createProgressBar(FriendsLogModel::whereDo(1)->where('id', '>', $last_id)->count());
  114. $bar->start();
  115. }
  116. do {
  117. $last_id = Redis::hget("v2173", "uneachlike") ?? 0;
  118. $eachlikes = FriendsLogModel::whereDo(1)->where('id', '>', $last_id)->take($this->take)->get();
  119. foreach ($eachlikes as $eachlike) {
  120. /** @var EachLikeModel $eachlike */
  121. DB::table('kdgx_fpdx_friends_log_v2_17_3')->insert([
  122. 'uid' => $eachlike->uid,
  123. 'tuid' => $eachlike->tuid,
  124. 'created_at' => $eachlike->created_at->timestamp,
  125. 'do' => 1,
  126. 'attach' => ""
  127. ]);
  128. Redis::hset("v2173", "uneachlike", $eachlike->id);
  129. if (!$deamon) {
  130. $bar->advance(1);
  131. }
  132. }
  133. } while ($eachlikes->isNotEmpty());
  134. if (!$deamon) {
  135. $bar->finish();
  136. }
  137. }
  138. // 旧的好友申请
  139. public function oldInvite($deamon = false)
  140. {
  141. $bar = null;
  142. if (!$deamon) {
  143. $last_id = Redis::hget("v2173", "old_invite") ?? 0;
  144. $bar = $this->output->createProgressBar(InvitationCardModel::where([
  145. ['uid', '!=', 0],
  146. ['invite_uid', '!=', 0]
  147. ])->where('id', '>', $last_id)->whereNull('kk')->count());
  148. $bar->start();
  149. }
  150. do {
  151. $last_id = Redis::hget("v2173", "old_invite") ?? 0;
  152. $invitations = InvitationCardModel::where([
  153. ['uid', '!=', 0],
  154. ['invite_uid', '!=', 0]
  155. ])->where('id', '>', $last_id)->whereNull('kk')->orderBy('id', 'asc')->take($this->take)->get();
  156. foreach ($invitations as $invitation) {
  157. /** @var InvitationCardModel $invitation */
  158. DB::table('kdgx_fpdx_friends_log_v2_17_3')->insert([
  159. 'uid' => $invitation->uid,
  160. 'tuid' => $invitation->invite_uid,
  161. 'created_at' => $invitation->created_at->timestamp,
  162. 'do' => 3,
  163. 'attach' => json_encode([
  164. [
  165. 'type' => -1,
  166. 'question' => 0,
  167. 'answer' => []
  168. ]
  169. ])
  170. ]);
  171. /** @var FriendApplyModel $send */
  172. $send = FriendApplyModel::firstOrNew([
  173. 'uid' => $invitation->uid,
  174. 'apply_uid' => $invitation->invite_uid
  175. ], [
  176. 'uid' => $invitation->uid,
  177. 'apply_uid' => $invitation->invite_uid
  178. ]);
  179. $send->last_at = $invitation->created_at->timestamp;
  180. $send->is_apply = 1;
  181. $send->last_msg = "向ta发出了好友申请";
  182. /** @var FriendApplyModel $apply */
  183. $apply = FriendApplyModel::firstOrNew([
  184. 'uid' => $invitation->invite_uid,
  185. 'apply_uid' => $invitation->uid
  186. ], [
  187. 'uid' => $invitation->invite_uid,
  188. 'apply_uid' => $invitation->uid
  189. ]);
  190. $apply->last_at = $invitation->created_at->timestamp;
  191. $apply->is_receive = 1;
  192. $apply->last_msg = "收到ta发出的好友申请";
  193. $send->save() && $apply->save();
  194. Redis::hset("v2173", "old_invite", $invitation->id);
  195. if (!$deamon) {
  196. $bar->advance(1);
  197. }
  198. }
  199. } while ($invitations->isNotEmpty());
  200. if (!$deamon) {
  201. $bar->finish();
  202. }
  203. }
  204. // 新的好友申请
  205. public function newInvite($deamon = false)
  206. {
  207. $bar = null;
  208. if (!$deamon) {
  209. $last_id = Redis::hget("v2173", "new_invite") ?? 0;
  210. $bar = $this->output->createProgressBar(InvitationCardModel::where([
  211. ['uid', '!=', 0],
  212. ['invite_uid', '!=', 0]
  213. ])->where('id', '>', $last_id)->whereNotNull('kk')->count());
  214. $bar->start();
  215. }
  216. do {
  217. $last_id = Redis::hget("v2173", "new_invite") ?? 0;
  218. $invitations = InvitationCardModel::where([
  219. ['uid', '!=', 0],
  220. ['invite_uid', '!=', 0]
  221. ])->where('id', '>', $last_id)->whereNotNull('kk')->orderBy('id', 'asc')->take($this->take)->get();
  222. foreach ($invitations as $invitation) {
  223. /** @var InvitationCardModel $invitation */
  224. try {
  225. $question = array();
  226. $uid = $invitation->uid;
  227. $tuid = $invitation->invite_uid;
  228. $time = $invitation->created_at->timestamp;
  229. $tmp = array(
  230. 'type' => $invitation->question_type,
  231. 'question' => 0 != $invitation->question_type ? $invitation->question_id : 0,
  232. 'answer' => [
  233. 'type' => 'text',
  234. 'value' => ""
  235. ]
  236. );
  237. switch ($invitation->question_type) {
  238. case 0:
  239. $tmp['answer']['type'] = "text";
  240. $tmp['answer']['value'] = $invitation->say_hello;
  241. break;
  242. case 2:
  243. case 3:
  244. if (strpos('oss.pocketuniversity.cn', $invitation->question_answer)) {
  245. $tmp['answer']['type'] = "url";
  246. $tmp['answer']['value'] = $invitation->question_answer;
  247. } else {
  248. $imp = explode(":", $invitation->question_answer);
  249. $tmp['answer']['type'] = $imp[0];
  250. $tmp['answer']['value'] = $imp[1];
  251. }
  252. break;
  253. default:
  254. break;
  255. }
  256. array_push($question, $tmp);
  257. DB::table('kdgx_fpdx_friends_log_tmp')->insert([
  258. 'uid' => $uid,
  259. 'tuid' => $tuid,
  260. 'created_at' => $time,
  261. 'do' => 3,
  262. 'attach' => json_encode($question)
  263. ]);
  264. /** @var FriendApplyModel $send */
  265. $send = FriendApplyModel::firstOrNew(
  266. array('uid' => $uid, 'apply_uid' => $tuid),
  267. ['uid' => $uid, 'apply_uid' => $tuid]
  268. );
  269. $send->last_at = $time;
  270. $send->is_apply = 1;
  271. $send->last_msg = "向ta发出了好友申请";
  272. /** @var FriendApplyModel $apply */
  273. $apply = FriendApplyModel::firstOrNew(
  274. array('uid' => $tuid, 'apply_uid' => $uid),
  275. ['uid' => $tuid, 'apply_uid' => $uid]
  276. );
  277. $apply->last_at = $time;
  278. $apply->is_receive = 1;
  279. $apply->last_msg = "收到ta发出的好友申请";
  280. $send->save() && $apply->save();
  281. } catch (\Exception $exception) {
  282. }
  283. Redis::hset("v2173", "new_invite", $invitation->id);
  284. }
  285. if (!$deamon) {
  286. $bar->advance($this->take);
  287. }
  288. } while ($invitations->isNotEmpty());
  289. if (!$deamon) {
  290. $bar->finish();
  291. }
  292. }
  293. // 同意申请
  294. public function agree($deamon = false)
  295. {
  296. $bar = null;
  297. if (!$deamon) {
  298. $last_id = Redis::hget("v2173", "agree") ?? 0;
  299. $bar = $this->output->createProgressBar(InvitationCardModel::where('id', '>', $last_id)
  300. ->where('receive_at', '>', 0)->count());
  301. $bar->start();
  302. }
  303. do {
  304. $last_id = Redis::hget("v2173", "agree") ?? 0;
  305. $applys = InvitationCardModel::where('id', '>', $last_id)
  306. ->where('receive_at', '>', 0)
  307. ->take($this->take)
  308. ->get();
  309. foreach ($applys as $apply) {
  310. if (!$deamon) {
  311. $bar->advance(1);
  312. }
  313. /** @var InvitationCardModel $apply */
  314. if (
  315. DB::table('kdgx_fpdx_friends_log_v2_17_3')->where([
  316. ['uid', $apply->invite_uid],
  317. ['tuid', $apply->uid],
  318. ['do', 4]
  319. ])->exists()
  320. ) {
  321. continue;
  322. }
  323. DB::table('kdgx_fpdx_friends_log_v2_17_3')->insert([
  324. 'uid' => $apply->invite_uid,
  325. 'tuid' => $apply->uid,
  326. 'created_at' => $apply->receive_at,
  327. 'do' => 4
  328. ]);
  329. Redis::hset("v2173", "agree", $apply->id);
  330. }
  331. } while ($applys->isNotEmpty());
  332. if (!$deamon) {
  333. $bar->finish();
  334. }
  335. }
  336. // 72h匹配
  337. public function pair($deamon = false)
  338. {
  339. $bar = null;
  340. if (!$deamon) {
  341. $last_id = Redis::hget("v2173", "pair") ?? 0;
  342. $bar = $this->output->createProgressBar(RoomModel::where('room_id', '>', $last_id)
  343. ->where('type', 1)->count());
  344. $bar->start();
  345. }
  346. do {
  347. $last_id = Redis::hget("v2173", "pair") ?? 0;
  348. $rooms = RoomModel::where('room_id', '>', $last_id)->where('type', 1)->take($this->take)
  349. ->get(['room_id', 'stage_id', 'member', 'create_time']);
  350. foreach ($rooms as $room) {
  351. /** @var RoomModel $room */
  352. DB::table('kdgx_fpdx_friends_log_tmp')->insert([
  353. 'uid' => $room->member[0],
  354. 'tuid' => $room->member[1],
  355. 'created_at' => $room->create_time,
  356. 'do' => 2,
  357. 'attach' => json_encode([
  358. 'room_id' => $room->room_id,
  359. 'stage_id' => $room->stage_id,
  360. 'created_at' => $room->create_time
  361. ])
  362. ]);
  363. Redis::hset("v2173", "pair", $room->room_id);
  364. if (!$deamon) {
  365. $bar->advance(1);
  366. }
  367. }
  368. } while ($rooms->isNotEmpty());
  369. if (!$deamon) {
  370. $bar->finish();
  371. }
  372. }
  373. // 晚安匹配
  374. public function goodnight($deamon = false)
  375. {
  376. $bar = null;
  377. if (!$deamon) {
  378. $last_id = Redis::hget("v2173", "goodnight") ?? 0;
  379. $bar = $this->output->createProgressBar(\App\Models\Goodnight\RoomModel::where(
  380. 'id',
  381. '>',
  382. $last_id
  383. )->count());
  384. $bar->start();
  385. }
  386. do {
  387. $last_id = Redis::hget("v2173", "goodnight") ?? 0;
  388. $rooms = \App\Models\Goodnight\RoomModel::where('id', '>', $last_id)->take($this->take)->get([
  389. 'id',
  390. 'created_at'
  391. ]);
  392. foreach ($rooms as $room) {
  393. /** @var \App\Models\Goodnight\RoomModel $room */
  394. $members = RoomMemberModel::where('room_id', $room->id)->get(['uid'])->toArray();
  395. $uid = $members[0]['uid'];
  396. $tuid = $members[1]['uid'];
  397. DB::table('kdgx_fpdx_friends_log_tmp')->insert([
  398. 'uid' => $uid,
  399. 'tuid' => $tuid,
  400. 'created_at' => $room->created_at->timestamp,
  401. 'do' => 7,
  402. 'attach' => json_encode([
  403. 'id' => $room->id,
  404. 'created_at' => $room->created_at->timestamp
  405. ])
  406. ]);
  407. Redis::hset("v2173", "goodnight", $room->id);
  408. }
  409. if (!$deamon) {
  410. $bar->advance($this->take);
  411. }
  412. } while ($rooms->isNotEmpty());
  413. if (!$deamon) {
  414. $bar->finish();
  415. }
  416. }
  417. // 开黑
  418. public function gteam($deamon = false)
  419. {
  420. $bar = null;
  421. if (!$deamon) {
  422. $last_id = Redis::hget("v2173", "gteam") ?? 0;
  423. $bar = $this->output->createProgressBar(\App\Models\Gteam\RoomModel::where('id', '>', $last_id)->count());
  424. $bar->start();
  425. }
  426. do {
  427. $last_id = Redis::hget("v2173", "gteam") ?? 0;
  428. $rooms = \App\Models\Gteam\RoomModel::where('id', '>', $last_id)->take($this->take)->get([
  429. 'id',
  430. 'created_at',
  431. 'type'
  432. ]);
  433. foreach ($rooms as $room) {
  434. /** @var \App\Models\Gteam\RoomModel $room */
  435. $members = \App\Models\Gteam\RoomMemberModel::where('room_id', $room->id)->get(['uid'])->toArray();
  436. $uid = $members[0]['uid'];
  437. $tuid = $members[1]['uid'];
  438. if (in_array($room->type, ['cj_wx', 'cj_qq'])) {
  439. $do = 6;
  440. } else {
  441. $do = 5;
  442. }
  443. DB::table('kdgx_fpdx_friends_log_tmp')->insert([
  444. 'uid' => $uid,
  445. 'tuid' => $tuid,
  446. 'created_at' => $room->created_at->timestamp,
  447. 'do' => $do,
  448. 'attach' => json_encode([
  449. 'id' => $room->id,
  450. 'type' => $room->type,
  451. 'created_at' => $room->created_at->timestamp
  452. ])
  453. ]);
  454. Redis::hset("v2173", "gteam", $room->id);
  455. }
  456. if (!$deamon) {
  457. $bar->advance($this->take);
  458. }
  459. } while ($rooms->isNotEmpty());
  460. if (!$deamon) {
  461. $bar->finish();
  462. }
  463. }
  464. public function hidefriend($deamon = false)
  465. {
  466. $bar = null;
  467. if (!$deamon) {
  468. $last_id = Redis::hget("v2173", "hidefriend") ?? 0;
  469. $bar = $this->output->createProgressBar(FriendModel::where('is_hide', 1)->where(
  470. 'id',
  471. '>',
  472. $last_id
  473. )->count());
  474. $bar->start();
  475. }
  476. do {
  477. $last_id = Redis::hget("v2173", "hidefriend") ?? 0;
  478. $friends = FriendModel::where('is_hide', 1)->where('id', '>', $last_id)->take($this->take)->get();
  479. foreach ($friends as $friend) {
  480. /** @var FriendModel $friend */
  481. /** @var FriendModel $other */
  482. $other = FriendModel::where([
  483. ['uid', $friend->friend_uid],
  484. ['friend_uid', $friend->uid]
  485. ])->first();
  486. $friend->is_friend = 0;
  487. $other->is_friend = 0;
  488. $friend->save();
  489. $other->save();
  490. Redis::hset("v2173", "hidefriend", $friend->id);
  491. }
  492. if (!$deamon) {
  493. $bar->advance($this->take);
  494. }
  495. } while ($friends->isNotEmpty());
  496. if (!$deamon) {
  497. $bar->finish();
  498. }
  499. }
  500. }