enterprise/worker/checkin.go

106 lines
2.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"
log "github.com/sirupsen/logrus"
2024-01-07 17:17:33 +08:00
"github.com/smbrave/goutil"
2024-01-22 23:21:37 +08:00
"gitlab.batiao8.com/open/gosdk/qyweixin"
2023-10-23 18:07:03 +08:00
"time"
2023-08-04 18:27:37 +08:00
)
2024-01-07 18:53:21 +08:00
type Checkin struct {
2024-01-07 17:17:33 +08:00
}
2024-01-07 18:53:21 +08:00
func (c *Checkin) SyncCheckinMonth(month string) error {
2024-01-07 17:17:33 +08:00
if month == "" {
month = time.Now().AddDate(0, -1, 0).Format("200601")
}
2023-08-04 18:27:37 +08:00
cfg := config.GetConfig()
2024-01-07 17:17:33 +08:00
startTime, _ := time.ParseInLocation("20060102", month+"01", time.Local)
endDay := startTime.AddDate(0, 1, -1).Format("2006-01-02")
startDay := startTime.Format("2006-01-02")
2024-01-22 23:21:37 +08:00
qyw := qyweixin.NewAppCheckin(&qyweixin.AppConfig{
Corpid: cfg.QyWeixin.Corpid,
2024-01-24 16:48:04 +08:00
Secret: cfg.QyWeixin.EnterpriseSecret,
Agent: cfg.QyWeixin.EnterpriseAgent,
2024-01-22 23:21:37 +08:00
})
2024-01-07 17:17:33 +08:00
users, err := qyw.GetCheckinEmployee(nil)
2023-08-04 18:27:37 +08:00
if err != nil {
2024-01-07 17:17:33 +08:00
log.Errorf("GetCheckinEmployee error :%s", err.Error())
2023-08-04 18:27:37 +08:00
return err
}
2024-01-07 17:17:33 +08:00
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
}
2024-01-07 18:53:21 +08:00
func (c *Checkin) SyncCheckinDay(day string) {
2024-01-07 17:17:33 +08:00
cfg := config.GetConfig()
2024-01-22 23:21:37 +08:00
qyw := qyweixin.NewAppCheckin(&qyweixin.AppConfig{
Corpid: cfg.QyWeixin.Corpid,
2024-01-24 16:48:04 +08:00
Secret: cfg.QyWeixin.EnterpriseSecret,
Agent: cfg.QyWeixin.EnterpriseAgent,
2024-01-22 23:21:37 +08:00
})
2024-01-07 17:17:33 +08:00
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
}
2024-01-22 23:21:37 +08:00
func (c *Checkin) saveToDB(user *qyweixin.UserCheckIn) error {
2024-01-07 17:17:33 +08:00
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
}
2023-08-04 18:27:37 +08:00
2024-01-17 22:30:38 +08:00
checkin.Exception = user.Exception
checkin.StartTime = user.StartTime
checkin.EndTime = user.EndTime
checkin.Month = user.Month
2024-01-07 17:17:33 +08:00
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
2023-08-04 18:27:37 +08:00
}
return nil
}