ViolationProduct.php 43 KB

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