Product.php 36 KB

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