ViolationProductDataJobs.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. namespace App\Jobs\Manager\Process;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Facades\Servers\Logs\Log;
  10. use App\Models\Manager\Process\ViolationProduct as ViolationProductModel;
  11. use App\Models\Manager\Personnel\Employee as EmployeeModel;
  12. use App\Models\Api\Process\ExecuteLog as ExecuteLogModel;
  13. use App\Models\Manager\Process\ScrapeData as ScrapeDataModel;
  14. use App\Models\Manager\WashConfig\ViolationStore as ViolationStoreModel;
  15. use App\Models\Manager\WashConfig\ViolationCompanyMember as ViolationCompanyMemberModel;
  16. use App\Models\Manager\Citys as CitysModel;
  17. use App\Models\Manager\Personnel\EmployeePlatform as EmployeePlatformModel;
  18. use App\Models\Manager\Personnel\EmployeeArea as EmployeeAreaModel;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Carbon;
  21. /**
  22. * 数据清洗-违规挂网商品数据队列
  23. * @author 唐远望
  24. * @version 1.0
  25. * @date 2025-12-10
  26. */
  27. class ViolationProductDataJobs implements ShouldQueue
  28. {
  29. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  30. protected $message_data;
  31. /**
  32. * Create a new job instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct(array $message_data)
  37. {
  38. $this->message_data = $message_data;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. try {
  48. $this->getViolationProductData($this->message_data);
  49. } catch (\Exception $e) {
  50. Log::info('job_error', '数据清洗-违规挂网商品数据队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  51. }
  52. }
  53. /**
  54. * 采集商品数据清洗
  55. * @author 唐远望
  56. * @version 1.0
  57. * @date 2025-12-10
  58. */
  59. public function getViolationProductData($message_data)
  60. {
  61. $CitysModel = new CitysModel();
  62. $EmployeeModel = new EmployeeModel();
  63. $ViolationProductModel = new ViolationProductModel();
  64. $ScrapeDataModel = new ScrapeDataModel();
  65. $ViolationStoreModel = new ViolationStoreModel();
  66. $ViolationCompanyMemberModel = new ViolationCompanyMemberModel();
  67. $EmployeePlatformModel = new EmployeePlatformModel();
  68. $EmployeeAreaModel = new EmployeeAreaModel();
  69. $platform = $message_data['platform']; //多个平台配置
  70. $product_name = $message_data['product_name']; //商品名称
  71. $product_specs = $message_data['product_specs']; //商品规格
  72. $store_scope = $message_data['store_scope']; //店铺范围1=全部店铺2=指定店铺
  73. $company_scope = $message_data['company_scope']; //公司范围1=全部公司2=指定公司
  74. $social_credit_code = $message_data['social_credit_code']; //社会信用代码
  75. $executeLog_id = $message_data['executeLog_id'];
  76. $category_name = $message_data['category_name']; //产品分类名称
  77. $specify_responsible_person = $message_data['specify_responsible_person']; //指派责任人 0=开启 1=关闭
  78. $limit = isset($message_data['limit']) ? $message_data['limit'] : 50;
  79. $page = isset($message_data['page']) ? $message_data['page'] : 1;
  80. $company_id = isset($message_data['company_id']) ? $message_data['company_id'] : 0;//品牌方公司ID
  81. $product_brand = $message_data['product_brand']; //商品品牌名称
  82. $product_keywords = $message_data['product_keyword']; //关键字
  83. $where = [];
  84. if ($platform != '0') {
  85. $platform = explode(',', $platform);
  86. $ScrapeDataModel = $ScrapeDataModel->whereIn('platform_id', $platform);
  87. }
  88. $yesterdayStart = Carbon::yesterday()->startOfDay()->getTimestamp(); // 昨天开始时间 00:00:00
  89. $yesterdayEnd = Carbon::yesterday()->endOfDay()->getTimestamp(); // 昨天结束时间 23:59:59
  90. $yesterdayStart = date('Y-m-d H:i:s', $yesterdayStart);
  91. $yesterdayEnd = date('Y-m-d H:i:s', $yesterdayEnd);
  92. $where[] = ['insert_time', '>=', $yesterdayStart];
  93. $where[] = ['insert_time', '<=', $yesterdayEnd];
  94. $where[] = ['product_name', 'like', '%' . $product_name . '%'];
  95. $where[] = ['product_specs', 'like', '%' . $product_specs . '%'];
  96. if ($product_brand) $where[] = ['product_name', 'like', '%' . $product_brand . '%'];
  97. if (!empty($product_keyword)) {
  98. foreach ($product_keywords as $key => $keywords) {
  99. if (trim($keywords) == '') continue;
  100. $where[] = ['product_name', 'like', '%' . $keywords . '%'];
  101. }
  102. }
  103. if ($company_scope == 2) {
  104. $ScrapeDataModel->whereIn('qualification_number', $social_credit_code);
  105. }
  106. $product_data_info = $ScrapeDataModel->where($where)->paginate($limit, ['*'], 'page', $page)->toarray();
  107. $product_datas = $product_data_info['data'];
  108. if (empty($product_datas)) {
  109. if ($executeLog_id) {
  110. (new ExecuteLogModel())->where('id', $executeLog_id)->update(['status' => 0,'update_time' => time()]);
  111. }
  112. return true;
  113. }
  114. foreach ($product_datas as $product_data) {
  115. $province_name = $product_data['province_name'];
  116. $city_name = $product_data['city_name'];
  117. //特殊地区1级移除市
  118. if ($province_name && in_array($province_name, ['北京市', '天津市', '上海市', '重庆市'])) {
  119. //移除市这个字符
  120. $province_name = trim(str_replace('市', '', $province_name));
  121. } else if ($province_name && in_array($province_name, ['北京', '天津', '上海', '重庆'])) {
  122. } else if ($province_name && in_array($province_name, ['内蒙古', '广西', '西藏', '新疆', '宁夏'])) {
  123. switch ($province_name) {
  124. case '内蒙古':
  125. $province_name = '内蒙古自治区';
  126. break;
  127. case '广西':
  128. $province_name = '广西壮族自治区';
  129. break;
  130. case '西藏':
  131. $province_name = '西藏自治区';
  132. break;
  133. case '新疆':
  134. $province_name = '新疆维吾尔自治区';
  135. break;
  136. case '宁夏':
  137. $province_name = '宁夏回族自治区';
  138. break;
  139. }
  140. } else if ($province_name && in_array($province_name, ['内蒙古自治区', '广西壮族自治区', '西藏自治区', '新疆维吾尔自治区', '宁夏回族自治区'])) {
  141. } else if (strpos($province_name, '省') === false) {
  142. //是否存在市省,如果不存在则补全
  143. if (strpos($province_name, '省') === false) {
  144. $province_name = $province_name . '省';
  145. }
  146. }
  147. if ($city_name != '') {
  148. $city_name = $CitysModel->where([['id', '=', $product_data['city_id']], ['level', '=', '2']])->value('name');
  149. }
  150. $insert_product_data = [
  151. 'company_id' => $company_id,
  152. 'source_id' => $product_data['id'],
  153. 'platform' => $product_data['platform_id'],
  154. 'company_name' => $product_data['company_name'],
  155. 'product_name' => $product_name,
  156. 'product_specs' => $product_specs,
  157. 'inventory' => $product_data['inventory'] ? $product_data['inventory'] : '',
  158. 'sales' => $product_data['sales'] ? $product_data['sales'] : '',
  159. 'snapshot_url' => $product_data['snapshot_url'] ? $product_data['snapshot_url'] : '',
  160. 'online_posting_count' => $product_data['online_posting_count'],
  161. 'continuous_listing_count' => $product_data['continuous_listing_count'],
  162. 'link_url' => $product_data['link_url'],
  163. 'store_name' => $product_data['store_name'],
  164. 'social_credit_code' => $product_data['qualification_number'],
  165. 'province_id' => $product_data['province_id'],
  166. 'province_name' => $province_name,
  167. 'city_id' => $product_data['city_id'],
  168. 'city_name' => $city_name,
  169. 'area_info' => $product_data['area_info'],
  170. 'category_name' => $category_name,
  171. 'company_category_name' => '',
  172. 'first_responsible_person' => '',
  173. 'responsible_person' => '',
  174. 'source_responsible_person' => '',
  175. 'scrape_date' => $product_data['scrape_date'],
  176. 'shipment_province_id' => $product_data['shipment_province_id'],
  177. 'shipment_province_name' => $product_data['shipment_province_name'],
  178. 'shipment_city_id' => $product_data['shipment_city_id'],
  179. 'shipment_city_name' => $product_data['shipment_city_name'],
  180. ];
  181. //获取公司绑定责任人信息
  182. $company_data = $ViolationStoreModel->leftjoin('washconfig_company_category', 'washconfig_company_category.id', '=', 'washconfig_violation_store.category_id')
  183. ->where('washconfig_violation_store.social_credit_code', $product_data['qualification_number'])
  184. ->where('washconfig_violation_store.company_id', $company_id)
  185. ->select(['washconfig_violation_store.id', 'washconfig_company_category.name as category_name'])->first();
  186. $employee_id_list = [];
  187. if ($company_data) {
  188. $employee_id_list = $ViolationCompanyMemberModel->where('company_logid', $company_data->id)->pluck('employee_id')->toarray();
  189. $insert_product_data['company_category_name'] = $company_data->category_name ? $company_data->category_name : '';
  190. }
  191. //获取指定人员信息
  192. if ($specify_responsible_person == 0) {
  193. $where_query1 = [];
  194. $where_query2 = [];
  195. $where_query3 = [];
  196. //查询指定公司第一责任人
  197. if (!empty($employee_id_list)) {
  198. $where_query1[] = ['company_id', '=', $company_id];
  199. $where_query1[] = ['id', 'in', $employee_id_list];
  200. $where_query1[] = ['status', '=', 0];
  201. $where_query1[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  202. }
  203. //查询地区配置的第一责任人
  204. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  205. if (!empty($employee_id_area)) {
  206. $where_query2[] = ['company_id', '=', $company_id];
  207. $where_query2[] = ['id', 'in', $employee_id_area];
  208. $where_query2[] = ['status', '=', 0];
  209. $where_query2[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  210. }
  211. //查询平台配置的第一责任人
  212. $employee_id_platform = $EmployeePlatformModel->where('platform_id', $product_data['platform_id'])->pluck('employee_id')->toarray();
  213. if (!empty($employee_id_platform)) {
  214. $where_query3[] = ['company_id', '=', $company_id];
  215. $where_query3[] = ['id', 'in', $employee_id_platform];
  216. $where_query3[] = ['status', '=', 0];
  217. $where_query3[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  218. }
  219. //并行查询第一责任人
  220. $EmployeeModel = $EmployeeModel->where(function ($q) use ($employee_id_list, $company_id) {
  221. if (!empty($employee_id_list)) {
  222. $q->where('company_id', $company_id)
  223. ->orWhereIn('id', $employee_id_list)
  224. ->where('status', 0)
  225. ->where('duty_type', 1);
  226. }
  227. })->orWhere(function ($q) use ($employee_id_area, $company_id) {
  228. if (!empty($employee_id_area)) {
  229. $q->where('company_id', $company_id)
  230. ->whereIn('id', $employee_id_area)
  231. ->where('status', 0)
  232. ->where('duty_type', 1);
  233. }
  234. })->orWhere(function ($q) use ($employee_id_platform, $company_id) {
  235. if (!empty($employee_id_platform)) {
  236. $q->where('company_id', $company_id)
  237. ->whereIn('id', $employee_id_platform)
  238. ->where('status', 0)
  239. ->where('duty_type', 1);
  240. }
  241. });
  242. $first_responsible_person = $EmployeeModel->pluck('id')->implode(',');
  243. //当以上规则匹配不到责任人时,则去查询发货地区信息关联责任人
  244. if (trim($first_responsible_person) == '' && trim($product_data['shipment_city_id']) !='') {
  245. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  246. if (!empty($employee_id_area)) {
  247. $where_city[] = ['company_id', '=', $company_id];
  248. $where_city[] = ['id', 'in', $employee_id_area];
  249. $where_city[] = ['status', '=', 0];
  250. $where_city[] = ['duty_type', '=', 1]; //责任类型1=第一责任人,2=责任人
  251. $EmployeeModel = new EmployeeModel();
  252. $first_responsible_person = $EmployeeModel->where($where_city)->pluck('id')->implode(',');
  253. }
  254. }
  255. //调试记录查询条件
  256. $insert_product_data['first_responsible_person'] = $first_responsible_person;
  257. //查询责任人
  258. $where_query1 = [];
  259. $where_query2 = [];
  260. $where_query3 = [];
  261. //查询指定公司责任人
  262. if (!empty($employee_id_list)) {
  263. $where_query1[] = ['company_id', '=', $company_id];
  264. $where_query1[] = ['id', 'in', $employee_id_list];
  265. $where_query1[] = ['status', '=', 0];
  266. $where_query1[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  267. }
  268. //查询地区配置的员工
  269. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  270. if (!empty($employee_id_area)) {
  271. $where_query2[] = ['company_id', '=', $company_id];
  272. $where_query2[] = ['id', 'in', $employee_id_area];
  273. $where_query2[] = ['status', '=', 0];
  274. $where_query2[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  275. }
  276. //查询平台配置的员工
  277. $employee_id_platform = $EmployeePlatformModel->where('platform_id', $product_data['platform_id'])->pluck('employee_id')->toarray();
  278. if (!empty($employee_id_platform)) {
  279. $where_query3[] = ['company_id', '=', $company_id];
  280. $where_query3[] = ['id', 'in', $employee_id_platform];
  281. $where_query3[] = ['status', '=', 0];
  282. $where_query3[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  283. }
  284. //并行查询责任人
  285. $EmployeeModel = new EmployeeModel();
  286. $EmployeeModel = $EmployeeModel->where(function ($q) use ($employee_id_list, $company_id) {
  287. if (!empty($employee_id_list)) {
  288. $q->where('company_id', $company_id)
  289. ->orWhereIn('id', $employee_id_list)
  290. ->where('status', 0)
  291. ->where('duty_type', 2);//责任类型1=第一责任人,2=责任人
  292. }
  293. })->orWhere(function ($q) use ($employee_id_area, $company_id) {
  294. if (!empty($employee_id_area)) {
  295. $q->where('company_id', $company_id)
  296. ->whereIn('id', $employee_id_area)
  297. ->where('status', 0)
  298. ->where('duty_type', 2);//责任类型1=第一责任人,2=责任人
  299. }
  300. })->orWhere(function ($q) use ($employee_id_platform, $company_id) {
  301. if (!empty($employee_id_platform)) {
  302. $q->where('company_id', $company_id)
  303. ->whereIn('id', $employee_id_platform)
  304. ->where('status', 0)
  305. ->where('duty_type', 2);//责任类型1=第一责任人,2=责任人
  306. }
  307. });
  308. $responsible_person = $EmployeeModel->pluck('id')->implode(',');
  309. //当以上规则匹配不到责任人时,则去查询发货地区信息关联责任人
  310. if (trim($responsible_person) == '' && trim($product_data['shipment_city_id']) !='') {
  311. $employee_id_area = $EmployeeAreaModel->where('city_id', $product_data['city_id'])->pluck('employee_id')->toarray();
  312. if (!empty($employee_id_area)) {
  313. $where_city[] = ['company_id', '=', $company_id];
  314. $where_city[] = ['id', 'in', $employee_id_area];
  315. $where_city[] = ['status', '=', 0];
  316. $where_city[] = ['duty_type', '=', 2]; //责任类型1=第一责任人,2=责任人
  317. $EmployeeModel = new EmployeeModel();
  318. $responsible_person = $EmployeeModel->where($where_city)->pluck('id')->implode(',');
  319. }
  320. }
  321. $insert_product_data['responsible_person'] = $responsible_person;
  322. //溯源责任人
  323. $source_responsible_person = '';
  324. if ($first_responsible_person && $responsible_person) {
  325. //转换成数组,合并后在去重
  326. $first_responsible_person = explode(',', $first_responsible_person);
  327. $responsible_person = explode(',', $responsible_person);
  328. $source_responsible_person = array_unique(array_merge($first_responsible_person, $responsible_person));
  329. $source_responsible_person = ','.implode(',', $source_responsible_person).',';
  330. } else if ($first_responsible_person) {
  331. $source_responsible_person = $first_responsible_person;
  332. } else if ($responsible_person) {
  333. $source_responsible_person = $responsible_person;
  334. }
  335. $insert_product_data['source_responsible_person'] = $source_responsible_person;
  336. }
  337. //插入数据
  338. $ViolationProductModel->addViolationProduct($insert_product_data);
  339. }
  340. //继续执行下一页
  341. $message_data['page'] = $page + 1;
  342. $message_data['limit'] = $limit;
  343. ViolationProductDataJobs::dispatch($message_data);
  344. }
  345. public function failed(\Throwable $exception)
  346. {
  347. Log::info('job_error', '数据清洗-违规挂网商品数据队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
  348. }
  349. }