package biz import ( "context" "fmt" "time" err2 "voucher/api/err" v1 "voucher/api/v1" "voucher/internal/biz/bo" "voucher/internal/biz/vo" "voucher/internal/pkg/lock" ) func (v *VoucherBiz) GetByOutBizNo(ctx context.Context, req *bo.OrderCreateReqBo) (*bo.OrderBo, error) { order, err := v.OrderRepo.GetByOutBizNo(ctx, vo.OrderTypeCmb, req.OutBizNo) if err != nil && !err2.IsDbNotFound(err) { return nil, err } return order, nil } func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (orderNo string, err error) { order, err3 := v.GetByOutBizNo(ctx, req) if err3 != nil { return orderNo, err3 } if order != nil { if order.Status.IsFail() || order.Status.IsIng() { if err4 := v.orderRetry(ctx, order); err4 != nil { return orderNo, err4 } } return order.OrderNo, err } product, err3 := v.ProductRepo.GetByProductNo(ctx, req.ProductNo) if err3 != nil { return orderNo, err3 } order, err3 = v.order(ctx, req, product) if err3 != nil { return orderNo, err3 } return order.OrderNo, nil } func (v *VoucherBiz) CmbQuery(ctx context.Context, orderNo string) (resp *v1.CmbQueryReply, err error) { c := vo.CmbQueryLockKey.BuildCache([]string{orderNo}) err = lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error { order, err3 := v.OrderRepo.GetByOrderNo(ctx, orderNo) if err3 != nil { return err3 } if err = v.Query(ctx, order); err != nil { return err } status, err3 := order.Status.GetCmbStatusText() if err3 != nil { return err3 } resp = &v1.CmbQueryReply{ Ticket: order.OrderNo, Status: status.GetValue(), TransDate: time.Now().Format("20060102150405"), OrgNo: v.bc.Cmb.OrgNo, Ext: "", } return nil }) return } func (v *VoucherBiz) CmbProductQuery(ctx context.Context, productNo string) (reps *v1.CmbQueryProductReply, err error) { c := vo.CmbProductQueryLockKey.BuildCache([]string{productNo}) err = lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error { product, err3 := v.ProductRepo.GetByProductNo(ctx, productNo) if err3 != nil { return err3 } if !product.Channel.IsWeChat() { return fmt.Errorf("只支持微信") } wechatResp, err4 := v.WechatCpnRepo.QueryProduct(ctx, product.MchId, product.BatchNo) if err4 != nil { return err4 } reps = &v1.CmbQueryProductReply{ RespCode: vo.CmbResponseStatusSuccess.GetValue(), RespMsg: "成功", ActivityName: product.Name, ActivityId: product.ProductNo, Amount: "", MinAmount: "", AvailableType: "", AvailableDays: "", // 动态有效期天数 StartTime: "", EndTime: "", AvailableStock: "", Detail: *wechatResp.Description, } inputFormat := time.RFC3339 if wechatResp.AvailableBeginTime != nil { availableBeginTime, _ := time.Parse(inputFormat, *wechatResp.AvailableBeginTime) reps.StartTime = availableBeginTime.Format("2006-01-02 15:04:05.000") reps.SaleStartTime = reps.StartTime } if wechatResp.AvailableEndTime != nil { availableEndTime, _ := time.Parse(inputFormat, *wechatResp.AvailableEndTime) reps.EndTime = availableEndTime.Format("2006-01-02 15:04:05.000") reps.SaleEndTime = reps.EndTime } reps.Amount = fmt.Sprintf("%d", *wechatResp.StockUseRule.FixedNormalCoupon.CouponAmount) reps.MinAmount = fmt.Sprintf("%d", *wechatResp.StockUseRule.FixedNormalCoupon.TransactionMinimum) availableStock := *wechatResp.StockUseRule.MaxCoupons - *wechatResp.DistributedCoupons reps.AvailableStock = fmt.Sprintf("%d", availableStock) availableType, err3 := product.AvailableType.GetCmbAvailableType() if err3 != nil { return err3 } reps.AvailableType = availableType.GetValue() reps.AvailableDays = fmt.Sprintf("%d", product.AvailableDays) return nil }) return }