enterprise/server/service/qyweixin_approve.go

205 lines
5.1 KiB
Go
Raw Normal View History

2023-08-13 21:24:54 +08:00
package service
import (
2023-08-15 12:47:20 +08:00
butil "enterprise/base/util"
2023-08-13 21:24:54 +08:00
"enterprise/common/config"
"enterprise/common/dao"
2023-08-13 21:27:00 +08:00
"enterprise/common/global"
2023-08-13 21:24:54 +08:00
"enterprise/common/model"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
2024-01-22 22:50:20 +08:00
"gitlab.batiao8.com/open/gosdk/qyweixin"
2024-01-22 22:58:56 +08:00
"gitlab.batiao8.com/open/gosdk/wechat/message"
2023-08-13 21:24:54 +08:00
"strings"
)
var (
SpStatusCreated = 1
SpStatusPassed = 2
SpStatusRefused = 3
SpStatusCanceled = 4
)
type Approve struct {
2024-01-22 23:06:30 +08:00
approveClient *qyweixin.AppApprove
2023-08-13 21:24:54 +08:00
}
func (b *Approve) Reply(msg message.MixMessage) *message.Reply {
2024-01-07 18:53:21 +08:00
if b.approveClient == nil {
cfg := config.GetConfig()
2024-01-22 23:06:30 +08:00
b.approveClient = qyweixin.NewAppApprove(&qyweixin.AppConfig{
Corpid: cfg.QyWeixin.Corpid,
Secret: cfg.QyWeixin.ApproveSecret,
Agent: cfg.QyWeixin.ApproveAgent,
})
2024-01-07 18:53:21 +08:00
}
2023-08-13 21:24:54 +08:00
go b.handle(&msg)
return &message.Reply{message.MsgTypeText, message.NewText("")}
}
func (a *Approve) handle(msg *message.MixMessage) {
msgType := msg.MsgType
if msgType == message.MsgTypeEvent {
event := strings.ToUpper(string(msg.Event))
if event == message.EventClick {
a.handleClick(msg)
} else if event == "SYS_APPROVAL_CHANGE" {
a.handleApprovalChange(msg)
}
} else if msgType == message.MsgTypeText {
a.handleText(msg)
}
log.Infof(goutil.EncodeJSONIndent(msg))
}
func (a *Approve) handleApprovalChange(msg *message.MixMessage) {
spStatus := msg.ApprovalInfo.SpStatus
spNo := msg.ApprovalInfo.SpNo
2024-01-07 22:54:33 +08:00
templateId := msg.ApprovalInfo.TemplateId
2023-08-13 21:24:54 +08:00
if spStatus != SpStatusPassed {
2023-08-13 21:46:42 +08:00
return
2023-08-13 21:24:54 +08:00
}
2024-01-07 18:53:21 +08:00
2024-01-07 22:54:33 +08:00
detail, err := a.approveClient.GetDetail(spNo)
if err != nil {
log.Errorf("get spn detail error :%s", err.Error())
return
}
2024-01-07 23:23:37 +08:00
if templateId == model.ApprovalTpIdRefund {
2024-01-07 22:54:33 +08:00
a.handleRefund(detail)
2024-01-07 23:23:37 +08:00
} else if templateId == model.ApprovalTpIdVacation {
2024-01-07 22:54:33 +08:00
a.handleVacation(detail)
2024-01-07 23:23:37 +08:00
} else if templateId == model.ApprovalTpIdCheckin {
2024-01-07 22:54:33 +08:00
a.handleCheckin(detail)
2023-08-13 21:24:54 +08:00
}
}
2024-01-22 23:06:30 +08:00
func (a *Approve) handleVacation(detail *qyweixin.ApproveDetail) {
newData := new(model.ApprovalVacation)
newData.From(detail)
2024-01-07 22:54:33 +08:00
dbDao := dao.NewApprovalVacationDao()
old, err := dbDao.GetBySpNo(detail.SpNo)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
if old != nil {
newData.Id = old.Id
newData.CreateTime = old.CreateTime
err = dbDao.Update(newData)
} else {
_, err = dbDao.Create(newData)
}
2023-08-13 21:24:54 +08:00
2024-01-07 22:54:33 +08:00
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
2023-08-13 21:24:54 +08:00
}
2024-01-22 23:06:30 +08:00
func (a *Approve) handleCheckin(detail *qyweixin.ApproveDetail) {
newData := new(model.ApprovalCheckin)
newData.From(detail)
2024-01-07 22:54:33 +08:00
dbDao := dao.NewApprovalCheckinDao()
2024-01-07 18:53:21 +08:00
2024-01-07 22:54:33 +08:00
old, err := dbDao.GetBySpNo(detail.SpNo)
2023-08-13 21:24:54 +08:00
if err != nil {
2024-01-07 22:54:33 +08:00
log.Errorf("db error :%s", err.Error())
2023-08-13 21:24:54 +08:00
return
}
2024-01-07 22:54:33 +08:00
if old != nil {
newData.Id = old.Id
newData.CreateTime = old.CreateTime
err = dbDao.Update(newData)
} else {
_, err = dbDao.Create(newData)
}
2023-08-13 21:24:54 +08:00
2023-08-13 21:56:53 +08:00
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
2024-01-07 22:54:33 +08:00
}
2024-01-22 23:06:30 +08:00
func (a *Approve) handleRefund(detail *qyweixin.ApproveDetail) {
newData := new(model.ApprovalRefund)
newData.From(detail)
2024-01-07 22:54:33 +08:00
dbDao := dao.NewApprovalRefundDao()
2023-08-13 21:56:53 +08:00
2024-01-07 22:54:33 +08:00
old, err := dbDao.GetBySpNo(detail.SpNo)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
2023-08-13 21:56:53 +08:00
}
2024-01-07 22:54:33 +08:00
if old != nil {
newData.Id = old.Id
newData.CreateTime = old.CreateTime
err = dbDao.Update(newData)
2023-08-13 21:56:53 +08:00
} else {
2024-01-07 22:54:33 +08:00
_, err = dbDao.Create(newData)
2023-08-13 21:56:53 +08:00
}
2024-01-07 22:54:33 +08:00
2023-08-13 21:56:53 +08:00
if err != nil {
2023-08-13 21:24:54 +08:00
log.Errorf("db error :%s", err.Error())
return
}
// 支付费用
2024-01-24 16:38:05 +08:00
openid, err := a.approveClient.GetOpenid(newData.Username)
if err != nil {
log.Errorf("GetOpenid error :%s", err.Error())
return
}
2024-01-22 22:50:20 +08:00
var req qyweixin.PayReq
2024-01-07 22:54:33 +08:00
req.BillNo = fmt.Sprintf("BX%s%s", newData.SpNo, butil.CutTail(newData.Username, 12))
req.Title = fmt.Sprintf("【%s】报销", newData.RefundType)
2024-01-24 16:38:05 +08:00
req.Openid = openid
2024-01-07 22:54:33 +08:00
req.TotalAmount = int64(100 * newData.RefundAmount)
2024-01-22 22:50:20 +08:00
qyPay := qyweixin.NewAppPay(&qyweixin.PayConfig{
Corpid: config.GetConfig().QyWeixin.Corpid,
Secret: config.GetConfig().QyWeixin.PaySecret,
Agent: config.GetConfig().QyWeixin.PayAgent,
SerialNumber: config.GetConfig().WxPay.PaySerialNumber,
ApiKey: config.GetConfig().WxPay.PayApiKeyV2,
MchId: config.GetConfig().WxPay.PayMchId,
CertPem: config.GetConfig().WxPay.PayCertPem,
KeyPem: config.GetConfig().WxPay.PayKeyPem,
})
2024-01-24 16:19:06 +08:00
if err = qyPay.PayMoney(&req); err != nil {
2023-08-13 21:24:54 +08:00
log.Errorf("pay error :%s", err.Error())
return
}
2023-08-13 21:27:00 +08:00
message := make([]string, 0)
2024-01-07 22:54:33 +08:00
message = append(message, fmt.Sprintf("【红包发放】[%s]", newData.RefundType))
message = append(message, fmt.Sprintf("发放金额:%s", fmt.Sprintf("%.2f", newData.RefundAmount)))
message = append(message, fmt.Sprintf("员工名称:%s", newData.Username))
message = append(message, fmt.Sprintf("费用说明:%s", newData.RefundRemark))
2023-08-13 21:27:00 +08:00
if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error :%s", err.Error())
}
2024-01-07 22:54:33 +08:00
newData.Status = model.ApprovalRefundStatusPayed
if err := dao.NewApprovalRefundDao().Update(newData); err != nil {
2023-08-13 21:24:54 +08:00
log.Errorf("db error :%s", err.Error())
}
}
func (a *Approve) handleText(msg *message.MixMessage) {
}
func (a *Approve) handleClick(msg *message.MixMessage) {
}
func (a *Approve) sendText(message string) {
}