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" ) // GetOrganizationByID 根据ID获取组织 func GetOrganizationByID(orgGuid string) (*hr.OrganizationModel, *process.Process) { // 验证组织ID guid, err := unique.FromString(orgGuid) if err != nil { return nil, process.FailError(types.InvalidParamError, err) } // 查询组织 organization := &hr.OrganizationModel{} r := dblib.DBIns.DB.Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", guid).First(organization) if r.Error != nil { return nil, process.FailError(types.OrgNotFoundError, r.Error) } return organization, process.Success(200) } // UpdateOrganization 更新组织 func UpdateOrganization(orgGuid string, params CreateOrganizationRequest) (*hr.OrganizationModel, *process.Process) { // 获取组织 organization, proc := GetOrganizationByID(orgGuid) if proc.IsError() { return nil, proc } // 解析父组织ID if params.ParentGuid != "" { parentGuid, err := unique.FromString(params.ParentGuid) if err != nil { return nil, process.FailError(types.InvalidParamError, err) } // 验证父组织是否存在 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("父组织不存在")) } // 检查是否会形成循环引用 if isCircularReference(organization.RecordGuid.String(), parentGuid.String()) { return nil, process.FailError(types.CircularReferenceError, errors.New("不能将组织的子组织设为其父组织,会形成循环引用")) } organization.ParentGuid = parentGuid } } // 更新组织信息 if params.OrganizationName != "" { organization.OrganizationName = params.OrganizationName } if params.OrganizationType > 0 { organization.RecordType = int16(params.OrganizationType) } // 保存更新 r := dblib.DBIns.DB.Save(organization) if r.Error != nil { return nil, process.FailError(types.UpdateOrganizationError, r.Error) } return organization, process.Success(200) } // DeleteOrganization 删除组织(逻辑删除) func DeleteOrganization(orgGuid string) *process.Process { // 获取组织 org, proc := GetOrganizationByID(orgGuid) if proc.IsError() { return proc } // 检查是否有子组织 var childCount int64 r := dblib.DBIns.DB.Model(&hr.OrganizationModel{}).Where("ParentGuid = ? AND (RecordStatus & 524288) = 0", org.RecordGuid).Count(&childCount) if r.Error != nil { return process.FailError(types.QueryOrganizationError, r.Error) } if childCount > 0 { return process.FailError(types.DeleteOrganizationError, errors.New("该组织下有子组织,不能删除")) } // 使用原生SQL执行删除操作(设置删除标记) sql := "UPDATE " + hr.OrganizationTable + " SET RecordStatus = RecordStatus | 524288 WHERE RecordGuid = ?" r = dblib.DBIns.DB.Exec(sql, org.RecordGuid) if r.Error != nil { return process.FailError(types.DeleteOrganizationError, r.Error) } return process.Success(200) } // GetOrganizationTree 获取组织树结构 func GetOrganizationTree(rootOrgGuid string) (map[string]interface{}, *process.Process) { // 验证根组织ID var rootGuid unique.UUID var err error if rootOrgGuid != "" { rootGuid, err = unique.FromString(rootOrgGuid) if err != nil { return nil, process.FailError(types.InvalidParamError, err) } } else { // 如果未指定根组织,则查询顶级组织(ParentGuid为NilUUID) rootGuid = unique.NilUUID } // 查询根组织 var rootOrgs []*hr.OrganizationModel var r error if rootGuid == unique.NilUUID { // 查询所有顶级组织 result := dblib.DBIns.DB.Where("ParentGuid = ? AND (RecordStatus & 524288) = 0", rootGuid).Find(&rootOrgs) r = result.Error } else { // 查询指定的组织 rootOrg := &hr.OrganizationModel{} result := dblib.DBIns.DB.Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", rootGuid).First(rootOrg) r = result.Error if r == nil { rootOrgs = []*hr.OrganizationModel{rootOrg} } } if r != nil { return nil, process.FailError(types.QueryOrganizationError, r) } // 构建组织树 result := make(map[string]interface{}) result["organizations"] = buildOrganizationTree(rootOrgs) return result, process.Success(200) } // buildOrganizationTree 构建组织树结构 func buildOrganizationTree(orgs []*hr.OrganizationModel) []map[string]interface{} { result := make([]map[string]interface{}, 0, len(orgs)) for _, org := range orgs { // 查询子组织 var children []*hr.OrganizationModel dblib.DBIns.DB.Where("ParentGuid = ? AND (RecordStatus & 524288) = 0", org.RecordGuid).Find(&children) // 构建组织节点 node := map[string]interface{}{ "orgGuid": org.RecordGuid.String(), "orgId": org.DepartmentID, "name": org.OrganizationName, "type": org.RecordType, "parentGuid": org.ParentGuid.String(), } // 递归构建子组织 if len(children) > 0 { node["children"] = buildOrganizationTree(children) } result = append(result, node) } return result } // isCircularReference 检查是否会形成循环引用 func isCircularReference(orgGuid, parentGuid string) bool { // 如果尝试将组织的父级设为自身,则为循环引用 if orgGuid == parentGuid { return true } // 解析父组织ID parent, err := unique.FromString(parentGuid) if err != nil { return false } // 查询父组织的父级 parentOrg := &hr.OrganizationModel{} r := dblib.DBIns.DB.Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", parent).First(parentOrg) if r.Error != nil || parentOrg.ParentGuid == unique.NilUUID { return false } // 递归检查父级的父级是否最终指向原组织 return isCircularReference(orgGuid, parentOrg.ParentGuid.String()) }