Product.php 21 KB

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