Product.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. $all_data['sampling_cycle'] = $sampling_cycle;
  241. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  242. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  243. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  244. 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' => '采集开始时间必须大于当前时间']);
  245. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  246. $platform_string = $all_data['platform'];
  247. if(empty($platform_string)) return json_send(['code' => 'error', 'msg' => '平台不能为空']);
  248. $enable_full_quantity = request('enable_full_quantity', 0); //全量,0启用,1禁用
  249. $all_data['enable_full_quantity'] = $enable_full_quantity;
  250. if (trim($product_keyword) != '') {
  251. $product_keyword_count = count(explode(',', $product_keyword));
  252. if ($product_keyword_count > 5) {
  253. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  254. }
  255. }
  256. //查询是否存在
  257. $map = ['product_name' => $all_data['product_name']];
  258. if($product_brand != '') $map['product_brand'] = $product_brand;
  259. if ($is_admin != 1 && $company_id != 0) {
  260. $map['company_id'] = $company_id;
  261. $all_data['company_id'] = $company_id;
  262. } else {
  263. $map['company_id'] = $admin_company_id;
  264. $all_data['company_id'] = $admin_company_id;
  265. }
  266. $data = $ProductModel->where($map)->first();
  267. if (!empty($data)) {
  268. //继续校验规格是否存在
  269. $product_specs_tring = $all_data['product_specs'];
  270. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  271. $product_specs = $product_specs ? array_unique($product_specs) : '';
  272. //继续校平台是否存在
  273. $platforms = $platform_string ? explode(',', $platform_string) : '';
  274. $platforms = $platforms ? array_unique($platforms) : '';
  275. $platform_data = $ProductModel->platform_index_data();
  276. $map_where = ['product_name' => $all_data['product_name'],'status'=>'0','company_id'=> $all_data['company_id']];
  277. if ($product_brand != '') $map_where['product_brand'] = $product_brand;
  278. if (!empty($platforms) && $platforms != '') {
  279. foreach ($platforms as $platform) {
  280. $product_specs_log = $ProductModel
  281. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  282. ->where($map_where)
  283. ->where(function ($query) use ($product_specs) {
  284. // 平台条件(固定)
  285. $query->where(function ($query_li) use ($product_specs) {
  286. if (!empty($product_specs)) {
  287. foreach ($product_specs as $product_spec) {
  288. if (empty($product_specs)) continue;
  289. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  290. }
  291. }
  292. });
  293. })->first();
  294. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  295. if (!empty($product_specs_log)) {
  296. return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品规格记录']);
  297. }
  298. }
  299. }
  300. }
  301. // 写入数据表
  302. $result = $ProductModel->addProduct($all_data);
  303. // 如果操作失败
  304. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  305. // 记录行为
  306. $admin_id = request('access_token.uid', 0); //用户ID
  307. $table_name = $ProductModel->getTable();
  308. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  309. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了商品' . $all_data['product_name'] . '信息');
  310. // 告知结果
  311. return json_send(['code' => 'success', 'msg' => '新增成功']);
  312. }
  313. /**
  314. * 修改
  315. * @author 唐远望
  316. * @version 1.0
  317. * @date 2025-12-30
  318. *
  319. */
  320. public function edit(Request $request, ProductModel $ProductModel)
  321. {
  322. $request->scene('edit')->validate();
  323. $admin_company_id = request('admin_company_id', '0');
  324. $company_id = request('access_token.company_id', '0');
  325. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  326. // 接收参数
  327. $id = request('id', 0);
  328. // 接收数据
  329. $all_data = request()->all();
  330. $product_brand = request('product_brand', '');
  331. $product_keyword = request('product_keyword', '');
  332. $minimum_order_quantity = request('minimum_order_quantity', 1);
  333. $all_data['platform'] = request('platform', '');
  334. $all_data['product_specs'] = request('product_specs', '');
  335. $all_data['product_brand'] = $product_brand;
  336. $all_data['product_keyword'] = $product_keyword;
  337. $minimum_order_quantity = request('minimum_order_quantity', 1);
  338. $sampling_cycle = request('sampling_cycle', '0');
  339. $sampling_start_time = request('sampling_start_time', '');
  340. $sampling_end_time = request('sampling_end_time', '');
  341. $all_data['sampling_cycle'] = $sampling_cycle;
  342. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  343. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  344. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  345. 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' => '采集开始时间必须大于当前时间']);
  346. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  347. $platform_string = $all_data['platform'];
  348. if(empty($platform_string)) return json_send(['code' => 'error', 'msg' => '平台不能为空']);
  349. $enable_full_quantity = request('enable_full_quantity', 0); //全量,0启用,1禁用
  350. $all_data['enable_full_quantity'] = $enable_full_quantity;
  351. if (trim($product_keyword) != '') {
  352. $product_keyword_count = count(explode(',', $product_keyword));
  353. if ($product_keyword_count > 5) {
  354. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  355. }
  356. }
  357. //查询是否存在
  358. $map = ['product_brand' => $product_brand, 'product_name' => $all_data['product_name']];
  359. if ($is_admin != 1 && $company_id != 0) {
  360. $map['company_id'] = $company_id;
  361. } else {
  362. $map['company_id'] = $admin_company_id;
  363. }
  364. $data = $ProductModel->where($map)->where('id', '!=', $id)->first();
  365. if (!empty($data) && $enable_full_quantity == 1) {
  366. //继续校验规格是否存在
  367. $product_specs_tring = $all_data['product_specs'];
  368. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  369. $product_specs = $product_specs ? array_unique($product_specs) : '';
  370. //继续校平台是否存在
  371. $platforms = $platform_string ? explode(',', $platform_string) : '';
  372. $platforms = $platforms ? array_unique($platforms) : '';
  373. $platform_data = $ProductModel->platform_index_data();
  374. $map_where = ['product_name' => $all_data['product_name'],'status'=>'0','company_id'=> $all_data['company_id']];
  375. if($product_brand != '') $map_where['product_brand'] = $product_brand;
  376. foreach ($platforms as $platform) {
  377. $product_specs_log = $ProductModel
  378. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  379. ->where($map_where)
  380. ->where('id', '!=', $id)
  381. ->where(function ($query) use ($product_specs) {
  382. // 平台条件(固定)
  383. $query->where(function ($query_li) use ($product_specs) {
  384. foreach ($product_specs as $product_spec) {
  385. if (empty($product_specs)) continue;
  386. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  387. }
  388. });
  389. })->first();
  390. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  391. if (!empty($product_specs_log)) {
  392. return json_send(['code' => 'error', 'msg' => $platform_name.'存在重复的商品规格记录']);
  393. }
  394. }
  395. }else if (!empty($data) && $enable_full_quantity == 0) {
  396. $platforms = $platform_string ? explode(',', $platform_string) : '';
  397. $platforms = $platforms ? array_unique($platforms) : '';
  398. $platform_data = $ProductModel->platform_index_data();
  399. $map_where = ['product_name' => $all_data['product_name'],'status'=>'0','company_id'=> $all_data['company_id']];
  400. if($product_brand != '') $map_where['product_brand'] = $product_brand;
  401. if (!empty($platforms) && $platforms != '') {
  402. foreach ($platforms as $platform) {
  403. $product_specs_log = $ProductModel
  404. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  405. ->where($map_where)
  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. // 更新数据表
  415. $where = ['id' => $id];
  416. // 权限判断
  417. if ($is_admin != 1 && $company_id != 0) {
  418. $where['company_id'] = $company_id;
  419. } else {
  420. $where['company_id'] = $admin_company_id;
  421. }
  422. $Product = $ProductModel->where($where)->first();
  423. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  424. $oldData = $Product->toarray();
  425. $result = $ProductModel->editProduct_content($Product, $all_data);
  426. // 如果操作失败
  427. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  428. // 记录行为
  429. $admin_id = request('access_token.uid', 0); //用户ID
  430. $table_name = $ProductModel->getTable();
  431. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  432. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了商品' . $oldData['product_name'] . '信息');
  433. // 告知结果
  434. return json_send(['code' => 'success', 'msg' => '修改成功']);
  435. }
  436. /**
  437. * 修改状态
  438. * @author 唐远望
  439. * @version 1.0
  440. * @date 2025-12-30
  441. *
  442. */
  443. public function set_status(Request $request, ProductModel $ProductModel)
  444. {
  445. // 验证参数
  446. $request->scene('set_status')->validate();
  447. $admin_company_id = request('admin_company_id', '0');
  448. $company_id = request('access_token.company_id', '0');
  449. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  450. // 接收数据
  451. $id = request('id', 0);
  452. $status = request('status', 0);
  453. if ($status == 0) {
  454. //获取商品启用数量
  455. // $product_count = $ProductModel->where('status', 0)->count();
  456. //判断是否超过限制
  457. // if ($product_count >= 50) {
  458. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  459. // }
  460. }
  461. // 查询用户
  462. $where = ['id' => $id];
  463. if ($is_admin != 1 && $company_id != 0) {
  464. $where['company_id'] = $company_id;
  465. } else {
  466. $where['company_id'] = $admin_company_id;
  467. }
  468. $Product = $ProductModel->where($where)->first();
  469. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  470. if (!empty($Product)) {
  471. //继续校验规格是否存在
  472. $product_specs_tring = $Product->product_specs;
  473. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  474. $product_specs = $product_specs ? array_unique($product_specs) : '';
  475. //继续校平台是否存在
  476. $platform_string = $Product->platform;
  477. $platforms = $platform_string ? explode(',', $platform_string) : '';
  478. $platforms = $platforms ? array_unique($platforms) : '';
  479. $platform_data = $ProductModel->platform_index_data();
  480. foreach ($platforms as $platform) {
  481. $product_specs_log = $ProductModel
  482. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  483. ->where(['product_brand' => $Product->product_brand, 'product_name' => $Product->product_name, 'status' => '0', 'company_id' => $Product->company_id])
  484. ->where('id', '!=', $id)
  485. ->where(function ($query) use ($product_specs) {
  486. // 平台条件(固定)
  487. $query->where(function ($query_li) use ($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. })->first();
  494. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  495. if (!empty($product_specs_log)) {
  496. return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品规格启用记录']);
  497. }
  498. }
  499. }
  500. // 执行修改
  501. $result = $ProductModel->changeStatus($Product, $status);
  502. // 提示新增失败
  503. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  504. // 记录行为
  505. $admin_id = request('access_token.uid', 0); //用户ID
  506. $table_name = $ProductModel->getTable();
  507. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  508. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了商品' . $Product->product_name . '状态');
  509. // 告知结果
  510. return json_send(['code' => 'success', 'msg' => '设置成功']);
  511. }
  512. /**
  513. * 删除
  514. * @author 唐远望
  515. * @version 1.0
  516. * @date 2025-12-30
  517. *
  518. */
  519. public function delete(Request $request, ProductModel $ProductModel)
  520. {
  521. // 验证参数
  522. $request->scene('delete')->validate();
  523. $admin_company_id = request('admin_company_id', '0');
  524. $company_id = request('access_token.company_id', '0');
  525. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  526. // 接收数据
  527. $id = request('id', 0);
  528. // 查询用户
  529. $where = ['id' => $id];
  530. if ($is_admin != 1 && $company_id != 0) {
  531. $where['company_id'] = $company_id;
  532. } else {
  533. $where['company_id'] = $admin_company_id;
  534. }
  535. // 执行删除
  536. $Product = $ProductModel->where($where)->first();
  537. if (!$Product) {
  538. return json_send(['code' => 'error', 'msg' => '删除失败,记录不存在']);
  539. }
  540. DB::beginTransaction();
  541. try {
  542. $Product->delete();
  543. // 记录行为
  544. $admin_id = request('access_token.uid', 0); //用户ID
  545. $table_name = $ProductModel->getTable();
  546. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  547. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $Product->toarray(), [], '删除了商品' . $Product->product_name . '信息');
  548. // 告知结果
  549. DB::commit();
  550. return json_send(['code' => 'success', 'msg' => '删除成功']);
  551. // 成功处理...
  552. } catch (\Exception $e) {
  553. DB::rollBack();
  554. // 错误处理...
  555. return json_send(['code' => 'error', 'msg' => '删除失败']);
  556. }
  557. }
  558. }