This commit is contained in:
ziming 2025-07-01 16:59:03 +08:00
parent 31fb6e77da
commit b7c52ce699
9 changed files with 61 additions and 32 deletions

View File

@ -16,6 +16,7 @@ type ProductBo struct {
Channel vo.Channel Channel vo.Channel
AvailableType vo.AvailableType AvailableType vo.AvailableType
AvailableDays uint32 AvailableDays uint32
Amount int64
WarningBudget int64 WarningBudget int64
StartTime *time.Time StartTime *time.Time
EndTime *time.Time EndTime *time.Time

View File

@ -0,0 +1,21 @@
package do
import "time"
type WxResp struct {
Amount int64 // 券面额
AllBudget int64 // 总预算
AllStock int64 // 总库存
UsedStock int64 // 已使用库存
UsedBudget int64 // 已使用预算
AvailableStock int64 // 可用库存
RemainingBudget int64 // 剩余预算
StartTime *time.Time
EndTime *time.Time
}
type WarningPerson struct {
Mobile string `json:"mobile"`
Name string `json:"name"`
Tag string `json:"tag"`
}

View File

@ -14,9 +14,6 @@ type WarningBudget struct {
AvailableStock int64 // 可用库存 AvailableStock int64 // 可用库存
RemainingBudget int64 // 剩余预算 RemainingBudget int64 // 剩余预算
StockUsageRate float64 // 券使用率 StockUsageRate float64 // 券使用率
StartTime *time.Time
EndTime *time.Time
} }
type WarningBudgetLog struct { type WarningBudgetLog struct {
@ -24,13 +21,3 @@ type WarningBudgetLog struct {
Num int Num int
LastTime time.Time LastTime time.Time
} }
type WarningPerson struct {
Mobile string `json:"mobile"`
Name string `json:"name"`
Tag string `json:"tag"`
}
type WarningSend struct {
title, text string
}

View File

@ -3,8 +3,10 @@ package biz
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
"time" "time"
v1 "voucher/api/v1" v1 "voucher/api/v1"
"voucher/internal/biz/do"
"voucher/internal/biz/vo" "voucher/internal/biz/vo"
"voucher/internal/pkg/lock" "voucher/internal/pkg/lock"
) )
@ -78,3 +80,35 @@ func (v *VoucherBiz) CmbProductQuery(ctx context.Context, productNo string) (rep
return return
} }
func (v *VoucherBiz) WxResp(wxResp *cashcoupons.Stock) (reps *do.WxResp) {
availableStock := *wxResp.StockUseRule.MaxCoupons - *wxResp.DistributedCoupons
couponAmount := *wxResp.StockUseRule.FixedNormalCoupon.CouponAmount / 100
remainingBudget := availableStock * couponAmount
req := &do.WxResp{
Amount: couponAmount,
AllBudget: *wxResp.StockUseRule.MaxAmount / 100,
AllStock: *wxResp.StockUseRule.MaxCoupons,
UsedStock: *wxResp.DistributedCoupons,
UsedBudget: *wxResp.DistributedCoupons * couponAmount,
AvailableStock: availableStock,
RemainingBudget: remainingBudget,
}
inputFormat := time.RFC3339
if wxResp.AvailableBeginTime != nil {
availableBeginTime, _ := time.Parse(inputFormat, *wxResp.AvailableBeginTime)
req.StartTime = &availableBeginTime
}
if wxResp.AvailableEndTime != nil {
availableEndTime, _ := time.Parse(inputFormat, *wxResp.AvailableEndTime)
req.EndTime = &availableEndTime
}
return req
}

View File

@ -22,10 +22,7 @@ func (this *VoucherBiz) RegisterTag(ctx context.Context, batchNo string) error {
return err return err
} }
req, err := this.GetWarningBudget(stock, wxResp) req := this.WxResp(wxResp)
if err != nil {
return err
}
err = this.ProductRepo.UpdateWarningBudget(ctx, stock.ID, req) err = this.ProductRepo.UpdateWarningBudget(ctx, stock.ID, req)
if err != nil { if err != nil {

View File

@ -10,5 +10,5 @@ type ProductRepo interface {
FindWarningBudget(ctx context.Context, fun func(ctx context.Context, rows []*bo.ProductBo) error) error FindWarningBudget(ctx context.Context, fun func(ctx context.Context, rows []*bo.ProductBo) error) error
GetByBatchNo(ctx context.Context, batchNo string) (*bo.ProductBo, error) GetByBatchNo(ctx context.Context, batchNo string) (*bo.ProductBo, error)
GetByProductNo(ctx context.Context, productNo string) (*bo.ProductBo, error) GetByProductNo(ctx context.Context, productNo string) (*bo.ProductBo, error)
UpdateWarningBudget(ctx context.Context, id int32, req *do.WarningBudget) error UpdateWarningBudget(ctx context.Context, id int32, req *do.WxResp) error
} }

View File

@ -161,18 +161,6 @@ func (v *VoucherBiz) GetWarningBudget(product *bo.ProductBo, wxResp *cashcoupons
StockUsageRate: stockUsageRate, StockUsageRate: stockUsageRate,
} }
inputFormat := time.RFC3339
if wxResp.AvailableBeginTime != nil {
availableBeginTime, _ := time.Parse(inputFormat, *wxResp.AvailableBeginTime)
req.StartTime = &availableBeginTime
}
if wxResp.AvailableEndTime != nil {
availableEndTime, _ := time.Parse(inputFormat, *wxResp.AvailableEndTime)
req.EndTime = &availableEndTime
}
return req, nil return req, nil
} }
@ -183,7 +171,7 @@ func (v *VoucherBiz) Calculate(ctx context.Context, product *bo.ProductBo, wxRes
return err return err
} }
err = v.ProductRepo.UpdateWarningBudget(ctx, product.ID, req) err = v.ProductRepo.UpdateWarningBudget(ctx, product.ID, v.WxResp(wxResp))
if err != nil { if err != nil {
return err return err
} }

View File

@ -21,6 +21,7 @@ type Product struct {
Channel uint8 `gorm:"column:channel;not null;comment:1:微信 2:支付宝" json:"channel"` // 1:微信 2:支付宝 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"` 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"` AvailableDays uint32 `gorm:"column:available_days;not null;comment:领取后多少天内" json:"available_days"`
Amount int64 `gorm:"column:amount;not null;default:0" json:"amount"`
AllBudget int64 `gorm:"column:all_budget;not null;default:0" json:"all_budget"` AllBudget int64 `gorm:"column:all_budget;not null;default:0" json:"all_budget"`
RemainingBudget int64 `gorm:"column:remaining_budget;not null;default:0" json:"remaining_budget"` RemainingBudget int64 `gorm:"column:remaining_budget;not null;default:0" json:"remaining_budget"`
WarningBudget int64 `gorm:"column:warning_budget;not null;default:0" json:"warning_budget"` // 预警预算=0不做预警 WarningBudget int64 `gorm:"column:warning_budget;not null;default:0" json:"warning_budget"` // 预警预算=0不做预警

View File

@ -53,7 +53,7 @@ func (r *ProductRepoImpl) FindWarningBudget(ctx context.Context, fun func(ctx co
return nil return nil
} }
func (r *ProductRepoImpl) UpdateWarningBudget(ctx context.Context, id int32, req *do.WarningBudget) error { func (r *ProductRepoImpl) UpdateWarningBudget(ctx context.Context, id int32, req *do.WxResp) error {
now := time.Now() now := time.Now()