Product.php 21 KB

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