package biz import ( "context" "errors" "fmt" "github.com/wechatpay-apiv3/wechatpay-go/services/merchantexclusivecoupon" "gorm.io/gorm" "time" v1 "voucher/api/v1" "voucher/internal/biz/bo" "voucher/internal/biz/vo" "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 *v1.CmbQueryProductReply, 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("只支持微信") } wechatResp, err := v.WechatCpnRepo.QueryProduct(ctx, product.BatchNo) if err != nil { return err } reps = &v1.CmbQueryProductReply{ ActivityName: product.Name, ActivityId: product.ProductNo, StartTime: wechatResp.CouponUseRule.CouponAvailableTime.AvailableBeginTime.Format(time.DateTime), EndTime: wechatResp.CouponUseRule.CouponAvailableTime.AvailableEndTime.Format(time.DateTime), Detail: *wechatResp.DisplayPatternInfo.Description, Amount: "", MinAmount: "", AvailableType: "", AvailableDays: "", AvailableStock: "", } availableStock := *wechatResp.StockSendRule.MaxCoupons - *wechatResp.SendCountInformation.TotalSendNum reps.AvailableStock = fmt.Sprintf("%d", availableStock) if wechatResp.CouponUseRule.CouponAvailableTime.AvailableDayAfterReceive != nil { reps.AvailableType = vo.CmbAvailableTypeDynamic.GetValue() reps.AvailableDays = fmt.Sprintf("%d", *wechatResp.CouponUseRule.CouponAvailableTime.AvailableDayAfterReceive) } else { reps.AvailableType = vo.CmbAvailableTypeFixed.GetValue() } if *wechatResp.StockType == merchantexclusivecoupon.BUSIFAVORSTOCKTYPE_NORMAL { reps.Amount = fmt.Sprintf("%d", *wechatResp.CouponUseRule.FixedNormalCoupon.DiscountAmount) reps.MinAmount = fmt.Sprintf("%d", *wechatResp.CouponUseRule.FixedNormalCoupon.TransactionMinimum) } else if *wechatResp.StockType == merchantexclusivecoupon.BUSIFAVORSTOCKTYPE_DISCOUNT { reps.Amount = fmt.Sprintf("%d", *wechatResp.CouponUseRule.DiscountCoupon.DiscountPercent) reps.MinAmount = fmt.Sprintf("%d", *wechatResp.CouponUseRule.FixedNormalCoupon.TransactionMinimum) } return nil }) return }