enterprise/service/staff_user_vacation.go

108 lines
2.4 KiB
Go

package service
import (
"enterprise/common/dao"
"enterprise/common/model"
log "github.com/sirupsen/logrus"
"time"
)
type StaffUserVacation struct {
user *model.StaffUser
corp *model.Corp
month string
vacations []*model.ApprovalVacation
vacationsMap map[string]float64
}
func NewStaffUserVacation(corp *model.Corp, user *model.StaffUser, month string) *StaffUserVacation {
uv := &StaffUserVacation{
user: user,
corp: corp,
month: month,
vacationsMap: make(map[string]float64),
}
uv.loadArr()
uv.loadMaps()
return uv
}
func (v *StaffUserVacation) loadMaps() {
vacationMap := make(map[string]float64)
for _, vac := range v.vacations {
startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", vac.VacationStartTime, time.Local)
endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", vac.VacationEndTime, time.Local)
startDay := startTime.Format("2006-01-02")
endDay := endTime.Format("2006-01-02")
if startDay == endDay {
vacationMap[startDay] += vac.VacationDuration
continue
}
day := startTime.Format("2006-01-02")
for {
if day == startDay {
}
if day == endDay {
break
}
startTime = startTime.AddDate(0, 0, 1)
day = startTime.Format("2006-01-02")
}
}
//每天请假时长不超过1天
for k, duration := range vacationMap {
if duration > 1 {
vacationMap[k] = 1
}
}
v.vacationsMap = vacationMap
return
}
func (v *StaffUserVacation) loadArr() {
// 休假申请
approveVacations, err := dao.NewApprovalVacationDao().GetByUsername(v.user.CorpId, v.user.Username, v.month, "")
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
v.vacations = approveVacations
}
func (v *StaffUserVacation) GetTotalDay() float64 {
holiday := float64(0)
for _, vac := range v.vacations {
startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", vac.VacationStartTime, time.Local)
endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", vac.VacationEndTime, time.Local)
//同一天请假时长大于8小时算一天
if startTime.Format("2006-01-02") == endTime.Format("2006-01-02") && vac.VacationDuration > 1 {
holiday += 1
} else {
holiday += vac.VacationDuration
}
}
return holiday
}
func (v *StaffUserVacation) GetSurplusDay() float64 {
suplusDays := float64(0)
for _, duration := range v.vacationsMap {
if duration < 1 {
suplusDays += 1 - duration
}
}
return suplusDays
}