ViolationProduct.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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 = $ViolationProductModel
  272. ->where($map)
  273. ->orderByDesc('id')
  274. ->get()->toarray();
  275. // 分配数据
  276. if (!$result) return json_send(['code' => 'error', 'msg' => '暂无数据']);
  277. // 处理责任人展示信息
  278. $list_data = $this->processing_responsible_person($result);
  279. // 导出数据
  280. $this->export_download($list_data);
  281. }
  282. /**
  283. * 处理责任人展示信息
  284. * @author 唐远望
  285. * @version 1.0
  286. * @date 2025-12-17
  287. */
  288. public function processing_responsible_person($result)
  289. {
  290. $EmployeeModel = new EmployeeModel();
  291. //获取所有员工
  292. $employeee_data = $EmployeeModel->select(['id', 'name'])->get()->toarray();
  293. $employeee_list = [];
  294. if (!empty($employeee_data)) {
  295. foreach ($employeee_data as $key => $value) {
  296. $employeee_list[$value['id']] = $value['name'];
  297. }
  298. }
  299. if (isset($result) && count($result) > 0) {
  300. foreach ($result as $key => $value) {
  301. //查询第一责任人名称
  302. $first_responsible_person = $value['first_responsible_person'] != '' ? explode(',', $value['first_responsible_person']) : [];
  303. $first_responsible_person_name = [];
  304. if (!empty($first_responsible_person)) {
  305. foreach ($first_responsible_person as $k => $v) {
  306. if (isset($employeee_list[$v])) {
  307. $first_responsible_person_name[] = $employeee_list[$v];
  308. }
  309. }
  310. }
  311. $result[$key]['first_responsible_person_name'] = $first_responsible_person_name;
  312. //查询责任人名称
  313. $responsible_person = $value['responsible_person'] != '' ? explode(',', $value['responsible_person']) : [];
  314. $responsible_person_name = [];
  315. if (!empty($responsible_person)) {
  316. foreach ($responsible_person as $k => $v) {
  317. if (isset($employeee_list[$v])) {
  318. $responsible_person_name[] = $employeee_list[$v];
  319. }
  320. }
  321. }
  322. $result[$key]['responsible_person_name'] = $responsible_person_name;
  323. //查询来源责任人名称
  324. $source_responsible_person = $value['source_responsible_person'] != '' ? explode(',', $value['source_responsible_person']) : [];
  325. $source_responsible_person_name = [];
  326. if (!empty($source_responsible_person)) {
  327. foreach ($source_responsible_person as $k => $v) {
  328. if (isset($employeee_list[$v])) {
  329. $source_responsible_person_name[] = $employeee_list[$v];
  330. }
  331. }
  332. }
  333. $result[$key]['source_responsible_person_name'] = $source_responsible_person_name;
  334. }
  335. }
  336. return $result;
  337. }
  338. /**
  339. * 导出下载
  340. * @author 唐远望
  341. * @version 1.0
  342. * @date 2025-06-17
  343. */
  344. public function export_download($data)
  345. {
  346. // 创建一个新的 Spreadsheet 对象
  347. $spreadsheet = new Spreadsheet();
  348. $sheet = $spreadsheet->getActiveSheet();
  349. //合并单元格
  350. $sheet->mergeCells('A1:Q1');
  351. $sheet->setCellValue('A1', '禁止挂网商品导出(导出时间:' . date('Y-m-d H:i:s', time()) . ')'); // 设置合并后的单元格内容
  352. // 获取合并后的单元格样式对象
  353. $style = $sheet->getStyle('A1');
  354. // 设置水平居中和垂直居中
  355. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  356. // 然后设置行高以适应两行文本
  357. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  358. // 设置表头
  359. $sheet->setCellValue('A2', '第一责任人');
  360. $sheet->setCellValue('B2', '责任人');
  361. $sheet->setCellValue('C2', '平台');
  362. $sheet->setCellValue('D2', '商品分类');
  363. $sheet->setCellValue('E2', '商品名称');
  364. $sheet->setCellValue('F2', '商品规格');
  365. $sheet->setCellValue('G2', '累计挂网次数');
  366. $sheet->setCellValue('H2', '连续挂网次数');
  367. $sheet->setCellValue('I2', '链接地址');
  368. $sheet->setCellValue('J2', '店铺名称');
  369. $sheet->setCellValue('K2', '公司名称');
  370. $sheet->setCellValue('L2', '公司分类');
  371. $sheet->setCellValue('M2', '信用代码');
  372. $sheet->setCellValue('N2', '省份');
  373. $sheet->setCellValue('O2', '城市');
  374. $sheet->setCellValue('P2', '溯源责任人');
  375. $sheet->setCellValue('Q2', '处理状态');
  376. $sheet->setCellValue('R2', '任务状态');
  377. $sheet->setCellValue('S2', '记录时间');
  378. $platform_data = [
  379. '0' => '全部',
  380. '1' => '淘宝',
  381. '2' => '京东',
  382. '3' => '拼多多',
  383. '4' => '美团',
  384. '5' => '药师帮',
  385. '6' => '1药城',
  386. '7' => '药久久',
  387. ];
  388. $processing_status_text = [
  389. '1' => '待处理',
  390. ];
  391. $status_text = [
  392. '0' => '有效',
  393. '1' => '无效',
  394. ];
  395. // 填充数据
  396. $row = 3; // 从第2行开始
  397. foreach ($data as $item) {
  398. $first_responsible_person_name = !empty($item['first_responsible_person_name']) ? implode(',', $item['first_responsible_person_name']) : '';
  399. $responsible_person_name = !empty($item['responsible_person_name']) ? implode(',', $item['responsible_person_name']) : '';
  400. $source_responsible_person_name = !empty($item['source_responsible_person_name']) ? implode(',', $item['source_responsible_person_name']) : '';
  401. $sheet->setCellValue('A' . $row, $first_responsible_person_name);
  402. $sheet->setCellValue('B' . $row, $responsible_person_name);
  403. $sheet->setCellValue('C' . $row, isset($platform_data[$item['platform']]) ? $platform_data[$item['platform']] : '');
  404. $sheet->setCellValue('D' . $row, $item['category_name']);
  405. $sheet->setCellValue('E' . $row, $item['product_name']);
  406. $sheet->setCellValue('F' . $row, $item['product_specs']);
  407. $sheet->setCellValue('G' . $row, $item['online_posting_count']);
  408. $sheet->setCellValue('H' . $row, $item['continuous_listing_count']);
  409. $sheet->setCellValue('I' . $row, $item['link_url']);
  410. $sheet->setCellValue('J' . $row, $item['store_name']);
  411. $sheet->setCellValue('K' . $row, $item['company_name']);
  412. $sheet->setCellValue('L' . $row, $item['company_category_name']);
  413. $sheet->setCellValue('M' . $row, $item['social_credit_code']);
  414. $sheet->setCellValue('N' . $row, $item['province_name']);
  415. $sheet->setCellValue('O' . $row, $item['city_name']);
  416. $sheet->setCellValue('P' . $row, $source_responsible_person_name);
  417. $sheet->setCellValue('Q' . $row, isset($processing_status_text[$item['processing_status']]) ? $processing_status_text[$item['processing_status']] : '');
  418. $sheet->setCellValue('R' . $row, isset($status_text[$item['status']]) ? $status_text[$item['status']] : '');
  419. $sheet->setCellValue('S' . $row, date('Y-m-d H:i:s', $item['insert_time']));
  420. $row++;
  421. }
  422. foreach (range('A', 'P') as $column) {
  423. $sheet->getColumnDimension($column)->setAutoSize(true);
  424. }
  425. // 生成 Excel 文件
  426. $writer = new Xlsx($spreadsheet);
  427. // 直接输出到浏览器(下载)
  428. $filename = '禁止挂网商品数据' . date('YmdHis') . '.xlsx';
  429. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  430. header('Content-Disposition: attachment;filename="' . $filename . '"');
  431. header('Cache-Control: max-age=0');
  432. $writer->save('php://output');
  433. exit;
  434. }
  435. /**
  436. * 详情
  437. * @author 唐远望
  438. * @version 1.0
  439. * @date 2025-12-08
  440. */
  441. public function detail(Request $request, ViolationProductModel $ViolationProductModel, EmployeeModel $EmployeeModel)
  442. {
  443. $request->scene('detail')->validate();
  444. $admin_company_id = request('admin_company_id', '0');
  445. $company_id = request('access_token.company_id', '0');
  446. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  447. // 接收参数
  448. $id = request('id', 0);
  449. $map = ['id' => $id];
  450. // 权限判断
  451. if ($is_admin != 1 && $company_id != 0) {
  452. $map['company_id'] = $company_id;
  453. } else {
  454. $map['company_id'] = $admin_company_id;
  455. }
  456. $data = $ViolationProductModel->where($map)->first();
  457. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  458. //查询第一责任人名称
  459. $first_responsible_person = explode(',', $data->first_responsible_person);
  460. $first_responsible_person_name = $EmployeeModel->whereIn('id', $first_responsible_person)->pluck('name')->toarray();
  461. $data->first_responsible_person_name = $first_responsible_person_name;
  462. //查询责任人名称
  463. $responsible_person = explode(',', $data->responsible_person);
  464. $responsible_person_name = $EmployeeModel->whereIn('id', $responsible_person)->pluck('name')->toarray();
  465. $data->responsible_person_name = $responsible_person_name;
  466. //查询来源责任人名称
  467. $source_responsible_person = explode(',', $data->source_responsible_person);
  468. $source_responsible_person_name = $EmployeeModel->whereIn('id', $source_responsible_person)->pluck('name')->toarray();
  469. $data->source_responsible_person_name = $source_responsible_person_name;
  470. // 加载模板
  471. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  472. }
  473. /**
  474. * 添加
  475. * @author 唐远望
  476. * @version 1.0
  477. * @date 2025-12-08
  478. *
  479. */
  480. public function add(Request $request, ViolationProductModel $ViolationProductModel)
  481. {
  482. $request->scene('add')->validate();
  483. $admin_company_id = request('admin_company_id', '0');
  484. $company_id = request('access_token.company_id', '0');
  485. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  486. // 接收数据
  487. $all_data = request()->all();
  488. $store_scope = request('store_scope', '');
  489. $all_data['store_scope'] = $store_scope;
  490. //查询是否存在
  491. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs']];
  492. // 权限判断
  493. if ($is_admin != 1 && $company_id != 0) {
  494. $map['company_id'] = $company_id;
  495. $all_data['company_id'] = $company_id;
  496. } else {
  497. $map['company_id'] = $admin_company_id;
  498. $all_data['company_id'] = $admin_company_id;
  499. }
  500. $data = $ViolationProductModel->where($map)->first();
  501. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  502. // 写入数据表
  503. $result = $ViolationProductModel->addLowPriceGoods($all_data);
  504. // 如果操作失败
  505. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  506. // 告知结果
  507. return json_send(['code' => 'success', 'msg' => '新增成功']);
  508. }
  509. /**
  510. * 修改
  511. * @author 唐远望
  512. * @version 1.0
  513. * @date 2025-12-08
  514. *
  515. */
  516. public function edit(Request $request, ViolationProductModel $ViolationProductModel)
  517. {
  518. $request->scene('edit')->validate();
  519. $admin_company_id = request('admin_company_id', '0');
  520. $company_id = request('access_token.company_id', '0');
  521. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  522. // 接收参数
  523. $id = request('id', 0);
  524. // 接收数据
  525. $all_data = request()->all();
  526. $store_scope = request('store_scope', '');
  527. $all_data['store_scope'] = $store_scope;
  528. //查询是否存在
  529. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs']];
  530. // 权限判断
  531. if ($is_admin != 1 && $company_id != 0) {
  532. $map['company_id'] = $company_id;
  533. $all_data['company_id'] = $company_id;
  534. } else {
  535. $map['company_id'] = $admin_company_id;
  536. $all_data['company_id'] = $admin_company_id;
  537. }
  538. $data = $ViolationProductModel->where($map)->where('id', '!=', $id)->first();
  539. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  540. // 更新数据表
  541. $where = ['id' => $id];
  542. $result = $ViolationProductModel->updateLowPriceGoods($where, $all_data);
  543. // 如果操作失败
  544. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  545. // 告知结果
  546. return json_send(['code' => 'success', 'msg' => '修改成功']);
  547. }
  548. /**
  549. * 修改状态
  550. * @author 唐远望
  551. * @version 1.0
  552. * @date 2025-12-08
  553. *
  554. */
  555. public function set_status(Request $request, ViolationProductModel $ViolationProductModel)
  556. {
  557. // 验证参数
  558. $request->scene('set_status')->validate();
  559. $admin_company_id = request('admin_company_id', '0');
  560. $company_id = request('access_token.company_id', '0');
  561. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  562. // 接收数据
  563. $id = request('id', 0);
  564. $status = request('status', 0);
  565. // 查询用户
  566. $where = ['id' => $id];
  567. // 权限判断
  568. if ($is_admin != 1 && $company_id != 0) {
  569. $where['company_id'] = $company_id;
  570. } else {
  571. $where['company_id'] = $admin_company_id;
  572. }
  573. $ViolationProduct = $ViolationProductModel->where($where)->first();
  574. if (!$ViolationProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  575. $ViolationProduct->status = $status;
  576. $ViolationProduct->update_time = time();
  577. // 执行修改
  578. $result = $ViolationProduct->save();
  579. // 提示新增失败
  580. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  581. // 告知结果
  582. return json_send(['code' => 'success', 'msg' => '设置成功']);
  583. }
  584. /**
  585. * 修改处理状态
  586. * @author 唐远望
  587. * @version 1.0
  588. * @date 2025-12-08
  589. *
  590. */
  591. public function set_processing_status(Request $request, ViolationProductModel $ViolationProductModel)
  592. {
  593. // 验证参数
  594. $request->scene('set_processing_status')->validate();
  595. $admin_company_id = request('admin_company_id', '0');
  596. $company_id = request('access_token.company_id', '0');
  597. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  598. // 接收数据
  599. $id = request('id', 0);
  600. $processing_status = request('processing_status', 0);
  601. // 查询用户
  602. $where = ['id' => $id];
  603. // 权限判断
  604. if ($is_admin != 1 && $company_id != 0) {
  605. $where['company_id'] = $company_id;
  606. } else {
  607. $where['company_id'] = $admin_company_id;
  608. }
  609. $ViolationProduct = $ViolationProductModel->where($where)->first();
  610. if (!$ViolationProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  611. $ViolationProduct->processing_status = $processing_status;
  612. $ViolationProduct->update_time = time();
  613. // 执行修改
  614. $result = $ViolationProduct->save();
  615. // 提示新增失败
  616. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  617. // 告知结果
  618. return json_send(['code' => 'success', 'msg' => '设置成功']);
  619. }
  620. /**
  621. * 删除
  622. * @author 唐远望
  623. * @version 1.0
  624. * @date 2025-12-08
  625. *
  626. */
  627. public function delete(Request $request, ViolationProductModel $ViolationProductModel)
  628. {
  629. // 验证参数
  630. $request->scene('delete')->validate();
  631. $admin_company_id = request('admin_company_id', '0');
  632. $company_id = request('access_token.company_id', '0');
  633. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  634. // 接收数据
  635. $id = request('id', 0);
  636. // 查询用户
  637. $where = ['id' => $id];
  638. // 权限判断
  639. if ($is_admin != 1 && $company_id != 0) {
  640. $where['company_id'] = $company_id;
  641. } else {
  642. $where['company_id'] = $admin_company_id;
  643. }
  644. $ViolationProduct = $ViolationProductModel->where($where)->first();
  645. if (!$ViolationProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  646. // 执行删除
  647. $result = $ViolationProduct->delete();
  648. // 提示删除失败
  649. if (!$result) return json_send(['code' => 'error', 'msg' => '删除失败']);
  650. // 告知结果
  651. return json_send(['code' => 'success', 'msg' => '删除成功']);
  652. }
  653. /**
  654. * 执行数据清洗
  655. * @author 唐远望
  656. * @version 1.0
  657. * @date 2025-12-11
  658. *
  659. */
  660. public function data_cleaning(Request $request)
  661. {
  662. // 验证参数
  663. $request->scene('data_cleaning')->validate();
  664. $admin_company_id = request('admin_company_id', '0');
  665. $company_id = request('access_token.company_id', '0');
  666. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  667. $admin_id = request('access_token.uid', 0); //用户ID
  668. // 权限判断
  669. if ($is_admin != 1 && $company_id != 0) {
  670. } else {
  671. $company_id = $admin_company_id;
  672. }
  673. $message_data = ['page' => '1', 'limit' => 50, 'admin_id' => $admin_id, 'is_admin' => $is_admin, 'company_id' => $company_id];
  674. ViolationProductJobs::dispatch($message_data);
  675. // ViolationProductJobs::dispatchSync($message_data);
  676. // 告知结果
  677. return json_send(['code' => 'success', 'msg' => '执行成功']);
  678. }
  679. /**
  680. * 下载导入模板
  681. * @author 唐远望
  682. * @version 1.0
  683. * @date 2025-12-31
  684. *
  685. */
  686. public function download_template()
  687. {
  688. // 创建一个新的 Spreadsheet 对象
  689. $spreadsheet = new Spreadsheet();
  690. $sheet = $spreadsheet->getActiveSheet();
  691. //合并单元格
  692. $sheet->mergeCells('A1:P1');
  693. $sheet->setCellValue('A1', '禁止挂网商品导入模板'); // 设置合并后的单元格内容
  694. // 获取合并后的单元格样式对象
  695. $style = $sheet->getStyle('A1');
  696. // 设置水平居中和垂直居中
  697. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  698. // 然后设置行高以适应两行文本
  699. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  700. // 设置表头
  701. $sheet->setCellValue('A2', '第一责任人名称');
  702. $sheet->setCellValue('B2', '责任人名称');
  703. $sheet->setCellValue('C2', '平台名称*');
  704. $sheet->setCellValue('D2', '商品分类名称');
  705. $sheet->setCellValue('E2', '商品名称*');
  706. $sheet->setCellValue('F2', '商品规格*');
  707. $sheet->setCellValue('G2', '累计挂网次数');
  708. $sheet->setCellValue('H2', '连续挂网次数');
  709. $sheet->setCellValue('I2', '链接地址*');
  710. $sheet->setCellValue('J2', '店铺名称*');
  711. $sheet->setCellValue('K2', '公司名称*');
  712. $sheet->setCellValue('L2', '公司分类名称');
  713. $sheet->setCellValue('M2', '信用代码*');
  714. $sheet->setCellValue('N2', '省份*');
  715. $sheet->setCellValue('O2', '城市*');
  716. $sheet->setCellValue('P2', '溯源责任人');
  717. // 生成 Excel 文件
  718. $writer = new Xlsx($spreadsheet);
  719. // 直接输出到浏览器(下载)
  720. $filename = '禁止挂网商品导入模板.xlsx';
  721. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  722. header('Content-Disposition: attachment;filename="' . $filename . '"');
  723. header('Cache-Control: max-age=0');
  724. $writer->save('php://output');
  725. exit;
  726. }
  727. /**
  728. * 导入Excel数据
  729. * @author 唐远望
  730. * @version 1.0
  731. * @date 2025-12-31
  732. *
  733. */
  734. public function import_data(Request $request, ViolationProductModel $ViolationProductModel, EmployeeModel $EmployeeModel, CitysModel $CitysModel)
  735. {
  736. $request->scene('import_data')->validate();
  737. $admin_company_id = request('admin_company_id', '0');
  738. $company_id = request('access_token.company_id', '0');
  739. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  740. $file = $request->file('file');
  741. // 加载Excel文件
  742. $spreadsheet = IOFactory::load($file->getPathname());
  743. $sheet = $spreadsheet->getActiveSheet();
  744. // 获取所有数据
  745. $data = $sheet->toArray();
  746. if (empty($data) || count($data) < 2) return json_send(['code' => 'error', 'msg' => '导入数据为空']);
  747. $result = $EmployeeModel->select(['id', 'name'])->get()->keyBy('name')->toarray();
  748. $platform_data = $ViolationProductModel->platform_data();
  749. $city_id_data = $CitysModel->get_city_id_list();
  750. DB::beginTransaction();
  751. try {
  752. foreach ($data as $key => $item) {
  753. if ($key < 2) continue;
  754. //强制必传参数校验
  755. $res_data = $this->import_data_check($key, $item);
  756. if ($res_data) return json_send($res_data);
  757. $first_responsible_person_id = [];
  758. $first_responsible_person_name = $item[0];
  759. if ($first_responsible_person_name) {
  760. $first_responsible_person_name = explode(',', $first_responsible_person_name);
  761. foreach ($first_responsible_person_name as $k => $v) {
  762. if (isset($result[$v])) {
  763. $first_responsible_person_id[] = $result[$v]['id'];
  764. }
  765. }
  766. }
  767. $responsible_person_id = [];
  768. $responsible_person_name = $item[1];
  769. if ($responsible_person_name) {
  770. $responsible_person_name = explode(',', $responsible_person_name);
  771. foreach ($responsible_person_name as $k => $v) {
  772. if (isset($result[$v])) {
  773. $responsible_person_id[] = $result[$v]['id'];
  774. }
  775. }
  776. }
  777. $source_responsible_person_id = [];
  778. $source_responsible_person_name = $item[15];
  779. if ($source_responsible_person_name) {
  780. $source_responsible_person_name = explode(',', $source_responsible_person_name);
  781. foreach ($source_responsible_person_name as $k => $v) {
  782. if (isset($result[$v])) {
  783. $source_responsible_person_id[] = $result[$v]['id'];
  784. }
  785. }
  786. }
  787. $province_name = $item[13];
  788. if ($province_name && !strpos($province_name, '省')) {
  789. if (!in_array($province_name, ['北京', '天津', '上海', '重庆'])) {
  790. $province_name = $province_name . '省';
  791. }
  792. }
  793. $city_name = $item[14];
  794. if ($city_name && !strpos($city_name, '市')) {
  795. $city_name = $city_name . '市';
  796. }
  797. // 权限判断
  798. if ($is_admin != 1 && $company_id != 0) {
  799. $insert_product_data['company_id'] = $company_id;
  800. } else {
  801. $insert_product_data['company_id'] = $admin_company_id;
  802. }
  803. $insert_product_data['first_responsible_person'] = !empty($first_responsible_person_id) ? implode(',', $first_responsible_person_id) : ''; //第一责任人ID集合
  804. $insert_product_data['responsible_person'] = !empty($responsible_person_id) ? implode(',', $responsible_person_id) : ''; //责任人ID集合
  805. $insert_product_data['platform'] = isset($platform_data[$item[2]]) ? $platform_data[$item[2]] : '0'; // 平台
  806. $insert_product_data['category_name'] = $item[3] ? $item[4] : ''; // 商品分类
  807. $insert_product_data['product_name'] = $item[4]; // 商品名称
  808. $insert_product_data['product_specs'] = $item[5]; // 商品规格
  809. $insert_product_data['online_posting_count'] = isset($item[6]) ? $item[6] : 1; // 累计挂网次数
  810. $insert_product_data['continuous_listing_count'] = isset($item[7]) ? $item[7] : 1; // 连续挂网次数
  811. $insert_product_data['link_url'] = $item[8]; // 链接地址
  812. $insert_product_data['store_name'] = $item[9]; // 店铺名称
  813. $insert_product_data['company_name'] = $item[10]; // 公司名称
  814. $insert_product_data['company_category_name'] = isset($item[11]) ? $item[11] : ''; // 公司分类
  815. $insert_product_data['social_credit_code'] = $item[12]; // 信用代码
  816. $insert_product_data['province_name'] = $item[13]; // 省份
  817. $insert_product_data['province_id'] = isset($city_id_data[$province_name]) ? $city_id_data[$province_name]['id'] : 0; // 省份ID
  818. $insert_product_data['city_name'] = $item[14]; // 城市
  819. $insert_product_data['city_id'] = isset($city_id_data[$city_name]) ? $city_id_data[$city_name]['id'] : 0; // 城市ID
  820. $insert_product_data['area_info'] = ''; // 详细地址
  821. $insert_product_data['source_responsible_person'] = !empty($source_responsible_person_id) ? implode(',', $source_responsible_person_id) : ''; //溯源责任人ID集合
  822. $insert_product_data['processing_status'] = 1; //处理状态1=待处理2=购买中3=已溯源4=回收凭据已上传5=已回收6=拒绝回收7=已下架8=无法处理
  823. $insert_product_data['status'] = 0; //状态0=有效=无效
  824. $insert_product_data['insert_time'] = time();
  825. $insert_product_data['source_id'] = 0; // 原始数据ID
  826. //插入数据
  827. $ViolationProductModel->addViolationProduct($insert_product_data, true);
  828. }
  829. DB::commit();
  830. return json_send(['code' => 'success', 'msg' => '导入成功']);
  831. // 成功处理...
  832. } catch (\Exception $e) {
  833. DB::rollBack();
  834. // 错误处理...
  835. return json_send(['code' => 'error', 'msg' => '导入失败', 'data' => $e->getMessage()]);
  836. }
  837. }
  838. /**
  839. * 导入Excel数据必传参数校验
  840. * @author 唐远望
  841. * @version 1.0
  842. * @date 2025-12-31
  843. *
  844. */
  845. private function import_data_check($key, $item)
  846. {
  847. $key = $key + 1;
  848. if (!$item[2]) {
  849. return ['code' => 'error', 'msg' => "第{$key}行平台不能为空"];
  850. }
  851. if (!$item[4]) {
  852. return ['code' => 'error', 'msg' => "第{$key}行商品名称不能为空"];
  853. }
  854. if (!$item[5]) {
  855. return ['code' => 'error', 'msg' => "第{$key}行商品规格不能为空"];
  856. }
  857. if (!$item[8]) {
  858. return ['code' => 'error', 'msg' => "第{$key}行链接地址不能为空"];
  859. }
  860. if (!$item[9]) {
  861. return ['code' => 'error', 'msg' => "第{$key}行店铺名称不能为空"];
  862. }
  863. if (!$item[10]) {
  864. return ['code' => 'error', 'msg' => "第{$key}行公司名称不能为空"];
  865. }
  866. if (!$item[11]) {
  867. return ['code' => 'error', 'msg' => "第{$key}行信用代码不能为空"];
  868. }
  869. if (!$item[12]) {
  870. return ['code' => 'error', 'msg' => "第{$key}行省份不能为空"];
  871. }
  872. if (!$item[13]) {
  873. return ['code' => 'error', 'msg' => "第{$key}行城市不能为空"];
  874. }
  875. }
  876. }