package hr import ( "WiiCITMS/models/hr" "WiiGenerates/WiiCITMS/generates/v1/go/types" "WiiGoLibrary/apply/middle/process/v1" "WiiGoLibrary/framework/db/v1/utils/mssql/unique" "WiiGoLibrary/framework/hub/v1/dblib" "errors" ) // QueryOrganizationsRequest 查询组织请求参数 type QueryOrganizationsRequest struct { OrganizationName string `json:"organizationName"` // 组织名称,用于模糊查询 ParentGuid string `json:"parentGuid"` // 父组织ID,可选 IncludeChildren bool `json:"includeChildren"` // 是否包含子组织 Limit int `json:"limit"` // 分页参数,每页数量 Offset int `json:"offset"` // 分页参数,偏移量 } // CreateOrganizationRequest 创建组织请求参数 type CreateOrganizationRequest struct { OrganizationName string `json:"organizationName"` // 组织名称 ParentGuid string `json:"parentGuid"` // 父组织ID OrganizationType int `json:"organizationType"` // 组织类型:1-公司,2-部门,3-团队 Description string `json:"description"` // 组织描述 } // QueryOrganizations 查询组织列表 func QueryOrganizations(params QueryOrganizationsRequest) ([]*hr.OrganizationModel, *process.Process) { result := make([]*hr.OrganizationModel, 0) // 构建查询条件 db := dblib.DBIns.DB.Model(&hr.OrganizationModel{}).Where("(RecordStatus & 524288) = 0") // 按组织名称筛选 if params.OrganizationName != "" { db = db.Where("OrganizationName LIKE ?", "%"+params.OrganizationName+"%") } // 按父组织ID筛选 if params.ParentGuid != "" { parentGuid, err := unique.FromString(params.ParentGuid) if err == nil { db = db.Where("ParentGuid = ?", parentGuid) } } // 分页查询 limit := params.Limit if limit <= 0 { limit = 20 // 默认每页20条 } offset := params.Offset if offset < 0 { offset = 0 } // 执行查询 r := db.Limit(limit).Offset(offset).Find(&result) if r.Error != nil { return result, process.FailError(types.QueryOrganizationError, r.Error) } // 如果需要包含子组织,则递归查询 if params.IncludeChildren && params.ParentGuid != "" { for _, org := range result { // 查询子组织 children, proc := getChildOrganizations(org.RecordGuid.String()) if !proc.IsError() { result = append(result, children...) } } } return result, process.Success(200) } // getChildOrganizations 递归获取子组织 func getChildOrganizations(parentGuid string) ([]*hr.OrganizationModel, *process.Process) { result := make([]*hr.OrganizationModel, 0) // 验证父组织ID parent, err := unique.FromString(parentGuid) if err != nil { return nil, process.FailError(types.InvalidParamError, err) } // 查询直接子组织 r := dblib.DBIns.DB.Where("ParentGuid = ? AND (RecordStatus & 524288) = 0", parent).Find(&result) if r.Error != nil { return nil, process.FailError(types.QueryOrganizationError, r.Error) } // 递归查询所有子组织 allChildren := make([]*hr.OrganizationModel, 0) allChildren = append(allChildren, result...) for _, org := range result { children, proc := getChildOrganizations(org.RecordGuid.String()) if !proc.IsError() { allChildren = append(allChildren, children...) } } return allChildren, process.Success(200) } // CreateOrganization 创建组织 func CreateOrganization(params CreateOrganizationRequest) (*hr.OrganizationModel, *process.Process) { // 解析父组织ID parentGuid, err := unique.FromString(params.ParentGuid) if err != nil { parentGuid = unique.NilUUID } // 如果指定了父组织,验证父组织是否存在 if parentGuid != unique.NilUUID { parentOrg := &hr.OrganizationModel{} r := dblib.DBIns.DB.Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", parentGuid).First(parentOrg) if r.Error != nil { return nil, process.FailError(types.ParentOrgNotFoundError, errors.New("父组织不存在")) } } // 创建组织实例 organization := hr.OrganizationInstance() organization.OrganizationName = params.OrganizationName organization.ParentGuid = parentGuid organization.RecordType = int16(params.OrganizationType) // 保存到数据库 r := dblib.DBIns.DB.Create(organization) if r.Error != nil { return organization, process.FailError(types.CreateOrganizationError, r.Error) } return organization, process.Success(200) }