Product.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. $platform_string = $all_data['platform'];
  266. if (empty($platform_string)) return json_send(['code' => 'error', 'msg' => '平台不能为空']);
  267. $enable_full_quantity = request('enable_full_quantity', 0); //全量,0启用,1禁用
  268. $all_data['enable_full_quantity'] = $enable_full_quantity;
  269. if (trim($product_keyword) != '') {
  270. $product_keyword_count = count(explode(',', $product_keyword));
  271. if ($product_keyword_count > 5) {
  272. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  273. }
  274. }
  275. if ($enable_full_quantity == 1 && $all_data['product_specs'] == '') {
  276. return json_send(['code' => 'error', 'msg' => '非全量采集,商品规格不能为空']);
  277. }
  278. //查询是否存在
  279. $map = ['product_name' => $all_data['product_name']];
  280. if ($product_brand != '') $map['product_brand'] = $product_brand;
  281. if ($is_admin != 1 && $company_id != 0) {
  282. $map['company_id'] = $company_id;
  283. $all_data['company_id'] = $company_id;
  284. } else {
  285. $map['company_id'] = $admin_company_id;
  286. $all_data['company_id'] = $admin_company_id;
  287. }
  288. $Product = $ProductModel->where($map)->first();
  289. if (!empty($Product)) {
  290. //继续校验规格是否存在
  291. $product_specs_tring = $Product->product_specs;
  292. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  293. $product_specs = $product_specs ? array_unique($product_specs) : '';
  294. //继续校平台是否存在
  295. $platform_string = $Product->platform;
  296. $platforms = $platform_string ? explode(',', $platform_string) : '';
  297. $platforms = $platforms ? array_unique($platforms) : '';
  298. $platform_data = $ProductModel->platform_index_data();
  299. foreach ($platforms as $platform) {
  300. // 修正:将 orWhereRaw 改为 orWhere,并传入闭包
  301. $product_specs_log = $ProductModel
  302. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  303. ->where([
  304. 'product_brand' => $Product->product_brand,
  305. 'product_name' => $Product->product_name,
  306. 'status' => '0',
  307. 'company_id' => $Product->company_id
  308. ])
  309. ->where(function ($query) use ($product_specs) {
  310. $query->where(function ($query_li) use ($product_specs) {
  311. if (!empty($product_specs)) {
  312. foreach ($product_specs as $product_spec) {
  313. if (empty($product_spec)) continue;
  314. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  315. }
  316. } else {
  317. // 如果没有规格条件,添加一个永远为假的条件
  318. $query_li->whereRaw('1 = 0');
  319. }
  320. });
  321. })
  322. // 时间重叠条件:已存在记录的时间段与新增记录的时间段有重叠
  323. ->where(function ($query) use ($Product, $sampling_start_time, $sampling_end_time) {
  324. // 新增记录有完整的时间段
  325. if ($sampling_start_time > 0 && $sampling_end_time > 0) {
  326. $query->orwhere(function ($q) use ($sampling_end_time,$sampling_start_time ) {
  327. $q->where('sampling_start_time', '<=', $sampling_end_time)
  328. ->where('sampling_end_time', '>=', $sampling_start_time);
  329. })->orwhere(function ($q) use ($sampling_start_time ) {
  330. $q->where('sampling_start_time', '<=', $sampling_start_time)
  331. ->where('sampling_end_time', '=', 0);
  332. });
  333. }
  334. // 新增记录只有开始时间(没有结束时间,表示从开始时间到无限)
  335. elseif ($sampling_start_time > 0 && $sampling_end_time == 0) {
  336. $query->where(function ($q) use ($sampling_start_time) {
  337. // 已存在记录的开始时间 <= 新增记录的开始时间
  338. $q->where('sampling_start_time', '<=', $sampling_start_time);
  339. });
  340. }
  341. // 新增记录只有结束时间
  342. elseif ($Product->sampling_start_time == 0 && $Product->sampling_end_time > 0) {
  343. $query->where(function ($q) use ($Product) {
  344. $q->where('sampling_start_time', '<=', $Product->sampling_end_time);
  345. });
  346. }
  347. })
  348. ->first();
  349. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  350. if (!empty($product_specs_log)) {
  351. return json_send(['code' => 'error', 'msg' => $platform_name . '时间范围内存在重复的商品规格启用记录']);
  352. }
  353. }
  354. }
  355. // 写入数据表
  356. $result = $ProductModel->addProduct($all_data);
  357. // 如果操作失败
  358. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  359. // 记录行为
  360. $admin_id = request('access_token.uid', 0); //用户ID
  361. $table_name = $ProductModel->getTable();
  362. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  363. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了商品' . $all_data['product_name'] . '信息');
  364. // 告知结果
  365. return json_send(['code' => 'success', 'msg' => '新增成功']);
  366. }
  367. /**
  368. * 修改
  369. * @author 唐远望
  370. * @version 1.0
  371. * @date 2025-12-30
  372. *
  373. */
  374. public function edit(Request $request, ProductModel $ProductModel)
  375. {
  376. $request->scene('edit')->validate();
  377. $admin_company_id = request('admin_company_id', '0');
  378. $company_id = request('access_token.company_id', '0');
  379. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  380. // 接收参数
  381. $id = request('id', 0);
  382. // 接收数据
  383. $all_data = request()->all();
  384. $product_brand = request('product_brand', '');
  385. $product_keyword = request('product_keyword', '');
  386. $minimum_order_quantity = request('minimum_order_quantity', 1);
  387. $all_data['platform'] = request('platform', '');
  388. $all_data['product_specs'] = request('product_specs', '');
  389. $all_data['product_brand'] = $product_brand;
  390. $all_data['product_keyword'] = $product_keyword;
  391. $minimum_order_quantity = request('minimum_order_quantity', 1);
  392. $sampling_cycle = request('sampling_cycle', '');
  393. $sampling_start_time = request('sampling_start_time', '');
  394. $sampling_end_time = request('sampling_end_time', '');
  395. $round_number = request('round_number', 1);
  396. $is_multiple_accounts = request('is_multiple_accounts', 0);
  397. $round_number_config = request('round_number_config', '');
  398. $all_data['round_number_config'] = !empty($round_number_config) ? json_encode($round_number_config) : '';
  399. $all_data['is_multiple_accounts'] = $is_multiple_accounts;
  400. $all_data['round_number'] = $round_number;
  401. $all_data['sampling_cycle'] = $sampling_cycle;
  402. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  403. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  404. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  405. $sampling_start_time = $all_data['sampling_start_time'];
  406. $sampling_end_time = $all_data['sampling_end_time'];
  407. if (is_numeric($round_number) && $round_number > 3) return json_send(['code' => 'error', 'msg' => '轮次数量不能超过3']);
  408. 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' => '采集开始时间必须大于当前时间']);
  409. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  410. $platform_string = $all_data['platform'];
  411. if (empty($platform_string)) return json_send(['code' => 'error', 'msg' => '平台不能为空']);
  412. $enable_full_quantity = request('enable_full_quantity', 0); //全量,0启用,1禁用
  413. $all_data['enable_full_quantity'] = $enable_full_quantity;
  414. if (trim($product_keyword) != '') {
  415. $product_keyword_count = count(explode(',', $product_keyword));
  416. if ($product_keyword_count > 5) {
  417. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  418. }
  419. }
  420. if ($enable_full_quantity == 1 && $all_data['product_specs'] == '') {
  421. return json_send(['code' => 'error', 'msg' => '非全量采集,商品规格不能为空']);
  422. }
  423. //查询是否存在
  424. $map = ['product_brand' => $product_brand, 'product_name' => $all_data['product_name']];
  425. if ($is_admin != 1 && $company_id != 0) {
  426. $map['company_id'] = $company_id;
  427. } else {
  428. $map['company_id'] = $admin_company_id;
  429. }
  430. $all_data['company_id'] = $map['company_id'];
  431. $Product = $ProductModel->where($map)->where('id', '!=', $id)->first();
  432. if (!empty($Product)) {
  433. //继续校验规格是否存在
  434. $product_specs_tring = $Product->product_specs;
  435. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  436. $product_specs = $product_specs ? array_unique($product_specs) : '';
  437. //继续校平台是否存在
  438. $platform_string = $Product->platform;
  439. $platforms = $platform_string ? explode(',', $platform_string) : '';
  440. $platforms = $platforms ? array_unique($platforms) : '';
  441. $platform_data = $ProductModel->platform_index_data();
  442. foreach ($platforms as $platform) {
  443. // 修正:将 orWhereRaw 改为 orWhere,并传入闭包
  444. $product_specs_log = $ProductModel
  445. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  446. ->where([
  447. 'product_brand' => $Product->product_brand,
  448. 'product_name' => $Product->product_name,
  449. 'status' => '0',
  450. 'company_id' => $Product->company_id
  451. ])
  452. ->where('id', '!=', $Product->id)
  453. ->where(function ($query) use ($product_specs) {
  454. $query->where(function ($query_li) use ($product_specs) {
  455. if (!empty($product_specs)) {
  456. foreach ($product_specs as $product_spec) {
  457. if (empty($product_spec)) continue;
  458. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  459. }
  460. } else {
  461. // 如果没有规格条件,添加一个永远为假的条件
  462. $query_li->whereRaw('1 = 0');
  463. }
  464. });
  465. })
  466. // 时间重叠条件:已存在记录的时间段与新增记录的时间段有重叠
  467. ->where(function ($query) use ($Product, $sampling_start_time, $sampling_end_time) {
  468. // 新增记录有完整的时间段
  469. if ($sampling_start_time > 0 && $sampling_end_time > 0) {
  470. $query->orwhere(function ($q) use ($sampling_end_time,$sampling_start_time ) {
  471. $q->where('sampling_start_time', '<=', $sampling_end_time)
  472. ->where('sampling_end_time', '>=', $sampling_start_time);
  473. })->orwhere(function ($q) use ($sampling_start_time ) {
  474. $q->where('sampling_start_time', '<=', $sampling_start_time)
  475. ->where('sampling_end_time', '=', 0);
  476. });
  477. }
  478. // 新增记录只有开始时间(没有结束时间,表示从开始时间到无限)
  479. elseif ($sampling_start_time > 0 && $sampling_end_time == 0) {
  480. $query->where(function ($q) use ($sampling_start_time) {
  481. // 已存在记录的开始时间 <= 新增记录的开始时间
  482. $q->where('sampling_start_time', '<=', $sampling_start_time);
  483. });
  484. }
  485. // 新增记录只有结束时间
  486. elseif ($Product->sampling_start_time == 0 && $Product->sampling_end_time > 0) {
  487. $query->where(function ($q) use ($Product) {
  488. $q->where('sampling_start_time', '<=', $Product->sampling_end_time);
  489. });
  490. }
  491. })
  492. ->first();
  493. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  494. if (!empty($product_specs_log)) {
  495. return json_send(['code' => 'error', 'msg' => $platform_name . '时间范围内存在重复的商品规格启用记录']);
  496. }
  497. }
  498. }
  499. // 更新数据表
  500. $where = ['id' => $id];
  501. // 权限判断
  502. if ($is_admin != 1 && $company_id != 0) {
  503. $where['company_id'] = $company_id;
  504. } else {
  505. $where['company_id'] = $admin_company_id;
  506. }
  507. $Product = $ProductModel->where($where)->first();
  508. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  509. $oldData = $Product->toarray();
  510. $result = $ProductModel->editProduct_content($Product, $all_data);
  511. // 如果操作失败
  512. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  513. // 记录行为
  514. $admin_id = request('access_token.uid', 0); //用户ID
  515. $table_name = $ProductModel->getTable();
  516. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  517. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了商品' . $oldData['product_name'] . '信息');
  518. // 告知结果
  519. return json_send(['code' => 'success', 'msg' => '修改成功']);
  520. }
  521. /**
  522. * 修改状态
  523. * @author 唐远望
  524. * @version 1.0
  525. * @date 2025-12-30
  526. *
  527. */
  528. public function set_status(Request $request, ProductModel $ProductModel)
  529. {
  530. // 验证参数
  531. $request->scene('set_status')->validate();
  532. $admin_company_id = request('admin_company_id', '0');
  533. $company_id = request('access_token.company_id', '0');
  534. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  535. // 接收数据
  536. $id = request('id', 0);
  537. $status = request('status', 0);
  538. if ($status == 0) {
  539. //获取商品启用数量
  540. // $product_count = $ProductModel->where('status', 0)->count();
  541. //判断是否超过限制
  542. // if ($product_count >= 50) {
  543. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  544. // }
  545. }
  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. $Product = $ProductModel->where($where)->first();
  554. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  555. if (!empty($Product)) {
  556. //继续校验规格是否存在
  557. $product_specs_tring = $Product->product_specs;
  558. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  559. $product_specs = $product_specs ? array_unique($product_specs) : '';
  560. //继续校平台是否存在
  561. $platform_string = $Product->platform;
  562. $platforms = $platform_string ? explode(',', $platform_string) : '';
  563. $platforms = $platforms ? array_unique($platforms) : '';
  564. $platform_data = $ProductModel->platform_index_data();
  565. foreach ($platforms as $platform) {
  566. // 修正:将 orWhereRaw 改为 orWhere,并传入闭包
  567. $product_specs_log = $ProductModel
  568. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  569. ->where([
  570. 'product_brand' => $Product->product_brand,
  571. 'product_name' => $Product->product_name,
  572. 'status' => '0',
  573. 'company_id' => $Product->company_id
  574. ])
  575. ->where('id', '!=', $Product->id)
  576. ->where(function ($query) use ($product_specs) {
  577. $query->where(function ($query_li) use ($product_specs) {
  578. if (!empty($product_specs)) {
  579. foreach ($product_specs as $product_spec) {
  580. if (empty($product_spec)) continue;
  581. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  582. }
  583. } else {
  584. // 如果没有规格条件,添加一个永远为假的条件
  585. $query_li->whereRaw('1 = 0');
  586. }
  587. });
  588. })
  589. // 时间重叠条件:已存在记录的时间段与新增记录的时间段有重叠
  590. ->where(function ($query) use ($Product) {
  591. // 新增记录有完整的时间段
  592. if ($Product->sampling_start_time > 0 && $Product->sampling_end_time > 0) {
  593. $query->orwhere(function ($q) use ($Product ) {
  594. $q->where('sampling_start_time', '<=', $Product->sampling_end_time)
  595. ->where('sampling_end_time', '>=', $Product->sampling_start_time);
  596. })->orwhere(function ($q) use ($Product ) {
  597. $q->where('sampling_start_time', '<=', $Product->sampling_start_time)
  598. ->where('sampling_end_time', '=', 0);
  599. });
  600. }
  601. // 新增记录只有开始时间(没有结束时间,表示从开始时间到无限)
  602. elseif ($Product->sampling_start_time > 0 && $Product->sampling_end_time == 0) {
  603. $query->where(function ($q) use ($Product) {
  604. // 已存在记录的开始时间 <= 新增记录的开始时间
  605. $q->where('sampling_start_time', '<=', $Product->sampling_start_time);
  606. });
  607. }
  608. // 新增记录只有结束时间
  609. elseif ($Product->sampling_start_time == 0 && $Product->sampling_end_time > 0) {
  610. $query->where(function ($q) use ($Product) {
  611. $q->where('sampling_start_time', '<=', $Product->sampling_end_time);
  612. });
  613. }
  614. })
  615. ->first();
  616. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  617. if (!empty($product_specs_log)) {
  618. return json_send(['code' => 'error', 'msg' => $platform_name . '时间范围内存在重复的商品规格启用记录']);
  619. }
  620. }
  621. }
  622. // 执行修改
  623. $result = $ProductModel->changeStatus($Product, $status);
  624. // 提示新增失败
  625. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  626. // 记录行为
  627. $admin_id = request('access_token.uid', 0); //用户ID
  628. $table_name = $ProductModel->getTable();
  629. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  630. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了商品' . $Product->product_name . '状态');
  631. // 告知结果
  632. return json_send(['code' => 'success', 'msg' => '设置成功']);
  633. }
  634. /**
  635. * 删除
  636. * @author 唐远望
  637. * @version 1.0
  638. * @date 2025-12-30
  639. *
  640. */
  641. public function delete(Request $request, ProductModel $ProductModel)
  642. {
  643. // 验证参数
  644. $request->scene('delete')->validate();
  645. $admin_company_id = request('admin_company_id', '0');
  646. $company_id = request('access_token.company_id', '0');
  647. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  648. // 接收数据
  649. $id = request('id', 0);
  650. // 查询用户
  651. $where = ['id' => $id];
  652. if ($is_admin != 1 && $company_id != 0) {
  653. $where['company_id'] = $company_id;
  654. } else {
  655. $where['company_id'] = $admin_company_id;
  656. }
  657. // 执行删除
  658. $Product = $ProductModel->where($where)->first();
  659. if (!$Product) {
  660. return json_send(['code' => 'error', 'msg' => '删除失败,记录不存在']);
  661. }
  662. DB::beginTransaction();
  663. try {
  664. $Product->delete();
  665. // 记录行为
  666. $admin_id = request('access_token.uid', 0); //用户ID
  667. $table_name = $ProductModel->getTable();
  668. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  669. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $Product->toarray(), [], '删除了商品' . $Product->product_name . '信息');
  670. // 告知结果
  671. DB::commit();
  672. return json_send(['code' => 'success', 'msg' => '删除成功']);
  673. // 成功处理...
  674. } catch (\Exception $e) {
  675. DB::rollBack();
  676. // 错误处理...
  677. return json_send(['code' => 'error', 'msg' => '删除失败']);
  678. }
  679. }
  680. }