WiiCITMS/process/hr/leave.go
2025-11-07 14:14:34 +08:00

106 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package hr
import (
"WiiCITMS/models/hr"
"WiiCITMS/process/common"
"WiiGenerates/WiiCITMS/generates/v1/go/types"
"WiiGoLibrary/apply/middle/process/v1"
"WiiGoLibrary/framework/db/v1/utils/mssql/unique"
"WiiGoLibrary/framework/hub/v1/dblib"
"errors"
"time"
)
// CreateLeaveRequest 创建请假申请请求参数
type CreateLeaveRequest struct {
StaffGuid string `json:"staffGuid"` // 请假员工ID
LeaveType int `json:"leaveType"` // 请假类型: 1-事假, 2-病假, 3-年假, 4-调休
StartTime time.Time `json:"startTime"` // 请假开始时间
EndTime time.Time `json:"endTime"` // 请假结束时间
Duration float32 `json:"duration"` // 请假时长(天)
Reason string `json:"reason"` // 请假原因
}
// QueryLeavesRequest 查询请假记录请求参数
type QueryLeavesRequest struct {
StaffGuid string `json:"staffGuid"` // 员工ID可选为空时查询所有
Limit int `json:"limit"` // 分页参数,每页数量
Offset int `json:"offset"` // 分页参数,偏移量
}
// CreateLeave 创建请假记录
func CreateLeave(params CreateLeaveRequest, operatorGuid string) (*hr.LeaveModel, *process.Process) {
// 验证操作者权限
// 如果是为自己创建请假,则不需要特定权限
// 如果是为他人创建请假,则需要有员工管理权限
if operatorGuid != params.StaffGuid {
// 检查是否有员工管理权限
result := CheckAccessControl(operatorGuid, common.OperationCreate, common.ResourceStaff, "")
if !result.HasPermission {
return nil, process.FailError(types.NoPermissionError, errors.New(result.ErrorMessage))
}
}
// 验证员工是否存在
staffGuid, err := unique.FromString(params.StaffGuid)
if err != nil {
return nil, process.FailError(types.InvalidCreateParamError, err)
}
staffModel := &hr.StaffModel{}
r := dblib.DBIns.DB.Where("RecordGuid = ? AND (RecordStatus & 524288) = 0", staffGuid).First(staffModel)
if r.Error != nil {
return nil, process.FailError(types.InvalidCreateStaffError, r.Error)
}
// 创建请假记录
leave := hr.LeaveInstance()
leave.StaffGuid = staffGuid
leave.LeaveType = int16(params.LeaveType)
leave.StartTime = params.StartTime
leave.EndTime = params.EndTime
leave.Duration = params.Duration
leave.Reason = params.Reason
// 保存记录
r = dblib.DBIns.DB.Create(leave)
if r.Error != nil {
return nil, process.FailError(types.CreateLeaveError, r.Error)
}
return leave, process.Success(200)
}
// QueryLeaves 查询请假记录
func QueryLeaves(params QueryLeavesRequest) ([]*hr.LeaveModel, *process.Process) {
result := make([]*hr.LeaveModel, 0)
db := dblib.DBIns.DB.Model(&hr.LeaveModel{}).Where("(RecordStatus & 524288) = 0")
// 按员工ID筛选
if params.StaffGuid != "" {
staffGuid, err := unique.FromString(params.StaffGuid)
if err == nil {
db = db.Where("StaffGuid = ?", staffGuid)
}
}
// 分页查询
limit := params.Limit
if limit <= 0 {
limit = 20 // 默认每页20条
}
offset := params.Offset
if offset <= 0 {
offset = 0
}
r := db.Limit(limit).Offset(offset).Order("CreateTime DESC").Find(&result)
if r.Error != nil {
return result, process.FailError(types.QueryLeaveError, r.Error)
}
return result, process.Success(200)
}