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"
|
2024-04-03 13:53:48 +08:00
|
|
|
"enterprise/common/global"
|
2023-08-04 18:27:37 +08:00
|
|
|
"enterprise/common/model"
|
2024-04-03 13:53:48 +08:00
|
|
|
"fmt"
|
2023-08-04 18:27:37 +08:00
|
|
|
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"
|
2024-04-03 13:53:48 +08:00
|
|
|
"strings"
|
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-04-03 13:53:48 +08:00
|
|
|
|
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-04-03 13:53:48 +08:00
|
|
|
|
|
|
|
func (c *Checkin) MonitorCheckinDay(day string) {
|
|
|
|
if day == "" {
|
|
|
|
day = time.Now().AddDate(0, 0, -1).Format("2006-01-02")
|
|
|
|
}
|
|
|
|
checkins, err := dao.NewCheckinDao().QueryDay(day)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("db error :%s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, checkin := range checkins {
|
|
|
|
if checkin.Exception == "" {
|
|
|
|
continue
|
|
|
|
}
|
2024-04-25 12:23:19 +08:00
|
|
|
approvals, err := dao.NewApprovalVacationDao().GetByUsername(checkin.Username, "", checkin.Day)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("db error :%s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(approvals) > 0 {
|
|
|
|
continue
|
|
|
|
}
|
2024-04-03 13:53:48 +08:00
|
|
|
message := make([]string, 0)
|
|
|
|
message = append(message, fmt.Sprintf("【考勤异常】%s", checkin.Username))
|
|
|
|
message = append(message, fmt.Sprintf("考勤日期:%s", checkin.Day))
|
2024-04-12 11:35:47 +08:00
|
|
|
message = append(message, fmt.Sprintf("上班时间:%s", goutil.TimeToDateTime(checkin.StartTime)))
|
|
|
|
message = append(message, fmt.Sprintf("下班时间:%s", goutil.TimeToDateTime(checkin.EndTime)))
|
|
|
|
message = append(message, fmt.Sprintf("异常原因:%s", checkin.Exception))
|
2024-04-25 12:23:19 +08:00
|
|
|
global.SendMessage([]string{"jiangyong", checkin.Username}, strings.Join(message, "\n"))
|
2024-04-03 13:53:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-31 22:54:06 +08:00
|
|
|
checkin.Rawdata = user.Rawdata
|
2024-01-17 22:30:38 +08:00
|
|
|
|
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
|
|
|
|
}
|