Product.php 42 KB

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