This commit is contained in:
ziming 2025-07-01 17:43:01 +08:00
parent 8c5396729e
commit 2b02cf7bf6
7 changed files with 40 additions and 84 deletions

View File

@ -20,3 +20,17 @@ type WarningPerson struct {
Name string `json:"name"`
Tag string `json:"tag"`
}
type WarningBudget struct {
StockName string // 券名称
StockId string // 券ID
StockNo string // 券编号
Amount int64 // 券面额
AllBudget int64 // 总预算
AllStock int64 // 总库存
UsedStock int64 // 已使用库存
UsedBudget int64 // 已使用预算
AvailableStock int64 // 可用库存
RemainingBudget int64 // 剩余预算
StockUsageRate float64 // 券使用率
}

View File

@ -1,23 +0,0 @@
package do
import "time"
type WarningBudget struct {
StockName string // 券名称
StockId string // 券ID
StockNo string // 券编号
Amount int64 // 券面额
AllBudget int64 // 总预算
AllStock int64 // 总库存
UsedStock int64 // 已使用库存
UsedBudget int64 // 已使用预算
AvailableStock int64 // 可用库存
RemainingBudget int64 // 剩余预算
StockUsageRate float64 // 券使用率
}
type WarningBudgetLog struct {
WarningBudget *WarningBudget
Num int
LastTime time.Time
}

View File

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

View File

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

View File

@ -3,7 +3,6 @@ package biz
import (
"sync"
"voucher/internal/biz/cmb"
"voucher/internal/biz/do"
"voucher/internal/biz/mixrepos"
"voucher/internal/biz/repo"
"voucher/internal/biz/wechatrepo"
@ -25,9 +24,8 @@ type VoucherBiz struct {
DingMixRepo mixrepos.DingMixRepo
CmbMixRepo mixrepos.CmbMixRepo
mu sync.RWMutex
queryMap map[string]bool
warningBudgeMap map[string]*do.WarningBudgetLog
mu sync.RWMutex
queryMap map[string]bool
}
func NewVoucherBiz(
@ -58,8 +56,7 @@ func NewVoucherBiz(
DingMixRepo: DingMixRepo,
CmbMixRepo: CmbMixRepo,
queryMap: make(map[string]bool),
warningBudgeMap: make(map[string]*do.WarningBudgetLog),
queryMap: make(map[string]bool),
}
}

View File

@ -29,61 +29,23 @@ func (s *VoucherBiz) WarningBudgetIncr(ctx context.Context, uid string) (int64,
}
}
// 如果发送次数超过 6 条,限制发送
if count > 6 {
if _, err = s.rdb.Rdb.Del(ctx, v.Key).Result(); err != nil {
return 0, err
}
// 如果发送次数超过 “指定” 条,清除再来
if count > 12 {
return 0, s.WarningBudgetIncrDel(ctx, v.Key)
}
return count, nil
}
func (this *VoucherBiz) WarningBudgetGet(uid string) *do.WarningBudgetLog {
func (s *VoucherBiz) WarningBudgetIncrDel(ctx context.Context, key string) error {
if w, ok := this.warningBudgeMap[uid]; ok {
return w
if _, err := s.rdb.Rdb.Del(ctx, key).Result(); err != nil {
return err
}
return nil
}
func (this *VoucherBiz) WarningBudgetSet(req *do.WarningBudget) {
this.warningBudgeMap[req.StockId] = &do.WarningBudgetLog{
WarningBudget: req,
Num: 1, // 默认1
LastTime: time.Now(),
}
}
func (this *VoucherBiz) WarningBudgetAdd(req *do.WarningBudget) *do.WarningBudgetLog {
this.mu.Lock()
defer this.mu.Unlock()
w := this.WarningBudgetGet(req.StockId)
if w == nil {
this.WarningBudgetSet(req)
} else {
w.LastTime = time.Now()
w.Num += 1
w.WarningBudget = req
}
return w
}
func (this *VoucherBiz) WarningBudgetRemove(uid string) {
this.mu.Lock()
defer this.mu.Unlock()
if _, ok := this.warningBudgeMap[uid]; ok {
delete(this.warningBudgeMap, uid)
}
}
func (v *VoucherBiz) WarningBudget(ctx context.Context) {
uid := "warningBudget"
@ -142,8 +104,7 @@ func (v *VoucherBiz) Calculate(ctx context.Context, product *bo.ProductBo, wxRes
w := v.WxResp(wxResp)
err := v.ProductRepo.UpdateWarningBudget(ctx, product.ID, w)
if err != nil {
if err := v.ProductRepo.UpdateByWxResp(ctx, product.ID, w); err != nil {
return err
}
@ -155,18 +116,27 @@ func (v *VoucherBiz) Calculate(ctx context.Context, product *bo.ProductBo, wxRes
}
if count == 1 {
return v.WarningSend(ctx, formatAsCard(product, w))
return v.WarningSend(ctx, product, w)
} else {
log.Warnf("预警查询,当前达到预警第[%d]次,暂不做通知", count)
}
if w.AllBudget > product.AllBudget {
}
}
return nil
}
func (v *VoucherBiz) WarningSend(ctx context.Context, str string) error {
func (v *VoucherBiz) WarningSend(ctx context.Context, product *bo.ProductBo, w *do.WxResp) error {
return v.DingMixRepo.SendMarkdownMessage(ctx, "券预算不足", str)
err := v.DingMixRepo.SendMarkdownMessage(ctx, "券预算不足", formatAsCard(product, w))
if err != nil {
return err
}
return nil
}
func formatAsCard(product *bo.ProductBo, req *do.WxResp) string {

View File

@ -53,11 +53,12 @@ func (r *ProductRepoImpl) FindWarningBudget(ctx context.Context, fun func(ctx co
return nil
}
func (r *ProductRepoImpl) UpdateWarningBudget(ctx context.Context, id int32, req *do.WxResp) error {
func (r *ProductRepoImpl) UpdateByWxResp(ctx context.Context, id int32, req *do.WxResp) error {
now := time.Now()
u := model.Product{
Amount: req.Amount,
AllBudget: req.AllBudget,
AvailableBudget: req.AvailableBudget,
UpdateTime: &now,