voucher/internal/service/cmb.go

154 lines
3.5 KiB
Go

package service
import (
"context"
"fmt"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/robfig/cron"
http2 "net/http"
"strconv"
v1 "voucher/api/v1"
"voucher/internal/biz"
"voucher/internal/biz/bo"
"voucher/internal/biz/mixrepos"
"voucher/internal/biz/vo"
"voucher/internal/biz/wechatrepo"
"voucher/internal/conf"
)
var _ v1.CmbHTTPServer = (*CmbService)(nil)
type CmbService struct {
bc *conf.Bootstrap
cron *cron.Cron
VoucherBiz *biz.VoucherBiz
CmbMixRepo mixrepos.CmbMixRepo
WechatCpnRepo wechatrepo.WechatCpnRepo
}
func NewCmbService(
bc *conf.Bootstrap,
cron *cron.Cron,
VoucherBiz *biz.VoucherBiz,
CmbMixRepo mixrepos.CmbMixRepo,
WechatCpnRepo wechatrepo.WechatCpnRepo,
) *CmbService {
return &CmbService{
bc: bc,
cron: cron,
VoucherBiz: VoucherBiz,
CmbMixRepo: CmbMixRepo,
WechatCpnRepo: WechatCpnRepo,
}
}
func (c *CmbService) GetResponse(ctx context.Context, replyBizContent []byte) (*v1.CmbReply, error) {
req := &bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusSuccess.GetValue(),
RespMsg: "成功",
BizContent: string(replyBizContent),
}
reply, err := c.CmbMixRepo.GetResponse(ctx, req)
if err != nil {
log.Errorf("build cmb response fail: %v", err)
return nil, err
}
return reply, nil
}
func (this *CmbService) NotifyRetry(ctx http.Context) error {
id := ctx.Vars().Get("id")
if id == "" {
return fmt.Errorf("id is empty")
}
orderNotifyId, err := strconv.ParseUint(id, 10, 64)
if err != nil {
return err
}
return this.VoucherBiz.PushNotifyRetryDelayMQ(ctx, 1, orderNotifyId)
}
func (this *CmbService) QueryOrder(ctx http.Context) error {
orderNo := ctx.Vars().Get("order_no")
if orderNo == "" {
return fmt.Errorf("orderNo is empty")
}
str, err := this.VoucherBiz.QueryOrder(ctx, orderNo)
if err != nil {
return err
}
return ctx.JSON(http2.StatusOK, map[string]interface{}{
"data": str,
})
}
func (this *CmbService) QueryByOrderNo(ctx http.Context) error {
// 获取访问ip
ip := ctx.Request().Header.Get("X-Forwarded-For")
if len(ip) == 0 {
ip = ctx.Request().RemoteAddr
}
if ip != "117.175.169.61" && ip != "127.0.0.1" {
return fmt.Errorf("ip check fail,IP:%s", ip)
}
orderNo := ctx.Vars().Get("order_no")
if orderNo == "" {
return fmt.Errorf("orderNo is empty")
}
order, err := this.VoucherBiz.OrderQuery(ctx, orderNo)
if err != nil {
return err
}
msg := "# <font color='green'>" +
"<h1>订单查询</h1>" +
"</font> \n" +
"<font color='black'>" +
"查询结果" +
"[<font color='red'>%s</font>]悉知" +
"</font>"
s := "\n订单号:%s\n招行订单号:%s\n订单状态:%s\nopenid:%s\n微信券ID:%s\n商品编号:%s\n批次号:%s\n商户号:%s\nappId:%s\n订单创建时间:%s\n" +
"领取成功时间(成功即有该值):%s\n最后一次核销时间(核销即有该值):%s\n订单备注说明(失败说明):%s\n"
receiveSuccessTimeStr := ""
if order.ReceiveSuccessTime != nil {
receiveSuccessTimeStr = order.ReceiveSuccessTime.Format("2006-01-02 15:04:05")
}
lastUseTimeStr := ""
if order.LastUseTime != nil {
lastUseTimeStr = order.LastUseTime.Format("2006-01-02 15:04:05")
}
orderMsg := fmt.Sprintf(s,
orderNo,
order.OutBizNo,
order.Status.GetText(),
order.Account,
order.VoucherNo,
order.ProductNo,
order.BatchNo,
order.MerchantNo,
order.AppID,
order.CreateTime.Format("2006-01-02 15:04:05"),
receiveSuccessTimeStr,
lastUseTimeStr,
order.Remark,
)
str := fmt.Sprintf(msg, orderMsg)
return ctx.String(http2.StatusOK, str)
}