Files
Christ 375e84d663 重构:统一应用服务与基础设施适配边界
将账号、认证、管理员、分析、自动化、卡券、聊天、商品、关键词、设置和订单能力迁移到 application 与 adapter,删除 Server 内对应的旧仓储和业务服务实现,并收紧架构检查。

完善批量发布与订单刷新生命周期、凭证写回、账号任务补偿、订单 reconciliation、通知和敏感数据边界;补充正常、失败、取消、并发、越权及多方言回归测试,并修正 MySQL 延迟任务表的 InnoDB 配置。

修复空卡密响应为 null 时前端加载失败的问题,重建嵌入式前端资产;重写重构总计划,清除历史流水并明确阶段 1 至 10 各对应一次完整迭代和一个 PR。

验证:make check;make test-server-race;npm test/typecheck/comments:check/build;Docker SQLite/MySQL 8.4/PostgreSQL 17 TestMultiDB_*。
2026-08-16 23:51:06 +08:00

145 lines
5.6 KiB
Go

package items
import (
"context"
"errors"
)
// CatalogWriteInput 是创建或完整保存本地商品所需的应用层输入。
type CatalogWriteInput struct {
// ItemID 是平台商品标识,也是账号范围内的业务唯一键。
ItemID string
// ItemTitle 是商品标题。
ItemTitle string
// ItemDescription 是商品描述。
ItemDescription string
// ItemCategory 是平台商品类目标识。
ItemCategory string
// ItemPrice 是商品价格文本。
ItemPrice string
// ItemDetail 是商品扩展详情 JSON。
ItemDetail string
// IsMultiSpec 表示商品是否启用多规格交付。
IsMultiSpec bool
// MultiQuantityDelivery 表示商品是否启用多数量交付。
MultiQuantityDelivery bool
}
// CatalogPatchInput 是更新本地商品时的可选字段集合;nil 表示保留旧值。
type CatalogPatchInput struct {
// ItemTitle 是待更新的商品标题;nil 表示保留原值。
ItemTitle *string
// ItemDescription 是待更新的商品描述;nil 表示保留原值。
ItemDescription *string
// ItemCategory 是待更新的商品类目标识;nil 表示保留原值。
ItemCategory *string
// ItemPrice 是待更新的商品价格文本;nil 表示保留原值。
ItemPrice *string
// ItemDetail 是待更新的商品扩展详情;nil 表示保留原值。
ItemDetail *string
// IsMultiSpec 是待更新的多规格开关;nil 表示保留原值。
IsMultiSpec *bool
// MultiQuantityDelivery 是待更新的多数量交付开关;nil 表示保留原值。
MultiQuantityDelivery *bool
}
// CatalogMutationRepository 定义本地商品写入和开关变更所需的最小持久化能力。
type CatalogMutationRepository interface {
// Get 读取更新操作需要的现有商品记录。
Get(context.Context, string, string) (CatalogItem, error)
// Upsert 创建或完整保存本地商品记录。
Upsert(context.Context, string, CatalogWriteInput) error
// Delete 逻辑删除商品及其商品级自动化规则。
Delete(context.Context, string, string) error
// SetMultiSpec 设置商品多规格开关。
SetMultiSpec(context.Context, string, string, bool) error
// SetMultiQuantity 设置商品多数量交付开关。
SetMultiQuantity(context.Context, string, string, bool) error
}
// CatalogMutationService 编排本地商品创建、更新、删除和交付开关变更。
type CatalogMutationService struct {
// repository 保存商品写入适配器。
repository CatalogMutationRepository
}
// NewCatalogMutationService 创建商品写应用服务并校验必需端口。
func NewCatalogMutationService(repository CatalogMutationRepository) (*CatalogMutationService, error) {
if repository == nil {
return nil, errors.New("商品写入仓储端口不能为空")
}
return &CatalogMutationService{repository: repository}, nil
}
// Create 创建或恢复指定账号下的本地商品记录。
func (service *CatalogMutationService) Create(ctx context.Context, cookieID string, input CatalogWriteInput) error {
if service == nil || service.repository == nil {
return errors.New("商品写入服务未初始化")
}
return service.repository.Upsert(ctx, cookieID, input)
}
// Update 读取现有商品、合并局部字段并保存完整记录。
func (service *CatalogMutationService) Update(ctx context.Context, cookieID, itemID string, patch CatalogPatchInput) error {
if service == nil || service.repository == nil {
return errors.New("商品写入服务未初始化")
}
// existing、err 保存更新前的商品记录及查询错误。
existing, err := service.repository.Get(ctx, cookieID, itemID)
if err != nil {
return err
}
// input 保存合并后的完整商品写入模型。
input := CatalogWriteInput{
ItemID: itemID, ItemTitle: existing.ItemTitle, ItemDescription: existing.ItemDescription,
ItemCategory: existing.ItemCategory, ItemPrice: existing.ItemPrice, ItemDetail: existing.ItemDetail,
IsMultiSpec: existing.IsMultiSpec, MultiQuantityDelivery: existing.MultiQuantityDelivery,
}
if patch.ItemTitle != nil {
input.ItemTitle = *patch.ItemTitle
}
if patch.ItemDescription != nil {
input.ItemDescription = *patch.ItemDescription
}
if patch.ItemCategory != nil {
input.ItemCategory = *patch.ItemCategory
}
if patch.ItemPrice != nil {
input.ItemPrice = *patch.ItemPrice
}
if patch.ItemDetail != nil {
input.ItemDetail = *patch.ItemDetail
}
if patch.IsMultiSpec != nil {
input.IsMultiSpec = *patch.IsMultiSpec
}
if patch.MultiQuantityDelivery != nil {
input.MultiQuantityDelivery = *patch.MultiQuantityDelivery
}
return service.repository.Upsert(ctx, cookieID, input)
}
// Delete 逻辑删除指定账号下的本地商品。
func (service *CatalogMutationService) Delete(ctx context.Context, cookieID, itemID string) error {
if service == nil || service.repository == nil {
return errors.New("商品写入服务未初始化")
}
return service.repository.Delete(ctx, cookieID, itemID)
}
// SetMultiSpec 更新指定商品的多规格交付开关。
func (service *CatalogMutationService) SetMultiSpec(ctx context.Context, cookieID, itemID string, enabled bool) error {
if service == nil || service.repository == nil {
return errors.New("商品写入服务未初始化")
}
return service.repository.SetMultiSpec(ctx, cookieID, itemID, enabled)
}
// SetMultiQuantity 更新指定商品的多数量交付开关。
func (service *CatalogMutationService) SetMultiQuantity(ctx context.Context, cookieID, itemID string, enabled bool) error {
if service == nil || service.repository == nil {
return errors.New("商品写入服务未初始化")
}
return service.repository.SetMultiQuantity(ctx, cookieID, itemID, enabled)
}