408 lines
11 KiB
Go
408 lines
11 KiB
Go
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"
|
||
)
|
||
|
||
// AssignPositionToOrgRequest 为组织分配岗位的请求参数
|
||
type AssignPositionToOrgRequest struct {
|
||
OrganizationGuid string `json:"organizationGuid"` // 组织ID
|
||
PositionGuid string `json:"positionGuid"` // 岗位ID
|
||
HeadCount int `json:"headCount"` // 岗位编制人数
|
||
}
|
||
|
||
// QueryOrgPositionRequest 查询组织岗位关系的请求参数
|
||
type QueryOrgPositionRequest struct {
|
||
OrganizationGuid string `json:"organizationGuid"` // 组织ID
|
||
PositionGuid string `json:"positionGuid"` // 岗位ID
|
||
Limit int `json:"limit"` // 分页参数,每页数量
|
||
Offset int `json:"offset"` // 分页参数,偏移量
|
||
}
|
||
|
||
// OrgPositionDTO 组织岗位关系DTO
|
||
type OrgPositionDTO struct {
|
||
RelationGuid string `json:"relationGuid"` // 关系记录ID
|
||
OrganizationGuid string `json:"organizationGuid"` // 组织ID
|
||
OrganizationName string `json:"organizationName"` // 组织名称
|
||
OrganizationType int `json:"organizationType"` // 组织类型
|
||
PositionGuid string `json:"positionGuid"` // 岗位ID
|
||
PositionName string `json:"positionName"` // 岗位名称
|
||
PositionCode string `json:"positionCode"` // 岗位代码
|
||
HeadCount int `json:"headCount"` // 岗位编制人数
|
||
Level int `json:"level"` // 岗位级别
|
||
CategoryID int `json:"categoryId"` // 岗位类别ID
|
||
CategoryName string `json:"categoryName"` // 岗位类别名称
|
||
IsManagement bool `json:"isManagement"` // 是否管理岗位
|
||
}
|
||
|
||
// AssignPositionToOrg 为组织分配岗位
|
||
func AssignPositionToOrg(params AssignPositionToOrgRequest) (*hr.OrgPositionRelModel, *process.Process) {
|
||
// 验证组织是否存在
|
||
org, proc := GetOrganizationByID(params.OrganizationGuid)
|
||
if proc.IsError() {
|
||
return nil, proc
|
||
}
|
||
|
||
// 验证岗位是否存在
|
||
position, proc := GetPositionByID(params.PositionGuid)
|
||
if proc.IsError() {
|
||
return nil, proc
|
||
}
|
||
|
||
// 检查岗位状态是否为启用
|
||
if position.Status != 1 {
|
||
return nil, process.FailError(types.PositionDisabledError, errors.New("岗位已禁用,不能分配"))
|
||
}
|
||
|
||
// 检查是否已存在相同的组织岗位关系
|
||
var count int64
|
||
r := dblib.DBIns.DB.Model(&hr.OrgPositionRelModel{}).
|
||
Where("OrganizationGuid = ? AND PositionGuid = ? AND (RecordStatus & 524288) = 0",
|
||
org.RecordGuid, position.RecordGuid).Count(&count)
|
||
if r.Error != nil {
|
||
return nil, process.FailError(types.QueryOrgPositionError, r.Error)
|
||
}
|
||
|
||
if count > 0 {
|
||
return nil, process.FailError(types.DuplicateOrgPositionError, errors.New("该组织已分配此岗位"))
|
||
}
|
||
|
||
// 创建组织岗位关系
|
||
relation := hr.OrgPositionRelInstance()
|
||
relation.OrganizationID = org.DepartmentID
|
||
relation.OrganizationGuid = org.RecordGuid
|
||
relation.PositionID = position.PositionID
|
||
relation.PositionGuid = position.RecordGuid
|
||
|
||
// 设置岗位编制人数
|
||
if params.HeadCount <= 0 {
|
||
relation.HeadCount = 1 // 默认为1
|
||
} else {
|
||
relation.HeadCount = params.HeadCount
|
||
}
|
||
|
||
// 保存到数据库
|
||
r = dblib.DBIns.DB.Create(relation)
|
||
if r.Error != nil {
|
||
return nil, process.FailError(types.CreateOrgPositionError, r.Error)
|
||
}
|
||
|
||
return relation, process.Success(200)
|
||
}
|
||
|
||
// UpdateOrgPositionHeadCount 更新组织岗位编制人数
|
||
func UpdateOrgPositionHeadCount(relationGuid string, headCount int) (*hr.OrgPositionRelModel, *process.Process) {
|
||
// 验证关系ID
|
||
guid, err := unique.FromString(relationGuid)
|
||
if err != nil {
|
||
return nil, process.FailError(types.InvalidParamError, err)
|
||
}
|
||
|
||
// 查询组织岗位关系
|
||
relation := &hr.OrgPositionRelModel{}
|
||
r := dblib.DBIns.DB.Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", guid).First(relation)
|
||
if r.Error != nil {
|
||
return nil, process.FailError(types.OrgPositionRelNotFoundError, r.Error)
|
||
}
|
||
|
||
// 验证编制人数
|
||
if headCount <= 0 {
|
||
return nil, process.FailError(types.InvalidParamError, errors.New("编制人数必须大于0"))
|
||
}
|
||
|
||
// 更新编制人数
|
||
relation.HeadCount = headCount
|
||
|
||
// 保存更新
|
||
r = dblib.DBIns.DB.Save(relation)
|
||
if r.Error != nil {
|
||
return nil, process.FailError(types.UpdateOrgPositionError, r.Error)
|
||
}
|
||
|
||
return relation, process.Success(200)
|
||
}
|
||
|
||
// RemovePositionFromOrg 从组织中移除岗位
|
||
func RemovePositionFromOrg(relationGuid string) *process.Process {
|
||
// 验证关系ID
|
||
guid, err := unique.FromString(relationGuid)
|
||
if err != nil {
|
||
return process.FailError(types.InvalidParamError, err)
|
||
}
|
||
|
||
// 查询组织岗位关系
|
||
relation := &hr.OrgPositionRelModel{}
|
||
r := dblib.DBIns.DB.Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", guid).First(relation)
|
||
if r.Error != nil {
|
||
return process.FailError(types.OrgPositionRelNotFoundError, r.Error)
|
||
}
|
||
|
||
// 使用原生SQL执行删除操作(设置删除标记)
|
||
sql := "UPDATE " + hr.OrgPositionRelTable + " SET RecordStatus = RecordStatus | 524288 WHERE RecordGuid = ?"
|
||
r = dblib.DBIns.DB.Exec(sql, guid)
|
||
if r.Error != nil {
|
||
return process.FailError(types.DeleteOrgPositionError, r.Error)
|
||
}
|
||
|
||
return process.Success(200)
|
||
}
|
||
|
||
// QueryPositionsByOrg 查询组织的岗位列表
|
||
func QueryPositionsByOrg(organizationGuid string, params QueryOrgPositionRequest) ([]OrgPositionDTO, *process.Process) {
|
||
result := make([]OrgPositionDTO, 0)
|
||
|
||
// 验证组织ID
|
||
orgGuid, err := unique.FromString(organizationGuid)
|
||
if err != nil {
|
||
return nil, process.FailError(types.InvalidParamError, err)
|
||
}
|
||
|
||
// 查询组织是否存在
|
||
org := &hr.OrganizationModel{}
|
||
r := dblib.DBIns.DB.Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", orgGuid).First(org)
|
||
if r.Error != nil {
|
||
return nil, process.FailError(types.OrgNotFoundError, r.Error)
|
||
}
|
||
|
||
// 分页参数处理
|
||
limit := params.Limit
|
||
if limit <= 0 {
|
||
limit = 20 // 默认每页20条
|
||
}
|
||
offset := params.Offset
|
||
if offset < 0 {
|
||
offset = 0
|
||
}
|
||
|
||
// 使用JOIN查询组织岗位关系及岗位信息
|
||
rows, err := dblib.DBIns.DB.Raw(`
|
||
SELECT
|
||
r.RecordGuid as RelationGuid,
|
||
o.RecordGuid as OrganizationGuid,
|
||
o.OrganizationName,
|
||
o.RecordType as OrganizationType,
|
||
p.RecordGuid as PositionGuid,
|
||
p.PositionName,
|
||
p.PositionCode,
|
||
r.HeadCount,
|
||
p.Level,
|
||
p.CategoryID,
|
||
p.CategoryName,
|
||
p.IsManagement
|
||
FROM
|
||
`+hr.OrgPositionRelTable+` r
|
||
JOIN
|
||
`+hr.OrganizationTable+` o ON r.OrganizationGuid = o.RecordGuid
|
||
JOIN
|
||
`+hr.PositionTable+` p ON r.PositionGuid = p.RecordGuid
|
||
WHERE
|
||
r.OrganizationGuid = ?
|
||
AND (r.RecordStatus & 524288) = 0
|
||
AND (o.RecordStatus & 524288) = 0
|
||
AND (p.RecordStatus & 524288) = 0
|
||
ORDER BY p.Level DESC, p.CategoryID ASC
|
||
LIMIT ? OFFSET ?
|
||
`, orgGuid, limit, offset).Rows()
|
||
|
||
if err != nil {
|
||
return nil, process.FailError(types.QueryOrgPositionError, err)
|
||
}
|
||
defer rows.Close()
|
||
|
||
// 遍历结果集
|
||
for rows.Next() {
|
||
var dto OrgPositionDTO
|
||
var orgGuidStr, posGuidStr, relGuidStr string
|
||
|
||
err := rows.Scan(
|
||
&relGuidStr,
|
||
&orgGuidStr,
|
||
&dto.OrganizationName,
|
||
&dto.OrganizationType,
|
||
&posGuidStr,
|
||
&dto.PositionName,
|
||
&dto.PositionCode,
|
||
&dto.HeadCount,
|
||
&dto.Level,
|
||
&dto.CategoryID,
|
||
&dto.CategoryName,
|
||
&dto.IsManagement,
|
||
)
|
||
|
||
if err != nil {
|
||
continue
|
||
}
|
||
|
||
dto.RelationGuid = relGuidStr
|
||
dto.OrganizationGuid = orgGuidStr
|
||
dto.PositionGuid = posGuidStr
|
||
|
||
result = append(result, dto)
|
||
}
|
||
|
||
return result, process.Success(200)
|
||
}
|
||
|
||
// QueryOrgsByPosition 查询拥有指定岗位的组织列表
|
||
func QueryOrgsByPosition(positionGuid string, params QueryOrgPositionRequest) ([]OrgPositionDTO, *process.Process) {
|
||
result := make([]OrgPositionDTO, 0)
|
||
|
||
// 验证岗位ID
|
||
posGuid, err := unique.FromString(positionGuid)
|
||
if err != nil {
|
||
return nil, process.FailError(types.InvalidParamError, err)
|
||
}
|
||
|
||
// 查询岗位是否存在
|
||
position := &hr.PositionModel{}
|
||
r := dblib.DBIns.DB.Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", posGuid).First(position)
|
||
if r.Error != nil {
|
||
return nil, process.FailError(types.PositionNotFoundError, r.Error)
|
||
}
|
||
|
||
// 分页参数处理
|
||
limit := params.Limit
|
||
if limit <= 0 {
|
||
limit = 20 // 默认每页20条
|
||
}
|
||
offset := params.Offset
|
||
if offset < 0 {
|
||
offset = 0
|
||
}
|
||
|
||
// 使用JOIN查询组织岗位关系及组织信息
|
||
rows, err := dblib.DBIns.DB.Raw(`
|
||
SELECT
|
||
r.RecordGuid as RelationGuid,
|
||
o.RecordGuid as OrganizationGuid,
|
||
o.OrganizationName,
|
||
o.RecordType as OrganizationType,
|
||
p.RecordGuid as PositionGuid,
|
||
p.PositionName,
|
||
p.PositionCode,
|
||
r.HeadCount,
|
||
p.Level,
|
||
p.CategoryID,
|
||
p.CategoryName,
|
||
p.IsManagement
|
||
FROM
|
||
`+hr.OrgPositionRelTable+` r
|
||
JOIN
|
||
`+hr.OrganizationTable+` o ON r.OrganizationGuid = o.RecordGuid
|
||
JOIN
|
||
`+hr.PositionTable+` p ON r.PositionGuid = p.RecordGuid
|
||
WHERE
|
||
r.PositionGuid = ?
|
||
AND (r.RecordStatus & 524288) = 0
|
||
AND (o.RecordStatus & 524288) = 0
|
||
AND (p.RecordStatus & 524288) = 0
|
||
LIMIT ? OFFSET ?
|
||
`, posGuid, limit, offset).Rows()
|
||
|
||
if err != nil {
|
||
return nil, process.FailError(types.QueryOrgPositionError, err)
|
||
}
|
||
defer rows.Close()
|
||
|
||
// 遍历结果集
|
||
for rows.Next() {
|
||
var dto OrgPositionDTO
|
||
var orgGuidStr, posGuidStr, relGuidStr string
|
||
|
||
err := rows.Scan(
|
||
&relGuidStr,
|
||
&orgGuidStr,
|
||
&dto.OrganizationName,
|
||
&dto.OrganizationType,
|
||
&posGuidStr,
|
||
&dto.PositionName,
|
||
&dto.PositionCode,
|
||
&dto.HeadCount,
|
||
&dto.Level,
|
||
&dto.CategoryID,
|
||
&dto.CategoryName,
|
||
&dto.IsManagement,
|
||
)
|
||
|
||
if err != nil {
|
||
continue
|
||
}
|
||
|
||
dto.RelationGuid = relGuidStr
|
||
dto.OrganizationGuid = orgGuidStr
|
||
dto.PositionGuid = posGuidStr
|
||
|
||
result = append(result, dto)
|
||
}
|
||
|
||
return result, process.Success(200)
|
||
}
|
||
|
||
// GetPositionOrgRelation 获取组织岗位关系详情
|
||
func GetPositionOrgRelation(relationGuid string) (*OrgPositionDTO, *process.Process) {
|
||
// 验证关系ID
|
||
guid, err := unique.FromString(relationGuid)
|
||
if err != nil {
|
||
return nil, process.FailError(types.InvalidParamError, err)
|
||
}
|
||
|
||
// 使用JOIN查询组织岗位关系、组织和岗位信息
|
||
var dto OrgPositionDTO
|
||
var orgGuidStr, posGuidStr, relGuidStr string
|
||
|
||
err = dblib.DBIns.DB.Raw(`
|
||
SELECT
|
||
r.RecordGuid as RelationGuid,
|
||
o.RecordGuid as OrganizationGuid,
|
||
o.OrganizationName,
|
||
o.RecordType as OrganizationType,
|
||
p.RecordGuid as PositionGuid,
|
||
p.PositionName,
|
||
p.PositionCode,
|
||
r.HeadCount,
|
||
p.Level,
|
||
p.CategoryID,
|
||
p.CategoryName,
|
||
p.IsManagement
|
||
FROM
|
||
`+hr.OrgPositionRelTable+` r
|
||
JOIN
|
||
`+hr.OrganizationTable+` o ON r.OrganizationGuid = o.RecordGuid
|
||
JOIN
|
||
`+hr.PositionTable+` p ON r.PositionGuid = p.RecordGuid
|
||
WHERE
|
||
r.RecordGuid = ?
|
||
AND (r.RecordStatus & 524288) = 0
|
||
AND (o.RecordStatus & 524288) = 0
|
||
AND (p.RecordStatus & 524288) = 0
|
||
`, guid).Row().Scan(
|
||
&relGuidStr,
|
||
&orgGuidStr,
|
||
&dto.OrganizationName,
|
||
&dto.OrganizationType,
|
||
&posGuidStr,
|
||
&dto.PositionName,
|
||
&dto.PositionCode,
|
||
&dto.HeadCount,
|
||
&dto.Level,
|
||
&dto.CategoryID,
|
||
&dto.CategoryName,
|
||
&dto.IsManagement,
|
||
)
|
||
|
||
if err != nil {
|
||
return nil, process.FailError(types.OrgPositionRelNotFoundError, err)
|
||
}
|
||
|
||
dto.RelationGuid = relGuidStr
|
||
dto.OrganizationGuid = orgGuidStr
|
||
dto.PositionGuid = posGuidStr
|
||
|
||
return &dto, process.Success(200)
|
||
}
|