scene('list')->validate(); // 查询条件 $map = []; $limit = request('limit', config('page_num', 10)); $status = request('status', ''); $start_time = request('start_time', ''); $end_time = request('end_time', ''); $product_name = request('product_name', ''); $platform = request('platform', ''); // 时间条件 if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)]; if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)]; // 其他条件 if (is_numeric($status)) $map[] = ['status', '=', $status]; if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"]; if (is_numeric($platform) || $platform) $map[] = ['platform', 'like', "%$platform%"]; // 查询数据 $result = $ProductModel->query() ->where($map) ->orderByDesc('id') ->paginate($limit)->toarray(); // 分配数据 if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]); if (isset($result['data']) && count($result['data']) > 0) { foreach ($result['data'] as $key => $value) { $result['data'][$key]['platform'] = isset($value['platform']) ? explode(',', $value['platform']) : ''; } } // 加载模板 return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]); } /** * 详情 * @author 唐远望 * @version 1.0 * @date 2025-12-30 */ public function detail(Request $request, ProductModel $ProductModel) { $request->scene('detail')->validate(); // 接收参数 $id = request('id', 0); $map = ['id' => $id]; $data = $ProductModel->where($map)->first(); if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']); $data->platform = isset($data->platform) ? explode(',', $data->platform) : ''; // 加载模板 return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]); } /** * 添加 * @author 唐远望 * @version 1.0 * @date 2025-12-30 * */ public function add(Request $request, ProductModel $ProductModel) { $request->scene('add')->validate(); //商品启用数量 $product_count = $ProductModel->where('status', 0)->count(); //判断是否超过限制 if ($product_count >= 50) { return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']); } // 接收数据 $all_data = request()->all(); //采集信息配置 $minimum_order_quantity = request('minimum_order_quantity', 0); $sampling_cycle = request('sampling_cycle', '0'); $sampling_start_time = request('sampling_start_time', ''); $sampling_end_time = request('sampling_end_time', ''); $all_data['sampling_cycle'] = $sampling_cycle; $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0'; $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0'; $all_data['minimum_order_quantity'] = $minimum_order_quantity; //查询是否存在 $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']]; $data = $ProductModel->where($map)->first(); if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']); // 写入数据表 $result = $ProductModel->addProduct($all_data); // 如果操作失败 if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']); // 记录行为 $admin_id = request('access_token.uid', 0); //用户ID $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否 $table_name = $ProductModel->getTable(); $notes_type = 1; //操作类型,1添加,2修改,3=删除 $this->addAdminHistory('采集配置-商品管理', $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了商品' . $all_data['product_name'] . '信息'); // 告知结果 return json_send(['code' => 'success', 'msg' => '新增成功']); } /** * 修改 * @author 唐远望 * @version 1.0 * @date 2025-12-30 * */ public function edit(Request $request, ProductModel $ProductModel) { $request->scene('edit')->validate(); // 接收参数 $id = request('id', 0); // 接收数据 $all_data = request()->all(); //采集信息配置 $minimum_order_quantity = request('minimum_order_quantity', 0); $sampling_cycle = request('sampling_cycle', '0'); $sampling_start_time = request('sampling_start_time', ''); $sampling_end_time = request('sampling_end_time', ''); $all_data['sampling_cycle'] = $sampling_cycle; $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0'; $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0'; $all_data['minimum_order_quantity'] = $minimum_order_quantity; //查询是否存在 $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']]; $data = $ProductModel->where($map)->where('id', '!=', $id)->first(); if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']); // 更新数据表 $where = ['id' => $id]; $Product = $ProductModel->where($where)->first(); if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']); $oldData = $Product->toarray(); $result = $ProductModel->editProduct_content($Product, $all_data); // 如果操作失败 if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']); // 记录行为 $admin_id = request('access_token.uid', 0); //用户ID $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否 $table_name = $ProductModel->getTable(); $notes_type = 2; //操作类型,1添加,2修改,3=删除 $this->addAdminHistory('采集配置-商品管理', $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了商品' . $oldData['product_name'] . '信息'); // 告知结果 return json_send(['code' => 'success', 'msg' => '修改成功']); } /** * 修改状态 * @author 唐远望 * @version 1.0 * @date 2025-12-30 * */ public function set_status(Request $request, ProductModel $ProductModel) { // 验证参数 $request->scene('set_status')->validate(); // 接收数据 $id = request('id', 0); $status = request('status', 0); if ($status == 0) { //获取商品启用数量 $product_count = $ProductModel->where('status', 0)->count(); //判断是否超过限制 // if ($product_count >= 50) { // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']); // } } // 查询用户 $where = ['id' => $id]; $Product = $ProductModel->where($where)->first(); if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']); // 执行修改 $result = $ProductModel->changeStatus($Product, $status); // 提示新增失败 if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']); // 记录行为 $admin_id = request('access_token.uid', 0); //用户ID $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否 $table_name = $ProductModel->getTable(); $notes_type = 2; //操作类型,1添加,2修改,3=删除 $this->addAdminHistory('采集配置-商品管理', $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了商品' . $Product->product_name . '状态'); // 告知结果 return json_send(['code' => 'success', 'msg' => '设置成功']); } /** * 删除 * @author 唐远望 * @version 1.0 * @date 2025-12-30 * */ public function delete(Request $request, ProductModel $ProductModel) { // 验证参数 $request->scene('delete')->validate(); // 接收数据 $id = request('id', 0); // 查询用户 $where = ['id' => $id]; // 执行删除 $Product = $ProductModel->where($where)->first(); if (!$Product) { return json_send(['code' => 'error', 'msg' => '删除失败,记录不存在']); } DB::beginTransaction(); try { $Product->delete(); // 记录行为 $admin_id = request('access_token.uid', 0); //用户ID $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否 $table_name = $ProductModel->getTable(); $notes_type = 3; //操作类型,1添加,2修改,3=删除 $this->addAdminHistory('采集配置-商品管理', $admin_id, $is_admin, $table_name, $notes_type, $Product->toarray(), [], '删除了商品' . $Product->product_name . '信息'); // 告知结果 DB::commit(); return json_send(['code' => 'success', 'msg' => '删除成功']); // 成功处理... } catch (\Exception $e) { DB::rollBack(); // 错误处理... return json_send(['code' => 'error', 'msg' => '删除失败']); } } }