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', 'platform', 'sampling_start_time', 'sampling_end_time'])->where($where)->get()->toArray(); if (empty($config_product_data_info)) { $config_collect_product_content .= '无' . ';'; //没有采集任务不处理 return true; } else { //实际采集到的所有商品 $where = []; $where[] = ['insert_time', '>=', $start_time_string]; $where[] = ['insert_time', '<=', $end_time_string]; $where[] = ['enterprise_id', '=', $company_id]; $scrape_data_list = $ScrapeDataModel->where($where)->get()->toarray(); foreach ($config_product_data_info as $product_data) { $sampling_start_time = $product_data['sampling_start_time']; $sampling_end_time = $product_data['sampling_end_time']; $platform_id_data = explode(',', $product_data['platform']); if ($sampling_start_time != '' && $sampling_end_time != '' && ($current_time < $sampling_start_time || $current_time > $sampling_end_time)) { continue; //如果当前时间小于采集开始时间或者大于采集结束时间则跳过 } if (isset($need_config_product_data[$product_data['product_name']])) { $platform_id_data_merge = array_merge($need_config_product_data[$product_data['product_name']], $platform_id_data); $platform_id_data_merge = array_unique($platform_id_data_merge); $need_config_product_data[$product_data['product_name']] = $platform_id_data_merge; } else { $need_config_product_data[$product_data['product_name']] = $platform_id_data; } } if (empty($need_config_product_data)) { return true; //没有采集任务不处理 } //更新平台名称 foreach ($need_config_product_data as $product_name => $platform_id_data) { $platform_name_list = ''; $platform_product_name_sting = ''; foreach ($platform_id_data as $platform_id) { $platform_name = $this->platform($platform_id); //实际采集到的商品 $where = []; $where[] = ['insert_time', '>=', $start_time_string]; $where[] = ['insert_time', '<=', $end_time_string]; $where[] = ['enterprise_id', '=', $company_id]; $where[] = ['platform_id', '=', $platform_id]; $where[] = ['product_name', 'like', '%' . $product_name . '%']; // 增加模糊匹配 $actual_product_data_count = 0; if (!empty($scrape_data_list)) { foreach ($scrape_data_list as $scrape_data) { if ($scrape_data['platform_id'] == $platform_id && strpos($scrape_data['product_name'], $product_name) !== false) { $actual_product_data_count++; } } } $platform_product_name_sting .= '【' . $platform_name . '】' . ':' . $actual_product_data_count . '条'; $platform_name_list .= $platform_name . '/'; } $actual_collect_product_content .= $product_name . $platform_product_name_sting . ';'; $product_name_sting = $product_name . '【' . $platform_name_list . '】'; $config_collect_product_content .= $product_name_sting . ';'; } } //配置的低价清洗商品 $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 { //实际清洗出来的低价商品 $where = []; $where[] = ['insert_time', '>=', strtotime($start_time_string)]; $where[] = ['insert_time', '<=', strtotime($end_time_string)]; $where[] = ['company_id', '=', $company_id]; $process_low_priceGoods = $ProcessLowPriceGoodsModel->where($where)->get()->toarray(); foreach ($config_low_product_data_info as $product_name) { $config_low_product_content .= $product_name['product_name'] . ';'; $cleaned_product_data_count = 0; if (!empty($process_low_priceGoods)) { foreach ($process_low_priceGoods as $process_low_priceGood) { if (strpos($process_low_priceGood['product_name'], $product_name['product_name']) !== false) { $cleaned_product_data_count++; } } } $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 { //实际清洗出来的违规商品 $where = []; $where[] = ['insert_time', '>=', strtotime($start_time_string)]; $where[] = ['insert_time', '<=', strtotime($end_time_string)]; $where[] = ['company_id', '=', $company_id]; $process_violation_products = $ProcessViolationProductModel->where($where)->get()->toarray(); foreach ($config_violation_product_data_info as $product_name) { $config_violation_product_content .= $product_name['product_name'] . ';'; $cleaned_product_data_count = 0; if (!empty($process_violation_products)) { foreach ($process_violation_products as $process_violation_product) { if (strpos($process_violation_product['product_name'], $product_name['product_name']) !== false) { $cleaned_product_data_count++; } } $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 { //实际清洗出来的违规店铺 $where = []; $where[] = ['insert_time', '>=', strtotime($start_time_string)]; $where[] = ['insert_time', '<=', strtotime($end_time_string)]; $where[] = ['company_id', '=', $company_id]; $process_violation_store = $ProcessViolationStoreModel->where($where)->get()->toarray(); foreach ($config_violation_store_data_info as $store_name) { $config_violation_store_content .= $store_name['store_name'] . ';'; $cleaned_store_data_count = 0; if (!empty($process_violation_store)) { foreach ($process_violation_store as $process_violation_store) { if (strpos($process_violation_store['store_name'], $store_name['store_name']) !== false) { $cleaned_store_data_count++; } } } $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; //发送预警通知 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()]); } } /** * platform 平台ID定义 * */ private function platform($platform_id) { //平台0=全部,1=淘宝,2=京东,3=拼多多,4=美团,5=药师帮,6=1药城,7=药久久,8=药易购,9=药帮忙,10=熊猫药药11=药房网 switch ($platform_id) { case 1: return '淘宝'; case 2: return '京东'; case 3: return '拼多多'; case 4: return '美团'; case 5: return '药师帮'; case 6: return '1药城'; case 7: return '药久久'; case 8: return '药易购'; case 9: return '药帮忙'; case 10: return '熊猫药药'; case 11: return '药房网'; default: return ''; } } /** * 构建美观的邮件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 '