ExportViolationProductJobs.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <?php
  2. namespace App\Jobs\Manager\Other;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use App\Models\Manager\Process\ViolationProduct as ViolationProductModel;
  9. use App\Models\Manager\Personnel\Employee as EmployeeModel;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  12. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  13. use App\Servers\Aliyun\Oss;
  14. use App\Facades\Servers\Logs\Log;
  15. use App\Models\Manager\Other\DownloadTask as DownloadTaskModel;
  16. use Illuminate\Support\Facades\Cache;
  17. use App\Models\Manager\Process\ViolationProductMember as ViolationProductMemberModel;
  18. use App\Models\Manager\External\Company as CompanyModel;
  19. /**
  20. * 违规处理-导出违规商品数据处理队列
  21. * @author 唐远望
  22. * @version 1.0
  23. * @date 2026-04-01
  24. */
  25. class ExportViolationProductJobs implements ShouldQueue
  26. {
  27. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  28. protected $message_data;
  29. protected $company_id;
  30. public $tries = 3; // 限制重试次数
  31. public $timeout = 1800; // 超时30分钟
  32. /**
  33. * Create a new job instance.
  34. *
  35. * @return void
  36. */
  37. public function __construct(array $message_data)
  38. {
  39. $this->message_data = $message_data;
  40. }
  41. /**
  42. * Execute the job.
  43. *
  44. * @return void
  45. */
  46. public function handle()
  47. {
  48. try {
  49. $page = isset($this->message_data['page']) ? $this->message_data['page'] : 1;
  50. $fileId = $this->message_data['file_id'];
  51. $company_id = $this->message_data['company_id'];
  52. $user_id = $this->message_data['user_id'];
  53. if ($page == 1) {
  54. $key_name = 'ExportViolationProductJobs_' . $company_id;
  55. Cache::put($key_name, [], 600); //缓存10分钟
  56. $key_task_name = 'ExportViolationProductJobs_task' . $company_id;
  57. Cache::put($key_task_name,'1', 600); //缓存10分钟
  58. $DownloadTaskModel = new DownloadTaskModel();
  59. $fileName = '禁止挂网商品数据' . $fileId . '.xlsx';
  60. $downloadLog = [
  61. 'insert_time' => time(),
  62. 'company_id' => $company_id,
  63. 'operator_userid' => $user_id,
  64. 'file_name' => $fileName,
  65. 'file_id' => $fileId,
  66. 'url' => '',
  67. ];
  68. // 写入数据表
  69. $DownloadTaskModel->insertGetId($downloadLog);
  70. }
  71. $this->export_excel($this->message_data);
  72. } catch (\Exception $e) {
  73. Log::info('job_error', '违规数据-导出禁止挂网数据处理队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  74. //失败后清除缓存
  75. Cache::forget('ExportViolationProductJobs_' . $this->company_id);
  76. Cache::forget('ExportViolationProductJobs_task' . $this->company_id);
  77. }
  78. }
  79. /**
  80. * 列表导出
  81. * @author 唐远望
  82. * @version 1.0
  83. * @date 2025-06-17
  84. */
  85. public function export_excel($message_data)
  86. {
  87. $ViolationProductModel = new ViolationProductModel();
  88. $ViolationProductMemberModel = new ViolationProductMemberModel();
  89. $EmployeeModel = new EmployeeModel();
  90. $CompanyModel = new CompanyModel();
  91. $admin_company_id = $message_data['admin_company_id'];
  92. $company_id = $message_data['company_id'];
  93. $is_admin = $message_data['is_admin']; //是否管理员操作 0=是1=否
  94. $user_id = $message_data['user_id'];
  95. $file_id = $message_data['file_id'];
  96. // 查询条件
  97. $map = [];
  98. $job_page = isset($message_data['job_page']) ? $message_data['job_page'] : 1;
  99. $limit = 1000; //每次处理1000条
  100. $status = $message_data['status'] ?? '';
  101. $start_time = $message_data['start_time'] ?? '';
  102. $end_time = $message_data['end_time'] ?? '';
  103. $product_name = $message_data['product_name'] ?? '';
  104. $product_names = $message_data['product_names'] ?? '';
  105. $first_responsible_person = $message_data['first_responsible_person'] ?? '';
  106. $responsible_person = $message_data['responsible_person'] ?? '';
  107. $platform = $message_data['platform'] ?? '';
  108. $company_name = $message_data['company_name'] ?? '';
  109. $store_name = $message_data['store_name'] ?? '';
  110. $anonymous_store_name = $message_data['anonymous_store_name'] ?? '';
  111. $store_names = $message_data['store_names'] ?? '';
  112. $source_responsible_person = $message_data['source_responsible_person'] ?? '';
  113. $processing_status = $message_data['processing_status'] ?? '';
  114. $product_specs = $message_data['product_specs'] ?? '';
  115. $online_posting_count = $message_data['online_posting_count'] ?? '';
  116. $category_name = $message_data['category_name'] ?? '';
  117. $province_ids = $message_data['province_ids'] ?? '';
  118. $city_ids = $message_data['city_ids'] ?? '';
  119. $shipment_province_ids = $message_data['shipment_province_ids'] ?? '';
  120. $shipment_city_ids = $message_data['shipment_city_ids'] ?? '';
  121. $product_brand = $message_data['product_brand'];
  122. $collection_time_start_time = $message_data['collection_time_start_time'] ?? '';
  123. $collection_time_end_time = $message_data['collection_time_end_time'] ?? '';
  124. $merge_province_ids = $message_data['merge_province_ids'] ?? '';
  125. $merge_city_ids = $message_data['merge_city_ids'] ?? '';
  126. $first_responsible_person_name = $message_data['first_responsible_person_name'] ?? '';
  127. $responsible_person_name = $message_data['responsible_person_name'] ?? '';
  128. $source_responsible_person_name = $message_data['source_responsible_person_name'] ?? '';
  129. // 时间条件
  130. if ($collection_time_start_time) $map[] = ['collection_time', '>=', strtotime($collection_time_start_time)];
  131. if ($collection_time_end_time) $map[] = ['collection_time', '<=', strtotime($collection_time_end_time)];
  132. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  133. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  134. // 其他条件
  135. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  136. if ($store_name) $map[] = ['store_name', 'like', "%$store_name%"];
  137. if ($anonymous_store_name) $map[] = ['anonymous_store_name', 'like', "%$anonymous_store_name%"];
  138. if ($category_name) $map[] = ['category_name', 'like', "%$category_name%"];
  139. if ($product_brand) $map[] = ['product_brand', 'like', "%$product_brand%"];
  140. if ($product_specs) $map[] = ['product_specs', 'like', "%$product_specs%"];
  141. $violation_product_where = [];
  142. // 权限判断
  143. if ($is_admin != 1 && $company_id != 0) {
  144. $violation_product_where['company_id'] = $company_id;
  145. } else {
  146. $violation_product_where['company_id'] = $admin_company_id;
  147. }
  148. $ViolationProductModel = $ViolationProductModel->where($violation_product_where);
  149. //多选平台查询
  150. if ($platform && is_string($platform)) {
  151. $platform = explode(',', $platform);
  152. $ViolationProductModel = $ViolationProductModel->whereIn('platform', $platform);
  153. }
  154. //多选处理状态查询
  155. if ($processing_status && is_string($processing_status)) {
  156. $processing_status = explode(',', $processing_status);
  157. $ViolationProductModel = $ViolationProductModel->whereIn('processing_status', $processing_status);
  158. }
  159. //多选状态查询
  160. if ($status && is_string($status)) {
  161. $status = explode(',', $status);
  162. $ViolationProductModel = $ViolationProductModel->whereIn('status', $status);
  163. }
  164. //多选店铺名称查询
  165. if ($store_names && is_string($store_names)) {
  166. $store_names = explode(',', $store_names);
  167. $ViolationProductModel = $ViolationProductModel->whereIn('store_name', $store_names);
  168. }
  169. //多选违规挂网次数查询
  170. if ($online_posting_count && is_string($online_posting_count)) {
  171. $online_posting_count = explode(',', $online_posting_count);
  172. $ViolationProductModel = $ViolationProductModel->whereIn('online_posting_count', $online_posting_count);
  173. }
  174. //多选商品查询
  175. if ($product_names && is_string($product_names)) {
  176. $product_names = explode(',', $product_names);
  177. $ViolationProductModel = $ViolationProductModel->whereIn('product_name', $product_names);
  178. }
  179. //多选公司查询
  180. if ($company_name && is_string($company_name)) {
  181. $company_name = explode(',', $company_name);
  182. $ViolationProductModel = $ViolationProductModel->whereIn('company_name', $company_name);
  183. }
  184. //多选第一责任人
  185. if ($first_responsible_person && is_string($first_responsible_person)) {
  186. $first_responsible_person = explode(',', $first_responsible_person);
  187. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $first_responsible_person)->where('duty_type', 1)->distinct('violation_product_logid')->select('violation_product_logid');
  188. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  189. $query1->select('violation_product_logid')->fromSub($subQuery, 'sub1');
  190. });
  191. }
  192. //第一责任人模糊查询
  193. if ($first_responsible_person_name !='') {
  194. $subQuery = $ViolationProductMemberModel->where([['employee_name','like',"%$first_responsible_person_name%"]])->where('duty_type', 1)->distinct('violation_product_logid')->select('violation_product_logid');
  195. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  196. $query1->select('violation_product_logid')->fromSub($subQuery, 'sub1');
  197. });
  198. }
  199. //多选责任人
  200. if ($responsible_person && is_string($responsible_person)) {
  201. $responsible_person = explode(',', $responsible_person);
  202. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $responsible_person)->where('duty_type', 2)->distinct('violation_product_logid')->select('violation_product_logid');
  203. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  204. $query1->select('violation_product_logid')->fromSub($subQuery, 'sub1');
  205. });
  206. }
  207. //责任人模糊查询
  208. if ($responsible_person_name !='') {
  209. $subQuery = $ViolationProductMemberModel->where([['employee_name','like',"%$responsible_person_name%"]])->where('duty_type', 2)->distinct('violation_product_logid')->select('violation_product_logid');
  210. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  211. $query1->select('violation_product_logid')->fromSub($subQuery, 'sub1');
  212. });
  213. }
  214. //多选溯源责任人
  215. if ($source_responsible_person && is_string($source_responsible_person)) {
  216. $source_responsible_person = explode(',', $source_responsible_person);
  217. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $source_responsible_person)->where('duty_type', 3)->distinct('violation_product_logid')->select('violation_product_logid');
  218. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  219. $query1->select('violation_product_logid')->fromSub($subQuery, 'sub1');
  220. });
  221. }
  222. //来源责任人模糊查询
  223. if ($source_responsible_person_name !='') {
  224. $subQuery = $ViolationProductMemberModel->where([['employee_name','like',"%$source_responsible_person_name%"]])->where('duty_type', 3)->distinct('violation_product_logid')->select('violation_product_logid');
  225. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  226. $query1->select('violation_product_logid')->fromSub($subQuery, 'sub1');
  227. });
  228. }
  229. //多选省份
  230. if ($province_ids && is_string($province_ids)) {
  231. $province_ids = explode(',', $province_ids);
  232. $ViolationProductModel = $ViolationProductModel->whereIn('province_id', $province_ids);
  233. }
  234. //多选城市
  235. if ($city_ids && is_string($city_ids)) {
  236. $city_ids = explode(',', $city_ids);
  237. $ViolationProductModel = $ViolationProductModel->whereIn('city_id', $city_ids);
  238. }
  239. //多选发货省份
  240. if ($shipment_province_ids && is_string($shipment_province_ids)) {
  241. $shipment_province_ids = explode(',', $shipment_province_ids);
  242. $ViolationProductModel = $ViolationProductModel->whereIn('shipment_province_id', $shipment_province_ids);
  243. }
  244. //多选发货城市
  245. if ($shipment_city_ids && is_string($shipment_city_ids)) {
  246. $shipment_city_ids = explode(',', $shipment_city_ids);
  247. $ViolationProductModel = $ViolationProductModel->whereIn('shipment_city_id', $shipment_city_ids);
  248. }
  249. //多合并省份
  250. if ($merge_province_ids && is_string($merge_province_ids)) {
  251. $merge_province_ids = explode(',', $merge_province_ids);
  252. $ViolationProductModel = $ViolationProductModel->whereIn('merge_province_id', $merge_province_ids);
  253. }
  254. //多合并市
  255. if ($merge_city_ids && is_string($merge_city_ids)) {
  256. $merge_city_ids = explode(',', $merge_city_ids);
  257. $ViolationProductModel = $ViolationProductModel->whereIn('merge_city_id', $merge_city_ids);
  258. }
  259. $this->company_id = $violation_product_where['company_id'];
  260. $personnel_roles_info = $EmployeeModel->leftjoin('personnel_roles', 'personnel_roles.id', '=', 'personnel_employee.role_id')
  261. ->where('personnel_employee.id', $user_id)->select(['personnel_employee.id', 'personnel_roles.identity'])
  262. ->first();
  263. //角色身份1=普通2=管理员
  264. if (!empty($personnel_roles_info) && $personnel_roles_info->identity == 2) {
  265. $is_admin = 1;
  266. }
  267. $query = $ViolationProductModel->where($map);
  268. if ($is_admin != 1 && $company_id != 0) {
  269. $query = $query->where(function ($q) use ($user_id) {
  270. $q->where('first_responsible_person', 'like', "%,$user_id,%")
  271. ->orWhere('responsible_person', 'like', "%,$user_id,%")
  272. ->orWhere('source_responsible_person', 'like', "%,$user_id,%");
  273. });
  274. }
  275. $snapshot_status = $CompanyModel->where(['id' => $violation_product_where['company_id']])->value('snapshot_status');
  276. $key_name = 'ExportViolationProductJobs_' . $company_id;
  277. $key_task_name = 'ExportViolationProductJobs_task' . $company_id;
  278. $cache_product_datas = Cache::get($key_name) ?? [];
  279. $page = isset($message_data['page']) ? $message_data['page'] : 1;
  280. $limit = isset($message_data['limit']) ? $message_data['limit'] : 1000;
  281. $product_data_info = $query->orderByDesc('insert_time')->orderByDesc('collection_time')->paginate($limit, ['*'], 'page', $page)->toarray();
  282. $select_product_datas = $product_data_info['data'];
  283. $last_page = $product_data_info['last_page'];
  284. if (empty($select_product_datas)) {
  285. return true;
  286. } else {
  287. //去重获取所有员工信息
  288. $ViolationProductMemberModel = new ViolationProductMemberModel();
  289. $violation_product_logid = array_column($select_product_datas, 'id');
  290. $member_list_data = $ViolationProductMemberModel->whereIn('violation_product_logid', $violation_product_logid)->select(['employee_id', 'employee_name'])->get()->toArray();
  291. $member_list = [];
  292. if (!empty($member_list_data)) {
  293. foreach ($member_list_data as $key => $value) {
  294. $member_list[$value['employee_id']] = $value['employee_name'];
  295. }
  296. }
  297. //处理责任人展示信息
  298. $select_product_datas = $this->processing_responsible_person($select_product_datas, $member_list);
  299. //如果查询完毕了,则直接处理//合并数据
  300. $product_datas = array_merge($select_product_datas, $cache_product_datas);
  301. // 转为集合并排序
  302. $collection = collect($product_datas);
  303. $sorted = $collection->sortByDesc(function ($item) {
  304. return $item['insert_time'] . '|' . $item['collection_time'];
  305. })->values()->toArray();
  306. Cache::put($key_name, $sorted, 600); //缓存10分钟
  307. Cache::put($key_task_name,'1', 600); //缓存10分钟
  308. if ($page < $last_page) {
  309. //继续执行下一页
  310. $message_data['page'] = $page + 1;
  311. $message_data['limit'] = $limit;
  312. ExportViolationProductJobs::dispatch($message_data);
  313. } else if ($page == $last_page) {
  314. $this->export_download($product_datas, $violation_product_where['company_id'], $file_id, $snapshot_status);
  315. }
  316. }
  317. }
  318. /**
  319. * 处理责任人展示信息
  320. * @author 唐远望
  321. * @version 1.0
  322. * @date 2025-12-17
  323. */
  324. public function processing_responsible_person($result, $member_list)
  325. {
  326. if (isset($result) && count($result) > 0) {
  327. foreach ($result as $key => $value) {
  328. //查询第一责任人名称
  329. $first_responsible_person = explode(',', $value['first_responsible_person']);
  330. $first_responsible_person_name = [];
  331. foreach ($first_responsible_person as $k => $v) {
  332. if (isset($member_list[$v])) {
  333. $first_responsible_person_name[] = $member_list[$v];
  334. }
  335. }
  336. $result[$key]['first_responsible_person_name'] = !empty($first_responsible_person_name) ? implode(',', $first_responsible_person_name) : '';
  337. //查询责任人名称
  338. $responsible_person = explode(',', $value['responsible_person']);
  339. $responsible_person_name = [];
  340. foreach ($responsible_person as $k => $v) {
  341. if (isset($member_list[$v])) {
  342. $responsible_person_name[] = $member_list[$v];
  343. }
  344. }
  345. $result[$key]['responsible_person_name'] = !empty($responsible_person_name) ? implode(',', $responsible_person_name) : '';
  346. //查询来源责任人名称
  347. $source_responsible_person = explode(',', $value['source_responsible_person']);
  348. $source_responsible_person_name = [];
  349. foreach ($source_responsible_person as $k => $v) {
  350. if (isset($member_list[$v])) {
  351. $source_responsible_person_name[] = $member_list[$v];
  352. }
  353. }
  354. // 修复:确保赋值的是字符串,而不是数组
  355. $result[$key]['source_responsible_person_name'] = !empty($source_responsible_person_name) ? implode(',', $source_responsible_person_name) : '';
  356. // 确保所有需要的字段都存在
  357. if (!isset($result[$key]['merge_province_name'])) {
  358. $result[$key]['merge_province_name'] = $value['merge_province_name'] ?? '';
  359. }
  360. if (!isset($result[$key]['merge_city_name'])) {
  361. $result[$key]['merge_city_name'] = $value['merge_city_name'] ?? '';
  362. }
  363. }
  364. }
  365. return $result;
  366. }
  367. /**
  368. * 导出下载
  369. * @author 唐远望
  370. * @version 1.0
  371. * @date 2025-06-17
  372. */
  373. public function export_download($data, $company_id, $file_id, $snapshot_status)
  374. {
  375. // 创建一个新的 Spreadsheet 对象
  376. $spreadsheet = new Spreadsheet();
  377. $sheet = $spreadsheet->getActiveSheet();
  378. //合并单元格
  379. $sheet->mergeCells('A1:W1');
  380. $sheet->setCellValue('A1', '禁止挂网商品导出(导出时间:' . date('Y-m-d H:i:s', time()) . ')'); // 设置合并后的单元格内容
  381. // 获取合并后的单元格样式对象
  382. $style = $sheet->getStyle('A1');
  383. // 设置水平居中和垂直居中
  384. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  385. // 然后设置行高以适应两行文本
  386. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  387. // 设置表头
  388. if ($company_id == 5) {
  389. $sheet->setCellValue('A2', ' ');
  390. $sheet->setCellValue('B2', '责任人');
  391. $sheet->setCellValue('C2', '平台');
  392. $sheet->setCellValue('D2', ' ');
  393. $sheet->setCellValue('E2', '品牌名称');
  394. $sheet->setCellValue('F2', '商品名称');
  395. $sheet->setCellValue('G2', ' ');
  396. $sheet->setCellValue('H2', ' ');
  397. $sheet->setCellValue('I2', '快照URL');
  398. $sheet->setCellValue('J2', '商品规格');
  399. $sheet->setCellValue('K2', ' ');
  400. $sheet->setCellValue('L2', '连续挂网次数');
  401. $sheet->setCellValue('M2', '链接地址');
  402. $sheet->setCellValue('N2', '店铺名称');
  403. $sheet->setCellValue('O2', ' ');
  404. $sheet->setCellValue('P2', '公司名称');
  405. $sheet->setCellValue('Q2', ' ');
  406. $sheet->setCellValue('R2', ' ');
  407. $sheet->setCellValue('S2', '省份');
  408. $sheet->setCellValue('T2', '城市');
  409. $sheet->setCellValue('U2', '处理状态');
  410. $sheet->setCellValue('V2', '采集时间');
  411. $sheet->setCellValue('W2', '清洗时间');
  412. } else {
  413. $sheet->setCellValue('A2', '第一责任人');
  414. $sheet->setCellValue('B2', '责任人');
  415. $sheet->setCellValue('C2', '平台');
  416. $sheet->setCellValue('D2', '商品类型');
  417. $sheet->setCellValue('E2', '品牌名称');
  418. $sheet->setCellValue('F2', '商品名称');
  419. $sheet->setCellValue('G2', '库存');
  420. $sheet->setCellValue('H2', '销量');
  421. $sheet->setCellValue('I2', '快照URL');
  422. $sheet->setCellValue('J2', '商品规格');
  423. $sheet->setCellValue('K2', '累计挂网次数');
  424. $sheet->setCellValue('L2', '连续挂网次数');
  425. $sheet->setCellValue('M2', '链接地址');
  426. $sheet->setCellValue('N2', '店铺名称');
  427. $sheet->setCellValue('O2', '匿名店铺名称');
  428. $sheet->setCellValue('P2', '公司名称');
  429. $sheet->setCellValue('Q2', '商业类型');
  430. $sheet->setCellValue('R2', '信用代码');
  431. $sheet->setCellValue('S2', '省份');
  432. $sheet->setCellValue('T2', '城市');
  433. $sheet->setCellValue('U2', '处理状态');
  434. $sheet->setCellValue('V2', '采集时间');
  435. $sheet->setCellValue('W2', '清洗时间');
  436. }
  437. //平台0=全部,1=淘宝,2=京东,3=拼多多,4=美团,5=药师帮,6=1药城,7=药九九,8=药易购,9=药帮忙,10=熊猫药药11=药房网
  438. $platform_data = [
  439. '0' => '全部',
  440. '1' => '淘宝',
  441. '2' => '京东',
  442. '3' => '拼多多',
  443. '4' => '美团',
  444. '5' => '药师帮',
  445. '6' => '1药城',
  446. '7' => '药九九',
  447. '8' => '药易购',
  448. '9' => '药帮忙',
  449. '10' => '熊猫药药',
  450. '11' => '药房网',
  451. ];
  452. $processing_status_text = [
  453. '1' => '待处理',
  454. ];
  455. $status_text = [
  456. '0' => '有效',
  457. '1' => '无效',
  458. ];
  459. // 填充数据
  460. $row = 3; // 从第2行开始
  461. if ($company_id == 5) {
  462. foreach ($data as $item) {
  463. //关闭快照不展示数据
  464. if (!empty($snapshot_status) && $snapshot_status == 1) $item['snapshot_url'] = '';
  465. $sheet->setCellValue('A' . $row, '');
  466. $sheet->setCellValue('B' . $row, $item['responsible_person_name']);
  467. $sheet->setCellValue('C' . $row, isset($platform_data[$item['platform']]) ? $platform_data[$item['platform']] : '');
  468. $sheet->setCellValue('D' . $row, '');
  469. $sheet->setCellValue('E' . $row, $item['product_brand']);
  470. $sheet->setCellValue('F' . $row, $item['product_name']);
  471. $sheet->setCellValue('G' . $row, '');
  472. $sheet->setCellValue('H' . $row, '');
  473. $sheet->setCellValue('I' . $row, $item['snapshot_url'] != '' ? $item['snapshot_url'] : '暂无');
  474. $sheet->setCellValue('J' . $row, $item['product_specs']);
  475. $sheet->setCellValue('K' . $row, '');
  476. $sheet->setCellValue('L' . $row, $item['continuous_listing_count']);
  477. $sheet->setCellValue('M' . $row, $item['link_url']);
  478. $sheet->setCellValue('N' . $row, $item['store_name']);
  479. $sheet->setCellValue('O' . $row, '');
  480. $sheet->setCellValue('P' . $row, $item['company_name']);
  481. $sheet->setCellValue('Q' . $row, '');
  482. $sheet->setCellValue('R' . $row, '');
  483. $sheet->setCellValue('S' . $row, $item['merge_province_name']);
  484. $sheet->setCellValue('T' . $row, $item['merge_city_name']);
  485. $sheet->setCellValue('U' . $row, isset($processing_status_text[$item['processing_status']]) ? $processing_status_text[$item['processing_status']] : '');
  486. $sheet->setCellValue('V' . $row, !empty($item['collection_time']) ? date('Y-m-d H:i:s', $item['collection_time']) : '');
  487. $sheet->setCellValue('W' . $row, date('Y-m-d H:i:s', $item['insert_time']));
  488. $row++;
  489. }
  490. } else {
  491. foreach ($data as $item) {
  492. //关闭快照不展示数据
  493. if (!empty($snapshot_status) && $snapshot_status == 1) $item['snapshot_url'] = '';
  494. $sheet->setCellValue('A' . $row, $item['first_responsible_person_name']);
  495. $sheet->setCellValue('B' . $row, $item['responsible_person_name']);
  496. $sheet->setCellValue('C' . $row, isset($platform_data[$item['platform']]) ? $platform_data[$item['platform']] : '');
  497. $sheet->setCellValue('D' . $row, $item['category_name']);
  498. $sheet->setCellValue('E' . $row, $item['product_brand']);
  499. $sheet->setCellValue('F' . $row, $item['product_name']);
  500. $sheet->setCellValue('G' . $row, $item['inventory']);
  501. $sheet->setCellValue('H' . $row, $item['sales']);
  502. $sheet->setCellValue('I' . $row, $item['snapshot_url']);
  503. $sheet->setCellValue('J' . $row, $item['product_specs']);
  504. $sheet->setCellValue('K' . $row, $item['online_posting_count']);
  505. $sheet->setCellValue('L' . $row, $item['continuous_listing_count']);
  506. $sheet->setCellValue('M' . $row, $item['link_url']);
  507. $sheet->setCellValue('N' . $row, $item['store_name']);
  508. $sheet->setCellValue('O' . $row, $item['anonymous_store_name']);
  509. $sheet->setCellValue('P' . $row, $item['company_name']);
  510. $sheet->setCellValue('Q' . $row, $item['company_category_name']);
  511. $sheet->setCellValue('R' . $row, $item['social_credit_code']);
  512. $sheet->setCellValue('S' . $row, $item['merge_province_name']);
  513. $sheet->setCellValue('T' . $row, $item['merge_city_name']);
  514. $sheet->setCellValue('U' . $row, isset($processing_status_text[$item['processing_status']]) ? $processing_status_text[$item['processing_status']] : '');
  515. $sheet->setCellValue('V' . $row, !empty($item['collection_time']) ? date('Y-m-d H:i:s', $item['collection_time']) : '');
  516. $sheet->setCellValue('W' . $row, date('Y-m-d H:i:s', $item['insert_time']));
  517. $row++;
  518. }
  519. }
  520. foreach (range('A', 'W') as $column) {
  521. $sheet->getColumnDimension($column)->setAutoSize(true);
  522. }
  523. // 创建Excel文件
  524. $filename = '禁止挂网商品数据' . $file_id . '.xlsx';
  525. $path = public_path('uploads/exports/');
  526. $fullPath = $path . $filename;
  527. if (!is_dir($path)) mkdir($path, 0777, true);
  528. // 生成 Excel 文件
  529. $writer = new Xlsx($spreadsheet);
  530. $writer->save($fullPath);
  531. // 清理
  532. $spreadsheet->disconnectWorksheets();
  533. unset($spreadsheet, $writer);
  534. // //释放redis缓存
  535. Cache::forget('ExportViolationProductJobs_' . $company_id);
  536. Cache::forget('ExportViolationProductJobs_task' . $company_id);
  537. $Oss = new Oss();
  538. $oss_url = $Oss->uploadFile($filename, $fullPath);
  539. if ($oss_url) @unlink($fullPath);
  540. // 记录下载任务
  541. $downloadTask = DownloadTaskModel::where(['file_id' => $file_id, 'company_id' => $company_id])->first();
  542. if ($downloadTask) {
  543. $downloadTask->url = $oss_url;
  544. $downloadTask->file_dir_name = $filename;
  545. $downloadTask->update_time = time();
  546. $downloadTask->status = 1;
  547. $downloadTask->save();
  548. }
  549. return $oss_url;
  550. }
  551. public function failed(\Throwable $exception)
  552. {
  553. Log::info('job_error', '违规数据-导出违规商品数据队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
  554. $company_id = isset($this->message_data['company_id']) ? $this->message_data['company_id'] : 0;
  555. if ($company_id) {
  556. //失败后清除缓存
  557. Cache::forget('ExportViolationProductJobs_' . $company_id);
  558. Cache::forget('ExportViolationProductJobs_task' . $company_id);
  559. }
  560. }
  561. }