enterprise/server/service/qyweixin_approve.go

166 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"
"github.com/spf13/cast"
"strings"
"time"
)
var (
SpStatusCreated = 1
SpStatusPassed = 2
SpStatusRefused = 3
SpStatusCanceled = 4
)
type Approve struct {
approveClient *weixin.QyWeixinApprove
}
func (b *Approve) Reply(msg message.MixMessage) *message.Reply {
cfg := config.GetConfig()
b.approveClient = weixin.NewQyWeixinApprove(cfg.QyWeixin.Corpid, cfg.QyWeixin.ApproveSecret, cfg.QyWeixin.ApproveAgent)
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
spName := msg.ApprovalInfo.SpName
if spStatus != SpStatusPassed {
2023-08-13 21:46:42 +08:00
return
2023-08-13 21:24:54 +08:00
}
if spName == "费用报销" {
a.handleRefund(spNo)
} else if spName == "请假" {
a.handleHoliday(spNo)
}
}
func (a *Approve) handleHoliday(spNo string) {
}
func (a *Approve) handleRefund(spNo string) {
detail, err := a.approveClient.GetDetail(spNo)
if err != nil {
log.Errorf("get spn detail error :%s", err.Error())
return
}
2023-08-13 21:56:53 +08:00
refund, err := dao.NewApprovalRefundDao().GetBySpNo(spNo)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
isUpdate := true
if refund == nil {
refund = new(model.ApprovalRefund)
isUpdate = false
}
2023-08-13 21:24:54 +08:00
refund.Username = detail.Applyer.Userid
refund.SpNo = detail.SpNo
refund.ApplyTime = detail.ApplyTime
refund.Status = model.ApprovalRefundStatusCreated
for _, content := range detail.ApplyData.Contents {
key := content.Title[0].Text
var value string
if content.Control == "Selector" {
value = content.Value.Selector.Options[0].Value[0].Text
} else if content.Control == "Text" || content.Control == "Textarea" {
value = content.Value.Text
} else if content.Control == "Date" {
value = content.Value.Date.Timestamp
} else if content.Control == "Money" {
value = content.Value.NewMoney
} else if content.Control == "File" {
value = content.Value.Files[0].FileId
}
if key == "报销类型" {
refund.RefundType = value
} else if key == "发生时间" {
refund.RefundDate = time.Unix(cast.ToInt64(value), 0).Format("2006-01-02")
} else if key == "报销费用" {
refund.RefundAmount = cast.ToFloat64(value)
} else if key == "报销说明" {
refund.RefundRemark = value
}
}
2023-08-13 21:56:53 +08:00
if isUpdate {
err = dao.NewApprovalRefundDao().Update(refund)
} else {
_, err = dao.NewApprovalRefundDao().Create(refund)
}
if err != nil {
2023-08-13 21:24:54 +08:00
log.Errorf("db error :%s", err.Error())
return
}
// 支付费用
var req weixin.RedMoneyReq
2023-08-15 12:47:20 +08:00
req.BillNo = fmt.Sprintf("BX%s%s", refund.SpNo, butil.CutTail(refund.Username, 12))
2023-08-13 21:24:54 +08:00
req.Title = fmt.Sprintf("【%s】报销", refund.RefundType)
req.Userid = refund.Username
req.TotalAmount = int64(100 * refund.RefundAmount)
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)
message = append(message, fmt.Sprintf("【红包发放】[%s]", refund.RefundType))
message = append(message, fmt.Sprintf("发放金额:%s", fmt.Sprintf("%.2f", refund.RefundAmount)))
message = append(message, fmt.Sprintf("员工名称:%s", refund.Username))
message = append(message, fmt.Sprintf("费用说明:%s", refund.RefundRemark))
if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error :%s", err.Error())
}
2023-08-13 21:24:54 +08:00
refund.Status = model.ApprovalRefundStatusPayed
2023-08-13 21:56:53 +08:00
if err := dao.NewApprovalRefundDao().Update(refund); 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) {
}