diff --git a/go.mod b/go.mod index cfddba6..8abca77 100644 --- a/go.mod +++ b/go.mod @@ -74,6 +74,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/patrickmn/go-cache v2.1.0+incompatible // indirect github.com/sirupsen/logrus v1.4.0 // indirect + github.com/stretchr/testify v1.10.0 // indirect github.com/tidwall/gjson v1.13.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect diff --git a/go.sum b/go.sum index 80fa89b..2a25cba 100644 --- a/go.sum +++ b/go.sum @@ -133,7 +133,6 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= -github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -235,8 +234,9 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -245,8 +245,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tidwall/gjson v1.13.0 h1:3TFY9yxOQShrvmjdM76K+jc66zJeT6D3/VFFYCGQf7M= github.com/tidwall/gjson v1.13.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= diff --git a/internal/biz/bo/order_bo.go b/internal/biz/bo/order_bo.go index 7cc2835..4db36b1 100644 --- a/internal/biz/bo/order_bo.go +++ b/internal/biz/bo/order_bo.go @@ -27,6 +27,8 @@ type OrderBo struct { LastUseTime *time.Time CreateTime *time.Time UpdateTime *time.Time + + MiniMum int32 } type OrderCreateReqBo struct { diff --git a/internal/biz/bo/product_bo.go b/internal/biz/bo/product_bo.go index ea31ae8..06d70ac 100644 --- a/internal/biz/bo/product_bo.go +++ b/internal/biz/bo/product_bo.go @@ -13,6 +13,7 @@ type ProductBo struct { BatchName string BatchNo string MchId string + MiniMum int32 Channel vo.Channel AvailableType vo.AvailableType AvailableDays uint32 diff --git a/internal/biz/order.go b/internal/biz/order.go index 548cb86..5a03016 100644 --- a/internal/biz/order.go +++ b/internal/biz/order.go @@ -27,9 +27,9 @@ func (v *VoucherBiz) CmbOrder(ctx context.Context, request *v1.CmbRequest) (*v1. return nil, err } - // 通知kg - b, _ := json.Marshal(reply) - _ = v.bbToWx(ctx, order, string(b)) + if err = v.bbToWx(ctx, order, reply); err != nil { + log.Errorf("处理请求kg报错:%s", err.Error()) + } return reply, nil } @@ -41,8 +41,6 @@ func (c *VoucherBiz) cmbOrder(ctx context.Context, request *v1.CmbRequest) (*bo. return nil, err } - ctx2 := context.Background() - boReq := &bo.OrderCreateReqBo{ OutBizNo: bizContent.TransactionId, ProductNo: bizContent.ActivityId, @@ -54,7 +52,7 @@ func (c *VoucherBiz) cmbOrder(ctx context.Context, request *v1.CmbRequest) (*bo. NotifyUrl: c.bc.Cmb.NotifyUrl, } - order, err := c.Order(ctx2, boReq, bizContent) + order, err := c.Order(ctx, boReq, bizContent) if err != nil { return nil, err } @@ -77,6 +75,7 @@ func (v *VoucherBiz) Order(ctx context.Context, req *bo.OrderCreateReqBo, cmbReq if order != nil { + order.MiniMum = product.MiniMum if order.Status.IsFail() { if err4 := v.orderRetry(ctx, order); err4 != nil { @@ -89,9 +88,10 @@ func (v *VoucherBiz) Order(ctx context.Context, req *bo.OrderCreateReqBo, cmbReq order, err = v.order(ctx, req, product, cmbReq) if err != nil { - return order, err + return nil, err } + order.MiniMum = product.MiniMum return order, nil } @@ -102,24 +102,20 @@ func (v *VoucherBiz) order(ctx context.Context, req *bo.OrderCreateReqBo, produc return nil, err } - // 通知kx _ = v.cmbToBB(ctx, cmbReq) - // 注册通知标签 order.MerchantNo 批次创建商户, order.BatchNo 商品批次号 if err = v.registerNotifyTag(ctx, order.MerchantNo, order.BatchNo); err != nil { return nil, err } - // 真实发放 - //voucherNo, err := v.WechatCpnRepo.Order(ctx, order) - //if err != nil { - // if err3 := v.fail(ctx, order, err); err3 != nil { - // return nil, err3 - // } - // return nil, err - //} + voucherNo, err := v.WechatCpnRepo.Order(ctx, order) + if err != nil { + if err3 := v.fail(ctx, order, err); err3 != nil { + return nil, err3 + } + return nil, err + } - voucherNo := order.OrderNo // mock成功 if err = v.success(ctx, order, voucherNo); err != nil { return order, err } @@ -234,7 +230,12 @@ func (v *VoucherBiz) cmbToBB(ctx context.Context, cmbReq *v1.CmbOrderRequest) er return v.KxMixRepo.Request(ctx, req.GetNotice()) } -func (v *VoucherBiz) bbToWx(ctx context.Context, order *bo.OrderBo, cmbRes string) error { +func (v *VoucherBiz) bbToWx(ctx context.Context, order *bo.OrderBo, cmbReply *v1.CmbReply) error { + + b, err := json.Marshal(cmbReply) + if err != nil { + return err + } wxRes, err := json.Marshal(map[string]string{"coupon_id": order.VoucherNo}) if err != nil { @@ -247,10 +248,10 @@ func (v *VoucherBiz) bbToWx(ctx context.Context, order *bo.OrderBo, cmbRes strin AppId: order.AppID, StockCreatorMhId: order.MerchantNo, CouponValue: 0, - CouponMinimum: 0, + CouponMinimum: order.MiniMum, CouponId: order.VoucherNo, WxRes: string(wxRes), - CmbRes: cmbRes, + CmbRes: string(b), TransactionId: order.OutBizNo, } diff --git a/internal/data/mixrepoimpl/kx.go b/internal/data/mixrepoimpl/kx.go index f3dde71..2262065 100644 --- a/internal/data/mixrepoimpl/kx.go +++ b/internal/data/mixrepoimpl/kx.go @@ -3,9 +3,12 @@ package mixrepoimpl import ( "context" "github.com/go-kratos/kratos/v2/log" + "net/http" + "time" "voucher/internal/biz/kog" "voucher/internal/biz/mixrepos" "voucher/internal/conf" + "voucher/internal/pkg/request" ) // KxMixRepoImpl kog 空港 @@ -21,35 +24,22 @@ func (s *KxMixRepoImpl) Request(ctx context.Context, req *kog.Notice) error { body, err := req.Marshal() if err != nil { - log.Errorf("请求kx Marshal 报错:%s", err.Error()) return err } - log.Warnf("请求kx,reqBody:%s", string(body)) + h := http.Header{ + "Content-Type": []string{"application/json"}, + } - //h := http.Header{ - // "Content-Type": []string{"application/json"}, - //} - //url := s.bc.Cmb.KxNoticeUrl - // - //_, bodyBytes, err := request.Post(ctx, url, body, request.WithHeaders(h), request.WithTimeout(time.Second*20)) - //if err != nil { - // log.Errorf("请求kx报错,url:%s,body:%s,err:%v", url, string(body), err) - // return err - //} - // - //log.Warnf("请求kx,url:%s,reqBody:%s,respBody:%s", url, string(body), string(bodyBytes)) + url := s.bc.Cmb.KxNoticeUrl - //var response *v1.CmbReply - //if err = json.Unmarshal(bodyBytes, &response); err != nil { - // log.Errorf("请求kx返回数据解析报错:%s,url:%s,reqBody:%s,respBody:%s", err.Error(), url, string(bodyBytes)) - // return err - //} - // - //if response.RespCode != vo.CmbResponseStatusSuccess.GetValue() { - // log.Errorf("请求kx返回报错:msg:%s,url:%s,respBody:%s", response.RespMsg, url, string(bodyBytes)) - // return fmt.Errorf(response.RespMsg) - //} + _, bodyBytes, err := request.Post(ctx, url, body, request.WithHeaders(h), request.WithTimeout(time.Second*20)) + if err != nil { + log.Errorf("请求kx报错,url:%s,body:%s,err:%v", url, string(body), err) + return nil + } + + log.Warnf("请求kx,url:%s,reqBody:%s,respBody:%s", url, string(body), string(bodyBytes)) return nil } diff --git a/internal/data/model/product.gen.go b/internal/data/model/product.gen.go index f1e3e69..a5e6bc4 100644 --- a/internal/data/model/product.gen.go +++ b/internal/data/model/product.gen.go @@ -13,12 +13,13 @@ const TableNameProduct = "product" // Product mapped from table type Product struct { ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` - Name string `gorm:"column:name;not null;comment:商品名称" json:"name"` // 商品名称 - ProductNo string `gorm:"column:product_no;not null;comment:商品编号" json:"product_no"` // 商品编号 - BatchName string `gorm:"column:batch_name;not null;comment:批次名称" json:"batch_name"` // 批次名称 - BatchNo string `gorm:"column:batch_no;not null;comment:立减金批次号" json:"batch_no"` // 立减金批次号 - MchId string `gorm:"column:mch_id;not null;comment:商户号,创建批次的商户号" json:"mch_id"` // 商户号,创建批次的商户号 - Channel uint8 `gorm:"column:channel;not null;comment:1:微信 2:支付宝" json:"channel"` // 1:微信 2:支付宝 + Name string `gorm:"column:name;not null;comment:商品名称" json:"name"` // 商品名称 + ProductNo string `gorm:"column:product_no;not null;comment:商品编号" json:"product_no"` // 商品编号 + BatchName string `gorm:"column:batch_name;not null;comment:批次名称" json:"batch_name"` // 批次名称 + BatchNo string `gorm:"column:batch_no;not null;comment:立减金批次号" json:"batch_no"` // 立减金批次号 + MchId string `gorm:"column:mch_id;not null;comment:商户号,创建批次的商户号" json:"mch_id"` // 商户号,创建批次的商户号 + MiniMum int32 `gorm:"column:mini_mum;not null;comment:面额发券批次门槛,单位:分" json:"mini_mum"` // 面额发券批次门槛,单位:分 + Channel uint8 `gorm:"column:channel;not null;comment:1:微信 2:支付宝" json:"channel"` // 1:微信 2:支付宝 AvailableType uint8 `gorm:"column:available_type;not null;comment:1:固定有效期 2:动态有效期" json:"available_type"` AvailableDays uint32 `gorm:"column:available_days;not null;comment:领取后多少天内" json:"available_days"` CreateTime *time.Time `gorm:"column:create_time;not null" json:"create_time"`