ViolationProduct.php 46 KB

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