{"record":{"id":"ad470b85eec536d2","repo":"flipped-aurora/gin-vue-admin","slug":"error-ad470b","errorCode":null,"errorMessage":"该部门下存在用户,不允许删除","messagePattern":"该部门下存在用户,不允许删除","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_department.go","lineNumber":138,"sourceCode":"\t\t\"leader_id\": dept.LeaderId,\n\t\t\"status\":    dept.Status,\n\t}).Error\n}\n\n// DeleteSysDepartment 删除部门, 存在子部门或已有用户归属时禁止删除\nfunc (s *SysDepartmentService) DeleteSysDepartment(ctx context.Context, id uint) (err error) {\n\tif id == 0 {\n\t\treturn errors.New(\"部门ID不能为空\")\n\t}\n\tif !errors.Is(global.GVA_DB.WithContext(ctx).Where(\"parent_id = ?\", id).First(&system.SysDepartment{}).Error, gorm.ErrRecordNotFound) {\n\t\treturn errors.New(\"存在子部门,不允许删除\")\n\t}\n\tvar userCount int64\n\tif err = global.GVA_DB.WithContext(ctx).Model(&system.SysUser{}).Where(\"dept_id = ?\", id).Count(&userCount).Error; err != nil {\n\t\treturn err\n\t}\n\tif userCount > 0 {\n\t\treturn errors.New(\"该部门下存在用户,不允许删除\")\n\t}\n\tvar joinCount int64\n\tif err = global.GVA_DB.WithContext(ctx).Model(&system.SysUserDepartment{}).Where(\"sys_department_id = ?\", id).Count(&joinCount).Error; err != nil {\n\t\treturn err\n\t}\n\tif joinCount > 0 {\n\t\treturn errors.New(\"该部门下存在用户,不允许删除\")\n\t}\n\treturn global.GVA_DB.WithContext(ctx).Delete(&system.SysDepartment{}, id).Error\n}\n\n// GetSysDepartment 获取单个部门\nfunc (s *SysDepartmentService) GetSysDepartment(ctx context.Context, id uint) (dept system.SysDepartment, err error) {\n\terr = global.GVA_DB.WithContext(ctx).Preload(\"Leader\").First(&dept, id).Error\n\treturn\n}\n\n// GetSysDepartmentTree 获取部门树; 带名称搜索时平铺返回匹配项, 否则返回整棵树","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_department.go#L120-L156","documentation":"DeleteSysDepartment refuses to delete a department that still has users whose primary dept_id points at it. After the child-department check, it counts sys_user rows with dept_id = id and blocks deletion when userCount > 0.","triggerScenarios":"Deleting a department where one or more sys_user rows have dept_id equal to that department ID.","commonSituations":"Users were assigned to the department as their main department; bulk user import set dept_id; admin tries to remove an active team department.","solutions":["Reassign those users to another department (update their dept_id, e.g. via user edit or SetDepartmentUsers) before deleting","Use the department member management page to move users out first","Delete or reassign the users if they are no longer needed"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var userCount int64\nglobal.GVA_DB.Model(&system.SysUser{}).Where(\"dept_id = ?\", id).Count(&userCount)\nif userCount > 0 {\n    return fmt.Errorf(\"%d users still assigned to this department\", userCount)\n}","typeGuard":null,"tryCatchPattern":"if err := deptService.DeleteSysDepartment(ctx, id); err != nil {\n    if err.Error() == \"该部门下存在用户,不允许删除\" {\n        // route users to the member-reassignment flow\n    }\n    return err\n}","preventionTips":["Reassign users' dept_id before deleting a department","Show affected-user count in the delete confirmation dialog","Use the member management page to move users out first"],"tags":["department","user","referential-integrity","go"],"backgroundTag":"record-has-dependents","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}