1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2022/12/29
- * Time: 15:43
- */
- namespace App\Console\Repair;
- use App\Models\WarnGroup;
- use App\Models\WarnGroupDjuser;
- use App\Models\WarnGroupUser;
- use App\Models\WarnUser;
- use Illuminate\Console\Command;
- class WarnGroupDataRepair extends Command
- {
- protected $signature = 'WarnGroupDataRepair';
- protected $description = '预警组信息修补';
- public function __construct()
- {
- parent::__construct();
- }
- public function handle()
- {
- $this->info(date('H:i') . ' 开始处理预警组信息');
- $groupList = WarnGroup::query()->get();
- foreach($groupList as $groupInfo) {
- $userList = WarnGroupUser::query()->where('group_id', $groupInfo->id)->where('enable', 1)
- ->get();
- $userIdList = $userList->isNotEmpty() ? array_column($userList->toArray(), 'user_id') : [];
- $userListStr = implode(',', $userIdList);
- $djUserList = WarnGroupDjuser::query()->where('group_id', $groupInfo->id)->where('enable', 1)
- ->selectRaw('djcorp_id as corpid, djuser_id as user_id')->get();
- $djUserListStr = $djUserList->isNotEmpty() ? json_encode($djUserList, 256) : [];
- $update = [
- 'user_list' => $userListStr,
- 'dj_user_list' => $djUserListStr,
- ];
- WarnGroup::query()->where('id', $groupInfo->id)->update($update);
- }
- $this->info(date('H:i') . ' 预警组信息处理完成,开始清除已经失效的预警人ID');
- $warnUserList = WarnUser::query()->where('enable', 0)->get();
- if($warnUserList->isEmpty()) {
- $this->info(date('H:i') . ' 暂无已失效的预警人,处理完成');
- return true;
- }
- foreach($warnUserList as $value) {
- $userId = $value['id'];
- $warnGroupList = WarnGroup::query()->whereRaw('find_in_set('.$userId.',`user_list`)')->get();
- if($warnGroupList->isNotEmpty()) {
- foreach($warnGroupList as $groupInfo) {
- $userList = explode(',', $groupInfo->user_list);
- $key = array_search($userId, $userList);
- unset($userList[$key]);
- $newUserList = implode(',', $userList);
- WarnGroup::query()->where('id', $groupInfo->id)->update(['user_list' => $newUserList]);
- }
- }
- }
- $this->info(date('H:i') . ' 清除已经失效的预警人ID完成,全部信息处理完成');
- return true;
- }
- }
|