ViolationProduct.php 52 KB

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