enterprise/worker/autopay.go

153 lines
4.4 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) {
2023-08-18 18:33:48 +08:00
2023-08-10 20:38:57 +08:00
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
}
2023-08-23 18:34:53 +08:00
if isOndutyPay(checkin, userConfig) && checkin.Exception == "" {
2023-08-10 20:38:57 +08:00
payMoney := cast.ToInt64(userConfig.Get(model.CheckinOndutyMoney))
2023-08-10 21:24:54 +08:00
if payMoney == 0 {
payMoney = 2000
2023-08-10 20:38:57 +08:00
}
2023-08-10 21:24:54 +08:00
autoPayMoney(checkin, checkOndutyName, payMoney)
2023-08-10 20:38:57 +08:00
}
}
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 21:24:54 +08:00
payMoney := cast.ToInt64(userConfig.Get(model.CheckinOffdutyMoney))
if payMoney == 0 {
payMoney = 2000
2023-08-09 22:00:55 +08:00
}
2023-08-10 21:24:54 +08:00
autoPayMoney(checkin, checkOffdutyName, payMoney)
}
func isOndutyPay(checkin *model.Checkin, userConfig *model.UserConfig) bool {
hour := time.Unix(checkin.StartTime, 0).Hour()
if hour >= 10 {
return false
}
// 配置了上班打卡的直接发放
if cast.ToBool(userConfig.Get(model.CheckinOndutyMoneyEnable)) == true {
return true
}
// 周六加班也直接发放
if time.Now().Weekday() == time.Saturday {
return true
}
return false
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-10 21:24:54 +08:00
//cfg := config.GetConfig()
2023-08-09 22:00:55 +08:00
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-10 21:24:54 +08:00
req.Title = checkinType
2023-08-15 12:47:20 +08:00
req.BillNo = fmt.Sprintf("DK%s%s", time.Now().Format("20060102150405"), butil.CutTail(checkin.Username, 12))
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-11 09:58:09 +08:00
message = append(message, fmt.Sprintf("发放金额:%s", fmt.Sprintf("%.2f", float64(payMoney)/100.00)))
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)
2023-08-11 21:58:36 +08:00
checkinMoney.Username = checkin.Username
2023-08-09 22:00:55 +08:00
checkinMoney.CheckinId = checkin.Id
checkinMoney.BillNo = req.BillNo
checkinMoney.CheckinType = checkinType
checkinMoney.Day = checkin.Day
2023-08-10 21:24:54 +08:00
checkinMoney.BillAmount = payMoney
2023-08-09 22:00:55 +08:00
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
}