enterprise/worker/checkin.go

76 lines
1.8 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"
2023-08-09 22:00:55 +08:00
"enterprise/common/weixin"
2023-08-04 18:27:37 +08:00
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()
2023-08-13 21:24:54 +08:00
qyw := weixin.NewQyWeixinCheckin(cfg.QyWeixin.Corpid, cfg.QyWeixin.CheckinSecret, cfg.QyWeixin.CheckinAgent)
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
2023-08-10 19:26:09 +08:00
checkin.Username = user
2023-08-04 18:27:37 +08:00
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
}
2023-08-04 20:35:50 +08:00
if realCheckin.Exception != "" && realCheckin.Exception != checkin.Exception {
checkin.Exception = realCheckin.Exception
isUpdate = true
}
checkin.Rawdata = realCheckin.Rawdata
2023-08-04 18:27:37 +08:00
if isNew {
_, err = checkinDao.Create(checkin)
} else {
err = checkinDao.Update(checkin)
}
if err != nil {
log.Errorf("db error :%s", err.Error())
2023-08-10 20:38:57 +08:00
continue
2023-08-04 18:27:37 +08:00
}
2023-08-09 22:00:55 +08:00
if isNew {
go NotifyCheckinOnDuty(checkin)
}
2023-08-04 19:29:13 +08:00
if isUpdate {
2023-08-09 22:00:55 +08:00
go NotifyCheckinOffDuty(checkin)
2023-08-04 19:29:13 +08:00
}
2023-08-04 18:27:37 +08:00
}
return nil
}