| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- namespace App\Jobs\Manager\Process;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldBeUnique;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use App\Facades\Servers\Logs\Log;
- use App\Models\Manager\Process\ViolationProduct as ViolationProductModel;
- use App\Models\Manager\Personnel\Employee as EmployeeModel;
- use App\Models\Api\Process\ExecuteLog as ExecuteLogModel;
- use App\Models\Manager\Process\ScrapeData as ScrapeDataModel;
- use App\Models\Manager\WashConfig\ViolationStore as ViolationStoreModel;
- use App\Models\Manager\WashConfig\ViolationCompanyMember as ViolationCompanyMemberModel;
- use App\Models\Manager\Citys as CitysModel;
- /**
- * 数据清洗-违规挂网商品数据队列
- * @author 唐远望
- * @version 1.0
- * @date 2025-12-10
- */
- class ViolationProductDataJobs implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- protected $message_data;
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct(array $message_data)
- {
- $this->message_data = $message_data;
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- try {
- $this->getViolationProductData($this->message_data);
- } catch (\Exception $e) {
- Log::info('job_error', '数据清洗-违规挂网商品数据队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
- }
- }
- /**
- * 采集商品数据清洗
- * @author 唐远望
- * @version 1.0
- * @date 2025-12-10
- */
- public function getViolationProductData($message_data)
- {
- $CitysModel = new CitysModel();
- $EmployeeModel = new EmployeeModel();
- $ViolationProductModel = new ViolationProductModel();
- $ScrapeDataModel = new ScrapeDataModel();
- $ViolationStoreModel = new ViolationStoreModel();
- $ViolationCompanyMemberModel = new ViolationCompanyMemberModel();
- $platform = $message_data['platform']; //多个平台配置
- $product_name = $message_data['product_name']; //商品名称
- $product_specs = $message_data['product_specs']; //商品规格
- $store_scope = $message_data['store_scope']; //店铺范围1=全部店铺2=指定店铺
- $company_scope = $message_data['company_scope']; //公司范围1=全部公司2=指定公司
- $social_credit_code = $message_data['social_credit_code']; //社会信用代码
- $executeLog_id = $message_data['executeLog_id'];
- $category_name = $message_data['category_name']; //产品分类名称
- $specify_responsible_person = $message_data['specify_responsible_person']; //指派责任人 0=开启 1=关闭
- $limit = isset($message_data['limit']) ? $message_data['limit'] : 50;
- $page = isset($message_data['page']) ? $message_data['page'] : 1;
- $company_id = isset($message_data['company_id']) ? $message_data['company_id'] : 0;
- $where = [];
- if ($platform != '0') {
- $platform = explode(',', $platform);
- $ScrapeDataModel = $ScrapeDataModel->whereIn('platform_id', $platform);
- }
- $where[] = ['product_name', 'like', '%' . $product_name . '%'];
- $where[] = ['product_specs', 'like', '%' . $product_specs . '%'];
- if ($company_scope == 2) {
- $ScrapeDataModel->whereIn('qualification_number', $social_credit_code);
- }
- $product_data_info = $ScrapeDataModel->where($where)->paginate($limit, ['*'], 'page', $page)->toarray();
- $product_datas = $product_data_info['data'];
- if (empty($product_datas)) {
- if ($executeLog_id) {
- (new ExecuteLogModel())->where('id', $executeLog_id)->update(['status' => 0]);
- }
- return true;
- }
- foreach ($product_datas as $product_data) {
- $province_name = $product_data['province_name'];
- $city_name = $product_data['city_name'];
- //特殊地区1级移除市
- if ($province_name && in_array($province_name, ['北京市', '天津市', '上海市', '重庆市'])) {
- //移除市这个字符
- $province_name = trim(str_replace('市', '', $province_name));
- }else if ($province_name && in_array($province_name, ['北京', '天津', '上海', '重庆'])) {
-
- } else if ($province_name && in_array($province_name, ['内蒙古', '广西', '西藏', '新疆', '宁夏'])) {
- switch ($province_name) {
- case '内蒙古':
- $province_name = '内蒙古自治区';
- break;
- case '广西':
- $province_name = '广西壮族自治区';
- break;
- case '西藏':
- $province_name = '西藏自治区';
- break;
- case '新疆':
- $province_name = '新疆维吾尔自治区';
- break;
- case '宁夏':
- $province_name = '宁夏回族自治区';
- break;
- }
- } else if ($province_name && in_array($province_name, ['内蒙古自治区', '广西壮族自治区', '西藏自治区', '新疆维吾尔自治区', '宁夏回族自治区'])) {
-
- } else if (strpos($province_name, '省') === false) {
- //是否存在市省,如果不存在则补全
- if (strpos($province_name, '省') === false) {
- $province_name = $province_name . '省';
- }
- }
- if ($city_name != '') {
- $city_name = $CitysModel->where([['id', '=',$product_data['city_id']], ['level', '=', '2']])->value('name');
- }
- $insert_product_data = [
- 'company_id' => $company_id,
- 'source_id' => $product_data['id'],
- 'platform' => $product_data['platform_id'],
- 'company_name' => $product_data['company_name'],
- 'product_name' => $product_name,
- 'product_specs' => $product_specs,
- 'inventory' => $product_data['inventory'] ? $product_data['inventory'] : '',
- 'sales' => $product_data['sales'] ? $product_data['sales'] : '',
- 'snapshot_url' => $product_data['snapshot_url'] ? $product_data['snapshot_url'] : '',
- 'online_posting_count' => $product_data['online_posting_count'],
- 'continuous_listing_count' => $product_data['continuous_listing_count'],
- 'link_url' => $product_data['link_url'],
- 'store_name' => $product_data['store_name'],
- 'social_credit_code' => $product_data['qualification_number'],
- 'province_id' => $product_data['province_id'],
- 'province_name' => $province_name,
- 'city_id' => $product_data['city_id'],
- 'city_name' => $city_name,
- 'area_info' => $product_data['area_info'],
- 'category_name' => $category_name,
- 'company_category_name' => '',
- 'first_responsible_person' => '',
- 'responsible_person' => '',
- 'source_responsible_person' => '',
- 'scrape_date' => $product_data['scrape_date'],
- ];
- //获取公司绑定责任人信息
- $company_data = $ViolationStoreModel->leftjoin('washconfig_company_category', 'washconfig_company_category.id', '=', 'washconfig_violation_store.category_id')
- ->where('washconfig_violation_store.social_credit_code', $product_data['qualification_number'])
- ->where('washconfig_violation_store.company_id', $company_id)
- ->select(['washconfig_violation_store.id', 'washconfig_company_category.name as category_name'])->first();
- $employee_id_list = [];
- if ($company_data) {
- $employee_id_list = $ViolationCompanyMemberModel->where('company_logid', $company_data->id)->pluck('employee_id')->toarray();
- $insert_product_data['company_category_name'] = $company_data->category_name ? $company_data->category_name : '';
- }
- //获取指定责任人信息
- if ($specify_responsible_person == 0) {
- //查询指定公司责任人
- $EmployeeModel = $EmployeeModel->where('company_id', $company_id);
- //查询配置的第一责任人
- $where_query1 = [];
- if (!empty($employee_id_list)) {
- $EmployeeModel = $EmployeeModel->whereIn('id', $employee_id_list);
- } else {
- $where_query1[] = ['city_ids', 'like', '%,' . $product_data['city_id'] . ',%'];
- }
- $where_query1[] = ['status', '=', 0];
- $where_query1[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
- $first_responsible_person = $EmployeeModel->where($where_query1)->pluck('id')->implode(',');
- $insert_product_data['first_responsible_person'] = $first_responsible_person;
- //查询配置的责任人
- $where_query2 = [];
- $EmployeeModel = new EmployeeModel();
- $EmployeeModel = $EmployeeModel->where('company_id', $company_id);
- if (!empty($employee_id_list)) {
- $EmployeeModel = $EmployeeModel->whereIn('id', $employee_id_list);
- } else {
- $where_query2[] = ['city_ids', 'like', '%,' . $product_data['city_id'] . ',%'];
- }
- $where_query2[] = ['status', '=', 0];
- $where_query2[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
- $responsible_person = $EmployeeModel->where($where_query2)->pluck('id')->implode(',');
- $insert_product_data['responsible_person'] = $responsible_person;
- //溯源责任人
- $source_responsible_person = '';
- if ($first_responsible_person && $responsible_person) {
- $source_responsible_person = $first_responsible_person . ',' . $responsible_person;
- } else if ($first_responsible_person) {
- $source_responsible_person = $first_responsible_person;
- } else if ($responsible_person) {
- $source_responsible_person = $responsible_person;
- }
- $insert_product_data['source_responsible_person'] = $source_responsible_person;
- }
- //插入数据
- $ViolationProductModel->addViolationProduct($insert_product_data);
- }
- //继续执行下一页
- $message_data['page'] = $page + 1;
- $message_data['limit'] = $limit;
- ViolationProductDataJobs::dispatch($message_data);
- }
- public function failed(\Throwable $exception)
- {
- Log::info('job_error', '数据清洗-违规挂网商品数据队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
- }
- }
|