voucher/internal/service/cmb.go

232 lines
5.0 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.Request().Context(), orderNo)
if err != nil {
return err
}
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")
}
html := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>订单查询结果</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
h1 {
color: green;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>订单查询</h1>
<p>查询结果 [<span style="color: red;">%s</span>] 悉知</p>
<table>
<tr>
<th>订单号</th>
<td>%s</td>
</tr>
<tr>
<th>招行订单号</th>
<td>%s</td>
</tr>
<tr>
<th>订单状态</th>
<td>%s</td>
</tr>
<tr>
<th>openid</th>
<td>%s</td>
</tr>
<tr>
<th>微信券 ID</th>
<td>%s</td>
</tr>
<tr>
<th>商品编号</th>
<td>%s</td>
</tr>
<tr>
<th>批次号</th>
<td>%s</td>
</tr>
<tr>
<th>商户号</th>
<td>%s</td>
</tr>
<tr>
<th>appId</th>
<td>%s</td>
</tr>
<tr>
<th>订单创建时间</th>
<td>%s</td>
</tr>
<tr>
<th>领取成功时间(成功即有该值)</th>
<td>%s</td>
</tr>
<tr>
<th>最后一次核销时间(核销即有该值)</th>
<td>%s</td>
</tr>
<tr>
<th>订单备注说明(失败说明)</th>
<td>%s</td>
</tr>
</table>
</body>
</html>
`
str := fmt.Sprintf(html,
orderNo,
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,
)
return ctx.String(http2.StatusOK, str)
}