scene('list')->validate(); // 查询条件 $map = []; $limit = request('limit', config('page_num', 10)); $status = request('status', ''); $start_time = request('start_time', ''); $end_time = request('end_time', ''); $name = request('name', ''); // 时间条件 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 ($name) $map[] = ['name', 'like', "%$name%"]; // 查询数据 $result = $CompanyCategoryModel->query() ->where($map) ->orderByDesc('id') ->paginate($limit); // 分配数据 if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]); // 加载模板 return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]); } /** * 所有分类 * @author 唐远望 * @version 1.0 * @date 2025-12-08 * */ public function all(CompanyCategoryModel $CompanyCategoryModel) { $map = []; $status = request('status', '0'); $start_time = request('start_time', ''); $end_time = request('end_time', ''); $name = request('name', ''); // 时间条件 if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)]; if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)]; // 其他条件 if ($name) $map[] = ['name', 'like', "%$name%"]; if (is_numeric($status)) $map[] = ['status', '=', $status]; $result = $CompanyCategoryModel->query() ->where($map) ->select(['id', 'name']) ->orderByDesc('id') ->get(); // 分配数据 if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]); // 加载模板 return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]); } /** * 详情 * @author 唐远望 * @version 1.0 * @date 2025-12-19 */ public function detail(Request $request, CompanyCategoryModel $CompanyCategoryModel) { $request->scene('detail')->validate(); // 接收参数 $id = request('id', 0); $map = ['id' => $id]; $data = $CompanyCategoryModel->where($map)->first(); if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']); // 加载模板 return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]); } /** * 添加 * @author 唐远望 * @version 1.0 * @date 2025-12-19 * */ public function add(Request $request, CompanyCategoryModel $CompanyCategoryModel) { $request->scene('add')->validate(); // 接收数据 $all_data = request()->all(); $store_scope = request('store_scope', ''); $all_data['store_scope'] = $store_scope; //查询是否存在 $map = ['name' => $all_data['name']]; $data = $CompanyCategoryModel->where($map)->first(); if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']); // 写入数据表 $result = $CompanyCategoryModel->addCompanyCategory($all_data); // 如果操作失败 if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']); // 记录行为 $admin_id = request('access_token.uid', 0); //用户ID $table_name = $CompanyCategoryModel->getTable(); $notes_type = 1; //操作类型,1添加,2修改,3=删除 $this->addAdminHistory('清洗配置-公司分类管理', $admin_id, $table_name, $notes_type,[],$all_data, '新增了分类' . $all_data['name'] . '信息'); // 告知结果 return json_send(['code' => 'success', 'msg' => '新增成功']); } /** * 修改 * @author 唐远望 * @version 1.0 * @date 2025-12-19 * */ public function edit(Request $request, CompanyCategoryModel $CompanyCategoryModel) { $request->scene('edit')->validate(); // 接收参数 $id = request('id', 0); // 接收数据 $all_data = request()->all(); $store_scope = request('store_scope', ''); $all_data['store_scope'] = $store_scope; //查询是否存在 $map = ['name' => $all_data['name']]; $data = $CompanyCategoryModel->where($map)->where('id', '!=', $id)->first(); if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']); // 更新数据表 $where = ['id' => $id]; $CompanyCategory = $CompanyCategoryModel->where($where)->first(); if (!$CompanyCategory) return json_send(['code' => 'error', 'msg' => '记录不存在']); $oldData = $CompanyCategory->toArray(); $result = $CompanyCategoryModel->editCompanyCategory_content($CompanyCategory, $all_data); // 如果操作失败 if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']); // 记录行为 $admin_id = request('access_token.uid', 0); //用户ID $table_name = $CompanyCategoryModel->getTable(); $notes_type = 2; //操作类型,1添加,2修改,3=删除 $this->addAdminHistory('清洗配置-公司分类管理', $admin_id, $table_name, $notes_type,$oldData,$all_data, '修改了分类' .$oldData['name'] . '信息'); // 告知结果 return json_send(['code' => 'success', 'msg' => '修改成功']); } /** * 修改状态 * @author 唐远望 * @version 1.0 * @date 2025-12-19 * */ public function set_status(Request $request, CompanyCategoryModel $CompanyCategoryModel,ViolationStoreModel $ViolationStoreModel) { // 验证参数 $request->scene('set_status')->validate(); // 接收数据 $id = request('id', 0); $status = request('status', 0); //如果是禁用,校验是否被使用 if ($status == 1) { $violation_store_count = $ViolationStoreModel->where(['category_id' => $id])->count(); if ($violation_store_count > 0) { return json_send(['code' => 'error', 'msg' => '该分类下存在公司配置,禁用失败']); } } // 查询用户 $where = ['id' => $id]; $CompanyCategory = $CompanyCategoryModel->where($where)->first(); if (!$CompanyCategory) return json_send(['code' => 'error', 'msg' => '记录不存在']); // 执行修改 $result = $CompanyCategoryModel->changeStatus($CompanyCategory, $status); // 提示新增失败 if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']); // 记录行为 $admin_id = request('access_token.uid', 0); //用户ID $table_name = $CompanyCategoryModel->getTable(); $notes_type = 2; //操作类型,1添加,2修改,3=删除 $this->addAdminHistory('清洗配置-公司分类管理', $admin_id, $table_name, $notes_type,[], ['status' => $status], '修改了分类' .$CompanyCategory->name . '状态'); // 告知结果 return json_send(['code' => 'success', 'msg' => '设置成功']); } /** * 删除 * @author 唐远望 * @version 1.0 * @date 2025-12-19 * */ public function delete(Request $request, CompanyCategoryModel $CompanyCategoryModel,ViolationStoreModel $ViolationStoreModel) { // 验证参数 $request->scene('delete')->validate(); // 接收数据 $id = request('id', 0); $violation_store_count = $ViolationStoreModel->where(['category_id' => $id])->count(); if ($violation_store_count > 0) { return json_send(['code' => 'error', 'msg' => '该分类下存在公司配置,删除失败']); } // 查询用户 $where = ['id' => $id]; // 执行删除 $CompanyCategory = $CompanyCategoryModel->where($where)->first(); if (!$CompanyCategory) return json_send(['code' => 'error', 'msg' => '记录不存在']); $result = $CompanyCategory->delete(); // 提示删除失败 if (!$result) return json_send(['code' => 'error', 'msg' => '删除失败']); // 记录行为 $admin_id = request('access_token.uid', 0); //用户ID $table_name = $CompanyCategoryModel->getTable(); $notes_type = 3; //操作类型,1添加,2修改,3=删除 $this->addAdminHistory('清洗配置-公司分类管理', $admin_id, $table_name, $notes_type, $CompanyCategory->toArray(), [], '删除了分类' .$CompanyCategory->name . '信息'); // 告知结果 return json_send(['code' => 'success', 'msg' => '删除成功']); } }