enterprise/worker/checkin.go

67 lines
1.5 KiB
Go
Raw Normal View History

2023-08-04 11:13:41 +08:00
package worker
2023-08-04 18:27:37 +08:00
import (
"enterprise/common/config"
"enterprise/common/dao"
"enterprise/common/model"
"enterprise/common/qyweixin"
log "github.com/sirupsen/logrus"
2023-08-04 18:32:04 +08:00
"strings"
2023-08-04 18:27:37 +08:00
)
func SyncCheckin(day string) error {
cfg := config.GetConfig()
qyw := qyweixin.NewQyWeixin(cfg.QyWeixin.Corpid, cfg.QyWeixin.CheckinSecret)
2023-08-04 18:32:04 +08:00
users, err := qyw.GetCheckinEmployee(strings.Split(cfg.QyWeixin.CheckinGroup, ","))
2023-08-04 18:27:37 +08:00
if err != nil {
return err
}
checkinDao := dao.NewCheckinDao()
for _, user := range users {
isNew := false
2023-08-04 19:29:13 +08:00
isUpdate := false
2023-08-04 18:27:37 +08:00
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
}
2023-08-04 18:56:44 +08:00
if realCheckin == nil {
continue
}
2023-08-04 19:29:13 +08:00
if realCheckin.StartTime != 0 && realCheckin.StartTime != checkin.StartTime {
isUpdate = true
2023-08-04 18:27:37 +08:00
checkin.StartTime = realCheckin.StartTime
}
2023-08-04 19:29:13 +08:00
if realCheckin.EndTime != 0 && realCheckin.EndTime != checkin.EndTime {
isUpdate = true
2023-08-04 18:27:37 +08:00
checkin.EndTime = realCheckin.EndTime
}
if isNew {
_, err = checkinDao.Create(checkin)
} else {
err = checkinDao.Update(checkin)
}
if err != nil {
log.Errorf("db error :%s", err.Error())
}
2023-08-04 19:29:13 +08:00
if isUpdate {
go ViewCheckin(checkin)
}
2023-08-04 18:27:37 +08:00
}
return nil
}