fonchain-fiee/pkg/service/approval/approval_oa.go
2025-02-19 14:24:15 +08:00

920 lines
36 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 approval
import (
"context"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"errors"
"fmt"
approvalApi "github.com/fonchain_enterprise/fonchain-main/api/approval"
oaApi "github.com/fonchain_enterprise/fonchain-main/api/oa"
"github.com/fonchain_enterprise/fonchain-main/pkg/e"
"github.com/fonchain_enterprise/fonchain-main/pkg/model/login"
"github.com/fonchain_enterprise/fonchain-main/pkg/model/oa_model"
"github.com/fonchain_enterprise/fonchain-main/pkg/service"
"github.com/fonchain_enterprise/fonchain-main/pkg/service/oa_new/common"
"github.com/fonchain_enterprise/fonchain-main/pkg/service/oa_new/model"
"github.com/fonchain_enterprise/fonchain-main/pkg/service/oa_new/oa_logic"
"github.com/fonchain_enterprise/fonchain-main/pkg/utils/holiday"
"github.com/jinzhu/copier"
"strconv"
"strings"
"time"
)
func SyncApprovalToOA(req *approvalApi.CreateRequest) (saveRes *oaApi.CommonRes, err error) {
// 赋值 给 oa系统中的 oa 审批
oaApply := retOaApply(req, true, false, false)
//oaApply.ApprovalID = strconv.FormatUint(approvalID, 10)
//oaApply.StaffUID = strconv.FormatUint(req.SubmitterID, 10)
//oaApply.StaffName = req.SubmitterName
// 保存 oa 审批内容
saveRes, err = service.GrpcOAImpl.SaveOaApply(context.Background(), oaApply)
fmt.Println("保存 oa 审批 UUID 到 审批 开始")
fmt.Println("oa apply uuid is :", saveRes.ID)
fmt.Println("保存 oa 审批 UUID 到 审批 结束")
return
}
// 更新--通过或者拒绝
func UpdateApprovalStatusToOAStatus(req *approvalApi.CreateRequest) (updateRes *oaApi.CommonRes, err error) {
// 赋值 给 oa系统中的 oa 审批
oaApply := retOaApply(req, false, true, false)
// 保存 oa 审批内容
updateRes, err = service.GrpcOAImpl.UpdateOaApply(context.Background(), oaApply)
err = UpdateSlackBalance(req)
if err != nil {
fmt.Printf("更新假勤余额失败---%v", err)
}
return
}
// 撤销
func RevokeOaApproval(req *approvalApi.CreateRequest) (updateRes *oaApi.CommonRes, err error) {
// 赋值 给 oa系统中的 oa 审批
oaApply := retOaApply(req, false, false, true)
// 保存 oa 审批内容
updateRes, err = service.GrpcOAImpl.UpdateOaApply(context.Background(), oaApply)
err = UpdateSlackBalance(req)
if err != nil {
fmt.Printf("更新假勤余额失败---%v", err)
}
return
}
// 删除
func DeleteApproval(req *approvalApi.CreateRequest) (updateRes *oaApi.CommonRes, err error) {
// 赋值 给 oa系统中的 oa 审批
oaApply := retOaApply(req, false, false, false)
// 保存 oa 审批内容
updateRes, err = service.GrpcOAImpl.DeleteOaApply(context.Background(), oaApply)
req.Status = e.ApprovalWorkStatusRevokeOk
err = UpdateSlackBalance(req)
if err != nil {
fmt.Printf("更新假勤余额失败---%v", err)
}
return
}
func UpdateSlackBalance(req *approvalApi.CreateRequest) error {
// ty := 1 //增加假期天数 ty = 2 // 减少假期天数
fmt.Printf("更新假勤余额请求参数---%v", req.ApprovalOA.Leave)
switch req.Type {
case oa_model.TypeAnnualLeave, oa_model.TypeParentalLeave, oa_model.TypeNursingLeave:
fmt.Printf("这里是额度余额变动---:%v", req.Type)
if req.Status == e.ApprovalWorkStatusRevokeOk || req.Status == e.ApprovalWorkStatusFail { //撤销或者拒绝才会减去假勤余额
return modifyLeaveBalance(req, req.Type, 1)
} else {
return nil
}
case oa_model.TypeParentalLeaveApply, oa_model.TypeNursingLeaveApply:
fmt.Printf("这里是额度申请---:%v", req.Type)
if req.Status == e.ApprovalWorkStatusOk { //只有审批通过才会添加假勤余额
return updateFalseAttendanceStatus(req, req.Type, 2)
} else {
return nil
}
default:
return nil
}
}
func modifyLeaveBalance(req *approvalApi.CreateRequest, leaveType string, ty int) error {
date, _ := time.Parse("2006-01-02 15:04:05", req.ApprovalOA.Leave.ActionTime)
year := int32(date.Year())
_, err := service.GrpcOAImpl.ModifyTheLeaveBalance(context.Background(), &oaApi.ModifyTheLeaveBalanceReq{
StaffUid: req.SubmitterID,
LeaveType: leaveType,
Movement: int32(ty),
Year: year,
Days: req.ApprovalOA.Leave.Days,
})
if err != nil {
fmt.Printf("更新假勤余额失败---%v", err)
}
return err
}
func updateFalseAttendanceStatus(req *approvalApi.CreateRequest, leaveType string, ty int) error {
date, _ := time.Parse("2006-01-02 15:04:05", req.ApprovalOA.LeaveApply.ActionTime)
year := int32(date.Year())
_, err := service.GrpcOAImpl.UpdateFalseAttendanceStatus(context.Background(), &oaApi.UpdateFalseAttendanceStatusReq{
StaffUid: req.SubmitterID,
LeaveType: leaveType,
Year: year,
Days: req.ApprovalOA.LeaveApply.LeaveBalance,
Status: int32(ty),
})
if err != nil {
fmt.Printf("更新假勤余额失败---%v", err)
}
return err
}
func retOaApply(req *approvalApi.CreateRequest, save, updateStatus, revoke bool) (oaApply *oaApi.OAApply) {
oaApply = new(oaApi.OAApply)
fmt.Println("============== 打印 approval 审批内容 ========== 开始 =========")
fmt.Printf("CreateRequest info is : %+v \n", req)
fmt.Println("============== 打印 approval 审批内容 ========== 结束 =========")
// 通过 copier 赋值 可行
if req.ApprovalOA.MakeUp != nil {
copier.CopyWithOption(&oaApply, req.ApprovalOA.MakeUp, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.ApplyTimes, req.ApprovalOA.MakeUp.ApplyTimes, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.ApprovalUsers, req.ApprovalOA.MakeUp.ApprovalUsers, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.CopyUsers, req.ApprovalOA.MakeUp.CopyUsers, copier.Option{DeepCopy: true})
}
if req.ApprovalOA.OutWork != nil {
copier.CopyWithOption(&oaApply, req.ApprovalOA.OutWork, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.ApplyTimes, req.ApprovalOA.OutWork.ApplyTimes, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.ApprovalUsers, req.ApprovalOA.OutWork.ApprovalUsers, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.CopyUsers, req.ApprovalOA.OutWork.CopyUsers, copier.Option{DeepCopy: true})
}
if req.ApprovalOA.LeaveApply != nil {
copier.CopyWithOption(&oaApply, req.ApprovalOA.LeaveApply, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.ApprovalUsers, req.ApprovalOA.LeaveApply.ApprovalUsers, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.CopyUsers, req.ApprovalOA.LeaveApply.CopyUsers, copier.Option{DeepCopy: true})
}
if req.ApprovalOA.Turnover != nil {
copier.CopyWithOption(&oaApply, req.ApprovalOA.Turnover, copier.Option{DeepCopy: true})
//copier.CopyWithOption(&oaApply.ApplyTimes, req.ApprovalOA.Turnover.ApplyTimes, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.ApprovalUsers, req.ApprovalOA.Turnover.ApprovalUsers, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.CopyUsers, req.ApprovalOA.Turnover.CopyUsers, copier.Option{DeepCopy: true})
//copier.CopyWithOption(&oaApply.Positions, req.ApprovalOA.Turnover.Positions, copier.Option{DeepCopy: true})
}
if req.ApprovalOA.OverTime != nil {
copier.CopyWithOption(&oaApply, req.ApprovalOA.OverTime, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.ApplyTimes, req.ApprovalOA.OverTime.ApplyTimes, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.ApprovalUsers, req.ApprovalOA.OverTime.ApprovalUsers, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.CopyUsers, req.ApprovalOA.OverTime.CopyUsers, copier.Option{DeepCopy: true})
}
if req.ApprovalOA.Leave != nil {
copier.CopyWithOption(&oaApply, req.ApprovalOA.Leave, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.ApplyTimes, req.ApprovalOA.Leave.ApplyTimes, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.ApprovalUsers, req.ApprovalOA.Leave.ApprovalUsers, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.CopyUsers, req.ApprovalOA.Leave.CopyUsers, copier.Option{DeepCopy: true})
}
if req.ApprovalOA.BusinessTrip != nil {
copier.CopyWithOption(&oaApply, req.ApprovalOA.BusinessTrip, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.ApprovalUsers, req.ApprovalOA.BusinessTrip.ApprovalUsers, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.CopyUsers, req.ApprovalOA.BusinessTrip.CopyUsers, copier.Option{DeepCopy: true})
}
if req.ApprovalOA.GoOut != nil {
copier.CopyWithOption(&oaApply, req.ApprovalOA.GoOut, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.ApprovalUsers, req.ApprovalOA.GoOut.ApprovalUsers, copier.Option{DeepCopy: true})
copier.CopyWithOption(&oaApply.CopyUsers, req.ApprovalOA.GoOut.CopyUsers, copier.Option{DeepCopy: true})
}
oaApply.Remark = req.Reply
oaApply.ApplyType = req.Type
oaApply.ApprovalID = strconv.FormatUint(req.ID, 10)
oaApply.NowLevel = req.NowLevel
oaApply.NowUserId = req.NowUserId
oaApply.NowUserName = req.NowUserName
oaApply.RevokeReason = req.ApprovalOA.RevokeReason
oaApply.Domain = req.Domain
// 保存
oaApply.Status = int32(req.Status)
if save {
for i := 0; i < len(oaApply.ApprovalUsers); i++ {
oaApply.ApprovalUsers[i].Status = req.Status
}
}
// 更新 状态
if updateStatus {
for i := 0; i < len(oaApply.ApprovalUsers); i++ {
for j := 0; j < len(req.WorkFlows); j++ {
if oaApply.ApprovalUsers[i].ID == req.WorkFlows[j].UserID {
oaApply.ApprovalUsers[i].Status = req.WorkFlows[j].Status
oaApply.ApprovalUsers[i].Reply = req.WorkFlows[j].Reply
oaApply.ApprovalUsers[i].Tips = req.WorkFlows[j].Tips
}
}
}
}
if revoke {
fmt.Println("============== 打印 OA approvalUser ========== 开始 =========")
fmt.Printf("approvalUser is : %+v \n", oaApply.ApprovalUsers)
fmt.Println("============== 打印 OA approvalUser ========== 结束 =========")
//newApprovalUsers := make([]*oaApi.ApplyUser, 0)
for i := 0; i < len(oaApply.ApprovalUsers); i++ {
//newApprovalUsers = append(newApprovalUsers, &oaApi.ApplyUser{
// ID: req.ApprovalUsers[i].ID,
// Name: req.ApprovalUsers[i].Name,
// Status: e.ApprovalWorkStatusDoing,
//})
oaApply.ApprovalUsers[i].Status = e.ApprovalWorkStatusDoing
}
//oaApply.ApprovalUsers = newApprovalUsers
}
fmt.Println("============== 打印 OA 审批内容 ========== 开始 =========")
fmt.Printf("oa_apply info is : %+v \n", oaApply)
fmt.Println("============== 打印 OA 审批内容 ========== 结束 =========")
return oaApply
}
// 旧的 补卡规则
func CheckMakeUpApplyApprovalV1(req *approvalApi.CreateRequest) (err error) {
oaSettingReq := new(oaApi.OaSettingReq)
oaSettingReq.Keyword = e.MakeUpNum
oaSettingRes, err := service.GrpcOAImpl.QueryOaSetting(context.Background(), oaSettingReq)
if err != nil {
return err
}
if len(oaSettingRes.Data) != 1 {
return errors.New("查询OA补卡次数设置错误")
}
queryOaApplyReq := new(oaApi.ApplyRecordReq)
queryOaApplyReq.StaffUID = req.SubmitterID
queryOaApplyReq.ApplyType = []string{req.Type}
// 当前月 的 月初 和 月底
queryOaApplyReq.BeginTime = time.Now().Format("2006-01") + "-01" + " 00:00:00"
queryOaApplyReq.EndTime = time.Now().AddDate(0, 1, -1).Format("2006-01-02") + " 23:59:59"
queryOaApplyReq.ApplyStatus = []int32{e.ApprovalWorkStatusDoing, e.ApprovalWorkStatusOk}
queryOaApplyRes, err := service.GrpcOAImpl.QueryOaApply(context.Background(), queryOaApplyReq)
if err != nil {
return err
}
if oaSettingRes.Data[0].Value.Num > int32(queryOaApplyRes.Total) {
return nil
}
return errors.New("当月补卡次数已用完")
}
// 新的补卡规则
func CheckMakeUpApplyApproval(req *approvalApi.CreateRequest, userInfo login.Info) (err error) {
// 查询 考勤信息
workingTime, workingTimeErr := oa_logic.QueryWorkingTimeInUse(userInfo.PositionUsers, userInfo.ID)
if workingTimeErr != nil {
return workingTimeErr
}
logger.Infof("workingTime: %+v", workingTime)
fmt.Printf("workingTime: %+v", workingTime)
makeUpRule := new(oaApi.MakeUp)
for i := 0; i < len(workingTime.OaRules); i++ {
if workingTime.OaRules[i].Keyword == model.MakeUpRule && workingTime.OaRules[i].MakeUpRule != nil {
makeUpRule = workingTime.OaRules[i].MakeUpRule
}
}
if makeUpRule.Num == 0 {
return errors.New(e.ErrMissMakeUpRule)
}
// 获取当前考勤周期
//month := common.CurrentMonth()
// 根据补卡的时间 确定 考勤的周期
month := common.CheckAttendanceMonth(req.ApprovalOA.MakeUp.ApplyTimes[0].Date)
month = common.Add25ForMonth(month)
dates := holiday.SplitMonthV1(month, common.YYMMDD)
var queryMakeUpDates []string
// 暂时 使用 当前周期的 日期
queryMakeUpDates = append(queryMakeUpDates, dates...)
// 查询 oa审批
oaApplys, oaApplyErr := oa_logic.QueryOaApplyDates(userInfo.ID, queryMakeUpDates, []int32{e.ApprovalWorkStatusDoing, e.ApprovalWorkStatusOk}, []string{oa_model.TypeMakeUp})
if oaApplyErr != nil {
return oaApplyErr
}
if makeUpRule.Num > int64(len(oaApplys)) {
return nil
}
return errors.New("当月补卡次数已用完")
}
// 新的加班规则
func CheckOverTimeApplyApproval(req *approvalApi.CreateRequest, userInfo login.Info) (err error) {
// 查询 考勤信息
workingTime, workingTimeErr := oa_logic.QueryWorkingTimeInUse(userInfo.PositionUsers, userInfo.ID)
if workingTimeErr != nil {
return workingTimeErr
}
logger.Infof("workingTime: %+v", workingTime)
fmt.Printf("workingTime: %+v", workingTime)
overtimeRule := new(oaApi.Overtime)
for i := 0; i < len(workingTime.OaRules); i++ {
if workingTime.OaRules[i].Keyword == model.OvertimeRule && workingTime.OaRules[i].OvertimeRule != nil {
overtimeRule = workingTime.OaRules[i].OvertimeRule
}
}
// 获取当前考勤周期
month := common.CurrentMonth()
currentMonth := common.Add25ForMonth(month)
dates := holiday.SplitMonthV1(currentMonth, common.YYMMDD)
// 添加 上个考勤周期
if dates[0] == common.CurrentDateStr() {
addDate := common.PreDate().Format(common.YYMMDD)
if !common.IsInArray(addDate, dates) {
dates = append([]string{addDate}, dates...)
}
}
dateMap := make(map[string]string)
for i := 0; i < len(dates); i++ {
dateMap[dates[i]] = dates[i]
}
fmt.Println("========= CheckOverTimeApplyApproval SplitMonthV1 currentMonth ========================= ")
fmt.Printf("=== %+v\n", dates)
fmt.Println("========= CheckOverTimeApplyApproval SplitMonthV1 currentMonth ========================= ")
if overtimeRule.TimeLimit == model.TimeLimitMonth {
if dateMap[req.ApprovalOA.OverTime.ApplyTimes[0].Date] == "" && dateMap[req.ApprovalOA.OverTime.ApplyTimes[1].Date] == "" {
return errors.New("加班时间不在本次考勤周期内")
}
}
if overtimeRule.TimeLimit == model.TimeLimitYesterday {
if common.ConvertApplyTimeToTime(req.ApprovalOA.OverTime.ApplyTimes[0].Date, req.ApprovalOA.OverTime.ApplyTimes[0].Hour).Before(common.PreDateFour()) {
return errors.New("加班开始时间不得早于昨天凌晨4点")
}
}
return nil
}
func CheckLeaveApproval(req *approvalApi.CreateRequest, userInfo login.Info) (err error) {
if len(req.ApprovalOA.Leave.ApplyTimes) != 2 {
return errors.New(e.ErrApplyTime)
}
use, forMoney, canDayOffErr := oa_logic.QueryCanDayOffUserWithWorkTimeTemplate(userInfo)
if canDayOffErr != nil {
return canDayOffErr
}
checkLeaveReq := new(oa_model.CheckLeave)
checkLeaveReq.ApplyType = req.Type
checkLeaveReq.StartTime = req.ApprovalOA.Leave.ApplyTimes[0].Date
checkLeaveReq.M = req.ApprovalOA.Leave.ApplyTimes[0].M
checkLeaveReq.Children = req.ApprovalOA.Leave.Children
checkLeaveReq.Month = req.ApprovalOA.Leave.Month
checkLeaveReq.Dystocia = req.ApprovalOA.Leave.Dystocia
res, err := service.CheckLeaveHandle(checkLeaveReq, userInfo, forMoney)
if err != nil {
return err
}
fmt.Println("========= checkLeaveReq 1 ========================= ")
fmt.Printf("=== %+v\n", checkLeaveReq)
fmt.Println("========= checkLeaveReq 2 ========================= ")
fmt.Println("========= CheckLeaveHandle 1 ========================= ")
fmt.Printf("=== %+v\n", res)
fmt.Println("========= CheckLeaveHandle 2 ========================= ")
if res.OtherLeave.EndTime != "" {
req.ApprovalOA.Leave.ApplyTimes[1].Date = res.OtherLeave.EndTime
req.ApprovalOA.Leave.ApplyTimes[1].M = res.OtherLeave.EndM
if req.Type == oa_model.TypeMatingCheckLeave {
req.ApprovalOA.Leave.Days = 1 // 孕检假 默认是一次一天
}
}
if req.Type == oa_model.TypeAnnualLeave || req.Type == oa_model.TypeParentalLeave || req.Type == oa_model.TypeNursingLeave {
if strings.Split(req.ApprovalOA.Leave.ApplyTimes[0].Date, "-")[0] != time.Now().Format("2006") || strings.Split(req.ApprovalOA.Leave.ApplyTimes[1].Date, "-")[0] != time.Now().Format("2006") {
return errors.New(e.ErrNotInCurrentYear)
}
}
if req.Type == oa_model.TypeNursingLeave || req.Type == oa_model.TypeParentalLeave || req.Type == oa_model.TypeMatingCheckLeave {
if req.ApprovalOA.Leave.Days > res.CommonLeave.Balance {
return errors.New(e.ErrNoBalance)
}
}
//年假新处理
if req.Type == oa_model.TypeAnnualLeave {
date := req.ApprovalOA.Leave.ApplyTimes[0].Date
year, err1 := strconv.ParseInt(date[0:4], 10, 32)
if err1 != nil {
return err1
}
balanceList, err2 := service.GrpcOAImpl.GetLeaveBalanceList(context.Background(), &oaApi.LeaveBalanceListReq{
Page: 1,
PageSize: 10,
StaffUID: userInfo.ID,
Year: int32(year),
})
if err2 != nil {
return err2
}
fmt.Println("===========================新假期余额=======================================")
fmt.Println(year)
fmt.Println(balanceList)
if len(balanceList.Data) < 1 {
return errors.New("未查到假期余额")
}
if req.ApprovalOA.Leave.Days > balanceList.Data[0].AnnualLeave {
return errors.New(e.ErrNoBalance)
}
}
if req.Type == oa_model.TypeBreastFeedingLeave {
if req.ApprovalOA.Leave.Hours > res.CommonLeave.Balance {
return errors.New(e.ErrHourNoBalance)
}
}
if req.Type == oa_model.TypeDayOff {
//调休时间限制上月的26号到本月的25号
fmt.Println("====================================上月的26号到本月的25号=====================================================")
fmt.Println(isDateInRange(req.ApprovalOA.Leave.ApplyTimes[0].Date))
fmt.Println(isDateInRange(req.ApprovalOA.Leave.ApplyTimes[1].Date))
if !isDateInRange(req.ApprovalOA.Leave.ApplyTimes[0].Date) {
return errors.New(e.ErrNotInCurrentMonth)
}
if !isDateInRange(req.ApprovalOA.Leave.ApplyTimes[1].Date) {
return errors.New(e.ErrNotInCurrentMonth)
}
//if strings.Join(strings.Split(req.ApprovalOA.Leave.ApplyTimes[0].Date, "-")[:2], "-") != time.Now().Format("2006-01") || strings.Join(strings.Split(req.ApprovalOA.Leave.ApplyTimes[1].Date, "-")[:2], "-") != time.Now().Format("2006-01") {
// return errors.New(e.ErrNotInCurrentMonth)
//}
// 特殊设置的用户 可以无加班调休
users, userErr := QueryCanDayOffUser()
if userErr != nil {
return userErr
}
if forMoney {
if req.ApprovalOA.Leave.Hours > res.CommonLeave.Balance {
if users[req.SubmitterName] == req.SubmitterID || !use {
return nil
} else {
return errors.New(e.ErrNoBalance)
}
}
} else {
balance, CheckDayOffBalanceErr := service.CheckDayOffBalance(userInfo.ID)
if CheckDayOffBalanceErr != nil {
return CheckDayOffBalanceErr
}
if req.ApprovalOA.Leave.Hours > balance {
return errors.New(e.ErrNoBalance)
}
}
}
if req.ApprovalOA.Leave.Days <= 0 && req.ApprovalOA.Leave.Hours <= 0 {
return errors.New(e.ErrNoBalance)
}
return nil
}
func CheckOutWorkTime(req *approvalApi.CreateRequest) (err error) {
outWorkStartTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.ApprovalOA.OutWork.ApplyTimes[0].Date+" 00:00:00", time.Local)
nowTime, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02")+" 00:00:00", time.Local)
if !outWorkStartTime.Before(nowTime) {
return nil
}
return errors.New("当前外勤申请时间无效")
}
func CheckLeaveApprovalV1(req *approvalApi.CreateRequest) (err error) {
leaveApplyRecordReq := new(oaApi.LeaveApplyInfoReq)
leaveApplyRecordReq.StaffUID = req.SubmitterID
leaveApplyRecordReq.LeaveApplyType = e.CheckLeaveApplyMap[req.Type]
leaveApplyRecordReq.IsOver = e.IsNotOver
leaveApplyRecordRes, err := service.GrpcOAImpl.QueryLeaveApplyInfo(context.Background(), leaveApplyRecordReq)
if err != nil {
fmt.Println("=========== approval_oa QueryLeaveApplyInfo ====== start =====")
fmt.Println(err.Error())
fmt.Println("=========== approval_oa QueryLeaveApplyInfo ====== end =====")
return errors.New("未查询到相关假期余额申请")
}
if req.ApprovalOA.Leave.Days > leaveApplyRecordRes.LeaveApply.LeaveBalance {
return errors.New("假期余额不足")
}
return nil
}
func CheckLeaveApply(req *approvalApi.CreateRequest, userInfo login.Info) (err error) {
//leaveApplyRecordReq := new(oaApi.LeaveApplyInfoReq)
//leaveApplyRecordReq.StaffUID = req.SubmitterID
//leaveApplyRecordReq.LeaveApplyType = req.Type
//leaveApplyRecordReq.IsOver = e.IsNotOver
//leaveApplyRecordReq.UsefulTime = time.Now().Format("2006")
//leaveApplyRecordRes, err := service.GrpcOAImpl.QueryLeaveApplyInfo(context.Background(), leaveApplyRecordReq)
//if err != nil && err.Error() != e.ErrNotHaveBalance {
// fmt.Println("=========== approval_oa QueryLeaveApplyInfo ====== start =====")
// fmt.Println(err.Error())
// fmt.Println("=========== approval_oa QueryLeaveApplyInfo ====== end =====")
// //return errors.New("未查询到相关假期余额申请")
// return errors.New(e.ErrNotHaveBalance)
//}
//
//if leaveApplyRecordRes.LeaveApply != nil {
// return errors.New(e.ErrHaveBalanceApply)
//}
reqList := &approvalApi.ListRequest{}
reqList.Domain = req.Domain
reqList.Type = req.Type
reqList.UserID = userInfo.ID
reqList.Status = 1
res1, err := service.ApprovalProvider.MySubmit(context.Background(), reqList)
if err != nil {
return errors.New(e.ErrNotHaveBalance)
}
if len(res1.Data) > 0 {
for i := 0; i < len(res1.Data); i++ {
if strings.Split(strings.Split(res1.Data[i].ApprovalOA.LeaveApply.ActionTime, " ")[0], "-")[0] == time.Now().Format("2006") {
return errors.New(e.ErrHaveBalanceApply)
}
}
}
reqList.Status = 2
res2, err := service.ApprovalProvider.MySubmit(context.Background(), reqList)
if err != nil {
return errors.New(e.ErrNotHaveBalance)
}
for i := 0; i < len(res2.Data); i++ {
if strings.Split(strings.Split(res2.Data[i].ApprovalOA.LeaveApply.ActionTime, " ")[0], "-")[0] == time.Now().Format("2006") {
return errors.New(e.ErrHaveBalanceApply)
}
}
reqList.Status = 6
res3, err := service.ApprovalProvider.MySubmit(context.Background(), reqList)
if err != nil {
return errors.New(e.ErrNotHaveBalance)
}
for i := 0; i < len(res3.Data); i++ {
if strings.Split(strings.Split(res3.Data[i].ApprovalOA.LeaveApply.ActionTime, " ")[0], "-")[0] == time.Now().Format("2006") {
return errors.New(e.ErrHaveBalanceApply)
}
}
return nil
}
func CheckLeaveTime(req *approvalApi.CreateRequest, userInfo login.Info) (err error) {
queryOaApplyReq := new(oaApi.ApplyRecordReq)
queryOaApplyReq.StaffUID = req.SubmitterID
queryOaApplyReq.Page = 1
queryOaApplyReq.PageSize = 9999
if req.ApprovalOA.Leave != nil {
queryOaApplyReq.BeginTime = req.ApprovalOA.Leave.ApplyTimes[0].Date
queryOaApplyReq.EndTime = req.ApprovalOA.Leave.ApplyTimes[len(req.ApprovalOA.Leave.ApplyTimes)-1].Date
} else if req.ApprovalOA.OverTime != nil {
queryOaApplyReq.BeginTime = req.ApprovalOA.OverTime.ApplyTimes[0].Date
queryOaApplyReq.EndTime = req.ApprovalOA.OverTime.ApplyTimes[len(req.ApprovalOA.OverTime.ApplyTimes)-1].Date
// 如果是加班 则 剔除 外勤申请
queryOaApplyReq.ApplyType = append(queryOaApplyReq.ApplyType, common.WithOutOutworkApply...)
} else if req.ApprovalOA.OutWork != nil {
queryOaApplyReq.BeginTime = req.ApprovalOA.OutWork.ApplyTimes[0].Date
queryOaApplyReq.EndTime = req.ApprovalOA.OutWork.ApplyTimes[len(req.ApprovalOA.OutWork.ApplyTimes)-1].Date
// 如果是外勤申请 则 剔除 加班
queryOaApplyReq.ApplyType = append(queryOaApplyReq.ApplyType, common.WithOutOvertimeApply...)
} else if req.ApprovalOA.GoOut != nil { //外出
queryOaApplyReq.BeginTime = req.ApprovalOA.GoOut.ApplyTimes[0].Date
queryOaApplyReq.EndTime = req.ApprovalOA.GoOut.ApplyTimes[len(req.ApprovalOA.GoOut.ApplyTimes)-1].Date
queryOaApplyReq.ApplyType = append(queryOaApplyReq.ApplyType, common.WithGoOutApply...)
}
queryOaApplyReq.ApplyStatus = []int32{e.ApprovalWorkStatusDoing, e.ApprovalWorkStatusOk, e.ApprovalWorkStatusRevoking}
queryOaApplyRes, err := service.GrpcOAImpl.QueryOaApply(context.Background(), queryOaApplyReq)
if err != nil {
return err
}
// 查询 考勤信息
workingTime, workingTimeErr := oa_logic.QueryWorkingTimeInUse(userInfo.PositionUsers, userInfo.ID)
if workingTimeErr != nil {
return workingTimeErr
}
// 当前的申请
applyStartTimeStr := ""
applyEndTimeStr := ""
if req.ApprovalOA.Leave != nil {
applyTimes := make([]oa_model.ApplyTime, 0)
_ = copier.CopyWithOption(&applyTimes, req.ApprovalOA.Leave.ApplyTimes, copier.Option{DeepCopy: true})
applyStartTimeStr, applyEndTimeStr = oa_logic.ConvertApplyTime(applyTimes, workingTime.Time)
} else if req.ApprovalOA.OverTime != nil {
applyTimes := make([]oa_model.ApplyTime, 0)
_ = copier.CopyWithOption(&applyTimes, req.ApprovalOA.OverTime.ApplyTimes, copier.Option{DeepCopy: true})
applyStartTimeStr, applyEndTimeStr = oa_logic.ConvertApplyTime(applyTimes, workingTime.Time)
} else if req.ApprovalOA.OutWork != nil {
applyTimes := make([]oa_model.ApplyTime, 0)
_ = copier.CopyWithOption(&applyTimes, req.ApprovalOA.OutWork.ApplyTimes, copier.Option{DeepCopy: true})
applyStartTimeStr, applyEndTimeStr = oa_logic.ConvertApplyTime(applyTimes, workingTime.Time)
} else if req.ApprovalOA.GoOut != nil {
applyTimes := make([]oa_model.ApplyTime, 0)
_ = copier.CopyWithOption(&applyTimes, req.ApprovalOA.GoOut.ApplyTimes, copier.Option{DeepCopy: true})
applyStartTimeStr, applyEndTimeStr = oa_logic.ConvertApplyTime(applyTimes, workingTime.Time)
}
applyStartTime, _ := time.ParseInLocation("2006-01-02 15:04:05", applyStartTimeStr, time.Local)
applyEndTime, _ := time.ParseInLocation("2006-01-02 15:04:05", applyEndTimeStr, time.Local)
//fmt.Println("======== applyStartTime ,applyEndTime 1 =================")
//fmt.Println("======= applyStartTime", applyStartTime)
//fmt.Println("======= applyEndTime", applyEndTime)
//fmt.Println("======== applyStartTime ,applyEndTime 1 =================")
// 历史的申请
for i := 0; i < len(queryOaApplyRes.Data); i++ {
hadApplyStartTimeStr := ""
hadApplyEndTimeStr := ""
if queryOaApplyRes.Data[i] != nil {
applyTimes := make([]oa_model.ApplyTime, 0)
//如果是出差,循环走流程
if len(queryOaApplyRes.Data[i].ItineraryList) > 0 {
for _, itinerary := range queryOaApplyRes.Data[i].ItineraryList {
applyTimes2 := make([]oa_model.ApplyTime, 0)
_ = copier.CopyWithOption(&applyTimes2, itinerary.ApplyTimes, copier.Option{DeepCopy: true})
hadApplyStartTimeStr, hadApplyEndTimeStr = oa_logic.ConvertApplyTime(applyTimes2, workingTime.Time)
hadApplyStartTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hadApplyStartTimeStr, time.Local)
hadApplyEndTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hadApplyEndTimeStr, time.Local)
if (applyStartTime.After(hadApplyStartTime) && applyStartTime.Before(hadApplyEndTime)) || (applyEndTime.After(hadApplyStartTime) && applyEndTime.Before(hadApplyEndTime)) {
return errors.New(e.ErrSelectTimeRepeat)
}
// 当前申请时间 不包含 历史申请的时间段
/*if (!hadApplyStartTime.Before(applyStartTime) && !hadApplyStartTime.After(applyEndTime)) || (!hadApplyEndTime.Before(applyStartTime) && !hadApplyEndTime.After(applyEndTime)) {
return errors.New(e.ERR_SELECT_TIME_REPEAT)
}*/
if (hadApplyStartTime.After(applyStartTime) && hadApplyStartTime.Before(applyEndTime)) || (hadApplyEndTime.After(applyStartTime) && hadApplyEndTime.Before(applyEndTime)) {
return errors.New(e.ErrSelectTimeRepeat)
}
if applyStartTime.Equal(hadApplyStartTime) || applyEndTime.Equal(hadApplyEndTime) {
return errors.New(e.ErrSelectTimeRepeat)
}
}
return
} else {
_ = copier.CopyWithOption(&applyTimes, queryOaApplyRes.Data[i].ApplyTimes, copier.Option{DeepCopy: true})
hadApplyStartTimeStr, hadApplyEndTimeStr = oa_logic.ConvertApplyTime(applyTimes, workingTime.Time)
}
} else if req.ApprovalOA.OverTime != nil {
applyTimes := make([]oa_model.ApplyTime, 0)
_ = copier.CopyWithOption(&applyTimes, req.ApprovalOA.OverTime.ApplyTimes, copier.Option{DeepCopy: true})
hadApplyStartTimeStr, hadApplyEndTimeStr = oa_logic.ConvertApplyTime(applyTimes, workingTime.Time)
} else if req.ApprovalOA.OutWork != nil {
applyTimes := make([]oa_model.ApplyTime, 0)
_ = copier.CopyWithOption(&applyTimes, req.ApprovalOA.OutWork.ApplyTimes, copier.Option{DeepCopy: true})
hadApplyStartTimeStr, hadApplyEndTimeStr = oa_logic.ConvertApplyTime(applyTimes, workingTime.Time)
}
hadApplyStartTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hadApplyStartTimeStr, time.Local)
hadApplyEndTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hadApplyEndTimeStr, time.Local)
//fmt.Println("======== hadApplyStartTime ,hadApplyEndTime 2 =================")
//fmt.Println("======= hadApplyStartTime", hadApplyStartTime)
//fmt.Println("======= hadApplyEndTime", hadApplyEndTime)
//fmt.Println("======== hadApplyStartTime ,hadApplyEndTime 2 =================")
// 当前申请时间 不在 历史申请的时间段内
/*if (!applyStartTime.Before(hadApplyStartTime) && !applyStartTime.After(hadApplyEndTime)) || (!applyEndTime.Before(hadApplyStartTime) && !applyEndTime.After(hadApplyEndTime)) {
return errors.New(e.ERR_SELECT_TIME_REPEAT)
}*/
if (applyStartTime.After(hadApplyStartTime) && applyStartTime.Before(hadApplyEndTime)) || (applyEndTime.After(hadApplyStartTime) && applyEndTime.Before(hadApplyEndTime)) {
return errors.New(e.ErrSelectTimeRepeat)
}
// 当前申请时间 不包含 历史申请的时间段
/*if (!hadApplyStartTime.Before(applyStartTime) && !hadApplyStartTime.After(applyEndTime)) || (!hadApplyEndTime.Before(applyStartTime) && !hadApplyEndTime.After(applyEndTime)) {
return errors.New(e.ERR_SELECT_TIME_REPEAT)
}*/
if (hadApplyStartTime.After(applyStartTime) && hadApplyStartTime.Before(applyEndTime)) || (hadApplyEndTime.After(applyStartTime) && hadApplyEndTime.Before(applyEndTime)) {
return errors.New(e.ErrSelectTimeRepeat)
}
if applyStartTime.Equal(hadApplyStartTime) || applyEndTime.Equal(hadApplyEndTime) {
return errors.New(e.ErrSelectTimeRepeat)
}
}
return
}
// CheckBusinessTripTime 出差专用
func CheckBusinessTripTime(req *approvalApi.CreateRequest, userInfo login.Info) (err error) {
queryOaApplyReq := new(oaApi.ApplyRecordReq)
queryOaApplyReq.StaffUID = req.SubmitterID
queryOaApplyReq.Page = 1
queryOaApplyReq.PageSize = 9999
queryOaApplyReq.ApplyStatus = []int32{e.ApprovalWorkStatusDoing, e.ApprovalWorkStatusOk, e.ApprovalWorkStatusRevoking}
queryOaApplyReq.ApplyType = append(queryOaApplyReq.ApplyType, common.WithBusinessTripApply...)
if req.ApprovalOA.BusinessTrip != nil {
//每个行程都得走一遍检查流程
for _, itinerary := range req.ApprovalOA.BusinessTrip.ItineraryList {
queryOaApplyReq.BeginTime = itinerary.ApplyTimes[0].Date
queryOaApplyReq.EndTime = itinerary.ApplyTimes[len(itinerary.ApplyTimes)-1].Date
queryOaApplyRes, err := service.GrpcOAImpl.QueryOaApply(context.Background(), queryOaApplyReq)
if err != nil {
return err
}
// 查询 考勤信息
workingTime, workingTimeErr := oa_logic.QueryWorkingTimeInUse(userInfo.PositionUsers, userInfo.ID)
if workingTimeErr != nil {
return workingTimeErr
}
// 当前的申请
applyStartTimeStr := ""
applyEndTimeStr := ""
applyTimes := make([]oa_model.ApplyTime, 0)
_ = copier.CopyWithOption(&applyTimes, itinerary.ApplyTimes, copier.Option{DeepCopy: true})
applyStartTimeStr, applyEndTimeStr = oa_logic.ConvertApplyTime(applyTimes, workingTime.Time)
applyStartTime, _ := time.ParseInLocation("2006-01-02 15:04:05", applyStartTimeStr, time.Local)
applyEndTime, _ := time.ParseInLocation("2006-01-02 15:04:05", applyEndTimeStr, time.Local)
// 历史的申请
if len(queryOaApplyRes.Data) > 0 {
for i := 0; i < len(queryOaApplyRes.Data); i++ {
hadApplyStartTimeStr := ""
hadApplyEndTimeStr := ""
applyTimes2 := make([]oa_model.ApplyTime, 0)
_ = copier.CopyWithOption(&applyTimes2, queryOaApplyRes.Data[i].ApplyTimes, copier.Option{DeepCopy: true})
hadApplyStartTimeStr, hadApplyEndTimeStr = oa_logic.ConvertApplyTime(applyTimes2, workingTime.Time)
hadApplyStartTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hadApplyStartTimeStr, time.Local)
hadApplyEndTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hadApplyEndTimeStr, time.Local)
if (applyStartTime.After(hadApplyStartTime) && applyStartTime.Before(hadApplyEndTime)) || (applyEndTime.After(hadApplyStartTime) && applyEndTime.Before(hadApplyEndTime)) {
return errors.New(e.ErrSelectTimeRepeat)
}
if (hadApplyStartTime.After(applyStartTime) && hadApplyStartTime.Before(applyEndTime)) || (hadApplyEndTime.After(applyStartTime) && hadApplyEndTime.Before(applyEndTime)) {
return errors.New(e.ErrSelectTimeRepeat)
}
if applyStartTime.Equal(hadApplyStartTime) || applyEndTime.Equal(hadApplyEndTime) {
return errors.New(e.ErrSelectTimeRepeat)
}
}
}
}
}
return err
}
func isDateInRange(dateStr string) bool {
t, _ := time.Parse("2006-01-02", dateStr)
now := time.Now().Format("2006-01-02")
t1, _ := time.Parse("2006-01-02", now)
//上月25号
lastMonth25 := time.Date(t1.Year(), t1.Month()-1, 25, 0, 0, 0, 0, t1.Location())
//本月25号
thisMonth25 := time.Date(t1.Year(), t1.Month(), 25, 0, 0, 0, 0, t1.Location())
//下月26号
nextMonth26 := time.Date(t1.Year(), t1.Month()+1, 26, 0, 0, 0, 0, t1.Location())
//如果传入时间在本月25号之后,传入时间就得在下月26号之前
if t.After(thisMonth25) {
return t.Before(nextMonth26)
} else {
//如果不在本月25号之后则传入时间必须在上月25号之后
return t.After(lastMonth25)
}
}
func CheckLeaveApplyIsUse(req *approvalApi.CreateRequest) (err error) {
queryOaApplyReq := new(oaApi.ApplyRecordReq)
queryOaApplyReq.StaffUID = req.SubmitterID
queryOaApplyReq.ApplyType = []string{oa_model.LeaveApplyOaApplyMap[req.Type]}
// 当前月 的 年初 和 年底
queryOaApplyReq.BeginTime = time.Now().Format("2006") + "-01-01" + " 00:00:00"
queryOaApplyReq.EndTime = time.Now().Format("2006") + "-12-31" + " 23:59:59"
queryOaApplyReq.ApplyStatus = []int32{e.ApprovalWorkStatusDoing, e.ApprovalWorkStatusOk, e.ApprovalWorkStatusRevoking}
queryOaApplyRes, err := service.GrpcOAImpl.QueryOaApply(context.Background(), queryOaApplyReq)
if err != nil {
return err
}
if queryOaApplyRes.Total > 0 {
return errors.New(e.ErrIsUsed)
}
return nil
}
const (
CanDayOff = "canDayOff" // 无需加班,亦可调休
)
func QueryCanDayOffUser() (users map[string]uint64, err error) {
users = make(map[string]uint64, 0)
req := &oaApi.OaSettingReq{
Keyword: CanDayOff,
}
res, err := service.GrpcOAImpl.QueryOaSetting(context.Background(), req)
if err != nil {
return nil, errors.New(e.ErrQueryOaSetting)
}
if len(res.Data) > 0 {
for i := 0; i < len(res.Data); i++ {
if res.Data[i].Value.CanDayOff != nil {
for j := 0; j < len(res.Data[i].Value.CanDayOff); j++ {
users[res.Data[i].Value.CanDayOff[j].Name] = res.Data[i].Value.CanDayOff[j].ID
}
}
}
}
return users, nil
}
/*// 使用考勤组设置的 是否用于加班调休
func QueryCanDayOffUserWithWorkTimeTemplate(userInfo login.Info) (use bool, forMoney bool, err error) {
canDayOff := false
forMoney = false
// 查询 考勤信息
workingTime, workingTimeErr := oa_logic.QueryWorkingTimeInUse(userInfo.PositionUsers, userInfo.ID)
if workingTimeErr != nil {
canDayOff = true
return canDayOff, forMoney, workingTimeErr
}
logger.Infof("workingTime: %+v", workingTime)
fmt.Printf("workingTime: %+v", workingTime)
if len(workingTime.OaRules) > 0 {
for i := 0; i < len(workingTime.OaRules); i++ {
if workingTime.OaRules[i].Keyword == model.OvertimeRule && workingTime.OaRules[i].OvertimeRule != nil {
if workingTime.OaRules[i].OvertimeRule.ForDayOff == model.ForDayOffYes {
canDayOff = true
}
if workingTime.OaRules[i].OvertimeRule.ForDayOff == model.ForDayOffYes && workingTime.OaRules[i].OvertimeRule.ForMoney == model.ForMoneyYes {
forMoney = true
}
}
}
}
return canDayOff, forMoney, nil
}
*/