企微短剧业务系统

WarnGroupDataRepair.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/12/29
  6. * Time: 15:43
  7. */
  8. namespace App\Console\Repair;
  9. use App\Models\WarnGroup;
  10. use App\Models\WarnGroupDjuser;
  11. use App\Models\WarnGroupUser;
  12. use App\Models\WarnUser;
  13. use Illuminate\Console\Command;
  14. class WarnGroupDataRepair extends Command
  15. {
  16. protected $signature = 'WarnGroupDataRepair';
  17. protected $description = '预警组信息修补';
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. }
  22. public function handle()
  23. {
  24. $this->info(date('H:i') . ' 开始处理预警组信息');
  25. $groupList = WarnGroup::query()->get();
  26. foreach($groupList as $groupInfo) {
  27. $userList = WarnGroupUser::query()->where('group_id', $groupInfo->id)->where('enable', 1)
  28. ->get();
  29. $userIdList = $userList->isNotEmpty() ? array_column($userList->toArray(), 'user_id') : [];
  30. $userListStr = implode(',', $userIdList);
  31. $djUserList = WarnGroupDjuser::query()->where('group_id', $groupInfo->id)->where('enable', 1)
  32. ->selectRaw('djcorp_id as corpid, djuser_id as user_id')->get();
  33. $djUserListStr = $djUserList->isNotEmpty() ? json_encode($djUserList, 256) : [];
  34. $update = [
  35. 'user_list' => $userListStr,
  36. 'dj_user_list' => $djUserListStr,
  37. ];
  38. WarnGroup::query()->where('id', $groupInfo->id)->update($update);
  39. }
  40. $this->info(date('H:i') . ' 预警组信息处理完成,开始清除已经失效的预警人ID');
  41. $warnUserList = WarnUser::query()->where('enable', 0)->get();
  42. if($warnUserList->isEmpty()) {
  43. $this->info(date('H:i') . ' 暂无已失效的预警人,处理完成');
  44. return true;
  45. }
  46. foreach($warnUserList as $value) {
  47. $userId = $value['id'];
  48. $warnGroupList = WarnGroup::query()->whereRaw('find_in_set('.$userId.',`user_list`)')->get();
  49. if($warnGroupList->isNotEmpty()) {
  50. foreach($warnGroupList as $groupInfo) {
  51. $userList = explode(',', $groupInfo->user_list);
  52. $key = array_search($userId, $userList);
  53. unset($userList[$key]);
  54. $newUserList = implode(',', $userList);
  55. WarnGroup::query()->where('id', $groupInfo->id)->update(['user_list' => $newUserList]);
  56. }
  57. }
  58. }
  59. $this->info(date('H:i') . ' 清除已经失效的预警人ID完成,全部信息处理完成');
  60. return true;
  61. }
  62. }