阶段八:完成分析应用服务repository收口

This commit is contained in:
Christ committed 2026-08-15 03:00:38 +08:00
1 parent f46bdd1c8e
commit f613bb4eb8
4 files changed
+71 -19

No files matched your search

@@ -613,3 +613,4 @@ npm --prefix frontend run build
| 2026-08-15 | 完成阶段 8 第一个 PR 切片“Chat 窄 repository 接口与服务依赖收口” | 新增 `chat.Repository`,聊天服务只持有会话、消息和账号归属所需的最小持久化接口;完整 `db.Store` 仅在构造适配器时出现,新增 `NewWithRepository` 和内存替身测试,保持聊天消息幂等、会话归属与订阅语义不变;Chat 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:通知与账号任务窄 repository 边界 |
| 2026-08-15 | 完成阶段 8 第二个 PR 切片“账号任务窄 repository 与凭证门禁收口” | 新增 `automation.AccountTaskRepository`,账号任务协调器只持有账号启停/暂停、任务设置/租约、运行凭证和 Cookie 更新所需的最小接口;自动化中心保留 Store 装配适配器,Session 指纹阻断、续期恢复、任务幂等和 Cookie 同步语义保持不变;Automation 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:通知与应用服务 repository 边界 |
| 2026-08-15 | 完成阶段 8 第三个 PR 切片“通知与通信应用服务 repository 边界” | 新增 `notify.Repository`,通知器仅持有渠道、SMTP 系统设置和 outbox 所需的最小接口,保留同步发送、异步租约、重试和测试发送语义;新增通信应用服务窄 repository,账号任务、通知渠道/绑定和聊天历史持久化不再直接访问完整 `db.Store`,实时账号、MTOP 和聊天服务仍由 Server 装配;通知/Server 定向测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:剩余应用服务与事务 repository 边界 |
| 2026-08-15 | 完成阶段 8 第四个 PR 切片“分析应用服务只读 repository 边界” | 新增分析服务窄 repository,统一封装 Dashboard 固定统计、订单分析聚合、有效订单分页、卡密库存读取和数据库方言;应用服务不再持有完整 `Server` 或直接访问 `Store.Analytics/Cards`,查询阶段错误、金额表达式和响应聚合语义保持不变;Server 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:订单与发布应用服务 repository 边界 |
+57
View File
@@ -0,0 +1,57 @@
package server
import (
"context"
"database/sql"
"xianyu-go/internal/db"
)
// analyticsRepository 定义分析服务执行只读 SQL、读取卡密库存和获取数据库方言所需的最小能力。
type analyticsRepository interface {
// QueryRowContext 执行返回单行结果的分析查询。
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
// QueryContext 执行返回多行结果的分析查询。
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
// ListCardsForUser 返回用户拥有的卡密组。
ListCardsForUser(ctx context.Context, userID int64) ([]db.CardFull, error)
// Dialect 返回当前数据库方言。
Dialect() db.Dialect
}
// storeAnalyticsRepository 将完整 Store 适配为分析服务窄 repository。
type storeAnalyticsRepository struct {
// store 保存数据库聚合入口,仅在适配器内部使用。
store *db.Store
}
// QueryRowContext 委托单行分析查询。
func (r storeAnalyticsRepository) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row {
return r.store.Analytics.QueryRowContext(ctx, query, args...)
}
// QueryContext 委托多行分析查询。
func (r storeAnalyticsRepository) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
return r.store.Analytics.QueryContext(ctx, query, args...)
}
// ListCardsForUser 委托用户卡密组查询。
func (r storeAnalyticsRepository) ListCardsForUser(ctx context.Context, userID int64) ([]db.CardFull, error) {
return r.store.Cards.AllForUser(ctx, userID)
}
// Dialect 返回 Store 使用的数据库方言。
func (r storeAnalyticsRepository) Dialect() db.Dialect {
return r.store.Dialect
}
// newStoreAnalyticsRepository 从完整 Store 构造分析服务窄 repository。
func newStoreAnalyticsRepository(store *db.Store) analyticsRepository {
if store == nil || store.Analytics == nil || store.Cards == nil {
return nil
}
return storeAnalyticsRepository{store: store}
}
// 确保 Store 适配器始终覆盖分析服务所需的全部能力。
var _ analyticsRepository = storeAnalyticsRepository{}
+12 -18
View File
@@ -60,14 +60,12 @@ func (e *analyticsServiceError) Unwrap() error { return e.err }
// analyticsService 承载订单分析的数据库编排,不依赖 HTTP 请求或响应对象。
type analyticsService struct {
// server 提供订单仓储和数据库方言依赖。
server *Server
// repository 提供分析查询、卡密库存和数据库方言能力。
repository analyticsRepository
}
// DashboardStats 查询当前用户的数据概览和可用卡密库存。
func (svc *analyticsService) DashboardStats(ctx context.Context, userID int64) (dashboardStatsResponse, error) {
// s 是当前订单分析应用服务依赖的 Server。
s := svc.server
// counts 保存仪表盘各项计数。
counts := map[string]int64{"total_cookies": 0, "active_cookies": 0, "total_cards": 0, "available_card_stock": 0, "total_keywords": 0, "total_orders": 0}
// queries 保存用户范围内的固定统计查询。
@@ -85,7 +83,7 @@ func (svc *analyticsService) DashboardStats(ctx context.Context, userID int64) (
// count 是当前统计项的数据库结果。
var count int64
// err 是当前统计项查询错误。
if err := s.Store.Analytics.QueryRowContext(ctx, item.query, userID).Scan(&count); err != nil {
if err := svc.repository.QueryRowContext(ctx, item.query, userID).Scan(&count); err != nil {
return dashboardStatsResponse{}, err
}
counts[item.key] = count
@@ -93,7 +91,7 @@ func (svc *analyticsService) DashboardStats(ctx context.Context, userID int64) (
// activeCookies 是没有明确禁用记录的账号数量。
var activeCookies int64
// err 是活跃账号统计查询错误。
if err := s.Store.Analytics.QueryRowContext(ctx, `
if err := svc.repository.QueryRowContext(ctx, `
SELECT COUNT(*) FROM cookies c WHERE c.user_id=?
AND NOT EXISTS (SELECT 1 FROM cookie_status cs WHERE cs.cookie_id=c.id AND cs.enabled=0)
`, userID).Scan(&activeCookies); err != nil {
@@ -101,7 +99,7 @@ func (svc *analyticsService) DashboardStats(ctx context.Context, userID int64) (
}
counts["active_cookies"] = activeCookies
// cards 和 err 是当前用户的卡密组列表及查询错误。
cards, err := s.Store.Cards.AllForUser(ctx, userID)
cards, err := svc.repository.ListCardsForUser(ctx, userID)
if err != nil {
return dashboardStatsResponse{}, err
}
@@ -127,14 +125,12 @@ func (s *Server) analyticsApplication() *analyticsService {
// OrderAnalytics 查询收益及按日、状态、城市和商品维度聚合的订单分析结果。
func (svc *analyticsService) OrderAnalytics(ctx context.Context, query analyticsQuery) (orderAnalyticsResponse, error) {
// s 是当前订单分析应用服务依赖的 Server。
s := svc.server
// amountFilter 是已经排除非法金额的查询条件。
amountFilter := query.AmountFilter
// revenue 保存订单收益汇总结果。
var revenue analyticsRevenueStatsResponse
// err 是收益汇总查询错误。
if err := s.Store.Analytics.QueryRowContext(ctx, `
if err := svc.repository.QueryRowContext(ctx, `
SELECT COUNT(DISTINCT order_id), COALESCE(SUM(`+query.AmountClean+`),0),
COALESCE(AVG(`+query.AmountClean+`),0), COUNT(DISTINCT buyer_id), COUNT(DISTINCT item_id)
FROM orders `+query.Where+amountFilter, query.Params...).Scan(
@@ -145,7 +141,7 @@ func (svc *analyticsService) OrderAnalytics(ctx context.Context, query analytics
// daily 保存按用户本地日期聚合的统计结果。
daily := make([]analyticsDailyStatsResponse, 0)
// rows 和 err 是每日统计查询结果集及错误。
rows, err := s.Store.Analytics.QueryContext(ctx, `
rows, err := svc.repository.QueryContext(ctx, `
SELECT order_id,amount,created_at FROM orders `+query.Where+amountFilter, query.Params...)
if err != nil {
return orderAnalyticsResponse{}, &analyticsServiceError{stage: analyticsStageDaily, err: err}
@@ -210,7 +206,7 @@ func (svc *analyticsService) OrderAnalytics(ctx context.Context, query analytics
// statusMap 按归一化状态聚合订单数据。
statusMap := map[string]statusValue{}
// rows 和 err 是状态统计查询结果集及错误。
rows, err = s.Store.Analytics.QueryContext(ctx, `
rows, err = svc.repository.QueryContext(ctx, `
SELECT COALESCE(order_status,'unknown'), COUNT(DISTINCT order_id), COALESCE(SUM(`+query.AmountClean+`),0)
FROM orders `+query.Where+amountFilter+`
GROUP BY order_status ORDER BY COUNT(DISTINCT order_id) DESC`, query.Params...)
@@ -256,7 +252,7 @@ func (svc *analyticsService) OrderAnalytics(ctx context.Context, query analytics
// cityStats 保存按收货城市聚合的结果。
cityStats := make([]analyticsCityStatsResponse, 0)
// rows 和 err 是城市统计查询结果集及错误。
rows, err = s.Store.Analytics.QueryContext(ctx, `
rows, err = svc.repository.QueryContext(ctx, `
SELECT receiver_city, COUNT(DISTINCT order_id), COALESCE(SUM(`+query.AmountClean+`),0)
FROM orders `+query.Where+amountFilter+`
AND receiver_city IS NOT NULL AND receiver_city != ''
@@ -285,7 +281,7 @@ func (svc *analyticsService) OrderAnalytics(ctx context.Context, query analytics
// itemStats 保存按商品标识聚合的排行结果。
itemStats := make([]analyticsItemStatsResponse, 0)
// rows 和 err 是商品统计查询结果集及错误。
rows, err = s.Store.Analytics.QueryContext(ctx, `
rows, err = svc.repository.QueryContext(ctx, `
SELECT item_id, COUNT(DISTINCT order_id), COALESCE(SUM(`+query.AmountClean+`),0), COALESCE(AVG(`+query.AmountClean+`),0)
FROM orders `+query.Where+amountFilter+`
AND item_id IS NOT NULL AND item_id != ''
@@ -321,20 +317,18 @@ func (svc *analyticsService) OrderAnalytics(ctx context.Context, query analytics
// ValidOrders 查询有效订单分页明细。
func (svc *analyticsService) ValidOrders(ctx context.Context, query analyticsQuery, page, pageSize int) (validOrdersResponse, error) {
// s 是当前订单分析应用服务依赖的 Server。
s := svc.server
// amountFilter 是已经排除非法金额的查询条件。
amountFilter := query.AmountFilter
// total 是符合筛选条件的订单总数。
var total int
// err 是有效订单总数查询错误。
if err := s.Store.Analytics.QueryRowContext(ctx, `SELECT COUNT(*) FROM orders `+query.Where+amountFilter, query.Params...).Scan(&total); err != nil {
if err := svc.repository.QueryRowContext(ctx, `SELECT COUNT(*) FROM orders `+query.Where+amountFilter, query.Params...).Scan(&total); err != nil {
return validOrdersResponse{}, &analyticsServiceError{stage: analyticsStageValidCount, err: err}
}
// queryParams 是分页查询附加的 LIMIT/OFFSET 参数。
queryParams := append(append([]any{}, query.Params...), pageSize, (page-1)*pageSize)
// rows 和 err 是有效订单明细查询结果集及错误。
rows, err := s.Store.Analytics.QueryContext(ctx, `
rows, err := svc.repository.QueryContext(ctx, `
SELECT orders.order_id, COALESCE(orders.item_id,''), COALESCE(item_info.item_title,''),
COALESCE(item_info.item_detail,''), COALESCE(orders.buyer_id,''), COALESCE(orders.quantity,'1'),
orders.amount, COALESCE(orders.order_status,'unknown'), COALESCE(orders.cookie_id,''), orders.created_at
+1 -1
View File
@@ -21,7 +21,7 @@ func newApplicationServices(server *Server) *applicationServices {
itemPublish: &itemPublishService{server: server},
accountLogin: &accountLoginService{server: server},
communication: &communicationService{server: server, repository: newStoreCommunicationRepository(server.Store)},
analytics: &analyticsService{server: server},
analytics: &analyticsService{repository: newStoreAnalyticsRepository(server.Store)},
}
}