From 391b861a87591c7a92d23f26ca12d4b514120520 Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Thu, 6 Mar 2025 17:45:28 +0800 Subject: [PATCH] chcckin --- common/config/error.go | 15 ++- common/dao/checkin.go | 24 ++++ common/dao/staff_token.go | 81 +++++++++++++ common/model/staff_salary.go | 2 +- common/model/staff_token.go | 9 ++ common/session/base.go | 111 ++++++++++++++++++ common/session/session.go | 50 ++++++++ server/api/checkin.go | 57 +++++++++ server/controller/base.go | 51 ++++++++ server/controller/checkin.go | 41 +++++++ server/controller/salary.go | 1 + server/server.go | 16 ++- server/service/checkin.go | 34 ++++++ service/checkin.go | 79 +++++++++++++ .../salary_calculator_1002.go | 10 +- service/staff_salary.go | 2 +- worker/checkin.go | 98 ++-------------- worker/worker.go | 8 +- 18 files changed, 581 insertions(+), 108 deletions(-) create mode 100644 common/dao/staff_token.go create mode 100644 common/model/staff_token.go create mode 100644 common/session/base.go create mode 100644 common/session/session.go create mode 100644 server/api/checkin.go create mode 100644 server/controller/checkin.go create mode 100644 server/controller/salary.go create mode 100644 server/service/checkin.go create mode 100644 service/checkin.go diff --git a/common/config/error.go b/common/config/error.go index fddb220..cc0ad6c 100644 --- a/common/config/error.go +++ b/common/config/error.go @@ -7,9 +7,14 @@ var ( // 系统错误码 ErrOK = errors.T(0, "操作成功") - ErrPriv = errors.T(196, "没有权限") - ErrDb = errors.T(197, "数据库错误") - ErrRemote = errors.T(198, "第三方错误") - ErrInternal = errors.T(199, "内部错误") - ErrParam = errors.T(400, "参数错误") + ErrPriv = errors.T(196, "没有权限") + ErrDb = errors.T(197, "数据库错误") + ErrRemote = errors.T(198, "第三方错误") + ErrInternal = errors.T(199, "内部错误") + ErrParam = errors.T(400, "参数错误") + ErrNoData = errors.T(401, "没有数据") + ErrTokenExpire = errors.T(402, "登录过期") //客户端有特殊逻辑勿动,跳转登录页 + ErrTokenInvaild = errors.T(403, "TOKEN无效") //客户端有特殊逻辑勿动,跳转登录页 + ErrNoToken = errors.T(404, "没有TOKEN") //客户端有特殊逻辑勿动,跳转登录页 + ErrNoPerm = errors.T(405, "没有权限") ) diff --git a/common/dao/checkin.go b/common/dao/checkin.go index 69aa39a..f294943 100644 --- a/common/dao/checkin.go +++ b/common/dao/checkin.go @@ -110,3 +110,27 @@ func (d *CheckinDao) QueryDay(corpId int64, day string) ([]*model.Checkin, error } return u, nil } + +func (d *CheckinDao) QueryAdmin(page, size int, corpId int64, username, startDay, endDay string) ([]*model.Checkin, int64, error) { + tx := GetDB().Table(d.TableName()) + tx = tx.Where("corp_id = ?", corpId) + if startDay != "" { + tx = tx.Where("day >= ?", startDay) + } + if endDay != "" { + tx = tx.Where("day <= ?", endDay) + } + if username != "" { + tx = tx.Where("username = ?", username) + } + var count int64 + tx.Count(&count) + + tx.Offset((page - 1) * size).Limit(size) + var u []*model.Checkin + tx = tx.Find(&u) + if tx.Error != nil { + return nil, 0, tx.Error + } + return u, count, nil +} diff --git a/common/dao/staff_token.go b/common/dao/staff_token.go new file mode 100644 index 0000000..6a1b1c3 --- /dev/null +++ b/common/dao/staff_token.go @@ -0,0 +1,81 @@ +package dao + +import ( + "enterprise/common/model" + "github.com/smbrave/goutil" + "gorm.io/gorm" + "time" +) + +type StaffTokenDao struct { +} + +func NewStaffTokenDao() *StaffTokenDao { + return &StaffTokenDao{} +} + +func (d *StaffTokenDao) TableName() string { + return "staff_token" +} + +func (d *StaffTokenDao) Create(o *model.StaffToken) (int64, error) { + o.CreateTime = time.Now().Unix() + res := GetDB().Table(d.TableName()).Create(o) + return o.Id, res.Error +} + +func (d *StaffTokenDao) Update(o *model.StaffToken, fields ...string) error { + tx := GetDB().Table(d.TableName()) + tx.Where("id = ?", o.Id) + if len(fields) == 0 { + res := tx.Save(o) + return res.Error + } + + dat := goutil.ModelFieldValue(o, fields...) + return tx.Updates(dat).Error +} + +func (d *StaffTokenDao) Delete(id int64) error { + res := GetDB().Table(d.TableName()).Delete(&model.StaffToken{}, id) + return res.Error +} + +func (d *StaffTokenDao) Get(id int64) (*model.StaffToken, error) { + var u model.StaffToken + tx := GetDB().Table(d.TableName()) + tx = tx.Where("id = ?", id) + res := tx.First(&u) + if res.Error == gorm.ErrRecordNotFound { + return nil, nil + } + + if res.Error != nil { + return nil, res.Error + } + return &u, nil +} + +func (d *StaffTokenDao) GetByToken(token string) (*model.StaffToken, error) { + var o model.StaffToken + tx := GetDB().Table(d.TableName()) + tx = tx.Where("token = ?", token) + res := tx.First(&o) + + if res.Error == gorm.ErrRecordNotFound { + return nil, nil + } + + if res.Error != nil { + return nil, res.Error + } + return &o, nil +} + +func (d *StaffTokenDao) DeleteHistory(lastDay int) (int64, error) { + tx := GetDB().Table(d.TableName()) + deleteTime := time.Now().AddDate(0, 0, 0-lastDay).Unix() + tx.Where("expire_time <= ?", deleteTime) + tx.Delete(&model.StaffToken{}) + return tx.RowsAffected, tx.Error +} diff --git a/common/model/staff_salary.go b/common/model/staff_salary.go index 5483f7d..853a01e 100644 --- a/common/model/staff_salary.go +++ b/common/model/staff_salary.go @@ -12,7 +12,7 @@ type StaffSalary struct { Id int64 CorpId int64 UserId int64 - UserName string + Username string Month string Salary float64 AttendSalary float64 diff --git a/common/model/staff_token.go b/common/model/staff_token.go new file mode 100644 index 0000000..4da64e4 --- /dev/null +++ b/common/model/staff_token.go @@ -0,0 +1,9 @@ +package model + +type StaffToken struct { + Id int64 + UserId int64 + Token string + CreateTime int64 + ExpireTime int64 +} diff --git a/common/session/base.go b/common/session/base.go new file mode 100644 index 0000000..fa2dfa6 --- /dev/null +++ b/common/session/base.go @@ -0,0 +1,111 @@ +package session + +import ( + "encoding/json" + "enterprise/common/config" + "github.com/smbrave/goutil" + "reflect" +) + +type BaseResponse struct { + Code int `json:"code"` + Message string `json:"message"` +} + +type CommonResponse struct { + BaseResponse + Data interface{} `json:"data"` +} + +type RawResponse struct { + BaseResponse + Data json.RawMessage `json:"data"` +} + +type ListResponse struct { + BaseResponse + Data struct { + Total int64 `json:"total"` + TotalAmount string `json:"total_amount,omitempty"` + TotalSize string `json:"total_size,omitempty"` + Items interface{} `json:"items"` + } `json:"data,omitempty"` +} + +var ( + rspOk *BaseResponse +) + +func init() { + rspOk = new(BaseResponse) + rspOk.Message = "OK" + rspOk.Code = 0 +} + +func NewRspOk() *BaseResponse { + return rspOk +} + +func NewRawRsp(raw []byte) *RawResponse { + rsp := &RawResponse{ + BaseResponse: BaseResponse{ + Code: rspOk.Code, + Message: rspOk.Message, + }, + } + rsp.Data = raw + return rsp +} +func NewListRsp(total int64, items interface{}) *ListResponse { + rsp := &ListResponse{ + BaseResponse: BaseResponse{ + Code: rspOk.Code, + Message: rspOk.Message, + }, + } + rsp.Data.Total = total + rsp.Data.Items = items + return rsp +} + +func NewListSizeRsp(total int64, totalSize int64, items interface{}) *ListResponse { + rsp := &ListResponse{ + BaseResponse: BaseResponse{ + Code: rspOk.Code, + Message: rspOk.Message, + }, + } + rsp.Data.Total = total + rsp.Data.TotalSize = goutil.FormatBytes(totalSize) + rsp.Data.Items = items + return rsp +} + +func NewRsp(data interface{}) *CommonResponse { + return &CommonResponse{ + BaseResponse: BaseResponse{ + Code: rspOk.Code, + Message: rspOk.Message, + }, + Data: data, + } +} + +func CheckDBError(err error) { + if err != nil { + panic(config.ErrDb.New().Append(err)) + } +} + +func CheckParamError(err error) { + if err != nil { + panic(config.ErrParam.New().Append(err)) + } +} + +func CheckNilError(data interface{}, args ...string) { + val := reflect.ValueOf(data) + if val.IsNil() { + panic(config.ErrNoData.New().Append(args)) + } +} diff --git a/common/session/session.go b/common/session/session.go new file mode 100644 index 0000000..18d591d --- /dev/null +++ b/common/session/session.go @@ -0,0 +1,50 @@ +package session + +import ( + "enterprise/common/model" +) + +var ( + ContextSession = "session" + ContextRequest = "request" + ContextResponse = "response" + ContextHeader = "header" + ContextWriteEvent = "write_event" + ContextRefreshToken = "refresh_token" +) + +type AdminHeader struct { + Token string `header:"x-token" json:"-"` + RemoteIp string `header:"x-remote-ip" json:"public_ip"` +} + +type AdminSession struct { + adminHeader *AdminHeader + adminUser *model.StaffUser +} + +func NewAdminSession(header *AdminHeader, user *model.StaffUser) *AdminSession { + return &AdminSession{ + adminHeader: header, + adminUser: user, + } +} + +func (s *AdminSession) GetHeader() *AdminHeader { + return s.adminHeader +} + +func (s *AdminSession) GetAdmin() *model.StaffUser { + return s.adminUser +} + +func (s *AdminSession) GetCorpId() int64 { + return s.adminUser.CorpId +} + +func (s *AdminSession) GetUsername() string { + if s.adminUser != nil { + return s.adminUser.Username + } + return "" +} diff --git a/server/api/checkin.go b/server/api/checkin.go new file mode 100644 index 0000000..7031cec --- /dev/null +++ b/server/api/checkin.go @@ -0,0 +1,57 @@ +package api + +import ( + "enterprise/common/model" + "github.com/smbrave/goutil" + "github.com/spf13/cast" +) + +type BaseRequest struct { + Page int `form:"page"` + Size int `form:"size"` +} + +func (b *BaseRequest) Default() { + if b.Page <= 0 { + b.Page = 1 + } + if b.Size <= 0 { + b.Size = 10 + } +} + +type ListCheckin struct { + BaseRequest + Username string `form:"username"` + StartDay string `form:"start_day"` + EndDay string `form:"end_day"` +} + +type SyncCheckin struct { + StartDay string `form:"start_day"` + EndDay string `form:"end_day"` +} + +type Checkin struct { + Id string + Username string + Day string + Month string + Exception string + Rawdata string + StartTime string + EndTime string + CreateTime string +} + +func (c *Checkin) From(m *model.Checkin) { + c.Id = cast.ToString(m.Id) + c.Username = m.Username + c.Day = m.Day + c.Month = m.Month + c.StartTime = goutil.TimeToDateTime(m.StartTime) + c.StartTime = goutil.TimeToDateTime(m.EndTime) + c.CreateTime = goutil.TimeToDateTime(m.CreateTime) + c.Exception = m.Exception + c.Rawdata = m.Rawdata +} diff --git a/server/controller/base.go b/server/controller/base.go index ad76b43..9fb6af8 100644 --- a/server/controller/base.go +++ b/server/controller/base.go @@ -2,6 +2,9 @@ package controller import ( "enterprise/common/config" + "enterprise/common/dao" + "enterprise/common/model" + "enterprise/common/session" "fmt" "github.com/gin-gonic/gin" "github.com/gogap/errors" @@ -21,6 +24,54 @@ type Response struct { type Base struct { } +// 所有请求都必须走的逻辑,用于创建session +func (c *Base) Before(ctx *gin.Context) { + if ctx.Keys == nil { + ctx.Keys = make(map[string]interface{}) + } + + // 获取头部参数 + var header session.AdminHeader + if err := ctx.BindHeader(&header); err != nil { + panic(config.ErrParam.New().Append(err)) + } + + if header.Token == "" { // 新用户没有extra所以extra中的配置不能对新用户生效 + return + } + + var tk *model.StaffToken = nil + + tk, err := dao.NewStaffTokenDao().GetByToken(header.Token) + if err != nil { + panic(config.ErrDb.New().Append(err)) + } + + if tk == nil { + panic(config.ErrTokenInvaild.New()) + } + if time.Now().Unix() > tk.ExpireTime { + panic(config.ErrTokenExpire.New()) + } + staffUser, err := dao.NewStaffUserDao().Get(tk.UserId) + if err != nil { + panic(config.ErrDb.New().Append(err)) + } + if staffUser == nil { + panic(config.ErrTokenInvaild.New()) + } + + ctx.Keys[session.ContextSession] = session.NewAdminSession(&header, staffUser) +} + +func (c *Base) Token(ctx *gin.Context) { + // 只有需要传token的接口需要强制校验token + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + if sess.GetHeader().Token == "" { + panic(config.ErrNoToken.New()) + } +} + func (c *Base) Recovery(ctx *gin.Context) { defer func() { if err := recover(); err != nil { diff --git a/server/controller/checkin.go b/server/controller/checkin.go new file mode 100644 index 0000000..6c21f5f --- /dev/null +++ b/server/controller/checkin.go @@ -0,0 +1,41 @@ +package controller + +import ( + "enterprise/common/config" + "enterprise/common/dao" + "enterprise/common/session" + "enterprise/server/api" + "enterprise/server/service" + "github.com/gin-gonic/gin" + "github.com/spf13/cast" + "net/http" +) + +type StaffCheckin struct { +} + +func (s *StaffCheckin) List(ctx *gin.Context) { + var req api.ListCheckin + if err := ctx.ShouldBind(&req); err != nil { + panic(config.ErrParam.New().Append(err)) + } + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + total, items := new(service.Checkin).List(sess, &req) + ctx.JSON(http.StatusOK, session.NewListRsp(total, items)) +} + +func (s *StaffCheckin) Sync(ctx *gin.Context) { + var req api.SyncCheckin + if err := ctx.ShouldBind(&req); err != nil { + panic(config.ErrParam.New().Append(err)) + } + sess := ctx.Keys[session.ContextSession].(*session.AdminSession) + new(service.Checkin).Sync(sess, &req) + ctx.JSON(http.StatusOK, session.NewRspOk()) +} + +func (s *StaffCheckin) Delete(ctx *gin.Context) { + id := cast.ToInt64(ctx.Query("id")) + dao.NewCheckinDao().Delete(id) + ctx.JSON(http.StatusOK, session.NewRspOk()) +} diff --git a/server/controller/salary.go b/server/controller/salary.go new file mode 100644 index 0000000..b0b429f --- /dev/null +++ b/server/controller/salary.go @@ -0,0 +1 @@ +package controller diff --git a/server/server.go b/server/server.go index dd8ac61..2cda98d 100644 --- a/server/server.go +++ b/server/server.go @@ -10,18 +10,28 @@ func initRoutge(engine *gin.Engine) { qyweixin := new(controller.QyWeixin) staff := new(controller.Staff) base := new(controller.Base) + checkin := new(controller.StaffCheckin) apiGroup := engine.Group("/api") + noTokenGroup := engine.Group("/api") + + apiGroup.Use(base.Before).Use(base.Token) group := engine.Group("/") apiGroup.Use(base.Recovery) group.Use(base.Recovery) - apiGroup.Any("/qyweixin/approve/:cid", qyweixin.Approve) - apiGroup.Any("/qyweixin/pay/:cid", qyweixin.Pay) - apiGroup.Any("/qyweixin/handle/refund", qyweixin.HandleRefund) + + noTokenGroup.Any("/qyweixin/approve/:cid", qyweixin.Approve) + noTokenGroup.Any("/qyweixin/pay/:cid", qyweixin.Pay) + noTokenGroup.Any("/qyweixin/handle/refund", qyweixin.HandleRefund) group.GET("/staff/salary", staff.Salary) group.GET("/staff/sync/salary", staff.SyncStaffSalary) + apiGroup.GET("/checkin", checkin.List) + apiGroup.DELETE("/checkin", checkin.Delete) + apiGroup.Any("/checkin/sync", checkin.Sync) + engine.LoadHTMLGlob("conf/template/*") + } func Start() error { diff --git a/server/service/checkin.go b/server/service/checkin.go new file mode 100644 index 0000000..93381eb --- /dev/null +++ b/server/service/checkin.go @@ -0,0 +1,34 @@ +package service + +import ( + "enterprise/common/config" + "enterprise/common/dao" + "enterprise/common/session" + "enterprise/server/api" + CS "enterprise/service" +) + +type Checkin struct { +} + +func (c *Checkin) List(sess *session.AdminSession, req *api.ListCheckin) (int64, interface{}) { + + checkts, total, err := dao.NewCheckinDao().QueryAdmin(req.Page, req.Size, sess.GetCorpId(), req.Username, req.StartDay, req.EndDay) + if err != nil { + panic(config.ErrDb.New().Append(err)) + } + items := make([]*api.Checkin, 0) + for _, checkin := range checkts { + ch := new(api.Checkin) + ch.From(checkin) + items = append(items, ch) + } + return total, items +} + +func (c *Checkin) Sync(sess *session.AdminSession, req *api.SyncCheckin) { + err := new(CS.Checkin).SyncCheckin(sess.GetCorpId(), req.StartDay, req.EndDay) + if err != nil { + panic(config.ErrInternal.New().Append(err)) + } +} diff --git a/service/checkin.go b/service/checkin.go new file mode 100644 index 0000000..2724b97 --- /dev/null +++ b/service/checkin.go @@ -0,0 +1,79 @@ +package service + +import ( + "enterprise/common/dao" + "enterprise/common/model" + "git.u8t.cn/open/gosdk/qyweixin" + log "github.com/sirupsen/logrus" + "github.com/smbrave/goutil" +) + +type Checkin struct { +} + +func (c *Checkin) SyncCheckin(corpId int64, startDay, endDay string) error { + corp, err := dao.NewCorpDao().Get(corpId) + if err != nil { + log.Errorf("db error :%s", err.Error()) + return err + } + + cfg := corp.GetConfig() + qyw := qyweixin.NewAppCheckin(&qyweixin.AppConfig{ + Corpid: cfg.CorpId, + Secret: cfg.EnterpriseSecret, + Agent: cfg.EnterpriseAgent, + }) + + users, err := qyw.GetCheckinEmployee(nil) + if err != nil { + log.Errorf("GetCheckinEmployee error :%s", err.Error()) + return err + } + + userDatas, err := qyw.GetCheckinData(startDay, endDay, users) + if err != nil { + log.Errorf("GetCheckinData[%s-%s]%s error :%s", startDay, endDay, goutil.EncodeJSON(users), err.Error()) + return err + } + + for _, user := range userDatas { + c.saveToDB(corpId, user) + } + return nil +} + +func (c *Checkin) saveToDB(corpId int64, user *qyweixin.UserCheckIn) error { + checkin, err := dao.NewCheckinDao().GetByDay(user.UserId, user.Day) + if err != nil { + log.Errorf("db error :%s", err.Error()) + return err + } + isNew := false + if checkin == nil { + checkin = new(model.Checkin) + checkin.Day = user.Day + checkin.Month = user.Month + checkin.Username = user.UserId + isNew = true + } + + checkin.Exception = user.Exception + checkin.StartTime = user.StartTime + checkin.EndTime = user.EndTime + checkin.Month = user.Month + checkin.Rawdata = user.Rawdata + checkin.CorpId = corpId + + if isNew { + _, err = dao.NewCheckinDao().Create(checkin) + } else { + err = dao.NewCheckinDao().Update(checkin) + } + if err != nil { + log.Errorf("create/update db error :%s", err.Error()) + return err + } + + return nil +} diff --git a/service/salary_calculator/salary_calculator_1002.go b/service/salary_calculator/salary_calculator_1002.go index bd30a4d..251c8e6 100644 --- a/service/salary_calculator/salary_calculator_1002.go +++ b/service/salary_calculator/salary_calculator_1002.go @@ -32,15 +32,15 @@ func (s *SalaryCalculator1002) Calculate(salary *model.StaffSalary) { if salary.Month == "202502" { salary.ShouldDay = 21 - if salary.UserName == "luoyi" { + if salary.Username == "luoyi" { salary.AttendDay = float64(salary.ShouldDay - 5) - } else if strings.ToLower(salary.UserName) == "wangyan" { + } else if strings.ToLower(salary.Username) == "wangyan" { salary.AttendDay = float64(salary.ShouldDay) - 5.6 - } else if strings.ToLower(salary.UserName) == "zhouhong" { + } else if strings.ToLower(salary.Username) == "zhouhong" { salary.AttendDay = float64(salary.ShouldDay) - 5.6 - } else if strings.ToLower(salary.UserName) == "chenmin" { + } else if strings.ToLower(salary.Username) == "chenmin" { salary.AttendDay = 19 - } else if strings.ToLower(salary.UserName) == "xushiyu" { + } else if strings.ToLower(salary.Username) == "xushiyu" { salary.AttendDay = 7 } } diff --git a/service/staff_salary.go b/service/staff_salary.go index 53a694b..2959412 100644 --- a/service/staff_salary.go +++ b/service/staff_salary.go @@ -39,7 +39,7 @@ func (s *StaffSalary) CalcSalary(salary *model.StaffSalary, month string) (*mode salary.Month = month salary.CorpId = s.user.CorpId salary.UserId = s.user.Id - salary.UserName = s.user.Username + salary.Username = s.user.Username } entryTime, _ := time.ParseInLocation("2006-01-02", s.user.EntryDate, time.Local) diff --git a/worker/checkin.go b/worker/checkin.go index fdd80d9..287df30 100644 --- a/worker/checkin.go +++ b/worker/checkin.go @@ -4,8 +4,8 @@ import ( "enterprise/common/dao" "enterprise/common/global" "enterprise/common/model" + "enterprise/service" "fmt" - "git.u8t.cn/open/gosdk/qyweixin" log "github.com/sirupsen/logrus" "github.com/smbrave/goutil" "strings" @@ -24,78 +24,33 @@ func NewCheckin(corpId int64) *Checkin { corpConfig: corp.GetConfig(), } } -func (c *Checkin) SyncCheckinMonth(month string) error { + +func (c *Checkin) SyncCheckinMonth(month string) { if month == "" { month = time.Now().AddDate(0, -1, 0).Format("200601") } - cfg := c.corp.GetConfig() - startTime, _ := time.ParseInLocation("20060102", month+"01", time.Local) endDay := startTime.AddDate(0, 1, -1).Format("2006-01-02") startDay := startTime.Format("2006-01-02") - qyw := qyweixin.NewAppCheckin(&qyweixin.AppConfig{ - Corpid: cfg.CorpId, - Secret: cfg.EnterpriseSecret, - Agent: cfg.EnterpriseAgent, - }) - users, err := qyw.GetCheckinEmployee(nil) - if err != nil { - log.Errorf("GetCheckinEmployee error :%s", err.Error()) - return err - } + new(service.Checkin).SyncCheckin(c.corp.Id, startDay, endDay) - userDatas, err := qyw.GetCheckinData(startDay, endDay, users) - if err != nil { - log.Errorf("GetCheckinData[%s-%s]%s error :%s", startDay, endDay, goutil.EncodeJSON(users), err.Error()) - return err - } - for _, user := range userDatas { - c.saveToDB(user) - } - return nil } -func (c *Checkin) SyncCheckinDay(corpId int64, day string) { - corp, err := dao.NewCorpDao().Get(corpId) - if err != nil { - log.Errorf("db error :%s", err.Error()) - return - } - - cfg := corp.GetConfig() - qyw := qyweixin.NewAppCheckin(&qyweixin.AppConfig{ - Corpid: cfg.CorpId, - Secret: cfg.EnterpriseSecret, - Agent: cfg.EnterpriseAgent, - }) +func (c *Checkin) SyncCheckinDay(day string) { if day == "" { day = time.Now().AddDate(0, 0, -1).Format("2006-01-02") } - users, err := qyw.GetCheckinEmployee(nil) - if err != nil { - log.Errorf("GetCheckinEmployee error :%s", err.Error()) - return - } + new(service.Checkin).SyncCheckin(c.corp.Id, day, day) - userDatas, err := qyw.GetCheckinData(day, day, users) - if err != nil { - log.Errorf("GetCheckinData[%s-%s]%s error :%s", day, day, goutil.EncodeJSON(users), err.Error()) - return - } - - for _, user := range userDatas { - c.saveToDB(user) - } - return } -func (c *Checkin) MonitorCheckinDay(corpId int64, day string) { +func (c *Checkin) MonitorCheckinDay(day string) { if day == "" { day = time.Now().AddDate(0, 0, -1).Format("2006-01-02") } - checkins, err := dao.NewCheckinDao().QueryDay(corpId, day) + checkins, err := dao.NewCheckinDao().QueryDay(c.corp.Id, day) if err != nil { log.Errorf("db error :%s", err.Error()) return @@ -104,7 +59,7 @@ func (c *Checkin) MonitorCheckinDay(corpId int64, day string) { if checkin.Exception == "" { continue } - approvals, err := dao.NewApprovalVacationDao().GetByUsername(corpId, checkin.Username, "", checkin.Day) + approvals, err := dao.NewApprovalVacationDao().GetByUsername(c.corp.Id, checkin.Username, "", checkin.Day) if err != nil { log.Errorf("db error :%s", err.Error()) continue @@ -121,38 +76,3 @@ func (c *Checkin) MonitorCheckinDay(corpId int64, day string) { global.SendMessage([]string{"jiangyong", checkin.Username}, strings.Join(message, "\n")) } } - -func (c *Checkin) saveToDB(user *qyweixin.UserCheckIn) error { - checkin, err := dao.NewCheckinDao().GetByDay(user.UserId, user.Day) - if err != nil { - log.Errorf("db error :%s", err.Error()) - return err - } - isNew := false - if checkin == nil { - checkin = new(model.Checkin) - checkin.Day = user.Day - checkin.Month = user.Month - checkin.Username = user.UserId - isNew = true - } - - checkin.Exception = user.Exception - checkin.StartTime = user.StartTime - checkin.EndTime = user.EndTime - checkin.Month = user.Month - checkin.Rawdata = user.Rawdata - checkin.CorpId = c.corp.Id - - if isNew { - _, err = dao.NewCheckinDao().Create(checkin) - } else { - err = dao.NewCheckinDao().Update(checkin) - } - if err != nil { - log.Errorf("create/update db error :%s", err.Error()) - return err - } - - return nil -} diff --git a/worker/worker.go b/worker/worker.go index 8fb0756..6efeaa9 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -22,11 +22,11 @@ func InitCorp1002(cron *gocron.Scheduler) { //同步每日考勤数据 cron.Every(1).Day().At("05:00").Do(func() { checkIn := NewCheckin(corpId) - go checkIn.SyncCheckinDay(corpId, "") + go checkIn.SyncCheckinDay("") }) cron.Every(1).Day().At("08:00").Do(func() { checkIn := NewCheckin(corpId) - go checkIn.MonitorCheckinDay(corpId, "") + go checkIn.MonitorCheckinDay("") }) //每月1号同步上月考勤、补卡审批、请假审批、报销审批 @@ -65,11 +65,11 @@ func InitCorp1000(cron *gocron.Scheduler) { //同步每日考勤数据 cron.Every(1).Day().At("05:00").Do(func() { checkIn := NewCheckin(corpId) - go checkIn.SyncCheckinDay(corpId, "") + go checkIn.SyncCheckinDay("") }) cron.Every(1).Day().At("08:00").Do(func() { checkIn := NewCheckin(corpId) - go checkIn.MonitorCheckinDay(corpId, "") + go checkIn.MonitorCheckinDay("") }) //每月1号同步上月考勤、补卡审批、请假审批、报销审批