From 58d79525b20b6aaa134f794d6e04f90a25e5201c Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Fri, 4 Aug 2023 19:29:13 +0800 Subject: [PATCH] message --- common/config/config.go | 17 ++++++------ common/global/message.go | 58 ++++++++++++++++++++++++++++++++++++++++ conf/server.conf.dev | 3 ++- conf/server.conf.prod | 1 + go.mod | 9 +++++++ worker/autopay.go | 33 +++++++++++++++++++++++ worker/checkin.go | 10 +++++-- 7 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 common/global/message.go create mode 100644 worker/autopay.go diff --git a/common/config/config.go b/common/config/config.go index ac8de47..7731933 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -36,14 +36,15 @@ type Redis struct { } type QyWeixin struct { - Corpid string `toml:"corpid"` - CheckinAgent string `toml:"checkin_agent"` - CheckinSecret string `toml:"checkin_secret"` - CheckinGroup string `toml:"checkin_group"` - EnterpriseAgent string `toml:"enterprise_agent"` - EnterpriseSecret string `toml:"enterprise_secret"` - HrAgent string `toml:"hr_agent"` - HrSecret string `toml:"hr_secret"` + Corpid string `toml:"corpid"` + CheckinAgent string `toml:"checkin_agent"` + CheckinSecret string `toml:"checkin_secret"` + CheckinGroup string `toml:"checkin_group"` + CheckinPayThresold float64 `toml:"checkin_pay_thresold"` + EnterpriseAgent string `toml:"enterprise_agent"` + EnterpriseSecret string `toml:"enterprise_secret"` + HrAgent string `toml:"hr_agent"` + HrSecret string `toml:"hr_secret"` } type Config struct { diff --git a/common/global/message.go b/common/global/message.go new file mode 100644 index 0000000..a7762e6 --- /dev/null +++ b/common/global/message.go @@ -0,0 +1,58 @@ +package global + +import ( + "context" + "enterprise/common/config" + "github.com/ArtisanCloud/PowerWeChat/v3/src/work" + "github.com/ArtisanCloud/PowerWeChat/v3/src/work/message/request" + log "github.com/sirupsen/logrus" + "github.com/smbrave/goutil" + "github.com/spf13/cast" + "strings" +) + +var ( + wxEnterprise *work.Work +) + +func initWxWork() (*work.Work, error) { + cfg := config.GetConfig() + client, err := work.NewWork(&work.UserConfig{ + CorpID: cfg.QyWeixin.Corpid, + AgentID: cast.ToInt(cfg.QyWeixin.EnterpriseAgent), + Secret: cfg.QyWeixin.EnterpriseSecret, + OAuth: work.OAuth{ + Callback: "https://wecom.artisan-cloud.com/callback", + Scopes: nil, + }, + }) + if err != nil { + log.Errorf("config[%s] init error : %s", goutil.EncodeJSON(cfg), err.Error()) + return nil, err + } + + return client, nil +} + +func SendMessage(user []string, content string) error { + if wxEnterprise == nil { + wxM, err := initWxWork() + if err != nil { + return err + } + wxEnterprise = wxM + } + cfg := config.GetConfig() + receivers := user + message := &request.RequestMessageSendText{} + message.ToUser = strings.Join(receivers, "|") + message.MsgType = "text" + message.AgentID = cast.ToInt(cfg.QyWeixin.EnterpriseAgent) + message.Text = &request.RequestText{Content: content} + + if _, err := wxEnterprise.Message.SendText(context.Background(), message); err != nil { + log.Errorf("send message [%s] error : %s", goutil.EncodeJSON(message), err) + return err + } + return nil +} diff --git a/conf/server.conf.dev b/conf/server.conf.dev index e638e97..c048ce8 100644 --- a/conf/server.conf.dev +++ b/conf/server.conf.dev @@ -23,4 +23,5 @@ enterprise_agent = "1000009" enterprise_secret = "oMB24UhKe50-XPTg7vhnwoTuhEXaq5XeiHPAUtF4hOs" checkin_agent = "3010011" checkin_secret = "6ljYNGt4DonZLmr9SCtgkTlOvtqmsOchBrTWwGl_GpU" -checkin_group = "1,2" \ No newline at end of file +checkin_group = "1,2" +checkin_pay_thresold = 11.5 \ No newline at end of file diff --git a/conf/server.conf.prod b/conf/server.conf.prod index fc06749..fc2aefa 100644 --- a/conf/server.conf.prod +++ b/conf/server.conf.prod @@ -24,3 +24,4 @@ enterprise_secret = "oMB24UhKe50-XPTg7vhnwoTuhEXaq5XeiHPAUtF4hOs" checkin_agent = "3010011" checkin_secret = "6ljYNGt4DonZLmr9SCtgkTlOvtqmsOchBrTWwGl_GpU" checkin_group = "1,2" +checkin_pay_thresold = 11 diff --git a/go.mod b/go.mod index dc67d26..f1ac81c 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module enterprise go 1.18 require ( + github.com/ArtisanCloud/PowerWeChat/v3 v3.0.56 github.com/go-co-op/gocron v1.31.0 github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/mitchellh/mapstructure v1.5.0 @@ -16,6 +17,10 @@ require ( ) require ( + github.com/ArtisanCloud/PowerLibs/v3 v3.0.12 // indirect + github.com/ArtisanCloud/PowerSocialite/v3 v3.0.6 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect @@ -24,14 +29,18 @@ require ( github.com/jonboulle/clockwork v0.4.0 // indirect github.com/lestrrat-go/strftime v1.0.6 // indirect github.com/magiconair/properties v1.8.7 // indirect + github.com/patrickmn/go-cache v2.1.0+incompatible // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/redis/go-redis/v9 v9.0.3 // indirect github.com/robfig/cron/v3 v3.0.1 // indirect github.com/spf13/afero v1.9.5 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.4.2 // indirect go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.8.0 // indirect + go.uber.org/zap v1.21.0 // indirect golang.org/x/sys v0.8.0 // indirect golang.org/x/text v0.9.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/worker/autopay.go b/worker/autopay.go new file mode 100644 index 0000000..343c8da --- /dev/null +++ b/worker/autopay.go @@ -0,0 +1,33 @@ +package worker + +import ( + "enterprise/common/config" + "enterprise/common/global" + "enterprise/common/model" + log "github.com/sirupsen/logrus" + "github.com/smbrave/goutil" + "strings" + "time" +) + +func ViewCheckin(checkin *model.Checkin) { + if checkin.Exception != "" { + log.Errorf("execption[%s] %s", checkin.Exception, goutil.EncodeJSON(checkin)) + return + } + thresold := config.GetConfig().QyWeixin.CheckinPayThresold + duration := checkin.EndTime - checkin.StartTime + if duration < int64(3600*thresold) { + return + } + message := make([]string, 0) + message = append(message, "员工名称:%s", checkin.UserId) + message = append(message, "考勤日期:%s", checkin.Day) + message = append(message, "开始时间:%s", goutil.TimeToDateTime(checkin.StartTime)) + message = append(message, "结束时间:%s", goutil.TimeToDateTime(checkin.EndTime)) + message = append(message, "工作时长:%s", time.Duration(time.Duration(duration)*time.Second).String()) + + if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil { + log.Errorf("send message error :%s", err.Error()) + } +} diff --git a/worker/checkin.go b/worker/checkin.go index 98aea64..e7823fb 100644 --- a/worker/checkin.go +++ b/worker/checkin.go @@ -20,6 +20,7 @@ func SyncCheckin(day string) error { checkinDao := dao.NewCheckinDao() for _, user := range users { isNew := false + isUpdate := false checkin, err := checkinDao.GetByDay(user, day) if err != nil { log.Errorf("db error :%s", err.Error()) @@ -40,10 +41,12 @@ func SyncCheckin(day string) error { if realCheckin == nil { continue } - if realCheckin.StartTime != 0 { + if realCheckin.StartTime != 0 && realCheckin.StartTime != checkin.StartTime { + isUpdate = true checkin.StartTime = realCheckin.StartTime } - if realCheckin.EndTime != 0 { + if realCheckin.EndTime != 0 && realCheckin.EndTime != checkin.EndTime { + isUpdate = true checkin.EndTime = realCheckin.EndTime } if isNew { @@ -54,6 +57,9 @@ func SyncCheckin(day string) error { if err != nil { log.Errorf("db error :%s", err.Error()) } + if isUpdate { + go ViewCheckin(checkin) + } } return nil