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" "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 { if b.approveClient == nil { 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 templateId := msg.ApprovalInfo.TemplateId if spStatus != SpStatusPassed { return } detail, err := a.approveClient.GetDetail(spNo) if err != nil { log.Errorf("get spn detail error :%s", err.Error()) return } if templateId == model.ApprovalTpIdRefund { a.handleRefund(detail) } else if templateId == model.ApprovalTpIdVacation { a.handleVacation(detail) } else if templateId == model.ApprovalTpIdCheckin { a.handleCheckin(detail) } } 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) } if err != nil { log.Errorf("db error :%s", err.Error()) return } } func (a *Approve) handleCheckin(detail *weixin.ApproveDetail) { newData := detail.ToCheckin() dbDao := dao.NewApprovalCheckinDao() 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) } if err != nil { log.Errorf("db error :%s", err.Error()) return } } func (a *Approve) handleRefund(detail *weixin.ApproveDetail) { newData := detail.ToRefund() dbDao := dao.NewApprovalRefundDao() 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) } if err != nil { log.Errorf("db error :%s", err.Error()) return } // 支付费用 var req weixin.RedMoneyReq 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) 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]", 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)) if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil { log.Errorf("send message error :%s", err.Error()) } newData.Status = model.ApprovalRefundStatusPayed if err := dao.NewApprovalRefundDao().Update(newData); 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) { }