定义病猪子服务
This commit is contained in:
@@ -79,7 +79,7 @@ func NewApplication(configPath string) (*Application, error) {
|
|||||||
pigTransferLogRepo := repository.NewGormPigTransferLogRepository(storage.GetDB())
|
pigTransferLogRepo := repository.NewGormPigTransferLogRepository(storage.GetDB())
|
||||||
pigTradeRepo := repository.NewGormPigTradeRepository(storage.GetDB())
|
pigTradeRepo := repository.NewGormPigTradeRepository(storage.GetDB())
|
||||||
pigSickPigLogRepo := repository.NewGormPigSickLogRepository(storage.GetDB())
|
pigSickPigLogRepo := repository.NewGormPigSickLogRepository(storage.GetDB())
|
||||||
pigGroupMedicationLogRepo := repository.NewGormGroupMedicationLogRepository(storage.GetDB())
|
medicationLogRepo := repository.NewGormMedicationLogRepository(storage.GetDB())
|
||||||
|
|
||||||
// 初始化事务管理器
|
// 初始化事务管理器
|
||||||
unitOfWork := repository.NewGormUnitOfWork(storage.GetDB(), logger)
|
unitOfWork := repository.NewGormUnitOfWork(storage.GetDB(), logger)
|
||||||
@@ -87,7 +87,9 @@ func NewApplication(configPath string) (*Application, error) {
|
|||||||
// 初始化猪群管理领域
|
// 初始化猪群管理领域
|
||||||
pigPenTransferManager := pig.NewPigPenTransferManager(pigPenRepo, pigTransferLogRepo)
|
pigPenTransferManager := pig.NewPigPenTransferManager(pigPenRepo, pigTransferLogRepo)
|
||||||
pigTradeManager := pig.NewPigTradeManager(pigTradeRepo)
|
pigTradeManager := pig.NewPigTradeManager(pigTradeRepo)
|
||||||
pigBatchDomain := pig.NewPigBatchService(pigBatchRepo, pigBatchLogRepo, unitOfWork, pigPenTransferManager, pigTradeManager)
|
pigSickManager := pig.NewSickPigManager(pigSickPigLogRepo, medicationLogRepo)
|
||||||
|
pigBatchDomain := pig.NewPigBatchService(pigBatchRepo, pigBatchLogRepo, unitOfWork,
|
||||||
|
pigPenTransferManager, pigTradeManager, pigSickManager)
|
||||||
|
|
||||||
// --- 业务逻辑处理器初始化 ---
|
// --- 业务逻辑处理器初始化 ---
|
||||||
pigFarmService := service.NewPigFarmService(pigFarmRepo, pigPenRepo, pigBatchRepo, unitOfWork, logger)
|
pigFarmService := service.NewPigFarmService(pigFarmRepo, pigPenRepo, pigBatchRepo, unitOfWork, logger)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||||
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||||
)
|
)
|
||||||
|
|
||||||
// --- 业务错误定义 ---
|
// --- 业务错误定义 ---
|
||||||
@@ -56,3 +57,34 @@ type PigBatchService interface {
|
|||||||
|
|
||||||
UpdatePigBatchQuantity(operatorID uint, batchID uint, changeType models.LogChangeType, changeAmount int, changeReason string, happenedAt time.Time) error
|
UpdatePigBatchQuantity(operatorID uint, batchID uint, changeType models.LogChangeType, changeAmount int, changeReason string, happenedAt time.Time) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pigBatchService 是 PigBatchService 接口的具体实现。
|
||||||
|
// 它作为猪群领域的主服务,封装了所有业务逻辑。
|
||||||
|
type pigBatchService struct {
|
||||||
|
pigBatchRepo repository.PigBatchRepository // 猪批次仓库
|
||||||
|
pigBatchLogRepo repository.PigBatchLogRepository // 猪批次日志仓库
|
||||||
|
uow repository.UnitOfWork // 工作单元,用于管理事务
|
||||||
|
transferSvc PigPenTransferManager // 调栏子服务
|
||||||
|
tradeSvc PigTradeManager // 交易子服务
|
||||||
|
sickSvc SickPigManager // 病猪子服务
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPigBatchService 是 pigBatchService 的构造函数。
|
||||||
|
// 它通过依赖注入的方式,创建并返回一个 PigBatchService 接口的实例。
|
||||||
|
func NewPigBatchService(
|
||||||
|
pigBatchRepo repository.PigBatchRepository,
|
||||||
|
pigBatchLogRepo repository.PigBatchLogRepository,
|
||||||
|
uow repository.UnitOfWork,
|
||||||
|
transferSvc PigPenTransferManager,
|
||||||
|
tradeSvc PigTradeManager,
|
||||||
|
sickSvc SickPigManager,
|
||||||
|
) PigBatchService {
|
||||||
|
return &pigBatchService{
|
||||||
|
pigBatchRepo: pigBatchRepo,
|
||||||
|
pigBatchLogRepo: pigBatchLogRepo,
|
||||||
|
uow: uow,
|
||||||
|
transferSvc: transferSvc,
|
||||||
|
tradeSvc: tradeSvc,
|
||||||
|
sickSvc: sickSvc,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,40 +6,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// --- 领域服务实现 ---
|
// --- 领域服务实现 ---
|
||||||
|
|
||||||
// pigBatchService 是 PigBatchService 接口的具体实现。
|
|
||||||
// 它作为猪群领域的主服务,封装了所有业务逻辑。
|
|
||||||
type pigBatchService struct {
|
|
||||||
pigBatchRepo repository.PigBatchRepository // 猪批次仓库
|
|
||||||
pigBatchLogRepo repository.PigBatchLogRepository // 猪批次日志仓库
|
|
||||||
uow repository.UnitOfWork // 工作单元,用于管理事务
|
|
||||||
transferSvc PigPenTransferManager // 调栏子服务
|
|
||||||
tradeSvc PigTradeManager // 交易子服务
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewPigBatchService 是 pigBatchService 的构造函数。
|
|
||||||
// 它通过依赖注入的方式,创建并返回一个 PigBatchService 接口的实例。
|
|
||||||
func NewPigBatchService(
|
|
||||||
pigBatchRepo repository.PigBatchRepository,
|
|
||||||
pigBatchLogRepo repository.PigBatchLogRepository,
|
|
||||||
uow repository.UnitOfWork,
|
|
||||||
transferSvc PigPenTransferManager,
|
|
||||||
tradeSvc PigTradeManager,
|
|
||||||
) PigBatchService {
|
|
||||||
return &pigBatchService{
|
|
||||||
pigBatchRepo: pigBatchRepo,
|
|
||||||
pigBatchLogRepo: pigBatchLogRepo,
|
|
||||||
uow: uow,
|
|
||||||
transferSvc: transferSvc,
|
|
||||||
tradeSvc: tradeSvc,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreatePigBatch 实现了创建猪批次的逻辑,并同时创建初始批次日志。
|
// CreatePigBatch 实现了创建猪批次的逻辑,并同时创建初始批次日志。
|
||||||
func (s *pigBatchService) CreatePigBatch(operatorID uint, batch *models.PigBatch) (*models.PigBatch, error) {
|
func (s *pigBatchService) CreatePigBatch(operatorID uint, batch *models.PigBatch) (*models.PigBatch, error) {
|
||||||
// 业务规则可以在这里添加,例如检查批次号是否唯一等
|
// 业务规则可以在这里添加,例如检查批次号是否唯一等
|
||||||
|
|||||||
@@ -1 +1,28 @@
|
|||||||
package pig
|
package pig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SickPigManager 定义了与病猪管理相关的操作接口。
|
||||||
|
// 这是一个领域服务,负责协调病猪记录、用药等业务逻辑。
|
||||||
|
type SickPigManager interface {
|
||||||
|
}
|
||||||
|
|
||||||
|
// sickPigManager 是 SickPigManager 接口的具体实现。
|
||||||
|
// 它依赖于仓库接口来执行数据持久化操作。
|
||||||
|
type sickPigManager struct {
|
||||||
|
sickLogRepo repository.PigSickLogRepository
|
||||||
|
medicationLogRepo repository.MedicationLogRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSickPigManager 是 sickPigManager 的构造函数。
|
||||||
|
func NewSickPigManager(
|
||||||
|
sickLogRepo repository.PigSickLogRepository,
|
||||||
|
medicationLogRepo repository.MedicationLogRepository,
|
||||||
|
) SickPigManager {
|
||||||
|
return &sickPigManager{
|
||||||
|
sickLogRepo: sickLogRepo,
|
||||||
|
medicationLogRepo: medicationLogRepo,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -163,12 +163,12 @@ func (ps *PostgresStorage) creatingHyperTable() error {
|
|||||||
{models.RawMaterialPurchase{}, "purchase_date"},
|
{models.RawMaterialPurchase{}, "purchase_date"},
|
||||||
{models.RawMaterialStockLog{}, "happened_at"},
|
{models.RawMaterialStockLog{}, "happened_at"},
|
||||||
{models.FeedUsageRecord{}, "recorded_at"},
|
{models.FeedUsageRecord{}, "recorded_at"},
|
||||||
{models.GroupMedicationLog{}, "happened_at"},
|
{models.MedicationLog{}, "happened_at"},
|
||||||
{models.PigBatchLog{}, "happened_at"},
|
{models.PigBatchLog{}, "happened_at"},
|
||||||
{models.WeighingBatch{}, "weighing_time"},
|
{models.WeighingBatch{}, "weighing_time"},
|
||||||
{models.WeighingRecord{}, "weighing_time"},
|
{models.WeighingRecord{}, "weighing_time"},
|
||||||
{models.PigTransferLog{}, "transfer_time"},
|
{models.PigTransferLog{}, "transfer_time"},
|
||||||
{models.PigBatchSickPigLog{}, "happened_at"},
|
{models.PigSickLog{}, "happened_at"},
|
||||||
{models.PigPurchase{}, "purchase_date"},
|
{models.PigPurchase{}, "purchase_date"},
|
||||||
{models.PigSale{}, "sale_date"},
|
{models.PigSale{}, "sale_date"},
|
||||||
}
|
}
|
||||||
@@ -203,12 +203,12 @@ func (ps *PostgresStorage) applyCompressionPolicies() error {
|
|||||||
{models.RawMaterialPurchase{}, "raw_material_id"},
|
{models.RawMaterialPurchase{}, "raw_material_id"},
|
||||||
{models.RawMaterialStockLog{}, "raw_material_id"},
|
{models.RawMaterialStockLog{}, "raw_material_id"},
|
||||||
{models.FeedUsageRecord{}, "pen_id"},
|
{models.FeedUsageRecord{}, "pen_id"},
|
||||||
{models.GroupMedicationLog{}, "pig_batch_id"},
|
{models.MedicationLog{}, "pig_batch_id"},
|
||||||
{models.PigBatchLog{}, "pig_batch_id"},
|
{models.PigBatchLog{}, "pig_batch_id"},
|
||||||
{models.WeighingBatch{}, "pig_batch_id"},
|
{models.WeighingBatch{}, "pig_batch_id"},
|
||||||
{models.WeighingRecord{}, "weighing_batch_id"},
|
{models.WeighingRecord{}, "weighing_batch_id"},
|
||||||
{models.PigTransferLog{}, "pig_batch_id"},
|
{models.PigTransferLog{}, "pig_batch_id"},
|
||||||
{models.PigBatchSickPigLog{}, "pig_batch_id"},
|
{models.PigSickLog{}, "pig_batch_id"},
|
||||||
{models.PigPurchase{}, "pig_batch_id"},
|
{models.PigPurchase{}, "pig_batch_id"},
|
||||||
{models.PigSale{}, "pig_batch_id"},
|
{models.PigSale{}, "pig_batch_id"},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,8 +80,8 @@ const (
|
|||||||
ReasonTypeHealthCare MedicationReasonType = "保健"
|
ReasonTypeHealthCare MedicationReasonType = "保健"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GroupMedicationLog 记录了对整个猪批次的用药情况
|
// MedicationLog 记录了对整个猪批次的用药情况
|
||||||
type GroupMedicationLog struct {
|
type MedicationLog struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
PigBatchID uint `gorm:"not null;index;comment:关联的猪批次ID"`
|
PigBatchID uint `gorm:"not null;index;comment:关联的猪批次ID"`
|
||||||
MedicationID uint `gorm:"not null;index;comment:关联的药品ID"`
|
MedicationID uint `gorm:"not null;index;comment:关联的药品ID"`
|
||||||
@@ -94,6 +94,6 @@ type GroupMedicationLog struct {
|
|||||||
HappenedAt time.Time `gorm:"primaryKey;comment:用药时间"`
|
HappenedAt time.Time `gorm:"primaryKey;comment:用药时间"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (GroupMedicationLog) TableName() string {
|
func (MedicationLog) TableName() string {
|
||||||
return "group_medication_logs"
|
return "medication_logs"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func GetAllModels() []interface{} {
|
|||||||
&WeighingBatch{},
|
&WeighingBatch{},
|
||||||
&WeighingRecord{},
|
&WeighingRecord{},
|
||||||
&PigTransferLog{},
|
&PigTransferLog{},
|
||||||
&PigBatchSickPigLog{},
|
&PigSickLog{},
|
||||||
|
|
||||||
// Pig Buy & Sell
|
// Pig Buy & Sell
|
||||||
&PigPurchase{},
|
&PigPurchase{},
|
||||||
@@ -58,7 +58,7 @@ func GetAllModels() []interface{} {
|
|||||||
|
|
||||||
// Medication Models
|
// Medication Models
|
||||||
&Medication{},
|
&Medication{},
|
||||||
&GroupMedicationLog{},
|
&MedicationLog{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,22 +5,22 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GroupMedicationLogRepository 定义了与群体用药日志模型相关的数据库操作接口。
|
// MedicationLogRepository 定义了与群体用药日志模型相关的数据库操作接口。
|
||||||
type GroupMedicationLogRepository interface {
|
type MedicationLogRepository interface {
|
||||||
CreateGroupMedicationLog(log *models.GroupMedicationLog) error
|
CreateMedicationLog(log *models.MedicationLog) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// gormGroupMedicationLogRepository 是 GroupMedicationLogRepository 接口的 GORM 实现。
|
// gormMedicationLogRepository 是 MedicationLogRepository 接口的 GORM 实现。
|
||||||
type gormGroupMedicationLogRepository struct {
|
type gormMedicationLogRepository struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGormGroupMedicationLogRepository 创建一个新的 GroupMedicationLogRepository GORM 实现实例。
|
// NewGormMedicationLogRepository 创建一个新的 MedicationLogRepository GORM 实现实例。
|
||||||
func NewGormGroupMedicationLogRepository(db *gorm.DB) GroupMedicationLogRepository {
|
func NewGormMedicationLogRepository(db *gorm.DB) MedicationLogRepository {
|
||||||
return &gormGroupMedicationLogRepository{db: db}
|
return &gormMedicationLogRepository{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateGroupMedicationLog 创建一条新的群体用药日志记录
|
// CreateMedicationLog 创建一条新的群体用药日志记录
|
||||||
func (r *gormGroupMedicationLogRepository) CreateGroupMedicationLog(log *models.GroupMedicationLog) error {
|
func (r *gormMedicationLogRepository) CreateMedicationLog(log *models.MedicationLog) error {
|
||||||
return r.db.Create(log).Error
|
return r.db.Create(log).Error
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user