Product.php 17 KB

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