重构: 店铺套餐分配系统从加价模式改为返佣模式
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m18s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m18s
主要变更: - 重构分配模型:从加价模式(pricing_mode/pricing_value)改为返佣模式(base_commission + tier_commission) - 删除独立的 my_package 接口,统一到 /api/admin/packages(通过数据权限自动过滤) - 新增批量分配和批量调价功能,支持事务和性能优化 - 新增配置版本管理,订单创建时锁定返佣配置 - 新增成本价历史记录,支持审计和纠纷处理 - 新增统计缓存系统(Redis + 异步任务),优化梯度返佣计算性能 - 删除冗余的梯度佣金独立 CRUD 接口(合并到分配配置中) - 归档 3 个已完成的 OpenSpec changes 并同步 8 个新 capabilities 到 main specs 技术细节: - 数据库迁移:000026_refactor_shop_package_allocation - 新增 Store:AllocationConfigStore, PriceHistoryStore, CommissionStatsStore - 新增 Service:BatchAllocationService, BatchPricingService, CommissionStatsService - 新增异步任务:统计更新、定时同步、周期归档 - 测试覆盖:批量操作集成测试、梯度佣金 CRUD 清理验证 影响: - API 变更:删除 4 个梯度 CRUD 接口(POST/GET/PUT/DELETE /:id/tiers) - API 新增:批量分配、批量调价接口 - 数据模型:重构 shop_series_allocation 表结构 - 性能优化:批量操作使用 CreateInBatches,统计使用 Redis 缓存 相关文档: - openspec/changes/archive/2026-01-28-refactor-shop-package-allocation/ - openspec/specs/agent-available-packages/ - openspec/specs/allocation-config-versioning/ - 等 8 个新 capability specs
This commit is contained in:
98
internal/service/commission_stats/service.go
Normal file
98
internal/service/commission_stats/service.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package commission_stats
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
statsStore *postgres.ShopSeriesCommissionStatsStore
|
||||
}
|
||||
|
||||
func New(statsStore *postgres.ShopSeriesCommissionStatsStore) *Service {
|
||||
return &Service{
|
||||
statsStore: statsStore,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) GetCurrentStats(ctx context.Context, allocationID uint, periodType string) (*model.ShopSeriesCommissionStats, error) {
|
||||
now := time.Now()
|
||||
|
||||
stats, err := s.statsStore.GetCurrent(ctx, allocationID, periodType, now)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, errors.New(errors.CodeNotFound, "统计数据不存在")
|
||||
}
|
||||
return nil, fmt.Errorf("获取统计数据失败: %w", err)
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (s *Service) UpdateStats(ctx context.Context, allocationID uint, periodType string, salesCount int64, salesAmount int64) error {
|
||||
now := time.Now()
|
||||
periodStart, periodEnd := calculatePeriod(now, periodType)
|
||||
|
||||
stats, err := s.statsStore.GetCurrent(ctx, allocationID, periodType, now)
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
return fmt.Errorf("查询统计数据失败: %w", err)
|
||||
}
|
||||
|
||||
if stats == nil {
|
||||
stats = &model.ShopSeriesCommissionStats{
|
||||
AllocationID: allocationID,
|
||||
PeriodType: periodType,
|
||||
PeriodStart: periodStart,
|
||||
PeriodEnd: periodEnd,
|
||||
TotalSalesCount: salesCount,
|
||||
TotalSalesAmount: salesAmount,
|
||||
Status: "active",
|
||||
LastUpdatedAt: now,
|
||||
Version: 1,
|
||||
}
|
||||
return s.statsStore.Create(ctx, stats)
|
||||
}
|
||||
|
||||
return s.statsStore.IncrementSales(ctx, stats.ID, salesCount, salesAmount, stats.Version)
|
||||
}
|
||||
|
||||
func (s *Service) ArchiveCompletedPeriod(ctx context.Context, allocationID uint, periodType string) error {
|
||||
now := time.Now()
|
||||
stats, err := s.statsStore.GetCurrent(ctx, allocationID, periodType, now)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("查询统计数据失败: %w", err)
|
||||
}
|
||||
|
||||
return s.statsStore.CompletePeriod(ctx, stats.ID)
|
||||
}
|
||||
|
||||
func calculatePeriod(now time.Time, periodType string) (time.Time, time.Time) {
|
||||
var periodStart, periodEnd time.Time
|
||||
|
||||
switch periodType {
|
||||
case "monthly":
|
||||
periodStart = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
||||
periodEnd = periodStart.AddDate(0, 1, 0).Add(-time.Second)
|
||||
case "quarterly":
|
||||
quarter := (int(now.Month()) - 1) / 3
|
||||
periodStart = time.Date(now.Year(), time.Month(quarter*3+1), 1, 0, 0, 0, 0, now.Location())
|
||||
periodEnd = periodStart.AddDate(0, 3, 0).Add(-time.Second)
|
||||
case "yearly":
|
||||
periodStart = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, now.Location())
|
||||
periodEnd = periodStart.AddDate(1, 0, 0).Add(-time.Second)
|
||||
default:
|
||||
periodStart = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
||||
periodEnd = periodStart.AddDate(0, 1, 0).Add(-time.Second)
|
||||
}
|
||||
|
||||
return periodStart, periodEnd
|
||||
}
|
||||
Reference in New Issue
Block a user