enterprise/worker/checkin.go

106 lines
2.5 KiB
Go

package worker
import (
"enterprise/common/config"
"enterprise/common/dao"
"enterprise/common/model"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"gitlab.batiao8.com/open/gosdk/qyweixin"
"time"
)
type Checkin struct {
}
func (c *Checkin) SyncCheckinMonth(month string) error {
if month == "" {
month = time.Now().AddDate(0, -1, 0).Format("200601")
}
cfg := config.GetConfig()
startTime, _ := time.ParseInLocation("20060102", month+"01", time.Local)
endDay := startTime.AddDate(0, 1, -1).Format("2006-01-02")
startDay := startTime.Format("2006-01-02")
qyw := qyweixin.NewAppCheckin(&qyweixin.AppConfig{
Corpid: cfg.QyWeixin.Corpid,
Secret: cfg.QyWeixin.CheckinSecret,
Agent: cfg.QyWeixin.CheckinAgent,
})
users, err := qyw.GetCheckinEmployee(nil)
if err != nil {
log.Errorf("GetCheckinEmployee error :%s", err.Error())
return err
}
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
}
func (c *Checkin) SyncCheckinDay(day string) {
cfg := config.GetConfig()
qyw := qyweixin.NewAppCheckin(&qyweixin.AppConfig{
Corpid: cfg.QyWeixin.Corpid,
Secret: cfg.QyWeixin.CheckinSecret,
Agent: cfg.QyWeixin.CheckinAgent,
})
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
}
func (c *Checkin) saveToDB(user *qyweixin.UserCheckIn) error {
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
}
checkin.Exception = user.Exception
checkin.StartTime = user.StartTime
checkin.EndTime = user.EndTime
checkin.Month = user.Month
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
}
return nil
}