enterprise/worker/checkin.go

72 lines
1.7 KiB
Go

package worker
import (
"enterprise/common/config"
"enterprise/common/dao"
"enterprise/common/model"
"enterprise/common/qyweixin"
log "github.com/sirupsen/logrus"
"strings"
)
func SyncCheckin(day string) error {
cfg := config.GetConfig()
qyw := qyweixin.NewQyWeixin(cfg.QyWeixin.Corpid, cfg.QyWeixin.CheckinSecret)
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.UserId = 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())
}
if isUpdate {
go ViewCheckin(checkin)
}
}
return nil
}