Product.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. $sampling_cycle = isset($value['sampling_cycle']) ? explode(',', $value['sampling_cycle']) : '';
  77. $result['data'][$key]['sampling_cycle'] = $sampling_cycle ? array_filter($sampling_cycle) : '';
  78. $round_number_config = isset($value['round_number_config']) ? json_decode($value['round_number_config'], true) : '';
  79. $result['data'][$key]['round_number_config'] = $round_number_config ? array_filter($round_number_config) : '';
  80. }
  81. }
  82. // 加载模板
  83. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  84. }
  85. /**
  86. * 商品名称规格列表
  87. * @author 唐远望
  88. * @version 1.0
  89. * @date 2026-03-25
  90. *
  91. */
  92. public function product_name_specs_list(Request $request, ProductModel $ProductModel, ProductKeywordModel $ProductKeywordModel)
  93. {
  94. $request->scene('product_name_specs_list')->validate();
  95. $admin_company_id = request('admin_company_id', '0');
  96. $company_id = request('access_token.company_id', '0');
  97. $is_admin = request('access_token.is_admin', '0');
  98. // 查询条件
  99. $map = [];
  100. $map2 = [];
  101. $map3 = [];
  102. $start_time = request('start_time', '');
  103. $end_time = request('end_time', '');
  104. $product_name = request('product_name', '');
  105. $product_brand = request('product_brand', '');
  106. // 时间条件
  107. if ($start_time) {
  108. $map[] = ['insert_time', '>=', strtotime($start_time)];
  109. $map2[] = ['insert_time', '>=', strtotime($start_time)];
  110. $map3[] = ['insert_time', '>=', strtotime($start_time)];
  111. }
  112. if ($end_time) {
  113. $map[] = ['insert_time', '<=', strtotime($end_time)];
  114. $map2[] = ['insert_time', '<=', strtotime($end_time)];
  115. $map3[] = ['insert_time', '<=', strtotime($end_time)];
  116. }
  117. // 其他条件
  118. $map[] = ['status', '=', 0];
  119. if ($product_name) $map2[] = ['product_name', 'like', "%$product_name%"];
  120. if ($product_brand) $map[] = ['product_brand', 'like', "%$product_brand%"];
  121. // 权限判断
  122. if ($is_admin != 1 && $company_id != 0) {
  123. $map[] = ['company_id', '=', $company_id];
  124. $map2[] = ['company_id', '=', $company_id];
  125. $map3[] = ['company_id', '=', $company_id];
  126. } else {
  127. $map[] = ['company_id', '=', $admin_company_id];
  128. $map2[] = ['company_id', '=', $admin_company_id];
  129. $map3[] = ['company_id', '=', $admin_company_id];
  130. }
  131. // 查询数据
  132. $result = $ProductModel->query()
  133. ->where($map)
  134. ->where([['product_brand', '!=', ''], ['product_brand', '!=', null]])
  135. ->select([DB::raw('MAX(id) as id'), 'product_brand'])
  136. ->groupBy(['product_brand'])
  137. ->orderByDesc('id')->get()->toarray();
  138. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  139. foreach ($result as $key => $value) {
  140. $product_list = $ProductModel->query()
  141. ->where($map2)
  142. ->where([['product_name', '!=', ''], ['product_name', '!=', null]])
  143. ->where(['product_brand' => $value['product_brand']])
  144. ->select([DB::raw('MAX(id) as id'), 'product_name'])
  145. ->groupBy(['product_name'])
  146. ->orderByDesc('id')->get()->toarray();
  147. if (!empty($product_list)) {
  148. foreach ($product_list as $key2 => $value2) {
  149. $product_keyword = $ProductKeywordModel->query()
  150. ->where(['collect_product_id' => $value2['id']])
  151. ->select('keyword')
  152. ->groupBy(['keyword'])
  153. ->get()->toarray();
  154. $product_list[$key2]['product_keyword'] = $product_keyword ? array_column($product_keyword, 'keyword') : [];
  155. $product_specs = $ProductModel->query()
  156. ->where($map3)
  157. ->where([['product_specs', '!=', ''], ['product_specs', '!=', null]])
  158. ->where(['product_name' => $value2['product_name']])
  159. ->where(['product_brand' => $value['product_brand']])
  160. ->select([DB::raw('MAX(id) as id'), 'product_specs'])
  161. ->groupBy(['product_specs'])
  162. ->orderByDesc('id')->get()->toarray();
  163. $product_specs_tring = [];
  164. if (count($product_specs) > 0) {
  165. foreach ($product_specs as $key3 => $value3) {
  166. $product_specs_item = isset($value3['product_specs']) ? explode(',', $value3['product_specs']) : '';
  167. //合并数组
  168. if (!empty($product_specs_item)) {
  169. $product_specs_tring = array_merge($product_specs_tring, $product_specs_item);
  170. }
  171. }
  172. //去重
  173. $product_specs_tring = array_unique($product_specs_tring);
  174. }
  175. //移除空数组
  176. $product_list[$key2]['product_specs'] = $product_specs_tring ? array_filter($product_specs_tring) : '';
  177. }
  178. }
  179. $result[$key]['product_list'] = !empty($product_list) ? $product_list : [];
  180. }
  181. // 加载模板
  182. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  183. }
  184. /**
  185. * 详情
  186. * @author 唐远望
  187. * @version 1.0
  188. * @date 2025-12-30
  189. */
  190. public function detail(Request $request, ProductModel $ProductModel)
  191. {
  192. $request->scene('detail')->validate();
  193. $admin_company_id = request('admin_company_id', '0');
  194. $company_id = request('access_token.company_id', '0');
  195. $is_admin = request('access_token.is_admin', '0');
  196. // 接收参数
  197. $id = request('id', 0);
  198. $map = ['id' => $id];
  199. // 权限判断
  200. if ($is_admin != 1 && $company_id != 0) {
  201. $map['company_id'] = $company_id;
  202. } else {
  203. $map['company_id'] = $admin_company_id;
  204. }
  205. $data = $ProductModel->where($map)->with(['product_keyword'])->first();
  206. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  207. $data->platform = isset($data->platform) ? explode(',', $data->platform) : '';
  208. $product_specs = isset($data->product_specs) ? explode(',', $data->product_specs) : '';
  209. //移除空数组
  210. $data->product_specs = $product_specs ? array_filter($product_specs) : '';
  211. $sampling_cycle = isset($data->sampling_cycle) ? explode(',', $data->sampling_cycle) : '';
  212. $data->sampling_cycle = $sampling_cycle ? array_filter($sampling_cycle) : '';
  213. $round_number_config = isset($data->round_number_config) ? json_decode($data->round_number_config, true) : '';
  214. $data->round_number_config = $round_number_config ? array_filter($round_number_config) : '';
  215. // 加载模板
  216. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  217. }
  218. /**
  219. * 添加
  220. * @author 唐远望
  221. * @version 1.0
  222. * @date 2025-12-30
  223. *
  224. */
  225. public function add(Request $request, ProductModel $ProductModel)
  226. {
  227. $request->scene('add')->validate();
  228. $admin_company_id = request('admin_company_id', '0');
  229. $company_id = request('access_token.company_id', '0');
  230. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  231. //商品启用数量
  232. // $product_count = $ProductModel->where('status', 0)->count();
  233. //判断是否超过限制
  234. // if ($product_count >= 50) {
  235. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  236. // }
  237. // 接收数据
  238. $all_data = request()->all();
  239. $product_brand = request('product_brand', '');
  240. $product_keyword = request('product_keyword', '');
  241. $minimum_order_quantity = request('minimum_order_quantity', 1);
  242. $all_data['platform'] = request('platform', '');
  243. $all_data['product_specs'] = request('product_specs', '');
  244. $all_data['product_brand'] = $product_brand;
  245. $all_data['product_keyword'] = $product_keyword;
  246. $minimum_order_quantity = request('minimum_order_quantity', 1);
  247. $sampling_cycle = request('sampling_cycle', '');
  248. $sampling_start_time = request('sampling_start_time', '');
  249. $sampling_end_time = request('sampling_end_time', '');
  250. $round_number = request('round_number', 1);
  251. $is_multiple_accounts = request('is_multiple_accounts', 0);
  252. $round_number_config = request('round_number_config', '');
  253. $all_data['round_number_config'] = !empty($round_number_config) ? json_encode($round_number_config) : '';
  254. $all_data['is_multiple_accounts'] = $is_multiple_accounts;
  255. $all_data['round_number'] = $round_number;
  256. $all_data['sampling_cycle'] = $sampling_cycle;
  257. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  258. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  259. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  260. $sampling_start_time = $all_data['sampling_start_time'];
  261. $sampling_end_time = $all_data['sampling_end_time'];
  262. if (is_numeric($round_number) && $round_number > 3) return json_send(['code' => 'error', 'msg' => '轮次数量不能超过3']);
  263. 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' => '采集开始时间必须大于当前时间']);
  264. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  265. if ($all_data['sampling_start_time'] > $all_data['sampling_end_time']) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于开始时间']);
  266. $platform_string = $all_data['platform'];
  267. if (empty($platform_string)) return json_send(['code' => 'error', 'msg' => '平台不能为空']);
  268. $enable_full_quantity = request('enable_full_quantity', 0); //全量,0启用,1禁用
  269. $all_data['enable_full_quantity'] = $enable_full_quantity;
  270. if (trim($product_keyword) != '') {
  271. $product_keyword_count = count(explode(',', $product_keyword));
  272. if ($product_keyword_count > 5) {
  273. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  274. }
  275. }
  276. if ($enable_full_quantity == 1 && $all_data['product_specs'] == '') {
  277. return json_send(['code' => 'error', 'msg' => '非全量采集,商品规格不能为空']);
  278. }
  279. //查询是否存在
  280. $map = ['product_name' => $all_data['product_name']];
  281. if ($product_brand != '') $map['product_brand'] = $product_brand;
  282. if ($is_admin != 1 && $company_id != 0) {
  283. $map['company_id'] = $company_id;
  284. $all_data['company_id'] = $company_id;
  285. } else {
  286. $map['company_id'] = $admin_company_id;
  287. $all_data['company_id'] = $admin_company_id;
  288. }
  289. $Product = $ProductModel->where($map)->first();
  290. if (!empty($Product)) {
  291. //继续校验规格是否存在
  292. $product_specs_tring = $Product->product_specs;
  293. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  294. $product_specs = $product_specs ? array_unique($product_specs) : '';
  295. //继续校平台是否存在
  296. $platform_string = $Product->platform;
  297. $platforms = $platform_string ? explode(',', $platform_string) : '';
  298. $platforms = $platforms ? array_unique($platforms) : '';
  299. $platform_data = $ProductModel->platform_index_data();
  300. foreach ($platforms as $platform) {
  301. // 修正:将 orWhereRaw 改为 orWhere,并传入闭包
  302. $product_specs_log = $ProductModel
  303. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  304. ->where([
  305. 'product_brand' => $Product->product_brand,
  306. 'product_name' => $Product->product_name,
  307. 'status' => '0',
  308. 'company_id' => $Product->company_id
  309. ])
  310. ->where(function ($query) use ($product_specs) {
  311. $query->where(function ($query_li) use ($product_specs) {
  312. if (!empty($product_specs)) {
  313. foreach ($product_specs as $product_spec) {
  314. if (empty($product_spec)) continue;
  315. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  316. }
  317. } else {
  318. // 如果没有规格条件,添加一个永远为假的条件
  319. $query_li->whereRaw('1 = 0');
  320. }
  321. });
  322. })
  323. // 时间重叠条件:已存在记录的时间段与新增记录的时间段有重叠
  324. ->where(function ($query) use ($Product, $sampling_start_time, $sampling_end_time) {
  325. // 新增记录有完整的时间段
  326. if ($sampling_start_time > 0 && $sampling_end_time > 0) {
  327. $query->orwhere(function ($q) use ($sampling_end_time,$sampling_start_time ) {
  328. $q->where('sampling_start_time', '<=', $sampling_end_time)
  329. ->where('sampling_end_time', '>=', $sampling_start_time);
  330. })->orwhere(function ($q) use ($sampling_start_time ) {
  331. $q->where('sampling_start_time', '<=', $sampling_start_time)
  332. ->where('sampling_end_time', '=', 0);
  333. });
  334. }
  335. // 新增记录只有开始时间(没有结束时间,表示从开始时间到无限)
  336. elseif ($sampling_start_time > 0 && $sampling_end_time == 0) {
  337. $query->where(function ($q) use ($sampling_start_time) {
  338. // 已存在记录的开始时间 <= 新增记录的开始时间
  339. $q->where('sampling_start_time', '<=', $sampling_start_time);
  340. });
  341. }
  342. // 新增记录只有结束时间
  343. elseif ($Product->sampling_start_time == 0 && $Product->sampling_end_time > 0) {
  344. $query->where(function ($q) use ($Product) {
  345. $q->where('sampling_start_time', '<=', $Product->sampling_end_time);
  346. });
  347. }
  348. })
  349. ->first();
  350. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  351. if (!empty($product_specs_log)) {
  352. return json_send(['code' => 'error', 'msg' => $platform_name . '时间范围内存在重复的商品规格启用记录']);
  353. }
  354. }
  355. }
  356. // 写入数据表
  357. $result = $ProductModel->addProduct($all_data);
  358. // 如果操作失败
  359. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  360. // 记录行为
  361. $admin_id = request('access_token.uid', 0); //用户ID
  362. $table_name = $ProductModel->getTable();
  363. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  364. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了商品' . $all_data['product_name'] . '信息');
  365. // 告知结果
  366. return json_send(['code' => 'success', 'msg' => '新增成功']);
  367. }
  368. /**
  369. * 修改
  370. * @author 唐远望
  371. * @version 1.0
  372. * @date 2025-12-30
  373. *
  374. */
  375. public function edit(Request $request, ProductModel $ProductModel)
  376. {
  377. $request->scene('edit')->validate();
  378. $admin_company_id = request('admin_company_id', '0');
  379. $company_id = request('access_token.company_id', '0');
  380. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  381. // 接收参数
  382. $id = request('id', 0);
  383. // 接收数据
  384. $all_data = request()->all();
  385. $product_brand = request('product_brand', '');
  386. $product_keyword = request('product_keyword', '');
  387. $minimum_order_quantity = request('minimum_order_quantity', 1);
  388. $all_data['platform'] = request('platform', '');
  389. $all_data['product_specs'] = request('product_specs', '');
  390. $all_data['product_brand'] = $product_brand;
  391. $all_data['product_keyword'] = $product_keyword;
  392. $minimum_order_quantity = request('minimum_order_quantity', 1);
  393. $sampling_cycle = request('sampling_cycle', '');
  394. $sampling_start_time = request('sampling_start_time', '');
  395. $sampling_end_time = request('sampling_end_time', '');
  396. $round_number = request('round_number', 1);
  397. $is_multiple_accounts = request('is_multiple_accounts', 0);
  398. $round_number_config = request('round_number_config', '');
  399. $all_data['round_number_config'] = !empty($round_number_config) ? json_encode($round_number_config) : '';
  400. $all_data['is_multiple_accounts'] = $is_multiple_accounts;
  401. $all_data['round_number'] = $round_number;
  402. $all_data['sampling_cycle'] = $sampling_cycle;
  403. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  404. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  405. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  406. $sampling_start_time = $all_data['sampling_start_time'];
  407. $sampling_end_time = $all_data['sampling_end_time'];
  408. if (is_numeric($round_number) && $round_number > 3) return json_send(['code' => 'error', 'msg' => '轮次数量不能超过3']);
  409. 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' => '采集开始时间必须大于当前时间']);
  410. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  411. if ($all_data['sampling_start_time'] > $all_data['sampling_end_time']) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于开始时间']);
  412. $platform_string = $all_data['platform'];
  413. if (empty($platform_string)) return json_send(['code' => 'error', 'msg' => '平台不能为空']);
  414. $enable_full_quantity = request('enable_full_quantity', 0); //全量,0启用,1禁用
  415. $all_data['enable_full_quantity'] = $enable_full_quantity;
  416. if (trim($product_keyword) != '') {
  417. $product_keyword_count = count(explode(',', $product_keyword));
  418. if ($product_keyword_count > 5) {
  419. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  420. }
  421. }
  422. if ($enable_full_quantity == 1 && $all_data['product_specs'] == '') {
  423. return json_send(['code' => 'error', 'msg' => '非全量采集,商品规格不能为空']);
  424. }
  425. //查询是否存在
  426. $map = ['product_brand' => $product_brand, 'product_name' => $all_data['product_name']];
  427. if ($is_admin != 1 && $company_id != 0) {
  428. $map['company_id'] = $company_id;
  429. } else {
  430. $map['company_id'] = $admin_company_id;
  431. }
  432. $all_data['company_id'] = $map['company_id'];
  433. $Product = $ProductModel->where($map)->where('id', '!=', $id)->first();
  434. if (!empty($Product)) {
  435. //继续校验规格是否存在
  436. $product_specs_tring = $Product->product_specs;
  437. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  438. $product_specs = $product_specs ? array_unique($product_specs) : '';
  439. //继续校平台是否存在
  440. $platform_string = $Product->platform;
  441. $platforms = $platform_string ? explode(',', $platform_string) : '';
  442. $platforms = $platforms ? array_unique($platforms) : '';
  443. $platform_data = $ProductModel->platform_index_data();
  444. foreach ($platforms as $platform) {
  445. // 修正:将 orWhereRaw 改为 orWhere,并传入闭包
  446. $product_specs_log = $ProductModel
  447. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  448. ->where([
  449. 'product_brand' => $Product->product_brand,
  450. 'product_name' => $Product->product_name,
  451. 'status' => '0',
  452. 'company_id' => $Product->company_id
  453. ])
  454. ->where('id', '!=', $Product->id)
  455. ->where(function ($query) use ($product_specs) {
  456. $query->where(function ($query_li) use ($product_specs) {
  457. if (!empty($product_specs)) {
  458. foreach ($product_specs as $product_spec) {
  459. if (empty($product_spec)) continue;
  460. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  461. }
  462. } else {
  463. // 如果没有规格条件,添加一个永远为假的条件
  464. $query_li->whereRaw('1 = 0');
  465. }
  466. });
  467. })
  468. // 时间重叠条件:已存在记录的时间段与新增记录的时间段有重叠
  469. ->where(function ($query) use ($Product, $sampling_start_time, $sampling_end_time) {
  470. // 新增记录有完整的时间段
  471. if ($sampling_start_time > 0 && $sampling_end_time > 0) {
  472. $query->orwhere(function ($q) use ($sampling_end_time,$sampling_start_time ) {
  473. $q->where('sampling_start_time', '<=', $sampling_end_time)
  474. ->where('sampling_end_time', '>=', $sampling_start_time);
  475. })->orwhere(function ($q) use ($sampling_start_time ) {
  476. $q->where('sampling_start_time', '<=', $sampling_start_time)
  477. ->where('sampling_end_time', '=', 0);
  478. });
  479. }
  480. // 新增记录只有开始时间(没有结束时间,表示从开始时间到无限)
  481. elseif ($sampling_start_time > 0 && $sampling_end_time == 0) {
  482. $query->where(function ($q) use ($sampling_start_time) {
  483. // 已存在记录的开始时间 <= 新增记录的开始时间
  484. $q->where('sampling_start_time', '<=', $sampling_start_time);
  485. });
  486. }
  487. // 新增记录只有结束时间
  488. elseif ($Product->sampling_start_time == 0 && $Product->sampling_end_time > 0) {
  489. $query->where(function ($q) use ($Product) {
  490. $q->where('sampling_start_time', '<=', $Product->sampling_end_time);
  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. $where = ['id' => $id];
  503. // 权限判断
  504. if ($is_admin != 1 && $company_id != 0) {
  505. $where['company_id'] = $company_id;
  506. } else {
  507. $where['company_id'] = $admin_company_id;
  508. }
  509. $Product = $ProductModel->where($where)->first();
  510. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  511. $oldData = $Product->toarray();
  512. $result = $ProductModel->editProduct_content($Product, $all_data);
  513. // 如果操作失败
  514. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  515. // 记录行为
  516. $admin_id = request('access_token.uid', 0); //用户ID
  517. $table_name = $ProductModel->getTable();
  518. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  519. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了商品' . $oldData['product_name'] . '信息');
  520. // 告知结果
  521. return json_send(['code' => 'success', 'msg' => '修改成功']);
  522. }
  523. /**
  524. * 修改状态
  525. * @author 唐远望
  526. * @version 1.0
  527. * @date 2025-12-30
  528. *
  529. */
  530. public function set_status(Request $request, ProductModel $ProductModel)
  531. {
  532. // 验证参数
  533. $request->scene('set_status')->validate();
  534. $admin_company_id = request('admin_company_id', '0');
  535. $company_id = request('access_token.company_id', '0');
  536. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  537. // 接收数据
  538. $id = request('id', 0);
  539. $status = request('status', 0);
  540. if ($status == 0) {
  541. //获取商品启用数量
  542. // $product_count = $ProductModel->where('status', 0)->count();
  543. //判断是否超过限制
  544. // if ($product_count >= 50) {
  545. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  546. // }
  547. }
  548. // 查询用户
  549. $where = ['id' => $id];
  550. if ($is_admin != 1 && $company_id != 0) {
  551. $where['company_id'] = $company_id;
  552. } else {
  553. $where['company_id'] = $admin_company_id;
  554. }
  555. $Product = $ProductModel->where($where)->first();
  556. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  557. if (!empty($Product)) {
  558. //继续校验规格是否存在
  559. $product_specs_tring = $Product->product_specs;
  560. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  561. $product_specs = $product_specs ? array_unique($product_specs) : '';
  562. //继续校平台是否存在
  563. $platform_string = $Product->platform;
  564. $platforms = $platform_string ? explode(',', $platform_string) : '';
  565. $platforms = $platforms ? array_unique($platforms) : '';
  566. $platform_data = $ProductModel->platform_index_data();
  567. foreach ($platforms as $platform) {
  568. // 修正:将 orWhereRaw 改为 orWhere,并传入闭包
  569. $product_specs_log = $ProductModel
  570. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  571. ->where([
  572. 'product_brand' => $Product->product_brand,
  573. 'product_name' => $Product->product_name,
  574. 'status' => '0',
  575. 'company_id' => $Product->company_id
  576. ])
  577. ->where('id', '!=', $Product->id)
  578. ->where(function ($query) use ($product_specs) {
  579. $query->where(function ($query_li) use ($product_specs) {
  580. if (!empty($product_specs)) {
  581. foreach ($product_specs as $product_spec) {
  582. if (empty($product_spec)) continue;
  583. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  584. }
  585. } else {
  586. // 如果没有规格条件,添加一个永远为假的条件
  587. $query_li->whereRaw('1 = 0');
  588. }
  589. });
  590. })
  591. // 时间重叠条件:已存在记录的时间段与新增记录的时间段有重叠
  592. ->where(function ($query) use ($Product) {
  593. // 新增记录有完整的时间段
  594. if ($Product->sampling_start_time > 0 && $Product->sampling_end_time > 0) {
  595. $query->orwhere(function ($q) use ($Product ) {
  596. $q->where('sampling_start_time', '<=', $Product->sampling_end_time)
  597. ->where('sampling_end_time', '>=', $Product->sampling_start_time);
  598. })->orwhere(function ($q) use ($Product ) {
  599. $q->where('sampling_start_time', '<=', $Product->sampling_start_time)
  600. ->where('sampling_end_time', '=', 0);
  601. });
  602. }
  603. // 新增记录只有开始时间(没有结束时间,表示从开始时间到无限)
  604. elseif ($Product->sampling_start_time > 0 && $Product->sampling_end_time == 0) {
  605. $query->where(function ($q) use ($Product) {
  606. // 已存在记录的开始时间 <= 新增记录的开始时间
  607. $q->where('sampling_start_time', '<=', $Product->sampling_start_time);
  608. });
  609. }
  610. // 新增记录只有结束时间
  611. elseif ($Product->sampling_start_time == 0 && $Product->sampling_end_time > 0) {
  612. $query->where(function ($q) use ($Product) {
  613. $q->where('sampling_start_time', '<=', $Product->sampling_end_time);
  614. });
  615. }
  616. })
  617. ->first();
  618. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  619. if (!empty($product_specs_log)) {
  620. return json_send(['code' => 'error', 'msg' => $platform_name . '时间范围内存在重复的商品规格启用记录']);
  621. }
  622. }
  623. }
  624. // 执行修改
  625. $result = $ProductModel->changeStatus($Product, $status);
  626. // 提示新增失败
  627. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  628. // 记录行为
  629. $admin_id = request('access_token.uid', 0); //用户ID
  630. $table_name = $ProductModel->getTable();
  631. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  632. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了商品' . $Product->product_name . '状态');
  633. // 告知结果
  634. return json_send(['code' => 'success', 'msg' => '设置成功']);
  635. }
  636. /**
  637. * 删除
  638. * @author 唐远望
  639. * @version 1.0
  640. * @date 2025-12-30
  641. *
  642. */
  643. public function delete(Request $request, ProductModel $ProductModel)
  644. {
  645. // 验证参数
  646. $request->scene('delete')->validate();
  647. $admin_company_id = request('admin_company_id', '0');
  648. $company_id = request('access_token.company_id', '0');
  649. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  650. // 接收数据
  651. $id = request('id', 0);
  652. // 查询用户
  653. $where = ['id' => $id];
  654. if ($is_admin != 1 && $company_id != 0) {
  655. $where['company_id'] = $company_id;
  656. } else {
  657. $where['company_id'] = $admin_company_id;
  658. }
  659. // 执行删除
  660. $Product = $ProductModel->where($where)->first();
  661. if (!$Product) {
  662. return json_send(['code' => 'error', 'msg' => '删除失败,记录不存在']);
  663. }
  664. DB::beginTransaction();
  665. try {
  666. $Product->delete();
  667. // 记录行为
  668. $admin_id = request('access_token.uid', 0); //用户ID
  669. $table_name = $ProductModel->getTable();
  670. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  671. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $Product->toarray(), [], '删除了商品' . $Product->product_name . '信息');
  672. // 告知结果
  673. DB::commit();
  674. return json_send(['code' => 'success', 'msg' => '删除成功']);
  675. // 成功处理...
  676. } catch (\Exception $e) {
  677. DB::rollBack();
  678. // 错误处理...
  679. return json_send(['code' => 'error', 'msg' => '删除失败']);
  680. }
  681. }
  682. }