This commit is contained in:
李子铭 2025-03-06 17:00:30 +08:00
parent 5abc4151ea
commit 9d22d81cce
15 changed files with 175 additions and 110 deletions

View File

@ -55,6 +55,7 @@ cmb:
aid: "5efaa21263b94f669a1c90ed0279df20" aid: "5efaa21263b94f669a1c90ed0279df20"
keyAlias: "CO_PUB_KEY_SM2" keyAlias: "CO_PUB_KEY_SM2"
cmbKeyAlias: "SM2_CMBLIFE" cmbKeyAlias: "SM2_CMBLIFE"
orgNo: "orgNo" # 发码机构号,固定值,掌上生活优惠券系统提供
notifyUrl: "https://gateway.dev.cdlsxd.cn/ymt/jd/v1/notify" notifyUrl: "https://gateway.dev.cdlsxd.cn/ymt/jd/v1/notify"
#配置日志 #配置日志

View File

@ -1,14 +1,19 @@
package bo package bo
import "time" import (
"time"
"voucher/internal/biz/vo"
)
// OrderNotifyBo 领域实体Bo结构字段和模型字段保持一致 // OrderNotifyBo 领域实体Bo结构字段和模型字段保持一致
type OrderNotifyBo struct { type OrderNotifyBo struct {
ID uint64 ID uint64
OrderNo string OrderNo string
OutRequestNo string OutRequestNo string
Status vo.OrderNotifyStatus
Request string Request string
Responses string Responses string
Remark string
NotifyUrl string NotifyUrl string
CreateTime *time.Time CreateTime *time.Time
UpdateTime *time.Time UpdateTime *time.Time

View File

@ -12,6 +12,7 @@ type Cmb struct {
OrderRepo repo.OrderRepo OrderRepo repo.OrderRepo
OrderWechatRepo repo.OrderWechatRepo OrderWechatRepo repo.OrderWechatRepo
ProductRepo repo.ProductRepo ProductRepo repo.ProductRepo
OrderNotifyRepo repo.OrderNotifyRepo
WechatCpnRepo wechatrepo.WechatCpnRepo WechatCpnRepo wechatrepo.WechatCpnRepo
GenerateMixRepo mixrepos.GenerateMixRepo GenerateMixRepo mixrepos.GenerateMixRepo
CmbMixRepo mixrepos.CmbMixRepo CmbMixRepo mixrepos.CmbMixRepo
@ -22,6 +23,7 @@ func NewCmb(
orderRepo repo.OrderRepo, orderRepo repo.OrderRepo,
OrderWechatRepo repo.OrderWechatRepo, OrderWechatRepo repo.OrderWechatRepo,
ProductRepo repo.ProductRepo, ProductRepo repo.ProductRepo,
OrderNotifyRepo repo.OrderNotifyRepo,
WechatCpnRepo wechatrepo.WechatCpnRepo, WechatCpnRepo wechatrepo.WechatCpnRepo,
GenerateMixRepo mixrepos.GenerateMixRepo, GenerateMixRepo mixrepos.GenerateMixRepo,
CmbMixRepo mixrepos.CmbMixRepo, CmbMixRepo mixrepos.CmbMixRepo,
@ -31,6 +33,7 @@ func NewCmb(
OrderRepo: orderRepo, OrderRepo: orderRepo,
OrderWechatRepo: OrderWechatRepo, OrderWechatRepo: OrderWechatRepo,
ProductRepo: ProductRepo, ProductRepo: ProductRepo,
OrderNotifyRepo: OrderNotifyRepo,
WechatCpnRepo: WechatCpnRepo, WechatCpnRepo: WechatCpnRepo,
GenerateMixRepo: GenerateMixRepo, GenerateMixRepo: GenerateMixRepo,
CmbMixRepo: CmbMixRepo, CmbMixRepo: CmbMixRepo,

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"time"
v1 "voucher/api/v1" v1 "voucher/api/v1"
"voucher/internal/biz/bo" "voucher/internal/biz/bo"
"voucher/internal/biz/vo" "voucher/internal/biz/vo"
@ -41,7 +42,7 @@ func (v *Cmb) OrderConsume(ctx context.Context, order *bo.OrderBo) (outRequestNo
return return
} }
return orderWechat.OutRequestNo, nil return orderWechat.OutRequestNo, err
} }
func (v *Cmb) create(ctx context.Context, order *bo.OrderBo) (*bo.OrderWechatBo, error) { func (v *Cmb) create(ctx context.Context, order *bo.OrderBo) (*bo.OrderWechatBo, error) {
@ -91,10 +92,6 @@ func (v *Cmb) fail(ctx context.Context, order *bo.OrderBo, orderWechat *bo.Order
return v.OrderRepo.Fail(ctx, order.ID) return v.OrderRepo.Fail(ctx, order.ID)
} }
func (v *Cmb) QueryConsume(ctx context.Context, order *bo.OrderBo) (err error) {
return
}
func (v *Cmb) NotifyConsume(ctx context.Context, order *bo.OrderBo, orderOutRequestNo string) error { func (v *Cmb) NotifyConsume(ctx context.Context, order *bo.OrderBo, orderOutRequestNo string) error {
orderWechat, err := v.OrderWechatRepo.GetByOutRequestNo(ctx, orderOutRequestNo) orderWechat, err := v.OrderWechatRepo.GetByOutRequestNo(ctx, orderOutRequestNo)
@ -106,11 +103,15 @@ func (v *Cmb) NotifyConsume(ctx context.Context, order *bo.OrderBo, orderOutRequ
return fmt.Errorf("微信订单状态错误,不能通知:%s", order.Status.GetText()) return fmt.Errorf("微信订单状态错误,不能通知:%s", order.Status.GetText())
} }
status, err := orderWechat.Status.GetCmbStatusText()
if err != nil {
return err
}
req := &v1.CmbNotifyRequest{ req := &v1.CmbNotifyRequest{
Ticket: orderWechat.OrderNo, Ticket: orderWechat.OrderNo,
Status: "", Status: status.GetValue(),
TransDate: "", TransDate: time.Now().Format("20060102150405"),
OrgNo: "", OrgNo: v.bc.Cmb.OrgNo,
Ext: "", Ext: "",
} }
bizJsonBytes, err := json.Marshal(req) bizJsonBytes, err := json.Marshal(req)
@ -126,7 +127,31 @@ func (v *Cmb) NotifyConsume(ctx context.Context, order *bo.OrderBo, orderOutRequ
return err return err
} }
// todo requestBytes, err := json.Marshal(request)
fmt.Print(request) if err != nil {
return err
}
orderNotify, err := v.OrderNotifyRepo.Create(ctx, &bo.OrderNotifyBo{
OrderNo: order.OrderNo,
OutRequestNo: orderWechat.OutRequestNo,
Request: string(requestBytes),
NotifyUrl: order.NotifyUrl,
})
if err != nil {
return err
}
// todo 发起请求
fmt.Print(orderNotify)
return nil return nil
} }
func (v *Cmb) notifySuccess(ctx context.Context, id uint64) error {
return v.OrderNotifyRepo.Success(ctx, id, "")
}
func (v *Cmb) notifyFail(ctx context.Context, id uint64, remark string) error {
return v.OrderNotifyRepo.Fail(ctx, id, remark)
}

View File

@ -80,25 +80,6 @@ func (v *VoucherBiz) OrderConsume(ctx context.Context, orderNo string) (err erro
return return
} }
func (v *VoucherBiz) QueryConsume(ctx context.Context, orderNo string) (err error) {
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("query_consume_%s", orderNo), func(ctx context.Context) error {
order, err := v.OrderRepo.GetByOrderNo(ctx, orderNo)
if err != nil {
return err
}
if order.Type.IsCmb() {
return v.Cmb.QueryConsume(ctx, order)
}
return fmt.Errorf("订单类型错误:%s", order.Type.GetText())
})
return
}
func (v *VoucherBiz) NotifyConsume(ctx context.Context, orderNo, orderOutRequestNo string) (err error) { func (v *VoucherBiz) NotifyConsume(ctx context.Context, orderNo, orderOutRequestNo string) (err error) {
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("notify_consume_%s", orderNo), func(ctx context.Context) error { err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("notify_consume_%s", orderNo), func(ctx context.Context) error {

View File

@ -7,5 +7,6 @@ import (
type OrderNotifyRepo interface { type OrderNotifyRepo interface {
Create(ctx context.Context, req *bo.OrderNotifyBo) (*bo.OrderNotifyBo, error) Create(ctx context.Context, req *bo.OrderNotifyBo) (*bo.OrderNotifyBo, error)
UpdateResponses(ctx context.Context, id uint64, responses string) error Success(ctx context.Context, id uint64, responses string) error
Fail(ctx context.Context, id uint64, remark string) error
} }

View File

@ -0,0 +1,38 @@
package vo
type OrderNotifyStatus uint8
const (
OrderNotifyStatusWait OrderNotifyStatus = iota + 1
OrderNotifyStatusSuccess
OrderNotifyStatusFail
)
var OrderNotifyStatusMap = map[OrderNotifyStatus]string{
OrderNotifyStatusWait: "待请求",
OrderNotifyStatusSuccess: "请求成功",
OrderNotifyStatusFail: "请求失败",
}
func (s OrderNotifyStatus) GetText() string {
if t, ok := OrderNotifyStatusMap[s]; ok {
return t
}
return "未知请求状态"
}
func (s OrderNotifyStatus) GetValue() uint8 {
return uint8(s)
}
func (s OrderNotifyStatus) IsWait() bool {
return s == OrderNotifyStatusWait
}
func (s OrderNotifyStatus) IsSuccess() bool {
return s == OrderNotifyStatusSuccess
}
func (s OrderNotifyStatus) IsFail() bool {
return s == OrderNotifyStatusFail
}

View File

@ -12,6 +12,30 @@ const (
OrderWechatStatusExpired OrderWechatStatusExpired
) )
func (s OrderWechatStatus) GetValue() uint8 {
return uint8(s)
}
func (s OrderWechatStatus) IsWait() bool {
return s == OrderWechatStatusWait
}
func (s OrderWechatStatus) IsSuccess() bool {
return s == OrderWechatStatusSuccess
}
func (s OrderWechatStatus) IsFail() bool {
return s == OrderWechatStatusFail
}
func (s OrderWechatStatus) IsUse() bool {
return s == OrderWechatStatusUse
}
func (s OrderWechatStatus) CanNotify() bool {
return s.IsSuccess() || s.IsUse()
}
var OrderWechatStatusMap = map[OrderWechatStatus]string{ var OrderWechatStatusMap = map[OrderWechatStatus]string{
OrderWechatStatusWait: "待发放", OrderWechatStatusWait: "待发放",
OrderWechatStatusSuccess: "发放成功", OrderWechatStatusSuccess: "发放成功",
@ -38,27 +62,3 @@ func (s OrderWechatStatus) GetText() string {
} }
return "未知状态" return "未知状态"
} }
func (s OrderWechatStatus) GetValue() uint8 {
return uint8(s)
}
func (s OrderWechatStatus) IsWait() bool {
return s == OrderWechatStatusWait
}
func (s OrderWechatStatus) IsSuccess() bool {
return s == OrderWechatStatusSuccess
}
func (s OrderWechatStatus) IsFail() bool {
return s == OrderWechatStatusFail
}
func (s OrderWechatStatus) IsUse() bool {
return s == OrderWechatStatusUse
}
func (s OrderWechatStatus) CanNotify() bool {
return s.IsSuccess() || s.IsUse()
}

View File

@ -428,7 +428,8 @@ type Cmb struct {
CmbSm2Puk string `protobuf:"bytes,6,opt,name=cmbSm2Puk,proto3" json:"cmbSm2Puk,omitempty"` CmbSm2Puk string `protobuf:"bytes,6,opt,name=cmbSm2Puk,proto3" json:"cmbSm2Puk,omitempty"`
KeyAlias string `protobuf:"bytes,7,opt,name=keyAlias,proto3" json:"keyAlias,omitempty"` KeyAlias string `protobuf:"bytes,7,opt,name=keyAlias,proto3" json:"keyAlias,omitempty"`
CmbKeyAlias string `protobuf:"bytes,8,opt,name=cmbKeyAlias,proto3" json:"cmbKeyAlias,omitempty"` CmbKeyAlias string `protobuf:"bytes,8,opt,name=cmbKeyAlias,proto3" json:"cmbKeyAlias,omitempty"`
NotifyUrl string `protobuf:"bytes,9,opt,name=notifyUrl,proto3" json:"notifyUrl,omitempty"` OrgNo string `protobuf:"bytes,9,opt,name=orgNo,proto3" json:"orgNo,omitempty"`
NotifyUrl string `protobuf:"bytes,10,opt,name=notifyUrl,proto3" json:"notifyUrl,omitempty"`
} }
func (x *Cmb) Reset() { func (x *Cmb) Reset() {
@ -519,6 +520,13 @@ func (x *Cmb) GetCmbKeyAlias() string {
return "" return ""
} }
func (x *Cmb) GetOrgNo() string {
if x != nil {
return x.OrgNo
}
return ""
}
func (x *Cmb) GetNotifyUrl() string { func (x *Cmb) GetNotifyUrl() string {
if x != nil { if x != nil {
return x.NotifyUrl return x.NotifyUrl
@ -972,8 +980,8 @@ var file_conf_conf_proto_rawDesc = []byte{
0x0a, 0x1a, 0x6d, 0x63, 0x68, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x0a, 0x1a, 0x6d, 0x63, 0x68, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65,
0x53, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x4e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x4e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01,
0x28, 0x09, 0x52, 0x1a, 0x6d, 0x63, 0x68, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x28, 0x09, 0x52, 0x1a, 0x6d, 0x63, 0x68, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61,
0x74, 0x65, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x4e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x22, 0xf1, 0x74, 0x65, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x4e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x22, 0x87,
0x01, 0x0a, 0x03, 0x43, 0x6d, 0x62, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x69, 0x64, 0x18, 0x01, 0x20, 0x02, 0x0a, 0x03, 0x43, 0x6d, 0x62, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x69, 0x64, 0x18, 0x01, 0x20,
0x01, 0x28, 0x09, 0x52, 0x03, 0x6d, 0x69, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x61, 0x69, 0x64, 0x18, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6d, 0x69, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x61, 0x69, 0x64, 0x18,
0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x61, 0x69, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x6d, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x61, 0x69, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x6d,
0x32, 0x50, 0x72, 0x6b, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x6d, 0x32, 0x50, 0x32, 0x50, 0x72, 0x6b, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x6d, 0x32, 0x50,
@ -986,14 +994,16 @@ var file_conf_conf_proto_rawDesc = []byte{
0x61, 0x73, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6b, 0x65, 0x79, 0x41, 0x6c, 0x69, 0x61, 0x73, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6b, 0x65, 0x79, 0x41, 0x6c, 0x69,
0x61, 0x73, 0x12, 0x20, 0x0a, 0x0b, 0x63, 0x6d, 0x62, 0x4b, 0x65, 0x79, 0x41, 0x6c, 0x69, 0x61, 0x61, 0x73, 0x12, 0x20, 0x0a, 0x0b, 0x63, 0x6d, 0x62, 0x4b, 0x65, 0x79, 0x41, 0x6c, 0x69, 0x61,
0x73, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x63, 0x6d, 0x62, 0x4b, 0x65, 0x79, 0x41, 0x73, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x63, 0x6d, 0x62, 0x4b, 0x65, 0x79, 0x41,
0x6c, 0x69, 0x61, 0x73, 0x12, 0x1c, 0x0a, 0x09, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x55, 0x72, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6f, 0x72, 0x67, 0x4e, 0x6f, 0x18, 0x09, 0x20,
0x6c, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x55, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6f, 0x72, 0x67, 0x4e, 0x6f, 0x12, 0x1c, 0x0a, 0x09, 0x6e, 0x6f,
0x72, 0x6c, 0x22, 0x3a, 0x0a, 0x04, 0x4c, 0x6f, 0x67, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x62, 0x75, 0x74, 0x69, 0x66, 0x79, 0x55, 0x72, 0x6c, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6e,
0x73, 0x69, 0x6e, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x62, 0x75, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x55, 0x72, 0x6c, 0x22, 0x3a, 0x0a, 0x04, 0x4c, 0x6f, 0x67, 0x73,
0x73, 0x69, 0x6e, 0x65, 0x73, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x62, 0x75, 0x73, 0x69, 0x6e, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01,
0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x42, 0x17, 0x28, 0x09, 0x52, 0x08, 0x62, 0x75, 0x73, 0x69, 0x6e, 0x65, 0x73, 0x73, 0x12, 0x16, 0x0a, 0x06,
0x5a, 0x15, 0x76, 0x6f, 0x75, 0x63, 0x68, 0x65, 0x72, 0x2f, 0x63, 0x70, 0x6e, 0x2f, 0x63, 0x6f, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x61, 0x63,
0x6e, 0x66, 0x3b, 0x63, 0x6f, 0x6e, 0x66, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, 0x63, 0x65, 0x73, 0x73, 0x42, 0x17, 0x5a, 0x15, 0x76, 0x6f, 0x75, 0x63, 0x68, 0x65, 0x72, 0x2f,
0x63, 0x70, 0x6e, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x3b, 0x63, 0x6f, 0x6e, 0x66, 0x62, 0x06, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (

View File

@ -79,7 +79,8 @@ message Cmb {
string cmbSm2Puk = 6; string cmbSm2Puk = 6;
string keyAlias = 7; string keyAlias = 7;
string cmbKeyAlias = 8; string cmbKeyAlias = 8;
string notifyUrl = 9; string orgNo = 9;
string notifyUrl = 10;
} }
message Logs { message Logs {

View File

@ -76,12 +76,10 @@ func (s *CmbMixRepoImpl) GetRequest(_ context.Context, reqBo *bo.CmbRequestBo) (
return nil, err return nil, err
} }
date := time.Now().Format("20060102150405")
req := &v1.CmbRequest{ req := &v1.CmbRequest{
Mid: s.bc.Cmb.Mid, Mid: s.bc.Cmb.Mid,
Aid: s.bc.Cmb.Aid, Aid: s.bc.Cmb.Aid,
Date: date, Date: time.Now().Format("20060102150405"),
Random: string(cmb.RandomBytes(16)), Random: string(cmb.RandomBytes(16)),
KeyAlias: s.bc.Cmb.KeyAlias, KeyAlias: s.bc.Cmb.KeyAlias,
CmbKeyAlias: s.bc.Cmb.CmbKeyAlias, CmbKeyAlias: s.bc.Cmb.CmbKeyAlias,
@ -103,12 +101,10 @@ func (s *CmbMixRepoImpl) GetRequest(_ context.Context, reqBo *bo.CmbRequestBo) (
func (s *CmbMixRepoImpl) GetResponse(_ context.Context, reqBo *bo.CmbResponseBo) (*v1.CmbReply, error) { func (s *CmbMixRepoImpl) GetResponse(_ context.Context, reqBo *bo.CmbResponseBo) (*v1.CmbReply, error) {
date := time.Now().Format("20060102150405")
reply := &v1.CmbReply{ reply := &v1.CmbReply{
RespCode: reqBo.RespCode, RespCode: reqBo.RespCode,
RespMsg: reqBo.RespMsg, RespMsg: reqBo.RespMsg,
Date: date, Date: time.Now().Format("20060102150405"),
KeyAlias: s.bc.Cmb.KeyAlias, KeyAlias: s.bc.Cmb.KeyAlias,
CmbKeyAlias: s.bc.Cmb.CmbKeyAlias, CmbKeyAlias: s.bc.Cmb.CmbKeyAlias,
EncryptBody: "", EncryptBody: "",

View File

@ -15,8 +15,10 @@ type OrderNotify struct {
ID uint64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` ID uint64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
OrderNo string `gorm:"column:order_no;not null" json:"order_no"` OrderNo string `gorm:"column:order_no;not null" json:"order_no"`
OutRequestNo string `gorm:"column:out_request_no;not null" json:"out_request_no"` OutRequestNo string `gorm:"column:out_request_no;not null" json:"out_request_no"`
Status uint8 `gorm:"column:status;not null;comment:状态" json:"status"`
Request string `gorm:"column:request;not null" json:"request"` Request string `gorm:"column:request;not null" json:"request"`
Responses string `gorm:"column:responses" json:"responses"` Responses string `gorm:"column:responses" json:"responses"`
Remark string `gorm:"column:remark" json:"remark"`
NotifyUrl string `gorm:"column:notify_url;not null;comment:回调地址" json:"notify_url"` NotifyUrl string `gorm:"column:notify_url;not null;comment:回调地址" json:"notify_url"`
CreateTime *time.Time `gorm:"column:create_time;not null" json:"create_time"` CreateTime *time.Time `gorm:"column:create_time;not null" json:"create_time"`
UpdateTime *time.Time `gorm:"column:update_time" json:"update_time"` UpdateTime *time.Time `gorm:"column:update_time" json:"update_time"`

View File

@ -4,8 +4,10 @@ import (
"context" "context"
"gorm.io/gorm" "gorm.io/gorm"
"time" "time"
"unicode/utf8"
"voucher/internal/biz/bo" "voucher/internal/biz/bo"
"voucher/internal/biz/repo" "voucher/internal/biz/repo"
"voucher/internal/biz/vo"
"voucher/internal/data" "voucher/internal/data"
"voucher/internal/data/model" "voucher/internal/data/model"
) )
@ -31,6 +33,7 @@ func (p *OrderNotifyRepoImpl) Create(ctx context.Context, req *bo.OrderNotifyBo)
info := &model.OrderNotify{ info := &model.OrderNotify{
OrderNo: req.OrderNo, OrderNo: req.OrderNo,
OutRequestNo: req.OutRequestNo, OutRequestNo: req.OutRequestNo,
Status: vo.OrderNotifyStatusWait.GetValue(),
Request: req.Request, Request: req.Request,
NotifyUrl: req.NotifyUrl, NotifyUrl: req.NotifyUrl,
CreateTime: &now, CreateTime: &now,
@ -44,14 +47,16 @@ func (p *OrderNotifyRepoImpl) Create(ctx context.Context, req *bo.OrderNotifyBo)
return p.ToBo(info), nil return p.ToBo(info), nil
} }
func (p *OrderNotifyRepoImpl) UpdateResponses(ctx context.Context, id uint64, responses string) error { func (p *OrderNotifyRepoImpl) Success(ctx context.Context, id uint64, responses string) error {
now := time.Now() now := time.Now()
res := p.db.DB(ctx). res := p.db.DB(ctx).
Where(model.OrderNotify{ Where(model.OrderNotify{
ID: id, ID: id,
Status: vo.OrderNotifyStatusWait.GetValue(),
}). }).
Updates(model.OrderNotify{ Updates(model.OrderNotify{
Status: vo.OrderNotifyStatusSuccess.GetValue(),
Responses: responses, Responses: responses,
UpdateTime: &now, UpdateTime: &now,
}) })
@ -62,3 +67,32 @@ func (p *OrderNotifyRepoImpl) UpdateResponses(ctx context.Context, id uint64, re
return nil return nil
} }
func (p *OrderNotifyRepoImpl) Fail(ctx context.Context, id uint64, remark string) error {
if utf8.RuneCountInString(remark) > 100 {
runes := []rune(remark)
if len(runes) > 100 {
remark = string(runes[:100])
}
}
now := time.Now()
res := p.db.DB(ctx).
Where(model.OrderNotify{
ID: id,
Status: vo.OrderNotifyStatusWait.GetValue(),
}).
Updates(model.OrderNotify{
Status: vo.OrderNotifyStatusFail.GetValue(),
Remark: remark,
UpdateTime: &now,
})
if res.Error != nil {
return res.Error
}
return nil
}

View File

@ -5,6 +5,7 @@ import (
"github.com/go-kratos/kratos/v2/transport/http" "github.com/go-kratos/kratos/v2/transport/http"
v1 "voucher/api/v1" v1 "voucher/api/v1"
"voucher/internal/biz/bo" "voucher/internal/biz/bo"
"voucher/internal/biz/vo"
) )
func (s *VoucherService) CmbOrderMock(ctx http.Context) error { func (s *VoucherService) CmbOrderMock(ctx http.Context) error {
@ -20,7 +21,7 @@ func (s *VoucherService) CmbOrderMock(ctx http.Context) error {
} }
reply, err := s.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{ reply, err := s.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{
FuncName: cmbOrderFuncName, FuncName: vo.CmbOrderFuncName,
BizContent: string(bizJsonBytes), BizContent: string(bizJsonBytes),
}) })
if err != nil { if err != nil {
@ -43,7 +44,7 @@ func (s *VoucherService) CmbProductQueryMock(ctx http.Context) error {
} }
reply, err := s.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{ reply, err := s.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{
FuncName: cmbProductQueryFuncName, FuncName: vo.CmbProductQueryFuncName,
BizContent: string(bizJsonBytes), BizContent: string(bizJsonBytes),
}) })
if err != nil { if err != nil {

View File

@ -41,39 +41,6 @@ func (j *VoucherService) OrderConsumer(ctx context.Context, msg *mq.ConsumerMess
return nil return nil
} }
func (j *VoucherService) GetQueryConfig() *mq.ConsumerConfig {
elm, ok := j.bc.RocketMQ.EventMap["query"]
if !ok {
return nil
}
if !elm.IsOpenConsumer {
log.Warnf("query MQ is not open")
return nil
}
return &mq.ConsumerConfig{
TopicName: elm.Topic,
GroupName: elm.Group,
PerCoroutineCnt: int(elm.PerCoroutineCnt),
}
}
func (j *VoucherService) QueryConsumer(ctx context.Context, msg *mq.ConsumerMessage) error {
orderNo := msg.GetShardingKey()
if orderNo == "" {
log.Error("orderQuery 消费异常,获取 orderNo 失败")
return errors.New("orderQuery 消费异常,获取 orderNo 失败")
}
if err := j.VoucherBiz.QueryConsume(ctx, orderNo); err != nil {
log.Errorf("query 消费异常,orderNo:%s,error: %s", orderNo, err.Error())
}
return nil
}
func (j *VoucherService) GetNotifyConfig() *mq.ConsumerConfig { func (j *VoucherService) GetNotifyConfig() *mq.ConsumerConfig {
elm, ok := j.bc.RocketMQ.EventMap["notify"] elm, ok := j.bc.RocketMQ.EventMap["notify"]
if !ok { if !ok {