From cafc163b5e0e6e822c95e1de3d5915f27e2a7b8b Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Thu, 10 Aug 2023 20:38:57 +0800 Subject: [PATCH] config --- common/dao/user_config.go | 66 ++++++++++++++++++++++++++++++++ common/model/user_config.go | 24 ++++++++++++ worker/autopay.go | 75 +++++++++++++++++++++++++++---------- worker/checkin.go | 1 + 4 files changed, 147 insertions(+), 19 deletions(-) create mode 100644 common/dao/user_config.go create mode 100644 common/model/user_config.go diff --git a/common/dao/user_config.go b/common/dao/user_config.go new file mode 100644 index 0000000..b2c9fe9 --- /dev/null +++ b/common/dao/user_config.go @@ -0,0 +1,66 @@ +package dao + +import ( + "enterprise/common/model" + "gorm.io/gorm" + "time" +) + +type UserConfigDao struct { +} + +func NewUserConfigDao() *UserConfigDao { + return &UserConfigDao{} +} + +func (d *UserConfigDao) TableName() string { + return "user_config" +} + +func (d *UserConfigDao) Create(o *model.UserConfig) (int64, error) { + o.CreateTime = time.Now().Unix() + res := GetDB().Table(d.TableName()).Create(o) + return o.Id, res.Error +} + +func (d *UserConfigDao) Update(o *model.UserConfig) error { + o.UpdateTime = time.Now().Unix() + tx := GetDB().Table(d.TableName()) + res := tx.Save(o) + return res.Error +} + +func (d *UserConfigDao) Delete(id int64) error { + res := GetDB().Table(d.TableName()).Delete(&model.UserConfig{}, id) + return res.Error +} + +func (d *UserConfigDao) Get(id int64) (*model.UserConfig, error) { + var u model.UserConfig + tx := GetDB().Table(d.TableName()) + tx = tx.Where("id = ?", id) + res := tx.First(&u) + if res.Error == gorm.ErrRecordNotFound { + return nil, nil + } + + if res.Error != nil { + return nil, res.Error + } + return &u, nil +} + +func (d *UserConfigDao) GetByUsername(username string) (*model.UserConfig, error) { + var u model.UserConfig + tx := GetDB().Table(d.TableName()) + tx = tx.Where("username = ?", username) + res := tx.First(&u) + if res.Error == gorm.ErrRecordNotFound { + return nil, nil + } + + if res.Error != nil { + return nil, res.Error + } + return &u, nil +} diff --git a/common/model/user_config.go b/common/model/user_config.go new file mode 100644 index 0000000..0467a1d --- /dev/null +++ b/common/model/user_config.go @@ -0,0 +1,24 @@ +package model + +import "encoding/json" + +var ( + CheckinOndutyMoneyEnable = "checkin.onduty.money.enable" + CheckinOffdutyMoneyEnable = "checkin.offduty.money.enable" + CheckinOndutyMoney = "checkin.onduty.money" + CheckinOffdutyMoney = "checkin.offduty.money" +) + +type UserConfig struct { + Id int64 + Username string + Config string + CreateTime int64 + UpdateTime int64 +} + +func (u *UserConfig) Get(key string) interface{} { + config := make(map[string]interface{}) + json.Unmarshal([]byte(u.Config), &config) + return config[key] +} diff --git a/worker/autopay.go b/worker/autopay.go index 71a4978..aabc67a 100644 --- a/worker/autopay.go +++ b/worker/autopay.go @@ -15,18 +15,10 @@ import ( "time" ) -func NotifyCheckinOffDuty(checkin *model.Checkin) { - if checkin.Exception != "" { - log.Infof("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 - } - autoPayMoney(checkin, "下班打卡") -} +var ( + checkOndutyName = "上班打卡" + checkOffdutyName = "下班打卡" +) func NotifyCheckinOnDuty(checkin *model.Checkin) { message := make([]string, 0) @@ -38,9 +30,53 @@ func NotifyCheckinOnDuty(checkin *model.Checkin) { if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil { log.Errorf("send message error :%s", err.Error()) } + + userConfig, err := dao.NewUserConfigDao().GetByUsername(checkin.Username) + if err != nil { + log.Errorf("db error :%s", err.Error()) + return + } + if userConfig == nil { + return + } + + if cast.ToBool(userConfig.Get(model.CheckinOndutyMoneyEnable)) == true { + payMoney := cast.ToInt64(userConfig.Get(model.CheckinOndutyMoney)) + if payMoney >= 100 && payMoney <= 20000 { + autoPayMoney(checkin, checkOndutyName, payMoney) + } + } } -func autoPayMoney(checkin *model.Checkin, checkinType string) error { +func NotifyCheckinOffDuty(checkin *model.Checkin) { + if checkin.Exception != "" { + log.Infof("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 + } + + userConfig, err := dao.NewUserConfigDao().GetByUsername(checkin.Username) + if err != nil { + log.Errorf("db error :%s", err.Error()) + return + } + if userConfig == nil { + return + } + + if cast.ToBool(userConfig.Get(model.CheckinOffdutyMoneyEnable)) == true { + payMoney := cast.ToInt64(userConfig.Get(model.CheckinOffdutyMoney)) + if payMoney >= 100 && payMoney <= 20000 { + autoPayMoney(checkin, checkOffdutyName, payMoney) + } + } +} + +func autoPayMoney(checkin *model.Checkin, checkinType string, payMoney int64) error { cfg := config.GetConfig() checkinMoneyDao := dao.NewCheckinMoneyDao() @@ -54,10 +90,10 @@ func autoPayMoney(checkin *model.Checkin, checkinType string) error { } var req weixin.RedMoneyReq - req.TotalAmount = cast.ToInt64(cfg.QyWeixin.CheckinPayMoney) * 100 + req.TotalAmount = payMoney req.Title = cfg.QyWeixin.CheckinPayTitle req.BillNo = fmt.Sprintf("QY%s%s", time.Now().Format("20060102150405"), butil.RandomStr(6)) - req.Userid = "jiangyong" + req.Userid = checkin.Username if err := weixin.NewQyPay().PayRedMoney(&req); err != nil { log.Errorf("pay red money error :%s", err.Error()) return err @@ -65,13 +101,14 @@ func autoPayMoney(checkin *model.Checkin, checkinType string) error { message := make([]string, 0) duration := checkin.EndTime - checkin.StartTime - message = append(message, "【下班提醒】") + message = append(message, fmt.Sprintf("【红包发放】[%s]", checkinType)) message = append(message, fmt.Sprintf("员工名称:%s", checkin.Username)) message = append(message, fmt.Sprintf("考勤日期:%s", checkin.Day)) 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", (time.Duration(duration)*time.Second).String())) - + if checkinType == checkOffdutyName { + message = append(message, fmt.Sprintf("结束时间:%s", goutil.TimeToDateTime(checkin.EndTime))) + message = append(message, fmt.Sprintf("工作时长:%s", (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 30ffb10..8325b5c 100644 --- a/worker/checkin.go +++ b/worker/checkin.go @@ -61,6 +61,7 @@ func SyncCheckin(day string) error { } if err != nil { log.Errorf("db error :%s", err.Error()) + continue } if isNew { go NotifyCheckinOnDuty(checkin)