This commit is contained in:
jiangyong27 2023-08-10 20:38:57 +08:00
parent 44a0938bee
commit cafc163b5e
4 changed files with 147 additions and 19 deletions

66
common/dao/user_config.go Normal file
View File

@ -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
}

View File

@ -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]
}

View File

@ -15,18 +15,10 @@ import (
"time" "time"
) )
func NotifyCheckinOffDuty(checkin *model.Checkin) { var (
if checkin.Exception != "" { checkOndutyName = "上班打卡"
log.Infof("execption[%s] %s", checkin.Exception, goutil.EncodeJSON(checkin)) checkOffdutyName = "下班打卡"
return )
}
thresold := config.GetConfig().QyWeixin.CheckinPayThresold
duration := checkin.EndTime - checkin.StartTime
if duration < int64(3600*thresold) {
return
}
autoPayMoney(checkin, "下班打卡")
}
func NotifyCheckinOnDuty(checkin *model.Checkin) { func NotifyCheckinOnDuty(checkin *model.Checkin) {
message := make([]string, 0) 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 { if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error :%s", err.Error()) 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() cfg := config.GetConfig()
checkinMoneyDao := dao.NewCheckinMoneyDao() checkinMoneyDao := dao.NewCheckinMoneyDao()
@ -54,10 +90,10 @@ func autoPayMoney(checkin *model.Checkin, checkinType string) error {
} }
var req weixin.RedMoneyReq var req weixin.RedMoneyReq
req.TotalAmount = cast.ToInt64(cfg.QyWeixin.CheckinPayMoney) * 100 req.TotalAmount = payMoney
req.Title = cfg.QyWeixin.CheckinPayTitle req.Title = cfg.QyWeixin.CheckinPayTitle
req.BillNo = fmt.Sprintf("QY%s%s", time.Now().Format("20060102150405"), butil.RandomStr(6)) 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 { if err := weixin.NewQyPay().PayRedMoney(&req); err != nil {
log.Errorf("pay red money error :%s", err.Error()) log.Errorf("pay red money error :%s", err.Error())
return err return err
@ -65,13 +101,14 @@ func autoPayMoney(checkin *model.Checkin, checkinType string) error {
message := make([]string, 0) message := make([]string, 0)
duration := checkin.EndTime - checkin.StartTime 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.Username))
message = append(message, fmt.Sprintf("考勤日期:%s", checkin.Day)) 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.StartTime)))
if checkinType == checkOffdutyName {
message = append(message, fmt.Sprintf("结束时间:%s", goutil.TimeToDateTime(checkin.EndTime))) message = append(message, fmt.Sprintf("结束时间:%s", goutil.TimeToDateTime(checkin.EndTime)))
message = append(message, fmt.Sprintf("工作时长:%s", (time.Duration(duration)*time.Second).String())) message = append(message, fmt.Sprintf("工作时长:%s", (time.Duration(duration)*time.Second).String()))
}
if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil { if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error :%s", err.Error()) log.Errorf("send message error :%s", err.Error())
} }

View File

@ -61,6 +61,7 @@ func SyncCheckin(day string) error {
} }
if err != nil { if err != nil {
log.Errorf("db error :%s", err.Error()) log.Errorf("db error :%s", err.Error())
continue
} }
if isNew { if isNew {
go NotifyCheckinOnDuty(checkin) go NotifyCheckinOnDuty(checkin)