428 lines
12 KiB
Go
428 lines
12 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"
|
|||
|
|
"fmt"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// CreateStaffWithOrganizationRequest 创建员工并指定所属组织请求参数
|
|||
|
|
type CreateStaffWithOrganizationRequest struct {
|
|||
|
|
StaffName string `json:"staffName"` // 员工姓名
|
|||
|
|
Affiliated string `json:"affiliated"` // 所属组织ID
|
|||
|
|
StaffType int `json:"staffType"` // 员工类型:0-正式员工,1-实习员工,2-外包员工
|
|||
|
|
AffiliatedType int `json:"affiliatedType"` // 隶属关系类型:0-主部门,1-兼职部门
|
|||
|
|
Account string `json:"account"` // 账号(可选)
|
|||
|
|
UserGuid string `json:"userGuid"` // SSO平台用户ID(可选)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// BindStaffRequest 绑定SSO用户到员工请求参数
|
|||
|
|
type BindStaffRequest struct {
|
|||
|
|
StaffGuid string `json:"staffGuid"` // 员工GUID
|
|||
|
|
Account string `json:"account"` // 账号
|
|||
|
|
UserGuid string `json:"userGuid"` // SSO平台用户GUID
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// QueryStaffRequest 查询员工请求参数
|
|||
|
|
type QueryStaffRequest struct {
|
|||
|
|
StaffName string `json:"staffName"` // 员工姓名(模糊匹配)
|
|||
|
|
Account string `json:"account"` // 账号(精确匹配)
|
|||
|
|
UserGuid string `json:"userGuid"` // SSO平台用户ID
|
|||
|
|
OrganizationGuid string `json:"organizationGuid"` // 所属组织ID
|
|||
|
|
StaffType int `json:"staffType"` // 员工类型
|
|||
|
|
Limit int `json:"limit"` // 分页参数,每页数量
|
|||
|
|
Offset int `json:"offset"` // 分页参数,偏移量
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// StaffDetailDTO 员工详细信息DTO
|
|||
|
|
type StaffDetailDTO struct {
|
|||
|
|
StaffGuid string `json:"staffGuid"` // 员工GUID
|
|||
|
|
StaffName string `json:"staffName"` // 员工姓名
|
|||
|
|
JobID int64 `json:"jobId"` // 工号
|
|||
|
|
Account string `json:"account"` // 账号
|
|||
|
|
UserGuid string `json:"userGuid"` // SSO平台用户ID
|
|||
|
|
StaffType int `json:"staffType"` // 员工类型
|
|||
|
|
MainOrganization string `json:"mainOrganization"` // 主部门
|
|||
|
|
Positions []PositionInfo `json:"positions"` // 岗位信息
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// PositionInfo 岗位信息
|
|||
|
|
type PositionInfo struct {
|
|||
|
|
PositionGuid unique.UUID `json:"positionGuid"` // 岗位GUID
|
|||
|
|
PositionName string `json:"positionName"` // 岗位名称
|
|||
|
|
OrganizationGuid unique.UUID `json:"organizationGuid"` // 所属组织GUID
|
|||
|
|
OrganizationName string `json:"organizationName"` // 所属组织名称
|
|||
|
|
IsLeader bool `json:"isLeader"` // 是否是组织负责人
|
|||
|
|
IsMainPosition bool `json:"isMainPosition"` // 是否是主岗位
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// CreateStaffWithOrganization 创建员工并指定所属组织
|
|||
|
|
func CreateStaffWithOrganization(params CreateStaffWithOrganizationRequest) (*hr.StaffModel, *process.Process) {
|
|||
|
|
tx := dblib.DBIns.DB.Begin()
|
|||
|
|
|
|||
|
|
// 创建员工
|
|||
|
|
staff := hr.StaffInstance()
|
|||
|
|
staff.StaffName = params.StaffName
|
|||
|
|
staff.RecordType = int16(params.StaffType)
|
|||
|
|
|
|||
|
|
// 如果提供了账号和用户ID,则绑定用户
|
|||
|
|
if params.Account != "" {
|
|||
|
|
staff.Account = params.Account
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if params.UserGuid != "" {
|
|||
|
|
userGuid, err := unique.FromString(params.UserGuid)
|
|||
|
|
if err == nil {
|
|||
|
|
staff.UserGuid = userGuid
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 创建员工记录
|
|||
|
|
r := tx.Create(staff)
|
|||
|
|
if r.Error != nil {
|
|||
|
|
tx.Rollback()
|
|||
|
|
return nil, process.FailError(types.CreateStaffError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查组织是否有效
|
|||
|
|
organizationData := &hr.OrganizationModel{}
|
|||
|
|
r = tx.Model(&hr.OrganizationModel{}).Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", params.Affiliated).First(organizationData)
|
|||
|
|
if r.Error != nil {
|
|||
|
|
tx.Rollback()
|
|||
|
|
return nil, process.FailError(types.InvalidOrganizationError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 创建员工与组织关联关系
|
|||
|
|
staff2Organization := hr.Staff2OrganizationInstance()
|
|||
|
|
staff2Organization.ObjectGuid = staff.RecordGuid // 员工GUID
|
|||
|
|
staff2Organization.TargetGuid = organizationData.RecordGuid // 组织GUID
|
|||
|
|
staff2Organization.RecordType = int16(params.AffiliatedType) // 隶属关系类型
|
|||
|
|
r = tx.Create(staff2Organization)
|
|||
|
|
if r.Error != nil {
|
|||
|
|
tx.Rollback()
|
|||
|
|
return nil, process.FailError(types.CreateStaffError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
tx.Commit()
|
|||
|
|
return staff, process.Success(200)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// BindStaff 绑定SSO用户到员工
|
|||
|
|
func BindStaff(params BindStaffRequest) (*hr.StaffModel, *process.Process) {
|
|||
|
|
// 验证员工ID
|
|||
|
|
staffGuid, err := unique.FromString(params.StaffGuid)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, process.FailError(types.InvalidParamError, err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 查询员工
|
|||
|
|
staff := &hr.StaffModel{}
|
|||
|
|
r := dblib.DBIns.DB.Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", staffGuid).First(staff)
|
|||
|
|
if r.Error != nil {
|
|||
|
|
return nil, process.FailError(types.StaffNotFoundError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查账号是否已被使用
|
|||
|
|
if params.Account != "" {
|
|||
|
|
var count int64
|
|||
|
|
r = dblib.DBIns.DB.Model(&hr.StaffModel{}).
|
|||
|
|
Where("Account = ? AND RecordGuid != ? AND (RecordStatus & 524288) = 0", params.Account, staffGuid).
|
|||
|
|
Count(&count)
|
|||
|
|
|
|||
|
|
if r.Error != nil {
|
|||
|
|
return nil, process.FailError(types.QueryStaffError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if count > 0 {
|
|||
|
|
return nil, process.FailError(types.DuplicateAccountError, fmt.Errorf("账号已被使用"))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 更新账号
|
|||
|
|
staff.Account = params.Account
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查UserGuid是否已被使用
|
|||
|
|
if params.UserGuid != "" {
|
|||
|
|
userGuid, err := unique.FromString(params.UserGuid)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, process.FailError(types.InvalidParamError, err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var count int64
|
|||
|
|
r = dblib.DBIns.DB.Model(&hr.StaffModel{}).
|
|||
|
|
Where("UserGuid = ? AND RecordGuid != ? AND (RecordStatus & 524288) = 0", userGuid, staffGuid).
|
|||
|
|
Count(&count)
|
|||
|
|
|
|||
|
|
if r.Error != nil {
|
|||
|
|
return nil, process.FailError(types.QueryStaffError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if count > 0 {
|
|||
|
|
return nil, process.FailError(types.DuplicateUserGuidError, fmt.Errorf("用户ID已被绑定"))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 更新UserGuid
|
|||
|
|
staff.UserGuid = userGuid
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 保存更新
|
|||
|
|
r = dblib.DBIns.DB.Save(staff)
|
|||
|
|
if r.Error != nil {
|
|||
|
|
return nil, process.FailError(types.UpdateStaffError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return staff, process.Success(200)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UnbindStaff 解除员工与SSO用户的绑定
|
|||
|
|
func UnbindStaff(staffGuid string) (*hr.StaffModel, *process.Process) {
|
|||
|
|
// 验证员工ID
|
|||
|
|
guid, err := unique.FromString(staffGuid)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, process.FailError(types.InvalidParamError, err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 查询员工
|
|||
|
|
staff := &hr.StaffModel{}
|
|||
|
|
r := dblib.DBIns.DB.Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", guid).First(staff)
|
|||
|
|
if r.Error != nil {
|
|||
|
|
return nil, process.FailError(types.StaffNotFoundError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 解除绑定
|
|||
|
|
staff.UserGuid = unique.NilUUID
|
|||
|
|
staff.Account = ""
|
|||
|
|
|
|||
|
|
// 保存更新
|
|||
|
|
r = dblib.DBIns.DB.Save(staff)
|
|||
|
|
if r.Error != nil {
|
|||
|
|
return nil, process.FailError(types.UpdateStaffError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return staff, process.Success(200)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetStaff 获取员工详情
|
|||
|
|
func GetStaff(staffGuid string) (*StaffDetailDTO, *process.Process) {
|
|||
|
|
// 验证员工ID
|
|||
|
|
guid, err := unique.FromString(staffGuid)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, process.FailError(types.InvalidParamError, err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 查询员工基本信息
|
|||
|
|
staff := &hr.StaffModel{}
|
|||
|
|
r := dblib.DBIns.DB.Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", guid).First(staff)
|
|||
|
|
if r.Error != nil {
|
|||
|
|
return nil, process.FailError(types.StaffNotFoundError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 构建员工详情DTO
|
|||
|
|
result := &StaffDetailDTO{
|
|||
|
|
StaffGuid: staff.RecordGuid.String(),
|
|||
|
|
StaffName: staff.StaffName,
|
|||
|
|
JobID: staff.JobID,
|
|||
|
|
Account: staff.Account,
|
|||
|
|
UserGuid: staff.UserGuid.String(),
|
|||
|
|
StaffType: int(staff.RecordType),
|
|||
|
|
Positions: []PositionInfo{},
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 查询员工所在的组织
|
|||
|
|
rows, err := dblib.DBIns.DB.Raw(`
|
|||
|
|
SELECT
|
|||
|
|
o.RecordGuid as OrgGuid,
|
|||
|
|
o.OrganizationName,
|
|||
|
|
p.RecordGuid as PosGuid,
|
|||
|
|
p.PositionName,
|
|||
|
|
so.IsLeader,
|
|||
|
|
so.RecordType as RelType
|
|||
|
|
FROM
|
|||
|
|
`+hr.Staff2OrganizationTable+` so
|
|||
|
|
JOIN
|
|||
|
|
`+hr.OrganizationTable+` o ON so.TargetGuid = o.RecordGuid
|
|||
|
|
LEFT JOIN
|
|||
|
|
`+hr.PositionTable+` p ON so.PositionGuid = p.RecordGuid
|
|||
|
|
WHERE
|
|||
|
|
so.ObjectGuid = ?
|
|||
|
|
AND (so.RecordStatus & 524288) = 0
|
|||
|
|
AND (o.RecordStatus & 524288) = 0
|
|||
|
|
`, guid).Rows()
|
|||
|
|
|
|||
|
|
if err == nil {
|
|||
|
|
defer rows.Close()
|
|||
|
|
|
|||
|
|
// 处理查询结果
|
|||
|
|
for rows.Next() {
|
|||
|
|
var position PositionInfo
|
|||
|
|
var orgGuid unique.UUID
|
|||
|
|
var posGuid unique.UUID
|
|||
|
|
var isLeader bool
|
|||
|
|
var relType int16
|
|||
|
|
|
|||
|
|
err := rows.Scan(
|
|||
|
|
&orgGuid,
|
|||
|
|
&position.OrganizationName,
|
|||
|
|
&posGuid,
|
|||
|
|
&position.PositionName,
|
|||
|
|
&isLeader,
|
|||
|
|
&relType,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
if err != nil {
|
|||
|
|
continue
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
position.OrganizationGuid = orgGuid
|
|||
|
|
position.PositionGuid = posGuid
|
|||
|
|
position.IsLeader = isLeader
|
|||
|
|
position.IsMainPosition = (relType == 0) // 0表示主岗位
|
|||
|
|
|
|||
|
|
// 设置主部门
|
|||
|
|
if position.IsMainPosition {
|
|||
|
|
result.MainOrganization = position.OrganizationName
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
result.Positions = append(result.Positions, position)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return result, process.Success(200)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// QueryStaffs 查询员工列表
|
|||
|
|
func QueryStaffs(params QueryStaffRequest) ([]*StaffDetailDTO, *process.Process) {
|
|||
|
|
result := make([]*StaffDetailDTO, 0)
|
|||
|
|
|
|||
|
|
// 构建查询条件
|
|||
|
|
db := dblib.DBIns.DB.Model(&hr.StaffModel{}).Where("(RecordStatus & 524288) = 0")
|
|||
|
|
|
|||
|
|
// 按员工姓名模糊查询
|
|||
|
|
if params.StaffName != "" {
|
|||
|
|
db = db.Where("UserName LIKE ?", "%"+params.StaffName+"%")
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 按账号精确查询
|
|||
|
|
if params.Account != "" {
|
|||
|
|
db = db.Where("Account = ?", params.Account)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 按用户ID查询
|
|||
|
|
if params.UserGuid != "" {
|
|||
|
|
userGuid, err := unique.FromString(params.UserGuid)
|
|||
|
|
if err == nil {
|
|||
|
|
db = db.Where("UserGuid = ?", userGuid)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 按员工类型查询
|
|||
|
|
if params.StaffType > 0 {
|
|||
|
|
db = db.Where("RecordType = ?", params.StaffType)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 查询特定组织的员工
|
|||
|
|
var staffGuids []string
|
|||
|
|
if params.OrganizationGuid != "" {
|
|||
|
|
orgGuid, err := unique.FromString(params.OrganizationGuid)
|
|||
|
|
if err == nil {
|
|||
|
|
// 查询组织下的员工ID
|
|||
|
|
rows, err := dblib.DBIns.DB.Raw(`
|
|||
|
|
SELECT
|
|||
|
|
so.ObjectGuid
|
|||
|
|
FROM
|
|||
|
|
`+hr.Staff2OrganizationTable+` so
|
|||
|
|
WHERE
|
|||
|
|
so.TargetGuid = ?
|
|||
|
|
AND (so.RecordStatus & 524288) = 0
|
|||
|
|
`, orgGuid).Rows()
|
|||
|
|
|
|||
|
|
if err == nil {
|
|||
|
|
defer rows.Close()
|
|||
|
|
|
|||
|
|
// 处理查询结果
|
|||
|
|
for rows.Next() {
|
|||
|
|
var guidObj unique.UUID
|
|||
|
|
|
|||
|
|
err := rows.Scan(&guidObj)
|
|||
|
|
if err == nil {
|
|||
|
|
staffGuids = append(staffGuids, guidObj.String())
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if len(staffGuids) > 0 {
|
|||
|
|
db = db.Where("RecordGuid IN ?", staffGuids)
|
|||
|
|
} else {
|
|||
|
|
// 如果没有找到员工,返回空结果
|
|||
|
|
return result, process.Success(200)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 分页查询
|
|||
|
|
limit := params.Limit
|
|||
|
|
if limit <= 0 {
|
|||
|
|
limit = 20 // 默认每页20条
|
|||
|
|
}
|
|||
|
|
offset := params.Offset
|
|||
|
|
if offset < 0 {
|
|||
|
|
offset = 0
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 查询员工列表
|
|||
|
|
staffs := make([]*hr.StaffModel, 0)
|
|||
|
|
r := db.Limit(limit).Offset(offset).Find(&staffs)
|
|||
|
|
if r.Error != nil {
|
|||
|
|
return result, process.FailError(types.QueryStaffError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 转换为DetailDTO
|
|||
|
|
for _, staff := range staffs {
|
|||
|
|
detail, proc := GetStaff(staff.RecordGuid.String())
|
|||
|
|
if !proc.IsError() && detail != nil {
|
|||
|
|
result = append(result, detail)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return result, process.Success(200)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetStaffByUserGuid 根据UserGuid获取员工
|
|||
|
|
func GetStaffByUserGuid(userGuid string) (*StaffDetailDTO, *process.Process) {
|
|||
|
|
// 验证用户ID
|
|||
|
|
guid, err := unique.FromString(userGuid)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, process.FailError(types.InvalidParamError, err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 查询员工
|
|||
|
|
staff := &hr.StaffModel{}
|
|||
|
|
r := dblib.DBIns.DB.Where("UserGuid = ? AND (RecordStatus & 524288) = 0", guid).First(staff)
|
|||
|
|
if r.Error != nil {
|
|||
|
|
return nil, process.FailError(types.StaffNotFoundError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 获取详细信息
|
|||
|
|
return GetStaff(staff.RecordGuid.String())
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetStaffByAccount 根据账号获取员工
|
|||
|
|
func GetStaffByAccount(account string) (*StaffDetailDTO, *process.Process) {
|
|||
|
|
if account == "" {
|
|||
|
|
return nil, process.FailError(types.InvalidParamError, fmt.Errorf("账号不能为空"))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 查询员工
|
|||
|
|
staff := &hr.StaffModel{}
|
|||
|
|
r := dblib.DBIns.DB.Where("Account = ? AND (RecordStatus & 524288) = 0", account).First(staff)
|
|||
|
|
if r.Error != nil {
|
|||
|
|
return nil, process.FailError(types.StaffNotFoundError, r.Error)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 获取详细信息
|
|||
|
|
return GetStaff(staff.RecordGuid.String())
|
|||
|
|
}
|