package service import ( butil "enterprise/base/util" "enterprise/base/wechat/message" "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" ) 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 { return } 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 } 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 } 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 } } if isUpdate { err = dao.NewApprovalRefundDao().Update(refund) } else { _, err = dao.NewApprovalRefundDao().Create(refund) } if err != nil { log.Errorf("db error :%s", err.Error()) return } // 支付费用 var req weixin.RedMoneyReq req.BillNo = fmt.Sprintf("BX%s%s", refund.SpNo, butil.CutTail(refund.Username, 12)) 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 } 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()) } refund.Status = model.ApprovalRefundStatusPayed if err := dao.NewApprovalRefundDao().Update(refund); err != nil { 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) { }