ViolationProduct.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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:U1');
  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', '采集时间(Y-m-d H:i:s)*');
  762. // 生成 Excel 文件
  763. $writer = new Xlsx($spreadsheet);
  764. // 直接输出到浏览器(下载)
  765. $filename = '禁止挂网商品导入模板.xlsx';
  766. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  767. header('Content-Disposition: attachment;filename="' . $filename . '"');
  768. header('Cache-Control: max-age=0');
  769. $writer->save('php://output');
  770. exit;
  771. }
  772. /**
  773. * 导入Excel数据
  774. * @author 唐远望
  775. * @version 1.0
  776. * @date 2025-12-31
  777. *
  778. */
  779. public function import_data(Request $request, ViolationProductModel $ViolationProductModel, EmployeeModel $EmployeeModel, CitysModel $CitysModel)
  780. {
  781. $request->scene('import_data')->validate();
  782. $admin_company_id = request('admin_company_id', '0');
  783. $company_id = request('access_token.company_id', '0');
  784. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  785. $file = $request->file('file');
  786. // 加载Excel文件
  787. $spreadsheet = IOFactory::load($file->getPathname());
  788. $sheet = $spreadsheet->getActiveSheet();
  789. // 获取所有数据
  790. $data = $sheet->toArray();
  791. if (empty($data) || count($data) < 2) return json_send(['code' => 'error', 'msg' => '导入数据为空']);
  792. $result = $EmployeeModel->select(['id', 'name'])->get()->keyBy('name')->toarray();
  793. $platform_data = $ViolationProductModel->platform_data();
  794. $province_id_data = $CitysModel->get_province_id_list();
  795. $city_id_data = $CitysModel->get_city_id_list();
  796. // 过滤空行
  797. $data = array_filter($data, function ($row) {
  798. // 过滤掉所有值都为空的行
  799. return !empty(array_filter($row, function ($value) {
  800. return !is_null($value) && $value !== '';
  801. }));
  802. });
  803. DB::beginTransaction();
  804. try {
  805. foreach ($data as $key => $item) {
  806. $key_num = $key + 1;
  807. if ($key < 2) continue;
  808. //强制必传参数校验
  809. $res_data = $this->import_data_check($key, $item);
  810. if ($res_data) return json_send($res_data);
  811. $first_responsible_person_id = [];
  812. $first_responsible_person_name = $item[0];
  813. if ($first_responsible_person_name) {
  814. $first_responsible_person_name = explode(',', $first_responsible_person_name);
  815. foreach ($first_responsible_person_name as $k => $v) {
  816. $v = trim($v);
  817. if (isset($result[$v])) {
  818. $first_responsible_person_id[] = $result[$v]['id'];
  819. }
  820. }
  821. }
  822. $responsible_person_id = [];
  823. $responsible_person_name = $item[1];
  824. if ($responsible_person_name) {
  825. $responsible_person_name = explode(',', $responsible_person_name);
  826. foreach ($responsible_person_name as $k => $v) {
  827. $v = trim($v);
  828. if (isset($result[$v])) {
  829. $responsible_person_id[] = $result[$v]['id'];
  830. }
  831. }
  832. }
  833. $source_responsible_person_id = [];
  834. $province_name = $item[18];
  835. //特殊地区1级移除市
  836. if ($province_name && in_array($province_name, ['北京市', '天津市', '上海市', '重庆市'])) {
  837. //移除市这个字符
  838. $province_name = trim(str_replace('市', '', $province_name));
  839. } else if ($province_name && in_array($province_name, ['北京', '天津', '上海', '重庆'])) {
  840. } else if ($province_name && in_array($province_name, ['内蒙古', '广西', '西藏', '新疆', '宁夏'])) {
  841. switch ($province_name) {
  842. case '内蒙古':
  843. $province_name = '内蒙古自治区';
  844. break;
  845. case '广西':
  846. $province_name = '广西壮族自治区';
  847. break;
  848. case '西藏':
  849. $province_name = '西藏自治区';
  850. break;
  851. case '新疆':
  852. $province_name = '新疆维吾尔自治区';
  853. break;
  854. case '宁夏':
  855. $province_name = '宁夏回族自治区';
  856. break;
  857. }
  858. } else if ($province_name && in_array($province_name, ['内蒙古自治区', '广西壮族自治区', '西藏自治区', '新疆维吾尔自治区', '宁夏回族自治区'])) {
  859. } else if (strpos($province_name, '省') === false) {
  860. //是否存在市省,如果不存在则补全
  861. if (!empty($province_name) && strpos($province_name, '省') === false) {
  862. $province_name = $province_name . '省';
  863. }
  864. }
  865. if (!empty($province_name) && !isset($province_id_data[$province_name])) return json_send(['code' => 'error', 'msg' => "第{$key_num}行省份信息不完整", 'data' => $item]);
  866. $city_name = $item[19];
  867. if (!empty($city_name) && !isset($city_id_data[$city_name])) return json_send(['code' => 'error', 'msg' => "第{$key_num}行城市信息不完整", 'data' => $item]);
  868. // 权限判断
  869. if ($is_admin != 1 && $company_id != 0) {
  870. $insert_product_data['company_id'] = $company_id;
  871. } else {
  872. $insert_product_data['company_id'] = $admin_company_id;
  873. }
  874. $platform_id = isset($platform_data[$item[2]]) ? $platform_data[$item[2]] : '0'; // 平台ID
  875. if($platform_id == 0) return json_send(['code' => 'error', 'msg' => "第{$key_num}行平台信息不正确", 'data' => $item]);
  876. $insert_product_data['first_responsible_person'] = !empty($first_responsible_person_id) ? implode(',', $first_responsible_person_id) : ''; //第一责任人ID集合
  877. $insert_product_data['responsible_person'] = !empty($responsible_person_id) ? implode(',', $responsible_person_id) : ''; //责任人ID集合
  878. $insert_product_data['platform'] = isset($platform_data[$item[2]]) ? $platform_data[$item[2]] : '0'; // 平台
  879. $insert_product_data['category_name'] = $item[3] ? $item[4] : ''; // 商品分类
  880. $insert_product_data['product_brand'] = isset($item[4]) ? $item[4] : ''; // 品牌名称
  881. $insert_product_data['product_name'] = $item[5]; // 商品名称
  882. $insert_product_data['product_specs'] = $item[6]; // 商品规格
  883. $insert_product_data['online_posting_count'] = isset($item[7]) ? $item[7] : 1; // 累计挂网次数
  884. $insert_product_data['continuous_listing_count'] = isset($item[7]) ? $item[7] : 1; // 连续挂网次数
  885. $insert_product_data['sales'] = isset($item[9]) ? $item[9] : 0; // 销量
  886. $insert_product_data['inventory'] = isset($item[10]) ? $item[10] : 0; // 库存
  887. $insert_product_data['snapshot_url'] = isset($item[11]) ? $item[11] : ''; // 快照URL
  888. $insert_product_data['link_url'] = $item[12]; // 链接地址
  889. $insert_product_data['store_name'] = $item[13]; // 店铺名称
  890. $insert_product_data['anonymous_store_name'] = isset($item[14]) ? $item[14] : ''; // 匿名店铺名称
  891. $insert_product_data['company_name'] = $item[15]; // 公司名称
  892. $insert_product_data['company_category_name'] = isset($item[16]) ? $item[16] : ''; // 公司分类
  893. $insert_product_data['social_credit_code'] = isset($item[17]) ? $item[17] : ''; // 信用代码
  894. $insert_product_data['province_name'] = isset($item[18]) ? $item[18]:''; // 省份
  895. $insert_product_data['province_id'] = isset($province_id_data[$province_name]) ? $province_id_data[$province_name]['id'] : 0; // 省份ID
  896. $insert_product_data['city_name'] = isset($item[19]) ? $item[19] : ''; // 城市
  897. $insert_product_data['city_id'] = isset($city_id_data[$city_name]) ? $city_id_data[$city_name]['id'] : 0; // 城市ID
  898. $insert_product_data['area_info'] = ''; // 详细地址
  899. $insert_product_data['source_responsible_person'] = !empty($source_responsible_person_id) ? implode(',', $source_responsible_person_id) : ''; //溯源责任人ID集合
  900. $insert_product_data['processing_status'] = 1; //处理状态1=待处理2=购买中3=已溯源4=回收凭据已上传5=已回收6=拒绝回收7=已下架8=无法处理
  901. $insert_product_data['status'] = 0; //状态0=有效=无效
  902. $insert_product_data['insert_time'] = time();
  903. $insert_product_data['source_id'] = 0; // 原始数据ID
  904. $insert_product_data['scrape_date'] = date('Y-m-d',strtotime($item[20])); // 检索采集日期
  905. $insert_product_data['collection_time'] = strtotime($item[20]); // 采集时间
  906. //插入数据
  907. $ViolationProductModel->addViolationProduct($insert_product_data, true);
  908. }
  909. DB::commit();
  910. return json_send(['code' => 'success', 'msg' => '导入成功']);
  911. // 成功处理...
  912. } catch (\Exception $e) {
  913. DB::rollBack();
  914. // 错误处理...
  915. return json_send(['code' => 'error', 'msg' => '导入失败', 'data' => $e->getMessage()]);
  916. }
  917. }
  918. /**
  919. * 导入Excel数据必传参数校验
  920. * @author 唐远望
  921. * @version 1.0
  922. * @date 2025-12-31
  923. *
  924. */
  925. private function import_data_check($key, $item)
  926. {
  927. $key = $key + 1;
  928. if (!$item[2]) {
  929. return ['code' => 'error', 'msg' => "第{$key}行平台不能为空", 'data' => $item];
  930. }
  931. if (!$item[5]) {
  932. return ['code' => 'error', 'msg' => "第{$key}行商品名称不能为空", 'data' => $item];
  933. }
  934. if (!$item[6]) {
  935. return ['code' => 'error', 'msg' => "第{$key}行商品规格不能为空", 'data' => $item];
  936. }
  937. if (!$item[12]) {
  938. return ['code' => 'error', 'msg' => "第{$key}行链接地址不能为空", 'data' => $item];
  939. }
  940. if (!$item[13]) {
  941. return ['code' => 'error', 'msg' => "第{$key}行店铺名称不能为空", 'data' => $item];
  942. }
  943. if (!$item[15]) {
  944. return ['code' => 'error', 'msg' => "第{$key}行公司名称不能为空", 'data' => $item];
  945. }
  946. // if (!$item[17]) {
  947. // return ['code' => 'error', 'msg' => "第{$key}行信用代码不能为空", 'data' => $item];
  948. // }
  949. // if (!$item[18]) {
  950. // return ['code' => 'error', 'msg' => "第{$key}行省份不能为空", 'data' => $item];
  951. // }
  952. // if (!$item[19]) {
  953. // return ['code' => 'error', 'msg' => "第{$key}行城市不能为空", 'data' => $item];
  954. // }
  955. if (!$item[20]) {
  956. return ['code' => 'error', 'msg' => "第{$key}行采集时间不能为空", 'data' => $item];
  957. } else {
  958. //校验格式是否为Y-m-d H:i:s
  959. if (!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/', $item[20])) {
  960. return ['code' => 'error', 'msg' => "第{$key}行采集时间格式不正确", 'data' => $item];
  961. }
  962. if (strtotime($item[20]) > time()) {
  963. return ['code' => 'error', 'msg' => "第{$key}行采集时间不能大于当前时间", 'data' => $item];
  964. }
  965. }
  966. }
  967. }