ViolationProduct.php 55 KB

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