pro order use notify

This commit is contained in:
ziming 2025-06-09 14:31:11 +08:00
parent 03828585b1
commit 16fe9ff561
5 changed files with 91 additions and 2 deletions

View File

@ -23,6 +23,7 @@ type OrderBo struct {
Channel vo.Channel
Attach string
Remark string
TransactionId string
ReceiveSuccessTime *time.Time
LastUseTime *time.Time
CreateTime *time.Time

View File

@ -15,12 +15,14 @@ type OrderRepo interface {
GetByOutBizNo(ctx context.Context, t vo.OrderType, outBizNo string) (*bo.OrderBo, error)
GetByOrderNo(ctx context.Context, orderNo string) (*bo.OrderBo, error)
GetByVoucherNo(ctx context.Context, merchantNo, batchNo, voucherNo string) (*bo.OrderBo, error)
GetByTransactionId(ctx context.Context, stockCreatorMchId, stockID, transactionId string) (*bo.OrderBo, error)
Create(ctx context.Context, req *bo.OrderBo) (*bo.OrderBo, error)
GetByID(ctx context.Context, id uint64) (*bo.OrderBo, error)
Ing(ctx context.Context, id uint64) error
Success(ctx context.Context, id uint64, voucherNo string) error
Fail(ctx context.Context, id uint64, remark string) error
Used(ctx context.Context, id uint64) error
NotifyUsed(ctx context.Context, id uint64, transactionId string) error
Available(ctx context.Context, id uint64) error
Expired(ctx context.Context, id uint64) error
}

View File

@ -2,8 +2,10 @@ package biz
import (
"context"
"errors"
"fmt"
"github.com/go-kratos/kratos/v2/log"
"gorm.io/gorm"
errPb "voucher/api/err"
"voucher/internal/biz/bo"
"voucher/internal/biz/vo"
@ -16,7 +18,7 @@ func (v *VoucherBiz) WechatNotifyConsumer(ctx context.Context, tag string, req *
return lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error {
order, err := v.OrderRepo.GetByVoucherNo(ctx, req.PlainText.StockCreatorMchid, req.PlainText.StockID, req.PlainText.CouponID)
order, err := v.getByCouponID(ctx, req)
if err != nil {
return err
}
@ -27,7 +29,7 @@ func (v *VoucherBiz) WechatNotifyConsumer(ctx context.Context, tag string, req *
} else if req.PlainText.Status.IsUsed() {
return v.used(ctx, order)
return v.notifyUsed(ctx, order, req)
} else if req.PlainText.Status.IsExpired() {
@ -39,6 +41,52 @@ func (v *VoucherBiz) WechatNotifyConsumer(ctx context.Context, tag string, req *
})
}
func (this *VoucherBiz) getByCouponID(ctx context.Context, req *bo.WechatVoucherNotifyBo) (*bo.OrderBo, error) {
order, err := this.OrderRepo.GetByVoucherNo(ctx, req.PlainText.StockCreatorMchid, req.PlainText.StockID, req.PlainText.CouponID)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
order, err = this.OrderRepo.GetByTransactionId(ctx, req.PlainText.StockCreatorMchid, req.PlainText.StockID, req.PlainText.ConsumeInformation.TransactionID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fmt.Errorf("微信回调消费,订单不存在,StockCreatorMchid:%s,StockID:%s,CouponID:%s",
req.PlainText.StockCreatorMchid,
req.PlainText.StockID,
req.PlainText.CouponID,
)
}
return nil, err
}
return order, nil
}
return order, nil
}
func (v *VoucherBiz) notifyUsed(ctx context.Context, order *bo.OrderBo, req *bo.WechatVoucherNotifyBo) error {
if order.Status.IsUse() {
log.Warnf("券状态已是已使用,忽略不处理,orderNo:%s", order.OrderNo)
return nil
}
if err := v.OrderRepo.NotifyUsed(ctx, order.ID, req.PlainText.ConsumeInformation.TransactionID); err != nil {
return err
}
return v.notify(ctx, order)
}
func (v *VoucherBiz) used(ctx context.Context, order *bo.OrderBo) error {
if order.Status.IsUse() {

View File

@ -30,6 +30,7 @@ type Order struct {
Attach string `gorm:"column:attach;not null;comment:attach" json:"attach"`
ReceiveSuccessTime *time.Time `gorm:"column:receive_success_time" json:"receive_success_time"`
LastUseTime *time.Time `gorm:"column:last_use_time" json:"last_use_time"`
TransactionId string `gorm:"column:transaction_id;not null" json:"transaction_id"`
CreateTime *time.Time `gorm:"column:create_time" json:"create_time"`
UpdateTime *time.Time `gorm:"column:update_time" json:"update_time"`
}

View File

@ -229,6 +229,22 @@ func (p *OrderRepoImpl) GetByVoucherNo(ctx context.Context, merchantNo, batchNo,
return p.ToBo(info), nil
}
func (this *OrderRepoImpl) GetByTransactionId(ctx context.Context, stockCreatorMchId, stockID, transactionId string) (*bo.OrderBo, error) {
row := &model.Order{}
tx := this.DB(ctx).Where(model.Order{MerchantNo: stockCreatorMchId, BatchNo: stockID, TransactionId: transactionId}).First(&row)
if tx.Error != nil {
return nil, tx.Error
}
if tx.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound
}
return this.ToBo(row), nil
}
func (p *OrderRepoImpl) Ing(ctx context.Context, id uint64) error {
now := time.Now()
@ -338,6 +354,27 @@ func (p *OrderRepoImpl) Used(ctx context.Context, id uint64) error {
return nil
}
func (p *OrderRepoImpl) NotifyUsed(ctx context.Context, id uint64, transactionId string) error {
now := time.Now()
tx := p.DB(ctx).
Where(model.Order{
ID: id,
}).
Updates(model.Order{
Status: vo.OrderStatusUse.GetValue(),
TransactionId: transactionId,
LastUseTime: &now,
UpdateTime: &now,
})
if tx.Error != nil {
return fmt.Errorf("update db fail %w", tx.Error)
}
return nil
}
func (p *OrderRepoImpl) Expired(ctx context.Context, id uint64) error {
now := time.Now()