71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/services/merchantexclusivecoupon"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
"voucher/internal/biz/bo"
|
|
"voucher/internal/pkg/lock"
|
|
)
|
|
|
|
func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (orderNo string, err error) {
|
|
|
|
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("cmb_order_%s", req.OutBizNo), func(ctx context.Context) error {
|
|
|
|
order, err := v.OrderRepo.GetByOutBizNo(ctx, req.OutBizNo)
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return err
|
|
}
|
|
|
|
if order != nil {
|
|
orderNo = order.OrderNo
|
|
return nil
|
|
}
|
|
|
|
product, err := v.ProductRepo.GetByPNO(ctx, req.ProductNo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !product.Channel.IsWeChat() {
|
|
return fmt.Errorf("只支持微信")
|
|
}
|
|
|
|
if orderNo, err = v.Cmb.Order(ctx, req, product); err != nil {
|
|
return err
|
|
}
|
|
|
|
return v.PushOrderMQ(ctx, orderNo)
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
func (v *VoucherBiz) CmbProductQuery(ctx context.Context, productNo string) (reps *merchantexclusivecoupon.StockGetResponse, err error) {
|
|
|
|
err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("cmb_product_query_%s", productNo), func(ctx context.Context) error {
|
|
|
|
product, err := v.ProductRepo.GetByPNO(ctx, productNo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !product.Channel.IsWeChat() {
|
|
return fmt.Errorf("只支持微信")
|
|
}
|
|
|
|
reps, err = v.WechatCpnRepo.QueryProduct(ctx, product.BatchNo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return
|
|
}
|