Product.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. namespace App\Http\Controllers\Manager\Collect;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Manager\Collect\Product as Request;
  5. use App\Models\Manager\Collect\Product as ProductModel;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Models\Manager\Collect\ProductKeyword as ProductKeywordModel;
  8. /**
  9. * 采集配置-商品管理
  10. * @author 唐远望
  11. * @version 1.0
  12. * @date 2025-12-30
  13. */
  14. class Product extends Controller
  15. {
  16. /**
  17. * 列表
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2025-12-30
  21. *
  22. */
  23. public function list(Request $request, ProductModel $ProductModel)
  24. {
  25. $request->scene('list')->validate();
  26. $admin_company_id = request('admin_company_id', '0');
  27. $company_id = request('access_token.company_id', '0');
  28. $is_admin = request('access_token.is_admin', '0');
  29. // 查询条件
  30. $map = [];
  31. $limit = request('limit', config('page_num', 10));
  32. $status = request('status', '');
  33. $start_time = request('start_time', '');
  34. $end_time = request('end_time', '');
  35. $product_name = request('product_name', '');
  36. $platforms = request('platform', '');
  37. $product_brand = request('product_brand', '');
  38. // 时间条件
  39. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  40. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  41. // 其他条件
  42. if (is_numeric($status)) $map[] = ['status', '=', $status];
  43. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  44. if (is_numeric($platforms) || $platforms) {
  45. $ProductModel = $ProductModel
  46. ->where(function ($query) use ($platforms) {
  47. $platforms = explode(',', $platforms);
  48. foreach ($platforms as $platform) {
  49. $query->orWhereRaw("FIND_IN_SET(?, platform)", [$platform]);
  50. }
  51. });
  52. }
  53. if ($product_brand) $map[] = ['product_brand', 'like', "%$product_brand%"];
  54. // 权限判断
  55. if ($is_admin != 1 && $company_id != 0) {
  56. $map[] = ['company_id', '=', $company_id];
  57. } else {
  58. $map[] = ['company_id', '=', $admin_company_id];
  59. }
  60. // 查询数据
  61. $result = $ProductModel
  62. ->where($map)
  63. ->with(['product_keyword'])
  64. ->orderByDesc('id')
  65. ->paginate($limit)->toarray();
  66. // 分配数据
  67. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  68. if (isset($result['data']) && count($result['data']) > 0) {
  69. foreach ($result['data'] as $key => $value) {
  70. $result['data'][$key]['platform'] = isset($value['platform']) ? explode(',', $value['platform']) : '';
  71. $product_specs = isset($value['product_specs']) ? explode(',', $value['product_specs']) : '';
  72. //移除空数组
  73. $result['data'][$key]['product_specs'] = $product_specs ? array_filter($product_specs) : '';
  74. }
  75. }
  76. // 加载模板
  77. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  78. }
  79. /**
  80. * 商品名称规格列表
  81. * @author 唐远望
  82. * @version 1.0
  83. * @date 2026-03-25
  84. *
  85. */
  86. public function product_name_specs_list(Request $request, ProductModel $ProductModel, ProductKeywordModel $ProductKeywordModel)
  87. {
  88. $request->scene('product_name_specs_list')->validate();
  89. $admin_company_id = request('admin_company_id', '0');
  90. $company_id = request('access_token.company_id', '0');
  91. $is_admin = request('access_token.is_admin', '0');
  92. // 查询条件
  93. $map = [];
  94. $map2 = [];
  95. $map3 = [];
  96. $start_time = request('start_time', '');
  97. $end_time = request('end_time', '');
  98. $product_name = request('product_name', '');
  99. $product_brand = request('product_brand', '');
  100. // 时间条件
  101. if ($start_time) {
  102. $map[] = ['insert_time', '>=', strtotime($start_time)];
  103. $map2[] = ['insert_time', '>=', strtotime($start_time)];
  104. $map3[] = ['insert_time', '>=', strtotime($start_time)];
  105. }
  106. if ($end_time) {
  107. $map[] = ['insert_time', '<=', strtotime($end_time)];
  108. $map2[] = ['insert_time', '<=', strtotime($end_time)];
  109. $map3[] = ['insert_time', '<=', strtotime($end_time)];
  110. }
  111. // 其他条件
  112. $map[] = ['status', '=', 0];
  113. if ($product_name) $map2[] = ['product_name', 'like', "%$product_name%"];
  114. if ($product_brand) $map[] = ['product_brand', 'like', "%$product_brand%"];
  115. // 权限判断
  116. if ($is_admin != 1 && $company_id != 0) {
  117. $map[] = ['company_id', '=', $company_id];
  118. $map2[] = ['company_id', '=', $company_id];
  119. $map3[] = ['company_id', '=', $company_id];
  120. } else {
  121. $map[] = ['company_id', '=', $admin_company_id];
  122. $map2[] = ['company_id', '=', $admin_company_id];
  123. $map3[] = ['company_id', '=', $admin_company_id];
  124. }
  125. // 查询数据
  126. $result = $ProductModel->query()
  127. ->where($map)
  128. ->where([['product_brand', '!=', ''], ['product_brand', '!=', null]])
  129. ->select([DB::raw('MAX(id) as id'), 'product_brand'])
  130. ->groupBy(['product_brand'])
  131. ->orderByDesc('id')->get()->toarray();
  132. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  133. foreach ($result as $key => $value) {
  134. $product_list = $ProductModel->query()
  135. ->where($map2)
  136. ->where([['product_name', '!=', ''], ['product_name', '!=', null]])
  137. ->where(['product_brand' => $value['product_brand']])
  138. ->select([DB::raw('MAX(id) as id'), 'product_name'])
  139. ->groupBy(['product_name'])
  140. ->orderByDesc('id')->get()->toarray();
  141. if (!empty($product_list)) {
  142. foreach ($product_list as $key2 => $value2) {
  143. $product_keyword = $ProductKeywordModel->query()
  144. ->where(['collect_product_id' => $value2['id']])
  145. ->select('keyword')
  146. ->groupBy(['keyword'])
  147. ->get()->toarray();
  148. $product_list[$key2]['product_keyword'] = $product_keyword ? array_column($product_keyword, 'keyword') : [];
  149. $product_specs = $ProductModel->query()
  150. ->where($map3)
  151. ->where([['product_specs', '!=', ''], ['product_specs', '!=', null]])
  152. ->where(['product_name' => $value2['product_name']])
  153. ->where(['product_brand' => $value['product_brand']])
  154. ->select([DB::raw('MAX(id) as id'), 'product_specs'])
  155. ->groupBy(['product_specs'])
  156. ->orderByDesc('id')->get()->toarray();
  157. $product_specs_tring = [];
  158. if (count($product_specs) > 0) {
  159. foreach ($product_specs as $key3 => $value3) {
  160. $product_specs_item = isset($value3['product_specs']) ? explode(',', $value3['product_specs']) : '';
  161. //合并数组
  162. if (!empty($product_specs_item)) {
  163. $product_specs_tring = array_merge($product_specs_tring, $product_specs_item);
  164. }
  165. }
  166. //去重
  167. $product_specs_tring = array_unique($product_specs_tring);
  168. }
  169. //移除空数组
  170. $product_list[$key2]['product_specs'] = $product_specs_tring ? array_filter($product_specs_tring) : '';
  171. }
  172. }
  173. $result[$key]['product_list'] = !empty($product_list) ? $product_list : [];
  174. }
  175. // 加载模板
  176. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  177. }
  178. /**
  179. * 详情
  180. * @author 唐远望
  181. * @version 1.0
  182. * @date 2025-12-30
  183. */
  184. public function detail(Request $request, ProductModel $ProductModel)
  185. {
  186. $request->scene('detail')->validate();
  187. $admin_company_id = request('admin_company_id', '0');
  188. $company_id = request('access_token.company_id', '0');
  189. $is_admin = request('access_token.is_admin', '0');
  190. // 接收参数
  191. $id = request('id', 0);
  192. $map = ['id' => $id];
  193. // 权限判断
  194. if ($is_admin != 1 && $company_id != 0) {
  195. $map['company_id'] = $company_id;
  196. } else {
  197. $map['company_id'] = $admin_company_id;
  198. }
  199. $data = $ProductModel->where($map)->with(['product_keyword'])->first();
  200. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  201. $data->platform = isset($data->platform) ? explode(',', $data->platform) : '';
  202. $product_specs = isset($data->product_specs) ? explode(',', $data->product_specs) : '';
  203. //移除空数组
  204. $data->product_specs = $product_specs ? array_filter($product_specs) : '';
  205. // 加载模板
  206. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  207. }
  208. /**
  209. * 添加
  210. * @author 唐远望
  211. * @version 1.0
  212. * @date 2025-12-30
  213. *
  214. */
  215. public function add(Request $request, ProductModel $ProductModel)
  216. {
  217. $request->scene('add')->validate();
  218. $admin_company_id = request('admin_company_id', '0');
  219. $company_id = request('access_token.company_id', '0');
  220. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  221. //商品启用数量
  222. // $product_count = $ProductModel->where('status', 0)->count();
  223. //判断是否超过限制
  224. // if ($product_count >= 50) {
  225. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  226. // }
  227. // 接收数据
  228. $all_data = request()->all();
  229. $product_brand = request('product_brand', '');
  230. $product_keyword = request('product_keyword', '');
  231. $minimum_order_quantity = request('minimum_order_quantity', 1);
  232. $all_data['platform'] = request('platform', '');
  233. $all_data['product_specs'] = request('product_specs', '');
  234. $all_data['product_brand'] = $product_brand;
  235. $all_data['product_keyword'] = $product_keyword;
  236. $minimum_order_quantity = request('minimum_order_quantity', 1);
  237. $sampling_cycle = request('sampling_cycle', '0');
  238. $sampling_start_time = request('sampling_start_time', '');
  239. $sampling_end_time = request('sampling_end_time', '');
  240. $round_number = request('round_number', 1);
  241. $is_multiple_accounts = request('is_multiple_accounts', 0);
  242. $all_data['is_multiple_accounts'] = $is_multiple_accounts;
  243. $all_data['round_number'] = $round_number;
  244. $all_data['sampling_cycle'] = $sampling_cycle;
  245. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  246. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  247. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  248. if ($all_data['sampling_start_time'] && strtotime(date('Y-m-d', $all_data['sampling_start_time']) . '23:59:59') < time()) return json_send(['code' => 'error', 'msg' => '采集开始时间必须大于当前时间']);
  249. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  250. $platform_string = $all_data['platform'];
  251. if (empty($platform_string)) return json_send(['code' => 'error', 'msg' => '平台不能为空']);
  252. $enable_full_quantity = request('enable_full_quantity', 0); //全量,0启用,1禁用
  253. $all_data['enable_full_quantity'] = $enable_full_quantity;
  254. if (trim($product_keyword) != '') {
  255. $product_keyword_count = count(explode(',', $product_keyword));
  256. if ($product_keyword_count > 5) {
  257. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  258. }
  259. }
  260. if ($enable_full_quantity == 1 && $all_data['product_specs'] == '') {
  261. return json_send(['code' => 'error', 'msg' => '非全量采集,商品规格不能为空']);
  262. }
  263. //查询是否存在
  264. $map = ['product_name' => $all_data['product_name']];
  265. if ($product_brand != '') $map['product_brand'] = $product_brand;
  266. if ($is_admin != 1 && $company_id != 0) {
  267. $map['company_id'] = $company_id;
  268. $all_data['company_id'] = $company_id;
  269. } else {
  270. $map['company_id'] = $admin_company_id;
  271. $all_data['company_id'] = $admin_company_id;
  272. }
  273. $data = $ProductModel->where($map)->first();
  274. if (!empty($data)) {
  275. //继续校验规格是否存在
  276. $product_specs_tring = $all_data['product_specs'];
  277. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  278. $product_specs = $product_specs ? array_unique($product_specs) : '';
  279. //继续校平台是否存在
  280. $platforms = $platform_string ? explode(',', $platform_string) : '';
  281. $platforms = $platforms ? array_unique($platforms) : '';
  282. $platform_data = $ProductModel->platform_index_data();
  283. $map_where = ['product_name' => $all_data['product_name'], 'status' => '0', 'company_id' => $all_data['company_id']];
  284. if ($product_brand != '') $map_where['product_brand'] = $product_brand;
  285. if (!empty($platforms) && $platforms != '') {
  286. foreach ($platforms as $platform) {
  287. $product_specs_log = $ProductModel
  288. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  289. ->where($map_where)
  290. ->where(function ($query) use ($product_specs) {
  291. // 平台条件(固定)
  292. $query->where(function ($query_li) use ($product_specs) {
  293. if (!empty($product_specs)) {
  294. foreach ($product_specs as $product_spec) {
  295. if (empty($product_specs)) continue;
  296. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  297. }
  298. }
  299. });
  300. })->first();
  301. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  302. if (!empty($product_specs_log)) {
  303. return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品规格记录']);
  304. }
  305. }
  306. }
  307. }
  308. // 写入数据表
  309. $result = $ProductModel->addProduct($all_data);
  310. // 如果操作失败
  311. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  312. // 记录行为
  313. $admin_id = request('access_token.uid', 0); //用户ID
  314. $table_name = $ProductModel->getTable();
  315. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  316. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了商品' . $all_data['product_name'] . '信息');
  317. // 告知结果
  318. return json_send(['code' => 'success', 'msg' => '新增成功']);
  319. }
  320. /**
  321. * 修改
  322. * @author 唐远望
  323. * @version 1.0
  324. * @date 2025-12-30
  325. *
  326. */
  327. public function edit(Request $request, ProductModel $ProductModel)
  328. {
  329. $request->scene('edit')->validate();
  330. $admin_company_id = request('admin_company_id', '0');
  331. $company_id = request('access_token.company_id', '0');
  332. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  333. // 接收参数
  334. $id = request('id', 0);
  335. // 接收数据
  336. $all_data = request()->all();
  337. $product_brand = request('product_brand', '');
  338. $product_keyword = request('product_keyword', '');
  339. $minimum_order_quantity = request('minimum_order_quantity', 1);
  340. $all_data['platform'] = request('platform', '');
  341. $all_data['product_specs'] = request('product_specs', '');
  342. $all_data['product_brand'] = $product_brand;
  343. $all_data['product_keyword'] = $product_keyword;
  344. $minimum_order_quantity = request('minimum_order_quantity', 1);
  345. $sampling_cycle = request('sampling_cycle', '0');
  346. $sampling_start_time = request('sampling_start_time', '');
  347. $sampling_end_time = request('sampling_end_time', '');
  348. $round_number = request('round_number', 1);
  349. $is_multiple_accounts = request('is_multiple_accounts', 0);
  350. $all_data['is_multiple_accounts'] = $is_multiple_accounts;
  351. $all_data['round_number'] = $round_number;
  352. $all_data['sampling_cycle'] = $sampling_cycle;
  353. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  354. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  355. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  356. if ($all_data['sampling_start_time'] && strtotime(date('Y-m-d', $all_data['sampling_start_time']) . '23:59:59') < time()) return json_send(['code' => 'error', 'msg' => '采集开始时间必须大于当前时间']);
  357. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  358. $platform_string = $all_data['platform'];
  359. if (empty($platform_string)) return json_send(['code' => 'error', 'msg' => '平台不能为空']);
  360. $enable_full_quantity = request('enable_full_quantity', 0); //全量,0启用,1禁用
  361. $all_data['enable_full_quantity'] = $enable_full_quantity;
  362. if (trim($product_keyword) != '') {
  363. $product_keyword_count = count(explode(',', $product_keyword));
  364. if ($product_keyword_count > 5) {
  365. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  366. }
  367. }
  368. if ($enable_full_quantity == 1 && $all_data['product_specs'] == '') {
  369. return json_send(['code' => 'error', 'msg' => '非全量采集,商品规格不能为空']);
  370. }
  371. //查询是否存在
  372. $map = ['product_brand' => $product_brand, 'product_name' => $all_data['product_name']];
  373. if ($is_admin != 1 && $company_id != 0) {
  374. $map['company_id'] = $company_id;
  375. } else {
  376. $map['company_id'] = $admin_company_id;
  377. }
  378. $all_data['company_id'] = $map['company_id'];
  379. $data = $ProductModel->where($map)->where('id', '!=', $id)->first();
  380. if (!empty($data)) {
  381. //继续校验规格是否存在
  382. $product_specs_tring = $all_data['product_specs'];
  383. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  384. $product_specs = $product_specs ? array_unique($product_specs) : '';
  385. //继续校平台是否存在
  386. $platforms = $platform_string ? explode(',', $platform_string) : '';
  387. $platforms = $platforms ? array_unique($platforms) : '';
  388. $platform_data = $ProductModel->platform_index_data();
  389. $map_where = ['product_name' => $all_data['product_name'], 'status' => '0', 'company_id' => $all_data['company_id']];
  390. if ($product_brand != '') $map_where['product_brand'] = $product_brand;
  391. foreach ($platforms as $platform) {
  392. $product_specs_log = $ProductModel
  393. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  394. ->where($map_where)
  395. ->where('id', '!=', $id)
  396. ->where(function ($query) use ($product_specs) {
  397. // 平台条件(固定)
  398. $query->where(function ($query_li) use ($product_specs) {
  399. if (!empty($product_specs)) {
  400. foreach ($product_specs as $product_spec) {
  401. if (empty($product_specs)) continue;
  402. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  403. }
  404. }
  405. });
  406. })->first();
  407. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  408. if (!empty($product_specs_log)) {
  409. return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品规格记录']);
  410. }
  411. }
  412. }
  413. // 更新数据表
  414. $where = ['id' => $id];
  415. // 权限判断
  416. if ($is_admin != 1 && $company_id != 0) {
  417. $where['company_id'] = $company_id;
  418. } else {
  419. $where['company_id'] = $admin_company_id;
  420. }
  421. $Product = $ProductModel->where($where)->first();
  422. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  423. $oldData = $Product->toarray();
  424. $result = $ProductModel->editProduct_content($Product, $all_data);
  425. // 如果操作失败
  426. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  427. // 记录行为
  428. $admin_id = request('access_token.uid', 0); //用户ID
  429. $table_name = $ProductModel->getTable();
  430. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  431. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了商品' . $oldData['product_name'] . '信息');
  432. // 告知结果
  433. return json_send(['code' => 'success', 'msg' => '修改成功']);
  434. }
  435. /**
  436. * 修改状态
  437. * @author 唐远望
  438. * @version 1.0
  439. * @date 2025-12-30
  440. *
  441. */
  442. public function set_status(Request $request, ProductModel $ProductModel)
  443. {
  444. // 验证参数
  445. $request->scene('set_status')->validate();
  446. $admin_company_id = request('admin_company_id', '0');
  447. $company_id = request('access_token.company_id', '0');
  448. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  449. // 接收数据
  450. $id = request('id', 0);
  451. $status = request('status', 0);
  452. if ($status == 0) {
  453. //获取商品启用数量
  454. // $product_count = $ProductModel->where('status', 0)->count();
  455. //判断是否超过限制
  456. // if ($product_count >= 50) {
  457. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  458. // }
  459. }
  460. // 查询用户
  461. $where = ['id' => $id];
  462. if ($is_admin != 1 && $company_id != 0) {
  463. $where['company_id'] = $company_id;
  464. } else {
  465. $where['company_id'] = $admin_company_id;
  466. }
  467. $Product = $ProductModel->where($where)->first();
  468. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  469. if (!empty($Product)) {
  470. //继续校验规格是否存在
  471. $product_specs_tring = $Product->product_specs;
  472. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  473. $product_specs = $product_specs ? array_unique($product_specs) : '';
  474. //继续校平台是否存在
  475. $platform_string = $Product->platform;
  476. $platforms = $platform_string ? explode(',', $platform_string) : '';
  477. $platforms = $platforms ? array_unique($platforms) : '';
  478. $platform_data = $ProductModel->platform_index_data();
  479. foreach ($platforms as $platform) {
  480. $product_specs_log = $ProductModel
  481. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  482. ->where(['product_brand' => $Product->product_brand, 'product_name' => $Product->product_name, 'status' => '0', 'company_id' => $Product->company_id])
  483. ->where('id', '!=', $id)
  484. ->where(function ($query) use ($product_specs) {
  485. // 平台条件(固定)
  486. $query->where(function ($query_li) use ($product_specs) {
  487. if (!empty($product_specs)) {
  488. foreach ($product_specs as $product_spec) {
  489. if (empty($product_specs)) continue;
  490. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  491. }
  492. }
  493. });
  494. })->first();
  495. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  496. if (!empty($product_specs_log)) {
  497. return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品规格启用记录']);
  498. }
  499. }
  500. }
  501. // 执行修改
  502. $result = $ProductModel->changeStatus($Product, $status);
  503. // 提示新增失败
  504. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  505. // 记录行为
  506. $admin_id = request('access_token.uid', 0); //用户ID
  507. $table_name = $ProductModel->getTable();
  508. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  509. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了商品' . $Product->product_name . '状态');
  510. // 告知结果
  511. return json_send(['code' => 'success', 'msg' => '设置成功']);
  512. }
  513. /**
  514. * 删除
  515. * @author 唐远望
  516. * @version 1.0
  517. * @date 2025-12-30
  518. *
  519. */
  520. public function delete(Request $request, ProductModel $ProductModel)
  521. {
  522. // 验证参数
  523. $request->scene('delete')->validate();
  524. $admin_company_id = request('admin_company_id', '0');
  525. $company_id = request('access_token.company_id', '0');
  526. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  527. // 接收数据
  528. $id = request('id', 0);
  529. // 查询用户
  530. $where = ['id' => $id];
  531. if ($is_admin != 1 && $company_id != 0) {
  532. $where['company_id'] = $company_id;
  533. } else {
  534. $where['company_id'] = $admin_company_id;
  535. }
  536. // 执行删除
  537. $Product = $ProductModel->where($where)->first();
  538. if (!$Product) {
  539. return json_send(['code' => 'error', 'msg' => '删除失败,记录不存在']);
  540. }
  541. DB::beginTransaction();
  542. try {
  543. $Product->delete();
  544. // 记录行为
  545. $admin_id = request('access_token.uid', 0); //用户ID
  546. $table_name = $ProductModel->getTable();
  547. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  548. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $Product->toarray(), [], '删除了商品' . $Product->product_name . '信息');
  549. // 告知结果
  550. DB::commit();
  551. return json_send(['code' => 'success', 'msg' => '删除成功']);
  552. // 成功处理...
  553. } catch (\Exception $e) {
  554. DB::rollBack();
  555. // 错误处理...
  556. return json_send(['code' => 'error', 'msg' => '删除失败']);
  557. }
  558. }
  559. }