message_data = $message_data; } /** * Execute the job. * * @return void */ public function handle() { try { $ProductModel = new ProductModel(); $ScrapeDataModel = new ScrapeDataModel(); $ProcessLowPriceGoodsModel = new ProcessLowPriceGoodsModel(); $WashConfigLowPriceGoodsModel = new WashConfigLowPriceGoodsModel(); $ProcessViolationProductModel = new ProcessViolationProductModel(); $WashConfigViolationProductModel = new WashConfigViolationProductModel(); $ProcessViolationStoreModel = new ProcessViolationStoreModel(); $WashConfigViolationStoreModel = new WashConfigViolationStoreModel(); $SurveillanceWarningNoticeModel = new SurveillanceWarningNoticeModel(); //获取已经开启邮箱通知的用户 $email_notice_list = $SurveillanceWarningNoticeModel->where([['email', '!=', '']])->where('status', 0)->get()->toarray(); if (empty($email_notice_list)) return true; $company_id = isset($this->message_data['company_id']) ? $this->message_data['company_id'] : 0; //品牌方公司ID $company_info = $this->message_data['company_info']; $start_time = $this->message_data['start_time']; $end_time = $this->message_data['end_time']; $start_time_string = date('Y-m-d H:i:s', $start_time); $end_time_string = date('Y-m-d H:i:s', $end_time); $key_name = 'LowPriceProductSurveillanceJobs_' . $company_id . '_' . $start_time . '_' . $end_time; $is_end_select = Cache::get($key_name); if (!empty($is_end_select)) return true; $config_collect_product_content = ''; //配置采集商品任务: $actual_collect_product_content = ''; //实际采集到的商品: $config_low_product_content = ''; //配置的低价清洗商品: $cleaned_low_product_content = ''; //实际清洗出来的低价商品: $config_violation_product_content = ''; //配置的违规清洗商品: $cleaned_violation_product_content = ''; //实际清洗出来的违规商品: $config_violation_store_content = ''; //配置的违规店铺: $cleaned_violation_store_content = ''; //实际清洗出来的违规店铺: $current_time = time(); // 获取采集时间是星期几 $now_week = date('w', $current_time); if ($now_week == 0) { $now_week = 7; } //配置采集商品任务商品查询 $where = []; $where[] = ['company_id', '=', $company_id]; $where[] = ['status', '=', 0]; $need_config_product_data = []; $config_product_data_info = $ProductModel->WhereRaw("FIND_IN_SET(?, sampling_cycle)", [$now_week])->select(['product_name', 'sampling_start_time', 'sampling_end_time'])->where($where)->get()->toArray(); if (empty($config_product_data_info)) { $config_collect_product_content .= '无' . ';'; //没有采集任务不处理 return true; } else { $config_collect_product_list = []; foreach ($config_product_data_info as $product_name) { $sampling_start_time = $product_name['sampling_start_time']; $sampling_end_time = $product_name['sampling_end_time']; if ($sampling_start_time != '' && $sampling_end_time != '' && ($current_time < $sampling_start_time || $current_time > $sampling_end_time)) { continue; //如果当前时间小于采集开始时间或者大于采集结束时间则跳过 } $need_config_product_data[$product_name['product_name']] = $product_name; } if (empty($need_config_product_data)) { return true; //没有采集任务不处理 } foreach ($need_config_product_data as $product_name) { $config_collect_product_content .= $product_name['product_name'] . ';'; //实际采集到的商品 $where = []; $where[] = ['insert_time', '>=', $start_time_string]; $where[] = ['insert_time', '<=', $end_time_string]; $where[] = ['enterprise_id', '=', $company_id]; $where[] = ['product_name', 'like', '%' . $product_name['product_name'] . '%']; // 增加模糊匹配 $actual_product_data_count = $ScrapeDataModel->where($where)->count(); if ($actual_product_data_count < 1) { $actual_collect_product_content .= '无' . ';'; } else { $actual_collect_product_content .= $product_name['product_name'] . ':数量' . $actual_product_data_count . ';'; } } } //配置的低价清洗商品 $where = []; $where[] = ['company_id', '=', $company_id]; $where[] = ['status', '=', 0]; $config_low_product_data_info = $WashConfigLowPriceGoodsModel->select('product_name')->where($where)->groupBy('product_name')->get()->toArray(); if (empty($config_low_product_data_info)) { $config_low_product_content .= '无' . ';'; } else { foreach ($config_low_product_data_info as $product_name) { $config_low_product_content .= $product_name['product_name'] . ';'; //实际清洗出来的低价商品 $where = []; $where[] = ['insert_time', '>=', strtotime($start_time_string)]; $where[] = ['insert_time', '<=', strtotime($end_time_string)]; $where[] = ['company_id', '=', $company_id]; $where[] = ['product_name', 'like', '%' . $product_name['product_name'] . '%']; // 增加模糊匹配 $cleaned_product_data_count = $ProcessLowPriceGoodsModel->where($where)->count(); if ($cleaned_product_data_count < 1) { $cleaned_low_product_content .= '无' . ';'; } else { $cleaned_low_product_content .= $product_name['product_name'] . ':数量' . $cleaned_product_data_count . ';'; } } } //配置的违规清洗商品 $where = []; $where[] = ['company_id', '=', $company_id]; $where[] = ['insert_time', '>=', $start_time_string]; $config_violation_product_data_info = $WashConfigViolationProductModel->select('product_name')->where($where)->groupBy('product_name')->get()->toArray(); if (empty($config_violation_product_data_info)) { $config_violation_product_content .= '无' . ';'; } else { foreach ($config_violation_product_data_info as $product_name) { $config_violation_product_content .= $product_name['product_name'] . ';'; //实际清洗出来的违规商品 $where = []; $where[] = ['insert_time', '>=', strtotime($start_time_string)]; $where[] = ['insert_time', '<=', strtotime($end_time_string)]; $where[] = ['company_id', '=', $company_id]; $where[] = ['product_name', 'like', '%' . $product_name['product_name'] . '%']; // 增加模糊匹配 $cleaned_product_data_count = $ProcessViolationProductModel->where($where)->count(); if ($cleaned_product_data_count < 1) { $cleaned_violation_product_content .= '无' . ';'; } else { $cleaned_violation_product_content .= $product_name['product_name'] . ':数量' . $cleaned_product_data_count . ';'; } } } //配置的违规店铺 $where = []; $where[] = ['company_id', '=', $company_id]; $where[] = ['insert_time', '<=', $end_time_string]; $config_violation_store_data_info = $WashConfigViolationStoreModel->select('store_name')->where($where)->groupBy('store_name')->get()->toArray(); if (empty($config_violation_store_data_info)) { $config_violation_store_content .= '无' . ';'; } else { foreach ($config_violation_store_data_info as $store_name) { $config_violation_store_content .= $store_name['store_name'] . ';'; //实际清洗出来的违规店铺 $where = []; $where[] = ['insert_time', '>=', strtotime($start_time_string)]; $where[] = ['insert_time', '<=', strtotime($end_time_string)]; $where[] = ['company_id', '=', $company_id]; $where[] = ['store_name', 'like', '%' . $store_name['store_name'] . '%']; // 增加模糊匹配 $cleaned_store_data_count = $ProcessViolationStoreModel->where($where)->count(); if ($cleaned_store_data_count < 1) { $cleaned_violation_store_content .= '无' . ';'; } else { $cleaned_violation_store_content .= $store_name['store_name'] . ':数量' . $cleaned_store_data_count . ';'; } } } //合并展示内容 $email_title = '【' . $company_info['company_name'] . '】' . "数据监控-采集和清洗情况通知"; $email_content = $config_collect_product_content . $actual_collect_product_content . $config_low_product_content . $cleaned_low_product_content . $config_violation_product_content . $cleaned_violation_product_content . $config_violation_store_content . $cleaned_violation_store_content; $email_content_html = $this->buildEmailHtml( $config_collect_product_content, $actual_collect_product_content, $config_low_product_content, $cleaned_low_product_content, $config_violation_product_content, $cleaned_violation_product_content, $config_violation_store_content, $cleaned_violation_store_content, $start_time_string, $end_time_string ); // echo $email_content_html;exit; //发送预警通知 foreach ($email_notice_list as $email_notice) { $message_data = [ 'email' => $email_notice['email'], 'email_content' => json_encode(['title' => $email_title, 'content' => $email_content_html]), ]; SendEmailJobs::dispatch(['notice_data_info' => $message_data]); } //记录日志 } catch (\Exception $e) { Log::info('job_error', '低价商品数据数据-监控采集清洗情况队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]); } } /** * 构建美观的邮件HTML内容(内联样式版本) */ private function buildEmailHtml( $config_collect, $actual_collect, $config_low, $cleaned_low, $config_violation_product, $cleaned_violation_product, $config_violation_store, $cleaned_violation_store, $start_time_string, $end_time_string ) { $html = '
'; return $html; } /** * 构建单个数据区块(内联样式版本) */ private function buildSectionInline($title, $content, $color) { $formattedContent = $this->formatContentInline($content); return '