From 9868bd0923538325cff47bd85051617f1fad71fb Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Wed, 2 Apr 2025 15:46:39 +0800 Subject: [PATCH] staff_user --- common/model/corp.go | 2 +- server/controller/base.go | 19 +++++ server/controller/salary.go | 22 ++++++ server/server.go | 1 + server/service/qyweixin_approve.go | 2 +- service/staff_salary.go | 14 +++- service/staff_user_checkin.go | 13 ++++ service/staff_user_vacation.go | 107 +++++++++++++++++++++++++++++ worker/approval.go | 2 +- 9 files changed, 176 insertions(+), 6 deletions(-) create mode 100644 service/staff_user_checkin.go create mode 100644 service/staff_user_vacation.go diff --git a/common/model/corp.go b/common/model/corp.go index 6e59de6..468d336 100644 --- a/common/model/corp.go +++ b/common/model/corp.go @@ -26,7 +26,7 @@ type CorpConfig struct { SocialDeduct string `json:"social_deduct"` HouseDeduct string `json:"house_deduct"` - WorkerHouer string `json:"worker_houer"` + WorkerHour string `json:"worker_hour"` } type Corp struct { diff --git a/server/controller/base.go b/server/controller/base.go index 51969f8..31aa65a 100644 --- a/server/controller/base.go +++ b/server/controller/base.go @@ -1,6 +1,8 @@ package controller import ( + "bytes" + "encoding/json" "enterprise/common/config" "enterprise/common/dao" "enterprise/common/model" @@ -10,6 +12,7 @@ import ( "github.com/gogap/errors" log "github.com/sirupsen/logrus" "github.com/smbrave/goutil" + "io" "net/http" "runtime" "time" @@ -36,6 +39,22 @@ func (c *Base) Before(ctx *gin.Context) { panic(config.ErrParam.New().Append(err)) } + //记录请求参数 + ctx.Keys[session.ContextHeader] = header + if ctx.Request.Method == http.MethodPut || ctx.Request.Method == http.MethodPost { + bodyBytes, err := io.ReadAll(ctx.Request.Body) + if err != nil { + panic(config.ErrParam.New().Append(err)) + } + defer ctx.Request.Body.Close() + + bodyReader := bytes.NewReader(bodyBytes) + ctx.Request.Body = io.NopCloser(bodyReader) + var reqInf interface{} + json.Unmarshal(bodyBytes, &reqInf) + ctx.Keys[session.ContextRequest] = reqInf + } + if header.Token == "" { // 新用户没有extra所以extra中的配置不能对新用户生效 ctx.Keys[session.ContextSession] = session.NewAdminSession(&header, nil) return diff --git a/server/controller/salary.go b/server/controller/salary.go index 1098a8d..a887ac0 100644 --- a/server/controller/salary.go +++ b/server/controller/salary.go @@ -2,9 +2,12 @@ package controller import ( "enterprise/common/config" + "enterprise/common/dao" + "enterprise/common/model" "enterprise/server/api" "enterprise/server/service" "enterprise/server/session" + "enterprise/worker" "github.com/gin-gonic/gin" "github.com/spf13/cast" "net/http" @@ -70,3 +73,22 @@ func (s *Salary) Delete(ctx *gin.Context) { service.NewStaffSalary().Delete(ctx.Keys[session.ContextSession].(*session.AdminSession), id) ctx.JSON(http.StatusOK, session.NewRspOk()) } + +func (s *Salary) SyncStaffSalary(ctx *gin.Context) { + cid := cast.ToInt64(ctx.Query("cid")) + + var err error + var corp *model.Corp + if cid != 0 { + corp, err = dao.NewCorpDao().Get(cid) + session.CheckDBError(err) + } else { + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + corp, err = dao.NewCorpDao().GetByHost(sess.GetHeader().Host) + session.CheckDBError(err) + } + + session.CheckNilError(corp, "域名未绑定") + go new(worker.Staff).SyncStaffSalary(corp.Id, "") + ctx.JSON(http.StatusOK, session.NewRspOk()) +} diff --git a/server/server.go b/server/server.go index 335451f..d7931fe 100644 --- a/server/server.go +++ b/server/server.go @@ -30,6 +30,7 @@ func initRoutge(engine *gin.Engine) { group.GET("/staff/salary", controller.NewStaff().Salary) noTokenGroup.POST("/staff/login", controller.NewStaff().Login) + noTokenGroup.Any("/sync/salary", controller.NewSalary().SyncStaffSalary) apiGroup.GET("/staff/salary", controller.NewSalary().List) apiGroup.POST("/staff/salary", controller.NewSalary().Create) diff --git a/server/service/qyweixin_approve.go b/server/service/qyweixin_approve.go index 0bfc8e0..99f35b2 100644 --- a/server/service/qyweixin_approve.go +++ b/server/service/qyweixin_approve.go @@ -136,7 +136,7 @@ func (a *Approve) handleVacation(detail *qyweixin.ApproveDetail, spStatus int) { newData := new(model.ApprovalVacation) newData.From(detail) newData.CorpId = a.corp.Id - newData.VacationDuration = newData.VacationDuration / (float64(3600) * cast.ToFloat64(a.corp.GetConfig().WorkerHouer)) + newData.VacationDuration = newData.VacationDuration / (float64(3600) * cast.ToFloat64(a.corp.GetConfig().WorkerHour)) dbDao := dao.NewApprovalVacationDao() old, err := dbDao.GetBySpNo(a.corp.Id, detail.SpNo) diff --git a/service/staff_salary.go b/service/staff_salary.go index 7ac65a3..a3135cb 100644 --- a/service/staff_salary.go +++ b/service/staff_salary.go @@ -214,6 +214,7 @@ func (s *StaffSalary) getRealVacationDay(month string) (float64, float64) { holiday := float64(0) surplusHoliday := float64(0) + holidayMap := make(map[string]float64) for _, vac := range approveVacations { 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) @@ -224,8 +225,15 @@ func (s *StaffSalary) getRealVacationDay(month string) (float64, float64) { } else { holiday += vac.VacationDuration //不是整天数,把剩余的算上 - span := vac.VacationDuration - math.Floor(vac.VacationDuration) - surplusHoliday += goutil.If(math.Abs(span) < 0.000001, 0, 1-span) + if _, ok := holidayMap[vac.VacationDate]; !ok { + span := vac.VacationDuration - math.Floor(vac.VacationDuration) + if _, ok := holidayMap[vac.VacationDate]; !ok { + holidayMap[vac.VacationDate] = goutil.If(math.Abs(span) < 0.000001, 0, 1-span) + } else { + + } + + } } } return holiday, surplusHoliday @@ -297,7 +305,7 @@ func (s *StaffSalary) getRealWorkDay(month string) float64 { //其他按工作时长结算 if checkin.IsCheckin() { - shouldAttendHour := cast.ToFloat64(corp.GetConfig().WorkerHouer) + shouldAttendHour := cast.ToFloat64(corp.GetConfig().WorkerHour) lackSecond := int64(float64(3600)*(1.5+shouldAttendHour)) - (checkin.EndTime - checkin.StartTime) //加上午休的1.5小时 if lackSecond > 0 { lackHour := float64(lackSecond/3600 + 1) //按小时取整 diff --git a/service/staff_user_checkin.go b/service/staff_user_checkin.go new file mode 100644 index 0000000..3fd61e2 --- /dev/null +++ b/service/staff_user_checkin.go @@ -0,0 +1,13 @@ +package service + +import "enterprise/common/model" + +type StaffUserCheckin struct { + user *model.StaffUser +} + +func NewStaffUserCheckin(user *model.StaffUser) *StaffUserCheckin { + return &StaffUserCheckin{ + user: user, + } +} diff --git a/service/staff_user_vacation.go b/service/staff_user_vacation.go new file mode 100644 index 0000000..c31913f --- /dev/null +++ b/service/staff_user_vacation.go @@ -0,0 +1,107 @@ +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 +} diff --git a/worker/approval.go b/worker/approval.go index 3fe803d..c0a28f1 100644 --- a/worker/approval.go +++ b/worker/approval.go @@ -148,7 +148,7 @@ func (s *Approval) saveVacation(detail *qyweixin.ApproveDetail) { newData := new(model.ApprovalVacation) newData.CorpId = s.corp.Id newData.From(detail) - newData.VacationDuration = newData.VacationDuration / (cast.ToFloat64(s.corpConfig.WorkerHouer) * float64(3600)) + newData.VacationDuration = newData.VacationDuration / (cast.ToFloat64(s.corpConfig.WorkerHour) * float64(3600)) old, err := dbDao.GetBySpNo(s.corp.Id, detail.SpNo) if err != nil {