ViolationProduct.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. <?php
  2. namespace App\Http\Controllers\Manager\Process;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Manager\Process\ViolationProduct as Request;
  5. use App\Models\Manager\Process\ViolationProduct as ViolationProductModel;
  6. use App\Jobs\Manager\Process\ViolationProductJobs;
  7. use App\Models\Manager\Personnel\Employee as EmployeeModel;
  8. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  9. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  10. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  11. use PhpOffice\PhpSpreadsheet\IOFactory;
  12. use App\Models\Manager\Process\ViolationProductMember as ViolationProductMemberModel;
  13. use App\Models\Manager\Citys as CitysModel;
  14. use Illuminate\Support\Facades\DB;
  15. /**
  16. * 违规处理-违规商品
  17. * @author 唐远望
  18. * @version 1.0
  19. * @date 2025-12-08
  20. */
  21. class ViolationProduct extends Controller
  22. {
  23. /**
  24. * 列表
  25. * @author 唐远望
  26. * @version 1.0
  27. * @date 2025-12-08
  28. *
  29. */
  30. public function list(Request $request, ViolationProductModel $ViolationProductModel, EmployeeModel $EmployeeModel, ViolationProductMemberModel $ViolationProductMemberModel)
  31. {
  32. $request->scene('list')->validate();
  33. $admin_company_id = request('admin_company_id', '0');
  34. $company_id = request('access_token.company_id', '0');
  35. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  36. $user_id = request('access_token.uid', 0);
  37. // 查询条件
  38. $map = [];
  39. $limit = request('limit', config('page_num', 10));
  40. $status = request('status', '');
  41. $start_time = request('start_time', '');
  42. $end_time = request('end_time', '');
  43. $product_name = request('product_name', '');
  44. $product_names = request('product_names', '');
  45. $first_responsible_person = request('first_responsible_person', '');
  46. $responsible_person = request('responsible_person', '');
  47. $platform = request('platform', '');
  48. $company_name = request('company_name', '');
  49. $company_names = request('company_names', '');
  50. $store_name = request('store_name', '');
  51. $store_names = request('store_names', '');
  52. $source_responsible_person = request('source_responsible_person', '');
  53. $processing_status = request('processing_status', '');
  54. $product_specs = request('product_specs', '');
  55. $online_posting_count = request('online_posting_count', '');
  56. $category_name = request('category_name', '');
  57. $id = request('id', '');
  58. // 时间条件
  59. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  60. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  61. // 其他条件
  62. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  63. if ($store_name) $map[] = ['store_name', 'like', "%$store_name%"];
  64. if ($category_name) $map[] = ['category_name', 'like', "%$category_name%"];
  65. if ($company_name) $map[] = ['company_name', 'like', "%$company_name%"];
  66. if ($id) $map[] = ['id', '=', $id];
  67. $violation_product_where = [];
  68. // 权限判断
  69. if ($is_admin != 1 && $company_id != 0) {
  70. $violation_product_where['company_id'] = $company_id;
  71. } else {
  72. $violation_product_where['company_id'] = $admin_company_id;
  73. }
  74. $ViolationProductModel = $ViolationProductModel->where($violation_product_where);
  75. //多选平台查询
  76. if ($platform && is_string($platform)) {
  77. $platform = explode(',', $platform);
  78. $ViolationProductModel = $ViolationProductModel->whereIn('platform', $platform);
  79. }
  80. //多选处理状态查询
  81. if ($processing_status && is_string($processing_status)) {
  82. $processing_status = explode(',', $processing_status);
  83. $ViolationProductModel = $ViolationProductModel->whereIn('processing_status', $processing_status);
  84. }
  85. //多选状态查询
  86. if ($status && is_string($status)) {
  87. $status = explode(',', $status);
  88. $ViolationProductModel = $ViolationProductModel->whereIn('status', $status);
  89. }
  90. //多选店铺名称查询
  91. if ($store_names && is_string($store_names)) {
  92. $store_names = explode(',', $store_names);
  93. $ViolationProductModel = $ViolationProductModel->whereIn('store_name', $store_names);
  94. }
  95. //多选违规挂网次数查询
  96. if ($online_posting_count && is_string($online_posting_count)) {
  97. $online_posting_count = explode(',', $online_posting_count);
  98. $ViolationProductModel = $ViolationProductModel->whereIn('online_posting_count', $online_posting_count);
  99. }
  100. //多选规格查询
  101. if ($product_specs && is_string($product_specs)) {
  102. $product_specs = explode(',', $product_specs);
  103. $ViolationProductModel = $ViolationProductModel->whereIn('product_specs', $product_specs);
  104. }
  105. //多选商品查询
  106. if ($product_names && is_string($product_names)) {
  107. $product_names = explode(',', $product_names);
  108. $ViolationProductModel = $ViolationProductModel->whereIn('product_name', $product_names);
  109. }
  110. //多选公司查询
  111. if ($company_names && is_string($company_names)) {
  112. $company_names = explode(',', $company_names);
  113. $ViolationProductModel = $ViolationProductModel->whereIn('company_name', $company_names);
  114. }
  115. //多选第一责任人
  116. if ($first_responsible_person && is_string($first_responsible_person)) {
  117. $first_responsible_person = explode(',', $first_responsible_person);
  118. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $first_responsible_person)->distinct('violation_product_logid')->select('violation_product_logid');
  119. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  120. $query1->select('violation_product_logid')->fromSub($subQuery, 'sub1');
  121. });
  122. }
  123. //多选责任人
  124. if ($responsible_person && is_string($responsible_person)) {
  125. $responsible_person = explode(',', $responsible_person);
  126. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $responsible_person)->distinct('violation_product_logid')->select('violation_product_logid');
  127. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  128. $query1->select('violation_product_logid')->fromSub($subQuery, 'sub1');
  129. });
  130. }
  131. //多选溯源责任人
  132. if ($source_responsible_person && is_string($source_responsible_person)) {
  133. $source_responsible_person = explode(',', $source_responsible_person);
  134. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $source_responsible_person)->distinct('violation_product_logid')->select('violation_product_logid');
  135. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  136. $query1->select('violation_product_logid')->fromSub($subQuery, 'sub1');
  137. });
  138. }
  139. if ($is_admin != 1 && $company_id != 0) {
  140. $result = $ViolationProductModel->where(function ($q) use ($user_id) {
  141. $q->where('first_responsible_person', 'like', "%,$user_id,%")
  142. ->orWhere('responsible_person', 'like', "%,$user_id,%")
  143. ->orWhere('source_responsible_person', 'like', "%,$user_id,%");
  144. })
  145. ->where($map)
  146. ->orderByDesc('id')
  147. ->paginate($limit)->toarray();
  148. } else {
  149. $result = $ViolationProductModel
  150. ->where($map)
  151. ->orderByDesc('id')
  152. ->paginate($limit)->toarray();
  153. }
  154. // 分配数据
  155. if (!$result) json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  156. if (isset($result['data']) && count($result['data']) > 0) {
  157. foreach ($result['data'] as $key => $value) {
  158. //查询第一责任人名称
  159. $first_responsible_person = explode(',', $value['first_responsible_person']);
  160. $first_responsible_person_name = $EmployeeModel->whereIn('id', $first_responsible_person)->pluck('name')->toarray();
  161. $result['data'][$key]['first_responsible_person_name'] = $first_responsible_person_name;
  162. //查询责任人名称
  163. $responsible_person = explode(',', $value['responsible_person']);
  164. $responsible_person_name = $EmployeeModel->whereIn('id', $responsible_person)->pluck('name')->toarray();
  165. $result['data'][$key]['responsible_person_name'] = $responsible_person_name;
  166. //查询来源责任人名称
  167. $source_responsible_person = explode(',', $value['source_responsible_person']);
  168. $source_responsible_person_name = $EmployeeModel->whereIn('id', $source_responsible_person)->pluck('name')->toarray();
  169. $result['data'][$key]['source_responsible_person_name'] = $source_responsible_person_name;
  170. }
  171. }
  172. // 加载模板
  173. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  174. }
  175. /**
  176. * 列表导出
  177. * @author 唐远望
  178. * @version 1.0
  179. * @date 2025-06-17
  180. */
  181. public function export_excel(Request $request, ViolationProductModel $ViolationProductModel, EmployeeModel $EmployeeModel, ViolationProductMemberModel $ViolationProductMemberModel)
  182. {
  183. $request->scene('export_excel')->validate();
  184. $admin_company_id = request('admin_company_id', '0');
  185. $company_id = request('access_token.company_id', '0');
  186. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  187. // 查询条件
  188. $map = [];
  189. $limit = request('limit', config('page_num', 10));
  190. $status = request('status', '');
  191. $start_time = request('start_time', '');
  192. $end_time = request('end_time', '');
  193. $product_name = request('product_name', '');
  194. $product_names = request('product_names', '');
  195. $first_responsible_person = request('first_responsible_person', '');
  196. $responsible_person = request('responsible_person', '');
  197. $platform = request('platform', '');
  198. $company_name = request('company_name', '');
  199. $store_name = request('store_name', '');
  200. $store_names = request('store_names', '');
  201. $source_responsible_person = request('source_responsible_person', '');
  202. $processing_status = request('processing_status', '');
  203. $product_specs = request('product_specs', '');
  204. $online_posting_count = request('online_posting_count', '');
  205. $category_name = request('category_name', '');
  206. // 时间条件
  207. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  208. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  209. // 其他条件
  210. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  211. if ($store_name) $map[] = ['store_name', 'like', "%$store_name%"];
  212. if ($category_name) $map[] = ['category_name', 'like', "%$category_name%"];
  213. $violation_product_where = [];
  214. // 权限判断
  215. if ($is_admin != 1 && $company_id != 0) {
  216. $violation_product_where['company_id'] = $company_id;
  217. } else {
  218. $violation_product_where['company_id'] = $admin_company_id;
  219. }
  220. $ViolationProductModel = $ViolationProductModel->where($violation_product_where);
  221. //多选平台查询
  222. if ($platform && is_string($platform)) {
  223. $platform = explode(',', $platform);
  224. $ViolationProductModel = $ViolationProductModel->whereIn('platform', $platform);
  225. }
  226. //多选处理状态查询
  227. if ($processing_status && is_string($processing_status)) {
  228. $processing_status = explode(',', $processing_status);
  229. $ViolationProductModel = $ViolationProductModel->whereIn('processing_status', $processing_status);
  230. }
  231. //多选状态查询
  232. if ($status && is_string($status)) {
  233. $status = explode(',', $status);
  234. $ViolationProductModel = $ViolationProductModel->whereIn('status', $status);
  235. }
  236. //多选店铺名称查询
  237. if ($store_names && is_string($store_names)) {
  238. $store_names = explode(',', $store_names);
  239. $ViolationProductModel = $ViolationProductModel->whereIn('store_name', $store_names);
  240. }
  241. //多选违规挂网次数查询
  242. if ($online_posting_count && is_string($online_posting_count)) {
  243. $online_posting_count = explode(',', $online_posting_count);
  244. $ViolationProductModel = $ViolationProductModel->whereIn('online_posting_count', $online_posting_count);
  245. }
  246. //多选规格查询
  247. if ($product_specs && is_string($product_specs)) {
  248. $product_specs = explode(',', $product_specs);
  249. $ViolationProductModel = $ViolationProductModel->whereIn('product_specs', $product_specs);
  250. }
  251. //多选商品查询
  252. if ($product_names && is_string($product_names)) {
  253. $product_names = explode(',', $product_names);
  254. $ViolationProductModel = $ViolationProductModel->whereIn('product_name', $product_names);
  255. }
  256. //多选公司查询
  257. if ($company_name && is_string($company_name)) {
  258. $company_name = explode(',', $company_name);
  259. $ViolationProductModel = $ViolationProductModel->whereIn('company_name', $company_name);
  260. }
  261. //多选第一责任人
  262. if ($first_responsible_person && is_string($first_responsible_person)) {
  263. $first_responsible_person = explode(',', $first_responsible_person);
  264. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $first_responsible_person)->distinct('violation_product_logid')->select('violation_product_logid');
  265. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  266. $query1->select('violation_product_logid')->fromSub($subQuery, 'sub1');
  267. });
  268. }
  269. //多选责任人
  270. if ($responsible_person && is_string($responsible_person)) {
  271. $responsible_person = explode(',', $responsible_person);
  272. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $responsible_person)->distinct('violation_product_logid')->select('violation_product_logid');
  273. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  274. $query1->select('violation_product_logid')->fromSub($subQuery, 'sub1');
  275. });
  276. }
  277. //多选溯源责任人
  278. if ($source_responsible_person && is_string($source_responsible_person)) {
  279. $source_responsible_person = explode(',', $source_responsible_person);
  280. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $source_responsible_person)->distinct('violation_product_logid')->select('violation_product_logid');
  281. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  282. $query1->select('violation_product_logid')->fromSub($subQuery, 'sub1');
  283. });
  284. }
  285. $result_totle = $ViolationProductModel
  286. ->where($map)
  287. ->orderByDesc('id')
  288. ->count();
  289. // 数量过多时,限制导出
  290. if ($result_totle > 10000) {
  291. return json_send(['code' => 'error', 'msg' => '导出数据过多,请缩小查询条件']);
  292. }
  293. $result = $ViolationProductModel
  294. ->where($map)
  295. ->orderByDesc('id')
  296. ->get()->toarray();
  297. // 分配数据
  298. if (!$result) return json_send(['code' => 'error', 'msg' => '暂无数据']);
  299. // 处理责任人展示信息
  300. $list_data = $this->processing_responsible_person($result);
  301. // 导出数据
  302. $this->export_download($list_data);
  303. }
  304. /**
  305. * 处理责任人展示信息
  306. * @author 唐远望
  307. * @version 1.0
  308. * @date 2025-12-17
  309. */
  310. public function processing_responsible_person($result)
  311. {
  312. $EmployeeModel = new EmployeeModel();
  313. //获取所有员工
  314. $employeee_data = $EmployeeModel->select(['id', 'name'])->get()->toarray();
  315. $employeee_list = [];
  316. if (!empty($employeee_data)) {
  317. foreach ($employeee_data as $key => $value) {
  318. $employeee_list[$value['id']] = $value['name'];
  319. }
  320. }
  321. if (isset($result) && count($result) > 0) {
  322. foreach ($result as $key => $value) {
  323. //查询第一责任人名称
  324. $first_responsible_person = $value['first_responsible_person'] != '' ? explode(',', $value['first_responsible_person']) : [];
  325. $first_responsible_person_name = [];
  326. if (!empty($first_responsible_person)) {
  327. foreach ($first_responsible_person as $k => $v) {
  328. if (isset($employeee_list[$v])) {
  329. $first_responsible_person_name[] = $employeee_list[$v];
  330. }
  331. }
  332. }
  333. $result[$key]['first_responsible_person_name'] = $first_responsible_person_name;
  334. //查询责任人名称
  335. $responsible_person = $value['responsible_person'] != '' ? explode(',', $value['responsible_person']) : [];
  336. $responsible_person_name = [];
  337. if (!empty($responsible_person)) {
  338. foreach ($responsible_person as $k => $v) {
  339. if (isset($employeee_list[$v])) {
  340. $responsible_person_name[] = $employeee_list[$v];
  341. }
  342. }
  343. }
  344. $result[$key]['responsible_person_name'] = $responsible_person_name;
  345. //查询来源责任人名称
  346. $source_responsible_person = $value['source_responsible_person'] != '' ? explode(',', $value['source_responsible_person']) : [];
  347. $source_responsible_person_name = [];
  348. if (!empty($source_responsible_person)) {
  349. foreach ($source_responsible_person as $k => $v) {
  350. if (isset($employeee_list[$v])) {
  351. $source_responsible_person_name[] = $employeee_list[$v];
  352. }
  353. }
  354. }
  355. $result[$key]['source_responsible_person_name'] = $source_responsible_person_name;
  356. }
  357. }
  358. return $result;
  359. }
  360. /**
  361. * 导出下载
  362. * @author 唐远望
  363. * @version 1.0
  364. * @date 2025-06-17
  365. */
  366. public function export_download($data)
  367. {
  368. // 创建一个新的 Spreadsheet 对象
  369. $spreadsheet = new Spreadsheet();
  370. $sheet = $spreadsheet->getActiveSheet();
  371. //合并单元格
  372. $sheet->mergeCells('A1:Q1');
  373. $sheet->setCellValue('A1', '禁止挂网商品导出(导出时间:' . date('Y-m-d H:i:s', time()) . ')'); // 设置合并后的单元格内容
  374. // 获取合并后的单元格样式对象
  375. $style = $sheet->getStyle('A1');
  376. // 设置水平居中和垂直居中
  377. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  378. // 然后设置行高以适应两行文本
  379. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  380. // 设置表头
  381. $sheet->setCellValue('A2', '第一责任人');
  382. $sheet->setCellValue('B2', '责任人');
  383. $sheet->setCellValue('C2', '平台');
  384. $sheet->setCellValue('D2', '商品分类');
  385. $sheet->setCellValue('E2', '商品名称');
  386. $sheet->setCellValue('F2', '库存');
  387. $sheet->setCellValue('G2', '销量');
  388. $sheet->setCellValue('H2', '快照URL');
  389. $sheet->setCellValue('I2', '商品规格');
  390. $sheet->setCellValue('J2', '累计挂网次数');
  391. $sheet->setCellValue('K2', '连续挂网次数');
  392. $sheet->setCellValue('L2', '链接地址');
  393. $sheet->setCellValue('M2', '店铺名称');
  394. $sheet->setCellValue('N2', '公司名称');
  395. $sheet->setCellValue('O2', '公司分类');
  396. $sheet->setCellValue('P2', '信用代码');
  397. $sheet->setCellValue('Q2', '省份');
  398. $sheet->setCellValue('R2', '城市');
  399. $sheet->setCellValue('S2', '溯源责任人');
  400. $sheet->setCellValue('T2', '处理状态');
  401. $sheet->setCellValue('U2', '任务状态');
  402. $sheet->setCellValue('V2', '记录时间');
  403. $platform_data = [
  404. '0' => '全部',
  405. '1' => '淘宝',
  406. '2' => '京东',
  407. '3' => '拼多多',
  408. '4' => '美团',
  409. '5' => '药师帮',
  410. '6' => '1药城',
  411. '7' => '药久久',
  412. ];
  413. $processing_status_text = [
  414. '1' => '待处理',
  415. ];
  416. $status_text = [
  417. '0' => '有效',
  418. '1' => '无效',
  419. ];
  420. // 填充数据
  421. $row = 3; // 从第2行开始
  422. foreach ($data as $item) {
  423. $first_responsible_person_name = !empty($item['first_responsible_person_name']) ? implode(',', $item['first_responsible_person_name']) : '';
  424. $responsible_person_name = !empty($item['responsible_person_name']) ? implode(',', $item['responsible_person_name']) : '';
  425. $source_responsible_person_name = !empty($item['source_responsible_person_name']) ? implode(',', $item['source_responsible_person_name']) : '';
  426. $sheet->setCellValue('A' . $row, $first_responsible_person_name);
  427. $sheet->setCellValue('B' . $row, $responsible_person_name);
  428. $sheet->setCellValue('C' . $row, isset($platform_data[$item['platform']]) ? $platform_data[$item['platform']] : '');
  429. $sheet->setCellValue('D' . $row, $item['category_name']);
  430. $sheet->setCellValue('E' . $row, $item['product_name']);
  431. $sheet->setCellValue('F' . $row, $item['inventory']);
  432. $sheet->setCellValue('G' . $row, $item['sales']);
  433. $sheet->setCellValue('H' . $row, $item['snapshot_url']);
  434. $sheet->setCellValue('I' . $row, $item['product_specs']);
  435. $sheet->setCellValue('J' . $row, $item['online_posting_count']);
  436. $sheet->setCellValue('K' . $row, $item['continuous_listing_count']);
  437. $sheet->setCellValue('L' . $row, $item['link_url']);
  438. $sheet->setCellValue('M' . $row, $item['store_name']);
  439. $sheet->setCellValue('N' . $row, $item['company_name']);
  440. $sheet->setCellValue('O' . $row, $item['company_category_name']);
  441. $sheet->setCellValue('P' . $row, $item['social_credit_code']);
  442. $sheet->setCellValue('Q' . $row, $item['province_name']);
  443. $sheet->setCellValue('R' . $row, $item['city_name']);
  444. $sheet->setCellValue('S' . $row, $source_responsible_person_name);
  445. $sheet->setCellValue('T' . $row, isset($processing_status_text[$item['processing_status']]) ? $processing_status_text[$item['processing_status']] : '');
  446. $sheet->setCellValue('U' . $row, isset($status_text[$item['status']]) ? $status_text[$item['status']] : '');
  447. $sheet->setCellValue('V' . $row, date('Y-m-d H:i:s', $item['insert_time']));
  448. $row++;
  449. }
  450. foreach (range('A', 'P') as $column) {
  451. $sheet->getColumnDimension($column)->setAutoSize(true);
  452. }
  453. // 生成 Excel 文件
  454. $writer = new Xlsx($spreadsheet);
  455. // 直接输出到浏览器(下载)
  456. $filename = '禁止挂网商品数据' . date('YmdHis') . '.xlsx';
  457. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  458. header('Content-Disposition: attachment;filename="' . $filename . '"');
  459. header('Cache-Control: max-age=0');
  460. $writer->save('php://output');
  461. exit;
  462. }
  463. /**
  464. * 详情
  465. * @author 唐远望
  466. * @version 1.0
  467. * @date 2025-12-08
  468. */
  469. public function detail(Request $request, ViolationProductModel $ViolationProductModel, EmployeeModel $EmployeeModel)
  470. {
  471. $request->scene('detail')->validate();
  472. $admin_company_id = request('admin_company_id', '0');
  473. $company_id = request('access_token.company_id', '0');
  474. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  475. // 接收参数
  476. $id = request('id', 0);
  477. $map = ['id' => $id];
  478. // 权限判断
  479. if ($is_admin != 1 && $company_id != 0) {
  480. $map['company_id'] = $company_id;
  481. } else {
  482. $map['company_id'] = $admin_company_id;
  483. }
  484. $data = $ViolationProductModel->where($map)->first();
  485. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  486. //查询第一责任人名称
  487. $first_responsible_person = explode(',', $data->first_responsible_person);
  488. $first_responsible_person_name = $EmployeeModel->whereIn('id', $first_responsible_person)->pluck('name')->toarray();
  489. $data->first_responsible_person_name = $first_responsible_person_name;
  490. //查询责任人名称
  491. $responsible_person = explode(',', $data->responsible_person);
  492. $responsible_person_name = $EmployeeModel->whereIn('id', $responsible_person)->pluck('name')->toarray();
  493. $data->responsible_person_name = $responsible_person_name;
  494. //查询来源责任人名称
  495. $source_responsible_person = explode(',', $data->source_responsible_person);
  496. $source_responsible_person_name = $EmployeeModel->whereIn('id', $source_responsible_person)->pluck('name')->toarray();
  497. $data->source_responsible_person_name = $source_responsible_person_name;
  498. // 加载模板
  499. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  500. }
  501. /**
  502. * 添加
  503. * @author 唐远望
  504. * @version 1.0
  505. * @date 2025-12-08
  506. *
  507. */
  508. public function add(Request $request, ViolationProductModel $ViolationProductModel)
  509. {
  510. $request->scene('add')->validate();
  511. $admin_company_id = request('admin_company_id', '0');
  512. $company_id = request('access_token.company_id', '0');
  513. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  514. // 接收数据
  515. $all_data = request()->all();
  516. $store_scope = request('store_scope', '');
  517. $all_data['store_scope'] = $store_scope;
  518. //查询是否存在
  519. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs']];
  520. // 权限判断
  521. if ($is_admin != 1 && $company_id != 0) {
  522. $map['company_id'] = $company_id;
  523. $all_data['company_id'] = $company_id;
  524. } else {
  525. $map['company_id'] = $admin_company_id;
  526. $all_data['company_id'] = $admin_company_id;
  527. }
  528. $data = $ViolationProductModel->where($map)->first();
  529. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  530. // 写入数据表
  531. $result = $ViolationProductModel->addLowPriceGoods($all_data);
  532. // 如果操作失败
  533. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  534. // 告知结果
  535. return json_send(['code' => 'success', 'msg' => '新增成功']);
  536. }
  537. /**
  538. * 修改
  539. * @author 唐远望
  540. * @version 1.0
  541. * @date 2025-12-08
  542. *
  543. */
  544. public function edit(Request $request, ViolationProductModel $ViolationProductModel)
  545. {
  546. $request->scene('edit')->validate();
  547. $admin_company_id = request('admin_company_id', '0');
  548. $company_id = request('access_token.company_id', '0');
  549. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  550. // 接收参数
  551. $id = request('id', 0);
  552. // 接收数据
  553. $all_data = request()->all();
  554. $store_scope = request('store_scope', '');
  555. $all_data['store_scope'] = $store_scope;
  556. //查询是否存在
  557. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs']];
  558. // 权限判断
  559. if ($is_admin != 1 && $company_id != 0) {
  560. $map['company_id'] = $company_id;
  561. $all_data['company_id'] = $company_id;
  562. } else {
  563. $map['company_id'] = $admin_company_id;
  564. $all_data['company_id'] = $admin_company_id;
  565. }
  566. $data = $ViolationProductModel->where($map)->where('id', '!=', $id)->first();
  567. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  568. // 更新数据表
  569. $where = ['id' => $id];
  570. $result = $ViolationProductModel->updateLowPriceGoods($where, $all_data);
  571. // 如果操作失败
  572. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  573. // 告知结果
  574. return json_send(['code' => 'success', 'msg' => '修改成功']);
  575. }
  576. /**
  577. * 修改状态
  578. * @author 唐远望
  579. * @version 1.0
  580. * @date 2025-12-08
  581. *
  582. */
  583. public function set_status(Request $request, ViolationProductModel $ViolationProductModel)
  584. {
  585. // 验证参数
  586. $request->scene('set_status')->validate();
  587. $admin_company_id = request('admin_company_id', '0');
  588. $company_id = request('access_token.company_id', '0');
  589. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  590. // 接收数据
  591. $id = request('id', 0);
  592. $status = request('status', 0);
  593. // 查询用户
  594. $where = ['id' => $id];
  595. // 权限判断
  596. if ($is_admin != 1 && $company_id != 0) {
  597. $where['company_id'] = $company_id;
  598. } else {
  599. $where['company_id'] = $admin_company_id;
  600. }
  601. $ViolationProduct = $ViolationProductModel->where($where)->first();
  602. if (!$ViolationProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  603. $ViolationProduct->status = $status;
  604. $ViolationProduct->update_time = time();
  605. // 执行修改
  606. $result = $ViolationProduct->save();
  607. // 提示新增失败
  608. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  609. // 告知结果
  610. return json_send(['code' => 'success', 'msg' => '设置成功']);
  611. }
  612. /**
  613. * 修改处理状态
  614. * @author 唐远望
  615. * @version 1.0
  616. * @date 2025-12-08
  617. *
  618. */
  619. public function set_processing_status(Request $request, ViolationProductModel $ViolationProductModel)
  620. {
  621. // 验证参数
  622. $request->scene('set_processing_status')->validate();
  623. $admin_company_id = request('admin_company_id', '0');
  624. $company_id = request('access_token.company_id', '0');
  625. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  626. // 接收数据
  627. $id = request('id', 0);
  628. $processing_status = request('processing_status', 0);
  629. // 查询用户
  630. $where = ['id' => $id];
  631. // 权限判断
  632. if ($is_admin != 1 && $company_id != 0) {
  633. $where['company_id'] = $company_id;
  634. } else {
  635. $where['company_id'] = $admin_company_id;
  636. }
  637. $ViolationProduct = $ViolationProductModel->where($where)->first();
  638. if (!$ViolationProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  639. $ViolationProduct->processing_status = $processing_status;
  640. $ViolationProduct->update_time = time();
  641. // 执行修改
  642. $result = $ViolationProduct->save();
  643. // 提示新增失败
  644. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  645. // 告知结果
  646. return json_send(['code' => 'success', 'msg' => '设置成功']);
  647. }
  648. /**
  649. * 删除
  650. * @author 唐远望
  651. * @version 1.0
  652. * @date 2025-12-08
  653. *
  654. */
  655. public function delete(Request $request, ViolationProductModel $ViolationProductModel)
  656. {
  657. // 验证参数
  658. $request->scene('delete')->validate();
  659. $admin_company_id = request('admin_company_id', '0');
  660. $company_id = request('access_token.company_id', '0');
  661. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  662. // 接收数据
  663. $id = request('id', 0);
  664. // 查询用户
  665. $where = ['id' => $id];
  666. // 权限判断
  667. if ($is_admin != 1 && $company_id != 0) {
  668. $where['company_id'] = $company_id;
  669. } else {
  670. $where['company_id'] = $admin_company_id;
  671. }
  672. $ViolationProduct = $ViolationProductModel->where($where)->first();
  673. if (!$ViolationProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  674. // 执行删除
  675. $result = $ViolationProduct->delete();
  676. // 提示删除失败
  677. if (!$result) return json_send(['code' => 'error', 'msg' => '删除失败']);
  678. // 告知结果
  679. return json_send(['code' => 'success', 'msg' => '删除成功']);
  680. }
  681. /**
  682. * 执行数据清洗
  683. * @author 唐远望
  684. * @version 1.0
  685. * @date 2025-12-11
  686. *
  687. */
  688. public function data_cleaning(Request $request)
  689. {
  690. // 验证参数
  691. $request->scene('data_cleaning')->validate();
  692. $admin_company_id = request('admin_company_id', '0');
  693. $company_id = request('access_token.company_id', '0');
  694. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  695. $admin_id = request('access_token.uid', 0); //用户ID
  696. // 权限判断
  697. if ($is_admin != 1 && $company_id != 0) {
  698. } else {
  699. $company_id = $admin_company_id;
  700. }
  701. $message_data = ['page' => '1', 'limit' => 50, 'admin_id' => $admin_id, 'is_admin' => $is_admin, 'company_id' => $company_id];
  702. ViolationProductJobs::dispatch($message_data);
  703. // ViolationProductJobs::dispatchSync($message_data);
  704. // 告知结果
  705. return json_send(['code' => 'success', 'msg' => '执行成功']);
  706. }
  707. /**
  708. * 下载导入模板
  709. * @author 唐远望
  710. * @version 1.0
  711. * @date 2025-12-31
  712. *
  713. */
  714. public function download_template()
  715. {
  716. // 创建一个新的 Spreadsheet 对象
  717. $spreadsheet = new Spreadsheet();
  718. $sheet = $spreadsheet->getActiveSheet();
  719. //合并单元格
  720. $sheet->mergeCells('A1:P1');
  721. $sheet->setCellValue('A1', '禁止挂网商品导入模板'); // 设置合并后的单元格内容
  722. // 获取合并后的单元格样式对象
  723. $style = $sheet->getStyle('A1');
  724. // 设置水平居中和垂直居中
  725. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  726. // 然后设置行高以适应两行文本
  727. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  728. // 设置表头
  729. $sheet->setCellValue('A2', '第一责任人名称');
  730. $sheet->setCellValue('B2', '责任人名称');
  731. $sheet->setCellValue('C2', '平台名称*');
  732. $sheet->setCellValue('D2', '商品分类名称');
  733. $sheet->setCellValue('E2', '商品名称*');
  734. $sheet->setCellValue('F2', '商品规格*');
  735. $sheet->setCellValue('G2', '累计挂网次数');
  736. $sheet->setCellValue('H2', '连续挂网次数');
  737. $sheet->setCellValue('I2', '销量');
  738. $sheet->setCellValue('J2', '库存');
  739. $sheet->setCellValue('K2', '快照URL');
  740. $sheet->setCellValue('L2', '链接地址*');
  741. $sheet->setCellValue('M2', '店铺名称*');
  742. $sheet->setCellValue('N2', '公司名称*');
  743. $sheet->setCellValue('O2', '公司分类名称');
  744. $sheet->setCellValue('P2', '信用代码*');
  745. $sheet->setCellValue('Q2', '省份*');
  746. $sheet->setCellValue('R2', '城市*');
  747. $sheet->setCellValue('S2', '溯源责任人');
  748. // 生成 Excel 文件
  749. $writer = new Xlsx($spreadsheet);
  750. // 直接输出到浏览器(下载)
  751. $filename = '禁止挂网商品导入模板.xlsx';
  752. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  753. header('Content-Disposition: attachment;filename="' . $filename . '"');
  754. header('Cache-Control: max-age=0');
  755. $writer->save('php://output');
  756. exit;
  757. }
  758. /**
  759. * 导入Excel数据
  760. * @author 唐远望
  761. * @version 1.0
  762. * @date 2025-12-31
  763. *
  764. */
  765. public function import_data(Request $request, ViolationProductModel $ViolationProductModel, EmployeeModel $EmployeeModel, CitysModel $CitysModel)
  766. {
  767. $request->scene('import_data')->validate();
  768. $admin_company_id = request('admin_company_id', '0');
  769. $company_id = request('access_token.company_id', '0');
  770. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  771. $file = $request->file('file');
  772. // 加载Excel文件
  773. $spreadsheet = IOFactory::load($file->getPathname());
  774. $sheet = $spreadsheet->getActiveSheet();
  775. // 获取所有数据
  776. $data = $sheet->toArray();
  777. if (empty($data) || count($data) < 2) return json_send(['code' => 'error', 'msg' => '导入数据为空']);
  778. $result = $EmployeeModel->select(['id', 'name'])->get()->keyBy('name')->toarray();
  779. $platform_data = $ViolationProductModel->platform_data();
  780. $province_id_data = $CitysModel->get_province_id_list();
  781. $city_id_data = $CitysModel->get_city_id_list();
  782. DB::beginTransaction();
  783. try {
  784. foreach ($data as $key => $item) {
  785. $key_num = $key + 1;
  786. if ($key < 2) continue;
  787. //强制必传参数校验
  788. $res_data = $this->import_data_check($key, $item);
  789. if ($res_data) return json_send($res_data);
  790. $first_responsible_person_id = [];
  791. $first_responsible_person_name = $item[0];
  792. if ($first_responsible_person_name) {
  793. $first_responsible_person_name = explode(',', $first_responsible_person_name);
  794. foreach ($first_responsible_person_name as $k => $v) {
  795. if (isset($result[$v])) {
  796. $first_responsible_person_id[] = $result[$v]['id'];
  797. }
  798. }
  799. }
  800. $responsible_person_id = [];
  801. $responsible_person_name = $item[1];
  802. if ($responsible_person_name) {
  803. $responsible_person_name = explode(',', $responsible_person_name);
  804. foreach ($responsible_person_name as $k => $v) {
  805. if (isset($result[$v])) {
  806. $responsible_person_id[] = $result[$v]['id'];
  807. }
  808. }
  809. }
  810. $source_responsible_person_id = [];
  811. $source_responsible_person_name = $item[18];
  812. if ($source_responsible_person_name) {
  813. $source_responsible_person_name = explode(',', $source_responsible_person_name);
  814. foreach ($source_responsible_person_name as $k => $v) {
  815. if (isset($result[$v])) {
  816. $source_responsible_person_id[] = $result[$v]['id'];
  817. }
  818. }
  819. }
  820. $province_name = $item[16];
  821. //特殊地区1级移除市
  822. if ($province_name && in_array($province_name, ['北京市', '天津市', '上海市', '重庆市'])) {
  823. //移除市这个字符
  824. $province_name = trim(str_replace('市', '', $province_name));
  825. }else if ($province_name && in_array($province_name, ['北京', '天津', '上海', '重庆'])) {
  826. } else if ($province_name && in_array($province_name, ['内蒙古', '广西', '西藏', '新疆', '宁夏'])) {
  827. switch ($province_name) {
  828. case '内蒙古':
  829. $province_name = '内蒙古自治区';
  830. break;
  831. case '广西':
  832. $province_name = '广西壮族自治区';
  833. break;
  834. case '西藏':
  835. $province_name = '西藏自治区';
  836. break;
  837. case '新疆':
  838. $province_name = '新疆维吾尔自治区';
  839. break;
  840. case '宁夏':
  841. $province_name = '宁夏回族自治区';
  842. break;
  843. }
  844. } else if ($province_name && in_array($province_name, ['内蒙古自治区', '广西壮族自治区', '西藏自治区', '新疆维吾尔自治区', '宁夏回族自治区'])) {
  845. } else if (strpos($province_name, '省') === false) {
  846. //是否存在市省,如果不存在则补全
  847. if (strpos($province_name, '省') === false) {
  848. $province_name = $province_name . '省';
  849. }
  850. }
  851. if (!isset($province_id_data[$province_name])) return json_send(['code' => 'error', 'msg' => "第{$key_num}行省份信息不完整", 'data' => $item]);
  852. $city_name = $item[14];
  853. if (!isset($city_id_data[$city_name])) return json_send(['code' => 'error', 'msg' => "第{$key_num}行城市信息不完整", 'data' => $item]);
  854. // 权限判断
  855. if ($is_admin != 1 && $company_id != 0) {
  856. $insert_product_data['company_id'] = $company_id;
  857. } else {
  858. $insert_product_data['company_id'] = $admin_company_id;
  859. }
  860. $insert_product_data['first_responsible_person'] = !empty($first_responsible_person_id) ? implode(',', $first_responsible_person_id) : ''; //第一责任人ID集合
  861. $insert_product_data['responsible_person'] = !empty($responsible_person_id) ? implode(',', $responsible_person_id) : ''; //责任人ID集合
  862. $insert_product_data['platform'] = isset($platform_data[$item[2]]) ? $platform_data[$item[2]] : '0'; // 平台
  863. $insert_product_data['category_name'] = $item[3] ? $item[4] : ''; // 商品分类
  864. $insert_product_data['product_name'] = $item[4]; // 商品名称
  865. $insert_product_data['product_specs'] = $item[5]; // 商品规格
  866. $insert_product_data['online_posting_count'] = isset($item[6]) ? $item[6] : 1; // 累计挂网次数
  867. $insert_product_data['continuous_listing_count'] = isset($item[7]) ? $item[7] : 1; // 连续挂网次数
  868. $insert_product_data['sales'] = isset($item[8]) ? $item[8] : 0; // 销量
  869. $insert_product_data['inventory'] = isset($item[9]) ? $item[9] : 0; // 库存
  870. $insert_product_data['snapshot_url'] = isset($item[10]) ? $item[10] : ''; // 快照URL
  871. $insert_product_data['link_url'] = $item[11]; // 链接地址
  872. $insert_product_data['store_name'] = $item[12]; // 店铺名称
  873. $insert_product_data['company_name'] = $item[13]; // 公司名称
  874. $insert_product_data['company_category_name'] = isset($item[14]) ? $item[14] : ''; // 公司分类
  875. $insert_product_data['social_credit_code'] = $item[15]; // 信用代码
  876. $insert_product_data['province_name'] = $item[16]; // 省份
  877. $insert_product_data['province_id'] = isset($province_id_data[$province_name]) ? $province_id_data[$province_name]['id'] : 0; // 省份ID
  878. $insert_product_data['city_name'] = $item[17]; // 城市
  879. $insert_product_data['city_id'] = isset($city_id_data[$city_name]) ? $city_id_data[$city_name]['id'] : 0; // 城市ID
  880. $insert_product_data['area_info'] = ''; // 详细地址
  881. $insert_product_data['source_responsible_person'] = !empty($source_responsible_person_id) ? implode(',', $source_responsible_person_id) : ''; //溯源责任人ID集合
  882. $insert_product_data['processing_status'] = 1; //处理状态1=待处理2=购买中3=已溯源4=回收凭据已上传5=已回收6=拒绝回收7=已下架8=无法处理
  883. $insert_product_data['status'] = 0; //状态0=有效=无效
  884. $insert_product_data['insert_time'] = time();
  885. $insert_product_data['source_id'] = 0; // 原始数据ID
  886. //插入数据
  887. $ViolationProductModel->addViolationProduct($insert_product_data, true);
  888. }
  889. DB::commit();
  890. return json_send(['code' => 'success', 'msg' => '导入成功']);
  891. // 成功处理...
  892. } catch (\Exception $e) {
  893. DB::rollBack();
  894. // 错误处理...
  895. return json_send(['code' => 'error', 'msg' => '导入失败', 'data' => $e->getMessage()]);
  896. }
  897. }
  898. /**
  899. * 导入Excel数据必传参数校验
  900. * @author 唐远望
  901. * @version 1.0
  902. * @date 2025-12-31
  903. *
  904. */
  905. private function import_data_check($key, $item)
  906. {
  907. $key = $key + 1;
  908. if (!$item[2]) {
  909. return ['code' => 'error', 'msg' => "第{$key}行平台不能为空", 'data' => $item];
  910. }
  911. if (!$item[4]) {
  912. return ['code' => 'error', 'msg' => "第{$key}行商品名称不能为空", 'data' => $item];
  913. }
  914. if (!$item[5]) {
  915. return ['code' => 'error', 'msg' => "第{$key}行商品规格不能为空", 'data' => $item];
  916. }
  917. if (!$item[11]) {
  918. return ['code' => 'error', 'msg' => "第{$key}行链接地址不能为空", 'data' => $item];
  919. }
  920. if (!$item[12]) {
  921. return ['code' => 'error', 'msg' => "第{$key}行店铺名称不能为空", 'data' => $item];
  922. }
  923. if (!$item[13]) {
  924. return ['code' => 'error', 'msg' => "第{$key}行公司名称不能为空", 'data' => $item];
  925. }
  926. if (!$item[15]) {
  927. return ['code' => 'error', 'msg' => "第{$key}行信用代码不能为空", 'data' => $item];
  928. }
  929. if (!$item[16]) {
  930. return ['code' => 'error', 'msg' => "第{$key}行省份不能为空", 'data' => $item];
  931. }
  932. if (!$item[17]) {
  933. return ['code' => 'error', 'msg' => "第{$key}行城市不能为空", 'data' => $item];
  934. }
  935. }
  936. }