enterprise/worker/autopay.go

91 lines
3.0 KiB
Go

package worker
import (
butil "enterprise/base/util"
"enterprise/common/config"
"enterprise/common/dao"
"enterprise/common/global"
"enterprise/common/model"
"enterprise/common/weixin"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"strings"
"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, "下班打卡")
}
func NotifyCheckinOnDuty(checkin *model.Checkin) {
message := make([]string, 0)
message = append(message, "【上班提醒】")
message = append(message, fmt.Sprintf("员工名称:%s", checkin.UserId))
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())
}
}
func autoPayMoney(checkin *model.Checkin, checkinType string) error {
cfg := config.GetConfig()
checkinMoneyDao := dao.NewCheckinMoneyDao()
checkinMoney, err := checkinMoneyDao.GetByDay(checkin.UserId, checkin.Day, checkinType)
if err != nil {
log.Errorf("db error :%s", err.Error())
return err
}
if checkinMoney != nil {
return nil
}
var req weixin.RedMoneyReq
req.TotalAmount = cast.ToInt64(cfg.QyWeixin.CheckinPayMoney) * 100
req.Title = cfg.QyWeixin.CheckinPayTitle
req.BillNo = fmt.Sprintf("QY%s%s", time.Now().Format("20060102150405"), butil.RandomStr(6))
req.Userid = "jiangyong"
if err := weixin.NewQyPay().PayRedMoney(&req); err != nil {
log.Errorf("pay red money error :%s", err.Error())
return err
}
message := make([]string, 0)
duration := checkin.EndTime - checkin.StartTime
message = append(message, "【下班提醒】")
message = append(message, fmt.Sprintf("员工名称:%s", checkin.UserId))
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 err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error :%s", err.Error())
}
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
}