package worker import ( "enterprise/common/config" "enterprise/common/dao" "enterprise/common/model" "enterprise/common/weixin" log "github.com/sirupsen/logrus" "strings" "time" ) func SyncCheckin(day string) error { cfg := config.GetConfig() qyw := weixin.NewQyWeixinCheckin(cfg.QyWeixin.Corpid, cfg.QyWeixin.CheckinSecret, cfg.QyWeixin.CheckinAgent) users, err := qyw.GetCheckinEmployee(strings.Split(cfg.QyWeixin.CheckinGroup, ",")) if err != nil { return err } checkinDao := dao.NewCheckinDao() for _, user := range users { isNew := false isUpdate := false checkin, err := checkinDao.GetByDay(user, day) if err != nil { log.Errorf("db error :%s", err.Error()) continue } if checkin == nil { checkin = new(model.Checkin) checkin.Day = day checkin.Username = user isNew = true } realCheckin, err := qyw.GetCheckinData(day, user) if err != nil { log.Errorf("qyweixin get checkin error :%s", err.Error()) continue } if realCheckin == nil { continue } if realCheckin.StartTime != 0 && realCheckin.StartTime != checkin.StartTime { isUpdate = true checkin.StartTime = realCheckin.StartTime } if realCheckin.EndTime != 0 && realCheckin.EndTime != checkin.EndTime { isUpdate = true checkin.EndTime = realCheckin.EndTime } if realCheckin.Exception != "" && realCheckin.Exception != checkin.Exception { checkin.Exception = realCheckin.Exception isUpdate = true } checkin.Rawdata = realCheckin.Rawdata if isNew { _, err = checkinDao.Create(checkin) } else { err = checkinDao.Update(checkin) } if err != nil { log.Errorf("db error :%s", err.Error()) continue } if isNew { go NotifyCheckinOnDuty(checkin) } if isUpdate { go NotifyCheckinOffDuty(checkin) } time.Sleep(300 * time.Second) } return nil }