Product.php 18 KB

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