Product.php 18 KB

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