line('开始消息队列'); $url = env('NOTICE_URL'); $res = apiRequest::request($url); if (!$res['result'] || $res['errCode'] != '0000') { $this->error('request err: ' . $res['desc'] ?? ''); return 1; } $arr = $res['data']['notice_list'] ?? []; while ($items = array_splice($arr, 0, 500)) { $ids = array_column($items, 'notice_id'); if (!$ids) { continue; } $data = array_column($items, null, 'notice_id'); $hadIds = Notice::query()->select('notice_id')->whereIn('notice_id', $ids)->get()->toArray(); $hadIds = array_column($hadIds, 'notice_id'); $newIds = array_diff($ids, $hadIds); if (!$newIds) { continue; } $notices = []; $time = date('Y-m-d H:i:s'); $queue = []; foreach ($newIds as $id) { $notices[] = [ 'notice_id' => $id, 'notice_type'=>$data[$id]['type'], 'raw_content' => json_encode($data[$id]), 'created_at' => $time, ]; $queue[$data[$id]['type']][] = $data[$id]; } try { Notice::insert($notices); } catch (\Exception $exception) { $this->error('insert err:' . $exception->getMessage()); continue; } foreach ($queue as $type => $item) { \App\Jobs\notice::dispatch($type, $item); } } return 0; } }