2025-10-03 18:27:53 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-05 19:57:30 +08:00
|
|
|
|
"context"
|
2025-10-03 22:33:43 +08:00
|
|
|
|
"errors"
|
2025-10-04 01:28:05 +08:00
|
|
|
|
"fmt"
|
2025-10-03 22:33:43 +08:00
|
|
|
|
|
2025-10-23 10:52:40 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
|
|
|
|
|
domain_pig "git.huangwc.com/pig/pig-farm-controller/internal/domain/pig"
|
2025-10-03 18:27:53 +08:00
|
|
|
|
"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"
|
2025-10-03 22:33:43 +08:00
|
|
|
|
|
2025-10-03 18:27:53 +08:00
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// PigFarmService 提供了猪场资产管理的业务逻辑
|
|
|
|
|
|
type PigFarmService interface {
|
|
|
|
|
|
// PigHouse methods
|
2025-11-05 19:57:30 +08:00
|
|
|
|
CreatePigHouse(ctx context.Context, name, description string) (*dto.PigHouseResponse, error)
|
|
|
|
|
|
GetPigHouseByID(ctx context.Context, id uint) (*dto.PigHouseResponse, error)
|
|
|
|
|
|
ListPigHouses(ctx context.Context) ([]dto.PigHouseResponse, error)
|
|
|
|
|
|
UpdatePigHouse(ctx context.Context, id uint, name, description string) (*dto.PigHouseResponse, error)
|
|
|
|
|
|
DeletePigHouse(ctx context.Context, id uint) error
|
2025-10-03 18:27:53 +08:00
|
|
|
|
|
|
|
|
|
|
// Pen methods
|
2025-11-05 19:57:30 +08:00
|
|
|
|
CreatePen(ctx context.Context, penNumber string, houseID uint, capacity int) (*dto.PenResponse, error)
|
|
|
|
|
|
GetPenByID(ctx context.Context, id uint) (*dto.PenResponse, error)
|
|
|
|
|
|
ListPens(ctx context.Context) ([]*dto.PenResponse, error)
|
|
|
|
|
|
UpdatePen(ctx context.Context, id uint, penNumber string, houseID uint, capacity int, status models.PenStatus) (*dto.PenResponse, error)
|
|
|
|
|
|
DeletePen(ctx context.Context, id uint) error
|
2025-10-04 01:28:05 +08:00
|
|
|
|
// UpdatePenStatus 更新猪栏状态
|
2025-11-05 19:57:30 +08:00
|
|
|
|
UpdatePenStatus(ctx context.Context, id uint, newStatus models.PenStatus) (*dto.PenResponse, error)
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type pigFarmService struct {
|
2025-11-05 19:57:30 +08:00
|
|
|
|
ctx context.Context
|
2025-10-05 17:46:03 +08:00
|
|
|
|
farmRepository repository.PigFarmRepository
|
|
|
|
|
|
penRepository repository.PigPenRepository
|
|
|
|
|
|
batchRepository repository.PigBatchRepository
|
2025-11-05 19:57:30 +08:00
|
|
|
|
pigBatchService domain_pig.PigBatchService
|
|
|
|
|
|
uow repository.UnitOfWork // 工作单元,用于事务管理
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewPigFarmService 创建一个新的 PigFarmService 实例
|
2025-11-05 19:57:30 +08:00
|
|
|
|
func NewPigFarmService(ctx context.Context,
|
|
|
|
|
|
farmRepository repository.PigFarmRepository,
|
2025-10-05 17:46:03 +08:00
|
|
|
|
penRepository repository.PigPenRepository,
|
|
|
|
|
|
batchRepository repository.PigBatchRepository,
|
2025-10-23 10:52:40 +08:00
|
|
|
|
pigBatchService domain_pig.PigBatchService,
|
2025-10-05 17:46:03 +08:00
|
|
|
|
uow repository.UnitOfWork,
|
2025-11-05 19:57:30 +08:00
|
|
|
|
) PigFarmService {
|
2025-10-03 18:27:53 +08:00
|
|
|
|
return &pigFarmService{
|
2025-11-05 19:57:30 +08:00
|
|
|
|
ctx: ctx,
|
2025-10-05 17:46:03 +08:00
|
|
|
|
farmRepository: farmRepository,
|
|
|
|
|
|
penRepository: penRepository,
|
|
|
|
|
|
batchRepository: batchRepository,
|
2025-10-23 10:52:40 +08:00
|
|
|
|
pigBatchService: pigBatchService,
|
2025-10-05 17:46:03 +08:00
|
|
|
|
uow: uow,
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --- PigHouse Implementation ---
|
|
|
|
|
|
|
2025-11-05 19:57:30 +08:00
|
|
|
|
func (s *pigFarmService) CreatePigHouse(ctx context.Context, name, description string) (*dto.PigHouseResponse, error) {
|
|
|
|
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CreatePigHouse")
|
2025-10-03 18:27:53 +08:00
|
|
|
|
house := &models.PigHouse{
|
|
|
|
|
|
Name: name,
|
|
|
|
|
|
Description: description,
|
|
|
|
|
|
}
|
2025-11-05 19:57:30 +08:00
|
|
|
|
err := s.farmRepository.CreatePigHouse(serviceCtx, house)
|
2025-10-31 16:11:12 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &dto.PigHouseResponse{
|
|
|
|
|
|
ID: house.ID,
|
|
|
|
|
|
Name: house.Name,
|
|
|
|
|
|
Description: house.Description,
|
|
|
|
|
|
}, nil
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 19:57:30 +08:00
|
|
|
|
func (s *pigFarmService) GetPigHouseByID(ctx context.Context, id uint) (*dto.PigHouseResponse, error) {
|
|
|
|
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "GetPigHouseByID")
|
|
|
|
|
|
house, err := s.farmRepository.GetPigHouseByID(serviceCtx, id)
|
2025-10-31 16:11:12 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &dto.PigHouseResponse{
|
|
|
|
|
|
ID: house.ID,
|
|
|
|
|
|
Name: house.Name,
|
|
|
|
|
|
Description: house.Description,
|
|
|
|
|
|
}, nil
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 19:57:30 +08:00
|
|
|
|
func (s *pigFarmService) ListPigHouses(ctx context.Context) ([]dto.PigHouseResponse, error) {
|
|
|
|
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "ListPigHouses")
|
|
|
|
|
|
houses, err := s.farmRepository.ListPigHouses(serviceCtx)
|
2025-10-31 16:11:12 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
var resp []dto.PigHouseResponse
|
|
|
|
|
|
for _, house := range houses {
|
|
|
|
|
|
resp = append(resp, dto.PigHouseResponse{
|
|
|
|
|
|
ID: house.ID,
|
|
|
|
|
|
Name: house.Name,
|
|
|
|
|
|
Description: house.Description,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp, nil
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 19:57:30 +08:00
|
|
|
|
func (s *pigFarmService) UpdatePigHouse(ctx context.Context, id uint, name, description string) (*dto.PigHouseResponse, error) {
|
|
|
|
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "UpdatePigHouse")
|
2025-10-03 18:27:53 +08:00
|
|
|
|
house := &models.PigHouse{
|
|
|
|
|
|
Model: gorm.Model{ID: id},
|
|
|
|
|
|
Name: name,
|
|
|
|
|
|
Description: description,
|
|
|
|
|
|
}
|
2025-11-05 19:57:30 +08:00
|
|
|
|
rowsAffected, err := s.farmRepository.UpdatePigHouse(serviceCtx, house)
|
2025-10-03 18:27:53 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2025-10-04 00:58:29 +08:00
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
|
|
return nil, ErrHouseNotFound
|
|
|
|
|
|
}
|
2025-10-03 18:27:53 +08:00
|
|
|
|
// 返回更新后的完整信息
|
2025-11-05 19:57:30 +08:00
|
|
|
|
updatedHouse, err := s.farmRepository.GetPigHouseByID(serviceCtx, id)
|
2025-10-31 16:11:12 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &dto.PigHouseResponse{
|
|
|
|
|
|
ID: updatedHouse.ID,
|
|
|
|
|
|
Name: updatedHouse.Name,
|
|
|
|
|
|
Description: updatedHouse.Description,
|
|
|
|
|
|
}, nil
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 19:57:30 +08:00
|
|
|
|
func (s *pigFarmService) DeletePigHouse(ctx context.Context, id uint) error {
|
|
|
|
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "DeletePigHouse")
|
2025-10-03 22:33:43 +08:00
|
|
|
|
// 业务逻辑:检查猪舍是否包含猪栏
|
2025-11-05 19:57:30 +08:00
|
|
|
|
penCount, err := s.farmRepository.CountPensInHouse(serviceCtx, id)
|
2025-10-03 22:33:43 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if penCount > 0 {
|
|
|
|
|
|
return ErrHouseContainsPens
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 调用仓库层进行删除
|
2025-11-05 19:57:30 +08:00
|
|
|
|
rowsAffected, err := s.farmRepository.DeletePigHouse(serviceCtx, id)
|
2025-10-04 00:58:29 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
|
|
return ErrHouseNotFound
|
2025-10-03 22:33:43 +08:00
|
|
|
|
}
|
2025-10-04 00:58:29 +08:00
|
|
|
|
return nil
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --- Pen Implementation ---
|
|
|
|
|
|
|
2025-11-05 19:57:30 +08:00
|
|
|
|
func (s *pigFarmService) CreatePen(ctx context.Context, penNumber string, houseID uint, capacity int) (*dto.PenResponse, error) {
|
|
|
|
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CreatePen")
|
2025-10-03 22:33:43 +08:00
|
|
|
|
// 业务逻辑:验证所属猪舍是否存在
|
2025-11-05 19:57:30 +08:00
|
|
|
|
_, err := s.farmRepository.GetPigHouseByID(serviceCtx, houseID)
|
2025-10-03 22:33:43 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
return nil, ErrHouseNotFound
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 18:27:53 +08:00
|
|
|
|
pen := &models.Pen{
|
|
|
|
|
|
PenNumber: penNumber,
|
|
|
|
|
|
HouseID: houseID,
|
|
|
|
|
|
Capacity: capacity,
|
2025-10-03 23:58:37 +08:00
|
|
|
|
Status: models.PenStatusEmpty,
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
2025-11-05 19:57:30 +08:00
|
|
|
|
err = s.penRepository.CreatePen(serviceCtx, pen)
|
2025-10-31 16:11:12 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &dto.PenResponse{
|
|
|
|
|
|
ID: pen.ID,
|
|
|
|
|
|
PenNumber: pen.PenNumber,
|
|
|
|
|
|
HouseID: pen.HouseID,
|
|
|
|
|
|
Capacity: pen.Capacity,
|
|
|
|
|
|
Status: pen.Status,
|
|
|
|
|
|
}, nil
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 19:57:30 +08:00
|
|
|
|
func (s *pigFarmService) GetPenByID(ctx context.Context, id uint) (*dto.PenResponse, error) {
|
|
|
|
|
|
serviceCtx, logger := logs.Trace(ctx, s.ctx, "GetPenByID")
|
|
|
|
|
|
pen, err := s.penRepository.GetPenByID(serviceCtx, id)
|
2025-10-23 10:52:40 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 19:57:30 +08:00
|
|
|
|
currentPigCount, err := s.pigBatchService.GetCurrentPigsInPen(serviceCtx, id)
|
2025-10-23 10:52:40 +08:00
|
|
|
|
if err != nil {
|
2025-11-05 19:57:30 +08:00
|
|
|
|
logger.Errorf("获取猪栏 %d 存栏量失败: %v", id, err)
|
2025-10-23 10:52:40 +08:00
|
|
|
|
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
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 19:57:30 +08:00
|
|
|
|
func (s *pigFarmService) ListPens(ctx context.Context) ([]*dto.PenResponse, error) {
|
|
|
|
|
|
serviceCtx, logger := logs.Trace(ctx, s.ctx, "ListPens")
|
|
|
|
|
|
pens, err := s.penRepository.ListPens(serviceCtx)
|
2025-10-23 10:52:40 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var response []*dto.PenResponse
|
|
|
|
|
|
for _, pen := range pens {
|
2025-11-05 19:57:30 +08:00
|
|
|
|
currentPigCount, err := s.pigBatchService.GetCurrentPigsInPen(serviceCtx, pen.ID)
|
2025-10-23 10:52:40 +08:00
|
|
|
|
if err != nil {
|
2025-11-05 19:57:30 +08:00
|
|
|
|
logger.Errorf("获取猪栏 %d 存栏量失败: %v", pen.ID, err)
|
2025-10-23 10:52:40 +08:00
|
|
|
|
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
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 19:57:30 +08:00
|
|
|
|
func (s *pigFarmService) UpdatePen(ctx context.Context, id uint, penNumber string, houseID uint, capacity int, status models.PenStatus) (*dto.PenResponse, error) {
|
|
|
|
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "UpdatePen")
|
2025-10-03 22:33:43 +08:00
|
|
|
|
// 业务逻辑:验证所属猪舍是否存在
|
2025-11-05 19:57:30 +08:00
|
|
|
|
_, err := s.farmRepository.GetPigHouseByID(serviceCtx, houseID)
|
2025-10-03 22:33:43 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
return nil, ErrHouseNotFound
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 18:27:53 +08:00
|
|
|
|
pen := &models.Pen{
|
|
|
|
|
|
Model: gorm.Model{ID: id},
|
|
|
|
|
|
PenNumber: penNumber,
|
|
|
|
|
|
HouseID: houseID,
|
|
|
|
|
|
Capacity: capacity,
|
|
|
|
|
|
Status: status,
|
|
|
|
|
|
}
|
2025-11-05 19:57:30 +08:00
|
|
|
|
rowsAffected, err := s.penRepository.UpdatePen(serviceCtx, pen)
|
2025-10-03 18:27:53 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2025-10-04 00:58:29 +08:00
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
|
|
return nil, ErrPenNotFound
|
|
|
|
|
|
}
|
2025-10-03 18:27:53 +08:00
|
|
|
|
// 返回更新后的完整信息
|
2025-11-05 19:57:30 +08:00
|
|
|
|
updatedPen, err := s.penRepository.GetPenByID(serviceCtx, id)
|
2025-10-31 16:11:12 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &dto.PenResponse{
|
|
|
|
|
|
ID: updatedPen.ID,
|
|
|
|
|
|
PenNumber: updatedPen.PenNumber,
|
|
|
|
|
|
HouseID: updatedPen.HouseID,
|
|
|
|
|
|
Capacity: updatedPen.Capacity,
|
|
|
|
|
|
Status: updatedPen.Status,
|
|
|
|
|
|
PigBatchID: updatedPen.PigBatchID,
|
|
|
|
|
|
}, nil
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 19:57:30 +08:00
|
|
|
|
func (s *pigFarmService) DeletePen(ctx context.Context, id uint) error {
|
|
|
|
|
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "DeletePen")
|
2025-10-03 22:33:43 +08:00
|
|
|
|
// 业务逻辑:检查猪栏是否被活跃批次使用
|
2025-11-05 19:57:30 +08:00
|
|
|
|
pen, err := s.penRepository.GetPenByID(serviceCtx, id)
|
2025-10-03 22:33:43 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2025-10-04 00:58:29 +08:00
|
|
|
|
return ErrPenNotFound // 猪栏不存在
|
2025-10-03 22:33:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查猪栏是否关联了活跃批次
|
2025-10-04 00:58:29 +08:00
|
|
|
|
// 注意:pen.PigBatchID 是指针类型,需要检查是否为 nil
|
|
|
|
|
|
if pen.PigBatchID != nil && *pen.PigBatchID != 0 {
|
2025-11-05 19:57:30 +08:00
|
|
|
|
pigBatch, err := s.batchRepository.GetPigBatchByID(serviceCtx, *pen.PigBatchID)
|
2025-10-03 22:33:43 +08:00
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
// 如果批次活跃,则不能删除猪栏
|
2025-10-04 00:58:29 +08:00
|
|
|
|
if pigBatch != nil && pigBatch.IsActive() {
|
2025-10-03 22:33:43 +08:00
|
|
|
|
return ErrPenInUse
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 调用仓库层进行删除
|
2025-11-05 19:57:30 +08:00
|
|
|
|
rowsAffected, err := s.penRepository.DeletePen(serviceCtx, id)
|
2025-10-04 00:58:29 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
|
|
return ErrPenNotFound
|
2025-10-03 22:33:43 +08:00
|
|
|
|
}
|
2025-10-04 00:58:29 +08:00
|
|
|
|
return nil
|
2025-10-03 18:27:53 +08:00
|
|
|
|
}
|
2025-10-04 01:28:05 +08:00
|
|
|
|
|
|
|
|
|
|
// UpdatePenStatus 更新猪栏状态
|
2025-11-05 19:57:30 +08:00
|
|
|
|
func (s *pigFarmService) UpdatePenStatus(ctx context.Context, id uint, newStatus models.PenStatus) (*dto.PenResponse, error) {
|
|
|
|
|
|
serviceCtx, logger := logs.Trace(ctx, s.ctx, "UpdatePenStatus")
|
2025-10-04 01:28:05 +08:00
|
|
|
|
var updatedPen *models.Pen
|
|
|
|
|
|
err := s.uow.ExecuteInTransaction(func(tx *gorm.DB) error {
|
2025-11-05 19:57:30 +08:00
|
|
|
|
pen, err := s.penRepository.GetPenByIDTx(serviceCtx, tx, id)
|
2025-10-04 01:28:05 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
return ErrPenNotFound
|
|
|
|
|
|
}
|
2025-11-05 19:57:30 +08:00
|
|
|
|
logger.Errorf("更新猪栏状态失败: 获取猪栏 %d 信息错误: %v", id, err)
|
2025-10-04 01:28:05 +08:00
|
|
|
|
return fmt.Errorf("获取猪栏 %d 信息失败: %w", id, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 业务逻辑:根据猪栏的 PigBatchID 和当前状态,判断是否允许设置为 newStatus
|
2025-10-04 01:31:35 +08:00
|
|
|
|
if pen.PigBatchID != nil && *pen.PigBatchID != 0 { // 猪栏已被批次使用
|
|
|
|
|
|
if newStatus == models.PenStatusEmpty { // 猪栏已被批次使用,不能直接设置为空闲
|
2025-10-04 01:28:05 +08:00
|
|
|
|
return ErrPenStatusInvalidForOccupiedPen
|
|
|
|
|
|
}
|
2025-10-04 01:31:35 +08:00
|
|
|
|
} else { // 猪栏未被批次使用 (PigBatchID == nil)
|
|
|
|
|
|
if newStatus == models.PenStatusOccupied { // 猪栏未被批次使用,不能设置为使用中
|
2025-10-04 01:28:05 +08:00
|
|
|
|
return ErrPenStatusInvalidForUnoccupiedPen
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果新状态与旧状态相同,则无需更新
|
|
|
|
|
|
if pen.Status == newStatus {
|
|
|
|
|
|
updatedPen = pen // 返回原始猪栏,因为没有实际更新
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updates := map[string]interface{}{
|
|
|
|
|
|
"status": newStatus,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 19:57:30 +08:00
|
|
|
|
if err := s.penRepository.UpdatePenFieldsTx(serviceCtx, tx, id, updates); err != nil {
|
|
|
|
|
|
logger.Errorf("更新猪栏 %d 状态失败: %v", id, err)
|
2025-10-04 01:28:05 +08:00
|
|
|
|
return fmt.Errorf("更新猪栏 %d 状态失败: %w", id, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取更新后的猪栏信息
|
2025-11-05 19:57:30 +08:00
|
|
|
|
updatedPen, err = s.penRepository.GetPenByIDTx(serviceCtx, tx, id)
|
2025-10-04 01:28:05 +08:00
|
|
|
|
if err != nil {
|
2025-11-05 19:57:30 +08:00
|
|
|
|
logger.Errorf("更新猪栏状态后获取猪栏 %d 信息失败: %v", id, err)
|
2025-10-04 01:28:05 +08:00
|
|
|
|
return fmt.Errorf("更新猪栏状态后获取猪栏 %d 信息失败: %w", id, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2025-10-31 16:11:12 +08:00
|
|
|
|
return &dto.PenResponse{
|
|
|
|
|
|
ID: updatedPen.ID,
|
|
|
|
|
|
PenNumber: updatedPen.PenNumber,
|
|
|
|
|
|
HouseID: updatedPen.HouseID,
|
|
|
|
|
|
Capacity: updatedPen.Capacity,
|
|
|
|
|
|
Status: updatedPen.Status,
|
|
|
|
|
|
PigBatchID: updatedPen.PigBatchID,
|
|
|
|
|
|
}, nil
|
2025-10-04 01:28:05 +08:00
|
|
|
|
}
|