Product.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. namespace App\Http\Controllers\Manager\Collect;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Manager\Collect\Product as Request;
  5. use App\Models\Manager\Collect\Product as ProductModel;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Models\Manager\Collect\ProductKeyword as ProductKeywordModel;
  8. /**
  9. * 采集配置-商品管理
  10. * @author 唐远望
  11. * @version 1.0
  12. * @date 2025-12-30
  13. */
  14. class Product extends Controller
  15. {
  16. /**
  17. * 列表
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2025-12-30
  21. *
  22. */
  23. public function list(Request $request, ProductModel $ProductModel)
  24. {
  25. $request->scene('list')->validate();
  26. $admin_company_id = request('admin_company_id', '0');
  27. $company_id = request('access_token.company_id', '0');
  28. $is_admin = request('access_token.is_admin', '0');
  29. // 查询条件
  30. $map = [];
  31. $limit = request('limit', config('page_num', 10));
  32. $status = request('status', '');
  33. $start_time = request('start_time', '');
  34. $end_time = request('end_time', '');
  35. $product_name = request('product_name', '');
  36. $platforms = request('platform', '');
  37. $product_brand = request('product_brand', '');
  38. // 时间条件
  39. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  40. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  41. // 其他条件
  42. if (is_numeric($status)) $map[] = ['status', '=', $status];
  43. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  44. if (is_numeric($platforms) || $platforms) {
  45. $ProductModel = $ProductModel
  46. ->where(function ($query) use ($platforms) {
  47. foreach ($platforms as $platform) {
  48. $query->orWhereRaw("FIND_IN_SET(?, platform)", [$platform]);
  49. }
  50. });
  51. }
  52. if ($product_brand) $map[] = ['product_brand', 'like', "%$product_brand%"];
  53. // 权限判断
  54. if ($is_admin != 1 && $company_id != 0) {
  55. $map[] = ['company_id', '=', $company_id];
  56. } else {
  57. $map[] = ['company_id', '=', $admin_company_id];
  58. }
  59. // 查询数据
  60. $result = $ProductModel
  61. ->where($map)
  62. ->with(['product_keyword'])
  63. ->orderByDesc('id')
  64. ->paginate($limit)->toarray();
  65. // 分配数据
  66. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  67. if (isset($result['data']) && count($result['data']) > 0) {
  68. foreach ($result['data'] as $key => $value) {
  69. $result['data'][$key]['platform'] = isset($value['platform']) ? explode(',', $value['platform']) : '';
  70. $product_specs = isset($value['product_specs']) ? explode(',', $value['product_specs']) : '';
  71. //移除空数组
  72. $result['data'][$key]['product_specs'] = $product_specs ? array_filter($product_specs) : '';
  73. }
  74. }
  75. // 加载模板
  76. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  77. }
  78. /**
  79. * 商品名称规格列表
  80. * @author 唐远望
  81. * @version 1.0
  82. * @date 2026-03-25
  83. *
  84. */
  85. public function product_name_specs_list(Request $request, ProductModel $ProductModel, ProductKeywordModel $ProductKeywordModel)
  86. {
  87. $request->scene('product_name_specs_list')->validate();
  88. $admin_company_id = request('admin_company_id', '0');
  89. $company_id = request('access_token.company_id', '0');
  90. $is_admin = request('access_token.is_admin', '0');
  91. // 查询条件
  92. $map = [];
  93. $map2 = [];
  94. $map3 = [];
  95. $start_time = request('start_time', '');
  96. $end_time = request('end_time', '');
  97. $product_name = request('product_name', '');
  98. $product_brand = request('product_brand', '');
  99. // 时间条件
  100. if ($start_time) {
  101. $map[] = ['insert_time', '>=', strtotime($start_time)];
  102. $map2[] = ['insert_time', '>=', strtotime($start_time)];
  103. $map3[] = ['insert_time', '>=', strtotime($start_time)];
  104. }
  105. if ($end_time) {
  106. $map[] = ['insert_time', '<=', strtotime($end_time)];
  107. $map2[] = ['insert_time', '<=', strtotime($end_time)];
  108. $map3[] = ['insert_time', '<=', strtotime($end_time)];
  109. }
  110. // 其他条件
  111. $map[] = ['status', '=', 0];
  112. if ($product_name) $map2[] = ['product_name', 'like', "%$product_name%"];
  113. if ($product_brand) $map[] = ['product_brand', 'like', "%$product_brand%"];
  114. // 权限判断
  115. if ($is_admin != 1 && $company_id != 0) {
  116. $map[] = ['company_id', '=', $company_id];
  117. $map2[] = ['company_id', '=', $company_id];
  118. $map3[] = ['company_id', '=', $company_id];
  119. } else {
  120. $map[] = ['company_id', '=', $admin_company_id];
  121. $map2[] = ['company_id', '=', $admin_company_id];
  122. $map3[] = ['company_id', '=', $admin_company_id];
  123. }
  124. // 查询数据
  125. $result = $ProductModel->query()
  126. ->where($map)
  127. ->where([['product_brand', '!=', ''], ['product_brand', '!=', null]])
  128. ->select([DB::raw('MAX(id) as id'), 'product_brand'])
  129. ->groupBy(['product_brand'])
  130. ->orderByDesc('id')->get()->toarray();
  131. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  132. foreach ($result as $key => $value) {
  133. $product_list = $ProductModel->query()
  134. ->where($map2)
  135. ->where([['product_name', '!=', ''], ['product_name', '!=', null]])
  136. ->where(['product_brand' => $value['product_brand']])
  137. ->select([DB::raw('MAX(id) as id'), 'product_name'])
  138. ->groupBy(['product_name'])
  139. ->orderByDesc('id')->get()->toarray();
  140. if (!empty($product_list)) {
  141. foreach ($product_list as $key2 => $value2) {
  142. $product_keyword = $ProductKeywordModel->query()
  143. ->where(['collect_product_id' => $value2['id']])
  144. ->select('keyword')
  145. ->groupBy(['keyword'])
  146. ->get()->toarray();
  147. $product_list[$key2]['product_keyword'] = $product_keyword ? array_column($product_keyword, 'keyword') : [];
  148. $product_specs = $ProductModel->query()
  149. ->where($map3)
  150. ->where([['product_specs', '!=', ''], ['product_specs', '!=', null]])
  151. ->where(['product_name' => $value2['product_name']])
  152. ->where(['product_brand' => $value['product_brand']])
  153. ->select([DB::raw('MAX(id) as id'), 'product_specs'])
  154. ->groupBy(['product_specs'])
  155. ->orderByDesc('id')->get()->toarray();
  156. $product_specs_tring = [];
  157. if (count($product_specs) > 0) {
  158. foreach ($product_specs as $key3 => $value3) {
  159. $product_specs_item = isset($value3['product_specs']) ? explode(',', $value3['product_specs']) : '';
  160. //合并数组
  161. if (!empty($product_specs_item)) {
  162. $product_specs_tring = array_merge($product_specs_tring, $product_specs_item);
  163. }
  164. }
  165. //去重
  166. $product_specs_tring = array_unique($product_specs_tring);
  167. }
  168. //移除空数组
  169. $product_list[$key2]['product_specs'] = $product_specs_tring ? array_filter($product_specs_tring) : '';
  170. }
  171. }
  172. $result[$key]['product_list'] = !empty($product_list) ? $product_list : [];
  173. }
  174. // 加载模板
  175. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  176. }
  177. /**
  178. * 详情
  179. * @author 唐远望
  180. * @version 1.0
  181. * @date 2025-12-30
  182. */
  183. public function detail(Request $request, ProductModel $ProductModel)
  184. {
  185. $request->scene('detail')->validate();
  186. $admin_company_id = request('admin_company_id', '0');
  187. $company_id = request('access_token.company_id', '0');
  188. $is_admin = request('access_token.is_admin', '0');
  189. // 接收参数
  190. $id = request('id', 0);
  191. $map = ['id' => $id];
  192. // 权限判断
  193. if ($is_admin != 1 && $company_id != 0) {
  194. $map['company_id'] = $company_id;
  195. } else {
  196. $map['company_id'] = $admin_company_id;
  197. }
  198. $data = $ProductModel->where($map)->with(['product_keyword'])->first();
  199. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  200. $data->platform = isset($data->platform) ? explode(',', $data->platform) : '';
  201. $product_specs = isset($data->product_specs) ? explode(',', $data->product_specs) : '';
  202. //移除空数组
  203. $data->product_specs = $product_specs ? array_filter($product_specs) : '';
  204. // 加载模板
  205. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  206. }
  207. /**
  208. * 添加
  209. * @author 唐远望
  210. * @version 1.0
  211. * @date 2025-12-30
  212. *
  213. */
  214. public function add(Request $request, ProductModel $ProductModel)
  215. {
  216. $request->scene('add')->validate();
  217. $admin_company_id = request('admin_company_id', '0');
  218. $company_id = request('access_token.company_id', '0');
  219. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  220. //商品启用数量
  221. // $product_count = $ProductModel->where('status', 0)->count();
  222. //判断是否超过限制
  223. // if ($product_count >= 50) {
  224. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  225. // }
  226. // 接收数据
  227. $all_data = request()->all();
  228. $product_brand = request('product_brand', '');
  229. $product_keyword = request('product_keyword', '');
  230. $all_data['product_brand'] = $product_brand;
  231. $all_data['product_keyword'] = $product_keyword;
  232. if (trim($product_keyword) != '') {
  233. $product_keyword_count = count(explode(',', $product_keyword));
  234. if ($product_keyword_count > 5) {
  235. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  236. }
  237. }
  238. //采集信息配置
  239. $enable_full_quantity = request('enable_full_quantity', 1); //全量,0启用,1禁用
  240. $all_data['enable_full_quantity'] = $enable_full_quantity;
  241. if ($enable_full_quantity == 1 && !isset($all_data['product_specs'])) {
  242. return json_send(['code' => 'error', 'msg' => '非全量采集时,商品规格不能为空']);
  243. }
  244. $minimum_order_quantity = request('minimum_order_quantity', 1);
  245. $sampling_cycle = request('sampling_cycle', '0');
  246. $sampling_start_time = request('sampling_start_time', '');
  247. $sampling_end_time = request('sampling_end_time', '');
  248. $all_data['sampling_cycle'] = $sampling_cycle;
  249. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  250. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  251. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  252. 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' => '采集开始时间必须大于当前时间']);
  253. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  254. //查询是否存在
  255. $map = ['product_brand' => $product_brand, 'product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
  256. if ($is_admin != 1 && $company_id != 0) {
  257. $map['company_id'] = $company_id;
  258. $all_data['company_id'] = $company_id;
  259. } else {
  260. $map['company_id'] = $admin_company_id;
  261. $all_data['company_id'] = $admin_company_id;
  262. }
  263. $data = $ProductModel->where($map)->first();
  264. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  265. // 写入数据表
  266. $result = $ProductModel->addProduct($all_data);
  267. // 如果操作失败
  268. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  269. // 记录行为
  270. $admin_id = request('access_token.uid', 0); //用户ID
  271. $table_name = $ProductModel->getTable();
  272. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  273. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了商品' . $all_data['product_name'] . '信息');
  274. // 告知结果
  275. return json_send(['code' => 'success', 'msg' => '新增成功']);
  276. }
  277. /**
  278. * 修改
  279. * @author 唐远望
  280. * @version 1.0
  281. * @date 2025-12-30
  282. *
  283. */
  284. public function edit(Request $request, ProductModel $ProductModel)
  285. {
  286. $request->scene('edit')->validate();
  287. $admin_company_id = request('admin_company_id', '0');
  288. $company_id = request('access_token.company_id', '0');
  289. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  290. // 接收参数
  291. $id = request('id', 0);
  292. // 接收数据
  293. $all_data = request()->all();
  294. $product_brand = request('product_brand', '');
  295. $product_keyword = request('product_keyword', '');
  296. $all_data['product_brand'] = $product_brand;
  297. $all_data['product_keyword'] = $product_keyword;
  298. if (trim($product_keyword) != '') {
  299. $product_keyword_count = count(explode(',', $product_keyword));
  300. if ($product_keyword_count > 5) {
  301. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  302. }
  303. }
  304. //采集信息配置
  305. $enable_full_quantity = request('enable_full_quantity', 1); //全量,0启用,1禁用
  306. $all_data['enable_full_quantity'] = $enable_full_quantity;
  307. if ($enable_full_quantity == 1 && !isset($all_data['product_specs'])) {
  308. return json_send(['code' => 'error', 'msg' => '非全量采集时,商品规格不能为空']);
  309. }
  310. $minimum_order_quantity = request('minimum_order_quantity', 1);
  311. $sampling_cycle = request('sampling_cycle', '0');
  312. $sampling_start_time = request('sampling_start_time', '');
  313. $sampling_end_time = request('sampling_end_time', '');
  314. $all_data['sampling_cycle'] = $sampling_cycle;
  315. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  316. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  317. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  318. 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' => '采集开始时间必须大于当前时间']);
  319. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  320. //查询是否存在
  321. $map = ['product_brand' => $product_brand, 'product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
  322. if ($is_admin != 1 && $company_id != 0) {
  323. $map['company_id'] = $company_id;
  324. } else {
  325. $map['company_id'] = $admin_company_id;
  326. }
  327. $data = $ProductModel->where($map)->where('id', '!=', $id)->first();
  328. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  329. // 更新数据表
  330. $where = ['id' => $id];
  331. // 权限判断
  332. if ($is_admin != 1 && $company_id != 0) {
  333. $where['company_id'] = $company_id;
  334. } else {
  335. $where['company_id'] = $admin_company_id;
  336. }
  337. $Product = $ProductModel->where($where)->first();
  338. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  339. $oldData = $Product->toarray();
  340. $result = $ProductModel->editProduct_content($Product, $all_data);
  341. // 如果操作失败
  342. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  343. // 记录行为
  344. $admin_id = request('access_token.uid', 0); //用户ID
  345. $table_name = $ProductModel->getTable();
  346. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  347. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了商品' . $oldData['product_name'] . '信息');
  348. // 告知结果
  349. return json_send(['code' => 'success', 'msg' => '修改成功']);
  350. }
  351. /**
  352. * 修改状态
  353. * @author 唐远望
  354. * @version 1.0
  355. * @date 2025-12-30
  356. *
  357. */
  358. public function set_status(Request $request, ProductModel $ProductModel)
  359. {
  360. // 验证参数
  361. $request->scene('set_status')->validate();
  362. $admin_company_id = request('admin_company_id', '0');
  363. $company_id = request('access_token.company_id', '0');
  364. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  365. // 接收数据
  366. $id = request('id', 0);
  367. $status = request('status', 0);
  368. if ($status == 0) {
  369. //获取商品启用数量
  370. // $product_count = $ProductModel->where('status', 0)->count();
  371. //判断是否超过限制
  372. // if ($product_count >= 50) {
  373. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  374. // }
  375. }
  376. // 查询用户
  377. $where = ['id' => $id];
  378. if ($is_admin != 1 && $company_id != 0) {
  379. $where['company_id'] = $company_id;
  380. } else {
  381. $where['company_id'] = $admin_company_id;
  382. }
  383. $Product = $ProductModel->where($where)->first();
  384. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  385. // 执行修改
  386. $result = $ProductModel->changeStatus($Product, $status);
  387. // 提示新增失败
  388. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  389. // 记录行为
  390. $admin_id = request('access_token.uid', 0); //用户ID
  391. $table_name = $ProductModel->getTable();
  392. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  393. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了商品' . $Product->product_name . '状态');
  394. // 告知结果
  395. return json_send(['code' => 'success', 'msg' => '设置成功']);
  396. }
  397. /**
  398. * 删除
  399. * @author 唐远望
  400. * @version 1.0
  401. * @date 2025-12-30
  402. *
  403. */
  404. public function delete(Request $request, ProductModel $ProductModel)
  405. {
  406. // 验证参数
  407. $request->scene('delete')->validate();
  408. $admin_company_id = request('admin_company_id', '0');
  409. $company_id = request('access_token.company_id', '0');
  410. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  411. // 接收数据
  412. $id = request('id', 0);
  413. // 查询用户
  414. $where = ['id' => $id];
  415. if ($is_admin != 1 && $company_id != 0) {
  416. $where['company_id'] = $company_id;
  417. } else {
  418. $where['company_id'] = $admin_company_id;
  419. }
  420. // 执行删除
  421. $Product = $ProductModel->where($where)->first();
  422. if (!$Product) {
  423. return json_send(['code' => 'error', 'msg' => '删除失败,记录不存在']);
  424. }
  425. DB::beginTransaction();
  426. try {
  427. $Product->delete();
  428. // 记录行为
  429. $admin_id = request('access_token.uid', 0); //用户ID
  430. $table_name = $ProductModel->getTable();
  431. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  432. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $Product->toarray(), [], '删除了商品' . $Product->product_name . '信息');
  433. // 告知结果
  434. DB::commit();
  435. return json_send(['code' => 'success', 'msg' => '删除成功']);
  436. // 成功处理...
  437. } catch (\Exception $e) {
  438. DB::rollBack();
  439. // 错误处理...
  440. return json_send(['code' => 'error', 'msg' => '删除失败']);
  441. }
  442. }
  443. }