enterprise/worker/autopay.go

128 lines
3.9 KiB
Go
Raw Normal View History

2023-08-04 19:29:13 +08:00
package worker
import (
2023-08-09 22:00:55 +08:00
butil "enterprise/base/util"
2023-08-04 19:29:13 +08:00
"enterprise/common/config"
2023-08-09 22:00:55 +08:00
"enterprise/common/dao"
2023-08-04 19:29:13 +08:00
"enterprise/common/global"
"enterprise/common/model"
2023-08-09 22:00:55 +08:00
"enterprise/common/weixin"
2023-08-04 20:35:50 +08:00
"fmt"
2023-08-04 19:29:13 +08:00
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
2023-08-09 22:00:55 +08:00
"github.com/spf13/cast"
2023-08-04 19:29:13 +08:00
"strings"
"time"
)
2023-08-10 20:38:57 +08:00
var (
checkOndutyName = "上班打卡"
checkOffdutyName = "下班打卡"
)
func NotifyCheckinOnDuty(checkin *model.Checkin) {
message := make([]string, 0)
message = append(message, "【上班提醒】")
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)))
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)
}
}
}
2023-08-09 22:00:55 +08:00
func NotifyCheckinOffDuty(checkin *model.Checkin) {
2023-08-04 19:29:13 +08:00
if checkin.Exception != "" {
2023-08-09 22:00:55 +08:00
log.Infof("execption[%s] %s", checkin.Exception, goutil.EncodeJSON(checkin))
2023-08-04 19:29:13 +08:00
return
}
thresold := config.GetConfig().QyWeixin.CheckinPayThresold
duration := checkin.EndTime - checkin.StartTime
if duration < int64(3600*thresold) {
return
}
2023-08-09 22:00:55 +08:00
2023-08-10 20:38:57 +08:00
userConfig, err := dao.NewUserConfigDao().GetByUsername(checkin.Username)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
if userConfig == nil {
return
}
2023-08-09 22:00:55 +08:00
2023-08-10 20:38:57 +08:00
if cast.ToBool(userConfig.Get(model.CheckinOffdutyMoneyEnable)) == true {
payMoney := cast.ToInt64(userConfig.Get(model.CheckinOffdutyMoney))
if payMoney >= 100 && payMoney <= 20000 {
autoPayMoney(checkin, checkOffdutyName, payMoney)
}
2023-08-09 22:00:55 +08:00
}
}
2023-08-10 20:38:57 +08:00
func autoPayMoney(checkin *model.Checkin, checkinType string, payMoney int64) error {
2023-08-09 22:00:55 +08:00
cfg := config.GetConfig()
checkinMoneyDao := dao.NewCheckinMoneyDao()
2023-08-10 19:26:09 +08:00
checkinMoney, err := checkinMoneyDao.GetByDay(checkin.Username, checkin.Day, checkinType)
2023-08-09 22:00:55 +08:00
if err != nil {
log.Errorf("db error :%s", err.Error())
return err
}
if checkinMoney != nil {
return nil
}
var req weixin.RedMoneyReq
2023-08-10 20:38:57 +08:00
req.TotalAmount = payMoney
2023-08-09 22:00:55 +08:00
req.Title = cfg.QyWeixin.CheckinPayTitle
req.BillNo = fmt.Sprintf("QY%s%s", time.Now().Format("20060102150405"), butil.RandomStr(6))
2023-08-10 20:38:57 +08:00
req.Userid = checkin.Username
2023-08-09 22:00:55 +08:00
if err := weixin.NewQyPay().PayRedMoney(&req); err != nil {
log.Errorf("pay red money error :%s", err.Error())
return err
}
2023-08-04 19:29:13 +08:00
message := make([]string, 0)
2023-08-09 22:00:55 +08:00
duration := checkin.EndTime - checkin.StartTime
2023-08-10 20:38:57 +08:00
message = append(message, fmt.Sprintf("【红包发放】[%s]", checkinType))
2023-08-10 19:26:09 +08:00
message = append(message, fmt.Sprintf("员工名称:%s", checkin.Username))
2023-08-04 20:35:50 +08:00
message = append(message, fmt.Sprintf("考勤日期:%s", checkin.Day))
message = append(message, fmt.Sprintf("开始时间:%s", goutil.TimeToDateTime(checkin.StartTime)))
2023-08-10 20:38:57 +08:00
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()))
}
2023-08-04 19:29:13 +08:00
if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error :%s", err.Error())
}
2023-08-09 22:00:55 +08:00
checkinMoney = new(model.CheckinMoney)
checkinMoney.CheckinId = checkin.Id
checkinMoney.BillNo = req.BillNo
checkinMoney.CheckinType = checkinType
checkinMoney.Day = checkin.Day
if _, err := checkinMoneyDao.Create(checkinMoney); err != nil {
log.Errorf("create checkinMoney model error :%s", err.Error())
return err
}
return nil
2023-08-04 19:29:13 +08:00
}