ViolationProduct.php 47 KB

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