From fa553703fae57cad6ee1dc1d93440e582856045b Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Wed, 3 Apr 2024 13:53:48 +0800 Subject: [PATCH] checkin monitor --- common/dao/checkin.go | 12 ++++++++++++ worker/checkin.go | 27 +++++++++++++++++++++++++++ worker/worker.go | 3 +++ 3 files changed, 42 insertions(+) diff --git a/common/dao/checkin.go b/common/dao/checkin.go index 98b1131..f7c3c49 100644 --- a/common/dao/checkin.go +++ b/common/dao/checkin.go @@ -95,3 +95,15 @@ func (d *CheckinDao) Query(username, month string, filterException bool) ([]*mod } return u, nil } + +func (d *CheckinDao) QueryDay(day string) ([]*model.Checkin, error) { + tx := GetDB().Table(d.TableName()) + tx = tx.Where("day = ?", day) + + var u []*model.Checkin + tx = tx.Find(&u) + if tx.Error != nil { + return nil, tx.Error + } + return u, nil +} diff --git a/worker/checkin.go b/worker/checkin.go index 0153b33..fe855c4 100644 --- a/worker/checkin.go +++ b/worker/checkin.go @@ -3,10 +3,13 @@ package worker import ( "enterprise/common/config" "enterprise/common/dao" + "enterprise/common/global" "enterprise/common/model" + "fmt" log "github.com/sirupsen/logrus" "github.com/smbrave/goutil" "gitlab.batiao8.com/open/gosdk/qyweixin" + "strings" "time" ) @@ -44,6 +47,7 @@ func (c *Checkin) SyncCheckinMonth(month string) error { } return nil } + func (c *Checkin) SyncCheckinDay(day string) { cfg := config.GetConfig() qyw := qyweixin.NewAppCheckin(&qyweixin.AppConfig{ @@ -71,6 +75,29 @@ func (c *Checkin) SyncCheckinDay(day string) { } return } + +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 + } + message := make([]string, 0) + message = append(message, fmt.Sprintf("【考勤异常】%s", checkin.Username)) + message = append(message, fmt.Sprintf("考勤日期:%s", checkin.Day)) + message = append(message, fmt.Sprintf("上班时间:%s", goutil.TimeToDate(checkin.StartTime))) + message = append(message, fmt.Sprintf("下班时间:%s", goutil.TimeToDate(checkin.EndTime))) + global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")) + } +} + func (c *Checkin) saveToDB(user *qyweixin.UserCheckIn) error { checkin, err := dao.NewCheckinDao().GetByDay(user.UserId, user.Day) if err != nil { diff --git a/worker/worker.go b/worker/worker.go index 1541fbe..75ebdfb 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -22,6 +22,9 @@ func Init() error { cron.Every(1).Day().At("05:00").Do(func() { go checkIn.SyncCheckinDay("") }) + cron.Every(1).Day().At("08:00").Do(func() { + go checkIn.MonitorCheckinDay("") + }) //每月1号同步上月考勤、补卡审批、请假审批、报销审批 cron.Every(1).Month(1).At("05:30").Do(func() {