ExportViolationProductJobs.php 28 KB

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