enterprise/server/service/qyweixin_approve.go

181 lines
4.4 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/base/wechat/message"
"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"
"enterprise/common/weixin"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"strings"
)
var (
SpStatusCreated = 1
SpStatusPassed = 2
SpStatusRefused = 3
SpStatusCanceled = 4
)
type Approve struct {
approveClient *weixin.QyWeixinApprove
}
func (b *Approve) Reply(msg message.MixMessage) *message.Reply {
2024-01-07 18:53:21 +08:00
if b.approveClient == nil {
cfg := config.GetConfig()
b.approveClient = weixin.NewQyWeixinApprove(cfg.QyWeixin.Corpid, cfg.QyWeixin.ApproveSecret, cfg.QyWeixin.ApproveAgent)
}
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-07 22:54:33 +08:00
func (a *Approve) handleVacation(detail *weixin.ApproveDetail) {
newData := detail.ToVacation()
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-07 22:54:33 +08:00
func (a *Approve) handleCheckin(detail *weixin.ApproveDetail) {
newData := detail.ToCheckin()
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
}
func (a *Approve) handleRefund(detail *weixin.ApproveDetail) {
newData := detail.ToRefund()
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
}
// 支付费用
var req weixin.RedMoneyReq
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)
req.Userid = newData.Username
req.TotalAmount = int64(100 * newData.RefundAmount)
2023-08-13 21:24:54 +08:00
if err := weixin.NewQyPay().PayRedMoney(&req); err != nil {
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) {
}