issue_29 #32
@@ -1,12 +1,24 @@
|
|||||||
package pig
|
package pig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SickPigManager 定义了与病猪管理相关的操作接口。
|
// SickPigManager 定义了与病猪管理相关的操作接口。
|
||||||
// 这是一个领域服务,负责协调病猪记录、用药等业务逻辑。
|
// 这是一个领域服务,负责协调病猪记录、用药等业务逻辑。
|
||||||
type SickPigManager interface {
|
type SickPigManager interface {
|
||||||
|
// ProcessSickPigLog 处理病猪相关的日志事件。
|
||||||
|
// log 包含事件的基本信息,如 PigBatchID, PenID, PigIDs, ChangeCount, Reason, TreatmentLocation, Remarks, OperatorID, HappenedAt。
|
||||||
|
// Manager 内部会计算并填充 BeforeCount 和 AfterCount,并进行必要的业务校验和副作用处理。
|
||||||
|
ProcessSickPigLog(tx *gorm.DB, log *models.PigSickLog) error
|
||||||
|
|
||||||
|
// GetCurrentSickPigCount 获取指定批次当前患病猪只的总数
|
||||||
|
GetCurrentSickPigCount(tx *gorm.DB, batchID uint) (int, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// sickPigManager 是 SickPigManager 接口的具体实现。
|
// sickPigManager 是 SickPigManager 接口的具体实现。
|
||||||
@@ -26,3 +38,90 @@ func NewSickPigManager(
|
|||||||
medicationLogRepo: medicationLogRepo,
|
medicationLogRepo: medicationLogRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *sickPigManager) ProcessSickPigLog(tx *gorm.DB, log *models.PigSickLog) error {
|
||||||
|
// 1. 输入校验
|
||||||
|
if log == nil {
|
||||||
|
return errors.New("病猪日志不能为空")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关键字段校验
|
||||||
|
var missingFields []string
|
||||||
|
if log.PigBatchID == 0 {
|
||||||
|
missingFields = append(missingFields, "PigBatchID")
|
||||||
|
}
|
||||||
|
if log.ChangeCount == 0 {
|
||||||
|
missingFields = append(missingFields, "ChangeCount")
|
||||||
|
}
|
||||||
|
if log.Reason == "" {
|
||||||
|
missingFields = append(missingFields, "Reason")
|
||||||
|
}
|
||||||
|
if log.TreatmentLocation == "" {
|
||||||
|
missingFields = append(missingFields, "TreatmentLocation")
|
||||||
|
}
|
||||||
|
if log.HappenedAt.IsZero() {
|
||||||
|
missingFields = append(missingFields, "HappenedAt")
|
||||||
|
}
|
||||||
|
if log.OperatorID == 0 {
|
||||||
|
missingFields = append(missingFields, "OperatorID")
|
||||||
|
}
|
||||||
|
if log.PenID == 0 {
|
||||||
|
missingFields = append(missingFields, "PenID")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(missingFields) > 0 {
|
||||||
|
return fmt.Errorf("以下关键字段不能为空或零值: %v", missingFields)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 业务规则校验 - ChangeCount 与 Reason 的一致性
|
||||||
|
switch log.Reason {
|
||||||
|
case models.SickPigReasonTypeIllness, models.SickPigReasonTypeTransferIn:
|
||||||
|
if log.ChangeCount < 0 {
|
||||||
|
return fmt.Errorf("原因 '%s' 的 ChangeCount 必须为正数", log.Reason)
|
||||||
|
}
|
||||||
|
case models.SickPigReasonTypeRecovery, models.SickPigReasonTypeDeath, models.SickPigReasonTypeEliminate, models.SickPigReasonTypeTransferOut:
|
||||||
|
if log.ChangeCount > 0 {
|
||||||
|
return fmt.Errorf("原因 '%s' 的 ChangeCount 必须为负数", log.Reason)
|
||||||
|
}
|
||||||
|
case models.SickPigReasonTypeOther:
|
||||||
|
// 其他原因,ChangeCount 可以是任意值,但不能为0
|
||||||
|
if log.ChangeCount == 0 {
|
||||||
|
return errors.New("原因 '其他' 的 ChangeCount 不能为零")
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("未知的病猪日志原因类型: %s", log.Reason)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 获取当前病猪数量 (BeforeCount)
|
||||||
|
beforeCount, err := s.GetCurrentSickPigCount(tx, log.PigBatchID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("获取批次 %d 当前病猪数量失败: %w", log.PigBatchID, err)
|
||||||
|
}
|
||||||
|
log.BeforeCount = beforeCount
|
||||||
|
|
||||||
|
// 3. 计算变化后的数量 (AfterCount)
|
||||||
|
log.AfterCount = log.BeforeCount + log.ChangeCount
|
||||||
|
|
||||||
|
// 4. 业务规则校验 - 数量合法性
|
||||||
|
if log.AfterCount < 0 {
|
||||||
|
return fmt.Errorf("操作后病猪数量不能为负数,当前 %d,变化 %d", log.BeforeCount, log.ChangeCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 持久化 PigSickLog
|
||||||
|
if err := s.sickLogRepo.CreatePigSickLogTx(tx, log); err != nil {
|
||||||
|
return fmt.Errorf("创建 PigSickLog 失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *sickPigManager) GetCurrentSickPigCount(tx *gorm.DB, batchID uint) (int, error) {
|
||||||
|
lastLog, err := s.sickLogRepo.GetLastLogByBatchTx(tx, batchID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return 0, nil // 如果没有找到任何日志,表示当前病猪数量为0
|
||||||
|
}
|
||||||
|
return 0, fmt.Errorf("获取批次 %d 的最新病猪日志失败: %w", batchID, err)
|
||||||
|
}
|
||||||
|
return lastLog.AfterCount, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ type PigSickLog struct {
|
|||||||
gorm.Model
|
gorm.Model
|
||||||
PigBatchID uint `gorm:"primaryKey;comment:关联的猪批次ID"`
|
PigBatchID uint `gorm:"primaryKey;comment:关联的猪批次ID"`
|
||||||
PenID uint `gorm:"not null;index;comment:所在猪圈ID"`
|
PenID uint `gorm:"not null;index;comment:所在猪圈ID"`
|
||||||
PigIDs string `gorm:"size:500;comment:涉及的猪只ID列表,逗号分隔"`
|
|
||||||
ChangeCount int `gorm:"not null;comment:变化数量, 正数表示新增, 负数表示移除"`
|
ChangeCount int `gorm:"not null;comment:变化数量, 正数表示新增, 负数表示移除"`
|
||||||
Reason PigBatchSickPigReasonType `gorm:"size:20;not null;comment:变化原因 (如: 患病, 康复, 死亡, 转入, 转出, 其他)"`
|
Reason PigBatchSickPigReasonType `gorm:"size:20;not null;comment:变化原因 (如: 患病, 康复, 死亡, 转入, 转出, 其他)"`
|
||||||
BeforeCount int `gorm:"comment:变化前的数量"`
|
BeforeCount int `gorm:"comment:变化前的数量"`
|
||||||
|
|||||||
@@ -1,13 +1,20 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PigSickLogRepository 定义了与病猪日志模型相关的数据库操作接口。
|
// PigSickLogRepository 定义了与病猪日志模型相关的数据库操作接口。
|
||||||
type PigSickLogRepository interface {
|
type PigSickLogRepository interface {
|
||||||
|
// CreatePigSickLog 创建一条新的病猪日志记录
|
||||||
CreatePigSickLog(log *models.PigSickLog) error
|
CreatePigSickLog(log *models.PigSickLog) error
|
||||||
|
CreatePigSickLogTx(tx *gorm.DB, log *models.PigSickLog) error
|
||||||
|
|
||||||
|
// GetLastLogByBatchTx 在事务中获取指定批次和猪栏的最新一条 PigSickLog 记录
|
||||||
|
GetLastLogByBatchTx(tx *gorm.DB, batchID uint) (*models.PigSickLog, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// gormPigSickLogRepository 是 PigSickLogRepository 接口的 GORM 实现。
|
// gormPigSickLogRepository 是 PigSickLogRepository 接口的 GORM 实现。
|
||||||
@@ -22,5 +29,25 @@ func NewGormPigSickLogRepository(db *gorm.DB) PigSickLogRepository {
|
|||||||
|
|
||||||
// CreatePigSickLog 创建一条新的病猪日志记录
|
// CreatePigSickLog 创建一条新的病猪日志记录
|
||||||
func (r *gormPigSickLogRepository) CreatePigSickLog(log *models.PigSickLog) error {
|
func (r *gormPigSickLogRepository) CreatePigSickLog(log *models.PigSickLog) error {
|
||||||
return r.db.Create(log).Error
|
return r.CreatePigSickLogTx(r.db, log)
|
||||||
|
}
|
||||||
|
func (r *gormPigSickLogRepository) CreatePigSickLogTx(tx *gorm.DB, log *models.PigSickLog) error {
|
||||||
|
return tx.Create(log).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLastLogByBatchTx 在事务中获取指定批次和猪栏的最新一条 PigSickLog 记录
|
||||||
|
func (r *gormPigSickLogRepository) GetLastLogByBatchTx(tx *gorm.DB, batchID uint) (*models.PigSickLog, error) {
|
||||||
|
var lastLog models.PigSickLog
|
||||||
|
err := tx.
|
||||||
|
Where("pig_batch_id = ?", batchID).
|
||||||
|
Order("happened_at DESC"). // 按时间降序排列
|
||||||
|
First(&lastLog).Error // 获取第一条记录 (即最新一条)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, gorm.ErrRecordNotFound // 明确返回记录未找到错误
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &lastLog, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user