Product.php 28 KB

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