From 2b02cf7bf6b568f5d7e51dc1042c50b787977dbf Mon Sep 17 00:00:00 2001 From: ziming Date: Tue, 1 Jul 2025 17:43:01 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=84=E8=AD=A65?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/biz/do/product.go | 14 +++++++ internal/biz/do/warning_budget.go | 23 ----------- internal/biz/register_tag.go | 5 +-- internal/biz/repo/product.go | 2 +- internal/biz/voucher.go | 9 ++-- internal/biz/warning_budget.go | 68 +++++++++---------------------- internal/data/repoimpl/product.go | 3 +- 7 files changed, 40 insertions(+), 84 deletions(-) delete mode 100644 internal/biz/do/warning_budget.go diff --git a/internal/biz/do/product.go b/internal/biz/do/product.go index 168c919..2d48f72 100644 --- a/internal/biz/do/product.go +++ b/internal/biz/do/product.go @@ -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 // 券使用率 +} diff --git a/internal/biz/do/warning_budget.go b/internal/biz/do/warning_budget.go deleted file mode 100644 index 627cde2..0000000 --- a/internal/biz/do/warning_budget.go +++ /dev/null @@ -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 -} diff --git a/internal/biz/register_tag.go b/internal/biz/register_tag.go index c92c36c..c027c59 100644 --- a/internal/biz/register_tag.go +++ b/internal/biz/register_tag.go @@ -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 } diff --git a/internal/biz/repo/product.go b/internal/biz/repo/product.go index 9424a71..3333ea9 100644 --- a/internal/biz/repo/product.go +++ b/internal/biz/repo/product.go @@ -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 } diff --git a/internal/biz/voucher.go b/internal/biz/voucher.go index 734e2e1..7c92587 100644 --- a/internal/biz/voucher.go +++ b/internal/biz/voucher.go @@ -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), } } diff --git a/internal/biz/warning_budget.go b/internal/biz/warning_budget.go index c3f8d49..60ef93f 100644 --- a/internal/biz/warning_budget.go +++ b/internal/biz/warning_budget.go @@ -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 { diff --git a/internal/data/repoimpl/product.go b/internal/data/repoimpl/product.go index 5f758a2..336d490 100644 --- a/internal/data/repoimpl/product.go +++ b/internal/data/repoimpl/product.go @@ -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,