package worker import ( "enterprise/common/config" "enterprise/common/dao" "enterprise/common/global" "enterprise/common/model" "fmt" "git.u8t.cn/open/gosdk/qyweixin" log "github.com/sirupsen/logrus" "github.com/smbrave/goutil" "strings" "time" ) type Checkin struct { } func (c *Checkin) SyncCheckinMonth(month string) error { if month == "" { month = time.Now().AddDate(0, -1, 0).Format("200601") } cfg := config.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.QyWeixin.Corpid, Secret: cfg.QyWeixin.EnterpriseSecret, Agent: cfg.QyWeixin.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(user) } return nil } func (c *Checkin) SyncCheckinDay(day string) { cfg := config.GetConfig() qyw := qyweixin.NewAppCheckin(&qyweixin.AppConfig{ Corpid: cfg.QyWeixin.Corpid, Secret: cfg.QyWeixin.EnterpriseSecret, Agent: cfg.QyWeixin.EnterpriseAgent, }) 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 } 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(day string) { if day == "" { day = time.Now().AddDate(0, 0, -1).Format("2006-01-02") } checkins, err := dao.NewCheckinDao().QueryDay(day) if err != nil { log.Errorf("db error :%s", err.Error()) return } for _, checkin := range checkins { if checkin.Exception == "" { continue } approvals, err := dao.NewApprovalVacationDao().GetByUsername(checkin.Username, "", checkin.Day) if err != nil { log.Errorf("db error :%s", err.Error()) continue } if len(approvals) > 0 { continue } message := make([]string, 0) message = append(message, fmt.Sprintf("【考勤异常】%s", checkin.Username)) message = append(message, fmt.Sprintf("考勤日期:%s", checkin.Day)) message = append(message, fmt.Sprintf("上班时间:%s", goutil.TimeToDateTime(checkin.StartTime))) message = append(message, fmt.Sprintf("下班时间:%s", goutil.TimeToDateTime(checkin.EndTime))) message = append(message, fmt.Sprintf("异常原因:%s", checkin.Exception)) 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 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 }