猪栏信息接口增加猪栏当前存栏量
This commit is contained in:
@@ -1 +0,0 @@
|
||||
package management
|
||||
@@ -1 +0,0 @@
|
||||
package management
|
||||
@@ -1 +0,0 @@
|
||||
package management
|
||||
@@ -276,17 +276,7 @@ func (c *PigFarmController) GetPen(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp := dto.PenResponse{
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
}
|
||||
if pen.PigBatchID != nil {
|
||||
resp.PigBatchID = *pen.PigBatchID
|
||||
}
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", resp, action, "获取成功", resp)
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", pen, action, "获取成功", pen)
|
||||
}
|
||||
|
||||
// ListPens godoc
|
||||
@@ -306,22 +296,7 @@ func (c *PigFarmController) ListPens(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var resp []dto.PenResponse
|
||||
for _, pen := range pens {
|
||||
res := dto.PenResponse{
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
}
|
||||
if pen.PigBatchID != nil {
|
||||
res.PigBatchID = *pen.PigBatchID
|
||||
}
|
||||
resp = append(resp, res)
|
||||
}
|
||||
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", resp, action, "获取成功", resp)
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", pens, action, "获取成功", pens)
|
||||
}
|
||||
|
||||
// UpdatePen godoc
|
||||
@@ -362,14 +337,12 @@ func (c *PigFarmController) UpdatePen(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
resp := dto.PenResponse{
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
}
|
||||
if pen.PigBatchID != nil {
|
||||
resp.PigBatchID = *pen.PigBatchID
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
PigBatchID: pen.PigBatchID,
|
||||
}
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", resp, action, "更新成功", resp)
|
||||
}
|
||||
@@ -454,7 +427,7 @@ func (c *PigFarmController) UpdatePenStatus(ctx *gin.Context) {
|
||||
HouseID: pen.HouseID,
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
PigBatchID: *pen.PigBatchID,
|
||||
PigBatchID: pen.PigBatchID,
|
||||
}
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", resp, action, "更新成功", resp)
|
||||
}
|
||||
|
||||
@@ -11,12 +11,13 @@ type PigHouseResponse struct {
|
||||
|
||||
// PenResponse 定义了猪栏信息的响应结构
|
||||
type PenResponse struct {
|
||||
ID uint `json:"id"`
|
||||
PenNumber string `json:"pen_number"`
|
||||
HouseID uint `json:"house_id"`
|
||||
Capacity int `json:"capacity"`
|
||||
Status models.PenStatus `json:"status"`
|
||||
PigBatchID uint `json:"pig_batch_id"`
|
||||
ID uint `json:"id"`
|
||||
PenNumber string `json:"pen_number"`
|
||||
HouseID uint `json:"house_id"`
|
||||
Capacity int `json:"capacity"`
|
||||
Status models.PenStatus `json:"status"`
|
||||
PigBatchID *uint `json:"pig_batch_id,omitempty"`
|
||||
CurrentPigCount int `json:"current_pig_count"`
|
||||
}
|
||||
|
||||
// CreatePigHouseRequest 定义了创建猪舍的请求结构
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"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"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
@@ -22,8 +24,8 @@ type PigFarmService interface {
|
||||
|
||||
// Pen methods
|
||||
CreatePen(penNumber string, houseID uint, capacity int) (*models.Pen, error)
|
||||
GetPenByID(id uint) (*models.Pen, error)
|
||||
ListPens() ([]models.Pen, error)
|
||||
GetPenByID(id uint) (*dto.PenResponse, error)
|
||||
ListPens() ([]*dto.PenResponse, error)
|
||||
UpdatePen(id uint, penNumber string, houseID uint, capacity int, status models.PenStatus) (*models.Pen, error)
|
||||
DeletePen(id uint) error
|
||||
// UpdatePenStatus 更新猪栏状态
|
||||
@@ -35,13 +37,15 @@ type pigFarmService struct {
|
||||
farmRepository repository.PigFarmRepository
|
||||
penRepository repository.PigPenRepository
|
||||
batchRepository repository.PigBatchRepository
|
||||
uow repository.UnitOfWork // 工作单元,用于事务管理
|
||||
pigBatchService domain_pig.PigBatchService // Add domain PigBatchService dependency
|
||||
uow repository.UnitOfWork // 工作单元,用于事务管理
|
||||
}
|
||||
|
||||
// NewPigFarmService 创建一个新的 PigFarmService 实例
|
||||
func NewPigFarmService(farmRepository repository.PigFarmRepository,
|
||||
penRepository repository.PigPenRepository,
|
||||
batchRepository repository.PigBatchRepository,
|
||||
pigBatchService domain_pig.PigBatchService,
|
||||
uow repository.UnitOfWork,
|
||||
logger *logs.Logger) PigFarmService {
|
||||
return &pigFarmService{
|
||||
@@ -49,6 +53,7 @@ func NewPigFarmService(farmRepository repository.PigFarmRepository,
|
||||
farmRepository: farmRepository,
|
||||
penRepository: penRepository,
|
||||
batchRepository: batchRepository,
|
||||
pigBatchService: pigBatchService,
|
||||
uow: uow,
|
||||
}
|
||||
}
|
||||
@@ -132,12 +137,64 @@ func (s *pigFarmService) CreatePen(penNumber string, houseID uint, capacity int)
|
||||
return pen, err
|
||||
}
|
||||
|
||||
func (s *pigFarmService) GetPenByID(id uint) (*models.Pen, error) {
|
||||
return s.penRepository.GetPenByID(id)
|
||||
func (s *pigFarmService) GetPenByID(id uint) (*dto.PenResponse, error) {
|
||||
pen, err := s.penRepository.GetPenByID(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
currentPigCount, err := s.pigBatchService.GetCurrentPigsInPen(id)
|
||||
if err != nil {
|
||||
s.logger.Errorf("获取猪栏 %d 存栏量失败: %v", id, err)
|
||||
currentPigCount = 0 // 如果获取计数时出错,则默认为0
|
||||
}
|
||||
|
||||
response := &dto.PenResponse{
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
CurrentPigCount: currentPigCount,
|
||||
}
|
||||
|
||||
if pen.PigBatchID != nil {
|
||||
response.PigBatchID = pen.PigBatchID
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *pigFarmService) ListPens() ([]models.Pen, error) {
|
||||
return s.penRepository.ListPens()
|
||||
func (s *pigFarmService) ListPens() ([]*dto.PenResponse, error) {
|
||||
pens, err := s.penRepository.ListPens()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var response []*dto.PenResponse
|
||||
for _, pen := range pens {
|
||||
currentPigCount, err := s.pigBatchService.GetCurrentPigsInPen(pen.ID)
|
||||
if err != nil {
|
||||
s.logger.Errorf("获取猪栏 %d 存栏量失败: %v", pen.ID, err)
|
||||
currentPigCount = 0 // 如果获取计数时出错,则默认为0
|
||||
}
|
||||
|
||||
penResponse := &dto.PenResponse{
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
CurrentPigCount: currentPigCount,
|
||||
}
|
||||
|
||||
if pen.PigBatchID != nil {
|
||||
penResponse.PigBatchID = pen.PigBatchID
|
||||
}
|
||||
response = append(response, penResponse)
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *pigFarmService) UpdatePen(id uint, penNumber string, houseID uint, capacity int, status models.PenStatus) (*models.Pen, error) {
|
||||
|
||||
@@ -97,7 +97,7 @@ func NewApplication(configPath string) (*Application, error) {
|
||||
pigPenTransferManager, pigTradeManager, pigSickManager)
|
||||
|
||||
// --- 业务逻辑处理器初始化 ---
|
||||
pigFarmService := service.NewPigFarmService(pigFarmRepo, pigPenRepo, pigBatchRepo, unitOfWork, logger)
|
||||
pigFarmService := service.NewPigFarmService(pigFarmRepo, pigPenRepo, pigBatchRepo, pigBatchDomain, unitOfWork, logger)
|
||||
pigBatchService := service.NewPigBatchService(pigBatchDomain, logger)
|
||||
monitorService := service.NewMonitorService(
|
||||
sensorDataRepo,
|
||||
|
||||
@@ -58,6 +58,8 @@ type PigBatchService interface {
|
||||
|
||||
// GetCurrentPigQuantity 获取指定猪批次的当前猪只数量。
|
||||
GetCurrentPigQuantity(batchID uint) (int, error)
|
||||
// GetCurrentPigsInPen 获取指定猪栏的当前存栏量。
|
||||
GetCurrentPigsInPen(penID uint) (int, error)
|
||||
|
||||
UpdatePigBatchQuantity(operatorID uint, batchID uint, changeType models.LogChangeType, changeAmount int, changeReason string, happenedAt time.Time) error
|
||||
|
||||
@@ -163,3 +165,16 @@ func (s *pigBatchService) RemoveEmptyPenFromBatch(batchID uint, penID uint) erro
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *pigBatchService) GetCurrentPigsInPen(penID uint) (int, error) {
|
||||
var currentPigs int
|
||||
err := s.uow.ExecuteInTransaction(func(tx *gorm.DB) error {
|
||||
pigs, err := s.transferSvc.GetCurrentPigsInPen(tx, penID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
currentPigs = pigs
|
||||
return nil
|
||||
})
|
||||
return currentPigs, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user