Product.php 29 KB

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