194 lines
6.9 KiB
Go
194 lines
6.9 KiB
Go
package service
|
||
|
||
import (
|
||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||
domain_pig "git.huangwc.com/pig/pig-farm-controller/internal/domain/pig"
|
||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||
)
|
||
|
||
// PigBatchService 接口定义保持不变,继续作为应用层对外的契约。
|
||
type PigBatchService interface {
|
||
CreatePigBatch(operatorID uint, dto *dto.PigBatchCreateDTO) (*dto.PigBatchResponseDTO, error)
|
||
GetPigBatch(id uint) (*dto.PigBatchResponseDTO, error)
|
||
UpdatePigBatch(id uint, dto *dto.PigBatchUpdateDTO) (*dto.PigBatchResponseDTO, error)
|
||
DeletePigBatch(id uint) error
|
||
ListPigBatches(isActive *bool) ([]*dto.PigBatchResponseDTO, error)
|
||
AssignEmptyPensToBatch(batchID uint, penIDs []uint, operatorID uint) error
|
||
ReclassifyPenToNewBatch(fromBatchID uint, toBatchID uint, penID uint, operatorID uint, remarks string) error
|
||
RemoveEmptyPenFromBatch(batchID uint, penID uint) error
|
||
MovePigsIntoPen(batchID uint, toPenID uint, quantity int, operatorID uint, remarks string) error
|
||
}
|
||
|
||
// pigBatchService 的实现现在依赖于领域服务接口。
|
||
type pigBatchService struct {
|
||
logger *logs.Logger
|
||
domainService domain_pig.PigBatchService // 依赖注入领域服务
|
||
}
|
||
|
||
// NewPigBatchService 构造函数被修改,以注入领域服务。
|
||
func NewPigBatchService(domainService domain_pig.PigBatchService, logger *logs.Logger) PigBatchService {
|
||
return &pigBatchService{
|
||
logger: logger,
|
||
domainService: domainService,
|
||
}
|
||
}
|
||
|
||
// toPigBatchResponseDTO 负责将领域模型转换为应用层DTO,这个职责保留在应用层。
|
||
func (s *pigBatchService) toPigBatchResponseDTO(batch *models.PigBatch) *dto.PigBatchResponseDTO {
|
||
if batch == nil {
|
||
return nil
|
||
}
|
||
return &dto.PigBatchResponseDTO{
|
||
ID: batch.ID,
|
||
BatchNumber: batch.BatchNumber,
|
||
OriginType: batch.OriginType,
|
||
StartDate: batch.StartDate,
|
||
EndDate: batch.EndDate,
|
||
InitialCount: batch.InitialCount,
|
||
Status: batch.Status,
|
||
IsActive: batch.IsActive(),
|
||
CreateTime: batch.CreatedAt,
|
||
UpdateTime: batch.UpdatedAt,
|
||
}
|
||
}
|
||
|
||
// CreatePigBatch 现在将请求委托给领域服务处理。
|
||
func (s *pigBatchService) CreatePigBatch(operatorID uint, dto *dto.PigBatchCreateDTO) (*dto.PigBatchResponseDTO, error) {
|
||
// 1. DTO -> 领域模型
|
||
batch := &models.PigBatch{
|
||
BatchNumber: dto.BatchNumber,
|
||
OriginType: dto.OriginType,
|
||
StartDate: dto.StartDate,
|
||
InitialCount: dto.InitialCount,
|
||
Status: dto.Status,
|
||
}
|
||
|
||
// 2. 调用领域服务
|
||
createdBatch, err := s.domainService.CreatePigBatch(operatorID, batch)
|
||
if err != nil {
|
||
s.logger.Errorf("应用层: 创建猪批次失败: %v", err)
|
||
return nil, MapDomainError(err)
|
||
}
|
||
|
||
// 3. 领域模型 -> DTO
|
||
return s.toPigBatchResponseDTO(createdBatch), nil
|
||
}
|
||
|
||
// GetPigBatch 从领域服务获取数据并转换为DTO,同时处理错误转换。
|
||
func (s *pigBatchService) GetPigBatch(id uint) (*dto.PigBatchResponseDTO, error) {
|
||
batch, err := s.domainService.GetPigBatch(id)
|
||
if err != nil {
|
||
s.logger.Warnf("应用层: 获取猪批次失败, ID: %d, 错误: %v", id, err)
|
||
return nil, MapDomainError(err)
|
||
}
|
||
|
||
return s.toPigBatchResponseDTO(batch), nil
|
||
}
|
||
|
||
// UpdatePigBatch 协调获取、更新和保存的流程,并处理错误转换。
|
||
func (s *pigBatchService) UpdatePigBatch(id uint, dto *dto.PigBatchUpdateDTO) (*dto.PigBatchResponseDTO, error) {
|
||
// 1. 先获取最新的领域模型
|
||
existingBatch, err := s.domainService.GetPigBatch(id)
|
||
if err != nil {
|
||
s.logger.Warnf("应用层: 更新猪批次失败,获取原批次信息错误, ID: %d, 错误: %v", id, err)
|
||
return nil, MapDomainError(err)
|
||
}
|
||
|
||
// 2. 将DTO中的变更应用到模型上
|
||
if dto.BatchNumber != nil {
|
||
existingBatch.BatchNumber = *dto.BatchNumber
|
||
}
|
||
if dto.OriginType != nil {
|
||
existingBatch.OriginType = *dto.OriginType
|
||
}
|
||
if dto.StartDate != nil {
|
||
existingBatch.StartDate = *dto.StartDate
|
||
}
|
||
if dto.EndDate != nil {
|
||
existingBatch.EndDate = *dto.EndDate
|
||
}
|
||
if dto.InitialCount != nil {
|
||
existingBatch.InitialCount = *dto.InitialCount
|
||
}
|
||
if dto.Status != nil {
|
||
existingBatch.Status = *dto.Status
|
||
}
|
||
|
||
// 3. 调用领域服务执行更新
|
||
updatedBatch, err := s.domainService.UpdatePigBatch(existingBatch)
|
||
if err != nil {
|
||
s.logger.Errorf("应用层: 更新猪批次失败, ID: %d, 错误: %v", id, err)
|
||
return nil, MapDomainError(err)
|
||
}
|
||
|
||
// 4. 转换并返回结果
|
||
return s.toPigBatchResponseDTO(updatedBatch), nil
|
||
}
|
||
|
||
// DeletePigBatch 将删除操作委托给领域服务,并转换领域错误为应用层错误。
|
||
func (s *pigBatchService) DeletePigBatch(id uint) error {
|
||
err := s.domainService.DeletePigBatch(id)
|
||
if err != nil {
|
||
s.logger.Errorf("应用层: 删除猪批次失败, ID: %d, 错误: %v", id, err)
|
||
return MapDomainError(err)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// ListPigBatches 从领域服务获取列表并进行转换。
|
||
func (s *pigBatchService) ListPigBatches(isActive *bool) ([]*dto.PigBatchResponseDTO, error) {
|
||
batches, err := s.domainService.ListPigBatches(isActive)
|
||
if err != nil {
|
||
s.logger.Errorf("应用层: 批量查询猪批次失败: %v", err)
|
||
return nil, MapDomainError(err)
|
||
}
|
||
|
||
var responseDTOs []*dto.PigBatchResponseDTO
|
||
for _, batch := range batches {
|
||
responseDTOs = append(responseDTOs, s.toPigBatchResponseDTO(batch))
|
||
}
|
||
|
||
return responseDTOs, nil
|
||
}
|
||
|
||
// AssignEmptyPensToBatch 委托给领域服务
|
||
func (s *pigBatchService) AssignEmptyPensToBatch(batchID uint, penIDs []uint, operatorID uint) error {
|
||
err := s.domainService.AssignEmptyPensToBatch(batchID, penIDs, operatorID)
|
||
if err != nil {
|
||
s.logger.Errorf("应用层: 为猪批次分配空栏失败, 批次ID: %d, 错误: %v", batchID, err)
|
||
return MapDomainError(err)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// ReclassifyPenToNewBatch 委托给领域服务
|
||
func (s *pigBatchService) ReclassifyPenToNewBatch(fromBatchID uint, toBatchID uint, penID uint, operatorID uint, remarks string) error {
|
||
err := s.domainService.ReclassifyPenToNewBatch(fromBatchID, toBatchID, penID, operatorID, remarks)
|
||
if err != nil {
|
||
s.logger.Errorf("应用层: 划拨猪栏到新批次失败, 源批次ID: %d, 错误: %v", fromBatchID, err)
|
||
return MapDomainError(err)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// RemoveEmptyPenFromBatch 委托给领域服务
|
||
func (s *pigBatchService) RemoveEmptyPenFromBatch(batchID uint, penID uint) error {
|
||
err := s.domainService.RemoveEmptyPenFromBatch(batchID, penID)
|
||
if err != nil {
|
||
s.logger.Errorf("应用层: 从猪批次移除空栏失败, 批次ID: %d, 猪栏ID: %d, 错误: %v", batchID, penID, err)
|
||
return MapDomainError(err)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// MovePigsIntoPen 委托给领域服务
|
||
func (s *pigBatchService) MovePigsIntoPen(batchID uint, toPenID uint, quantity int, operatorID uint, remarks string) error {
|
||
err := s.domainService.MovePigsIntoPen(batchID, toPenID, quantity, operatorID, remarks)
|
||
if err != nil {
|
||
s.logger.Errorf("应用层: 将猪只移入猪栏失败, 批次ID: %d, 目标猪栏ID: %d, 错误: %v", batchID, toPenID, err)
|
||
return MapDomainError(err)
|
||
}
|
||
return nil
|
||
}
|