任务2.3
This commit is contained in:
@@ -53,12 +53,7 @@ func (c *PigFarmController) CreatePigHouse(ctx echo.Context) error {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪舍失败", action, "业务逻辑失败", req)
|
||||
}
|
||||
|
||||
resp := dto.PigHouseResponse{
|
||||
ID: house.ID,
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
}
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "创建成功", resp, action, "创建成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "创建成功", house, action, "创建成功", house)
|
||||
}
|
||||
|
||||
// GetPigHouse godoc
|
||||
@@ -86,12 +81,7 @@ func (c *PigFarmController) GetPigHouse(ctx echo.Context) error {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪舍失败", action, "业务逻辑失败", id)
|
||||
}
|
||||
|
||||
resp := dto.PigHouseResponse{
|
||||
ID: house.ID,
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
}
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", resp, action, "获取成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", house, action, "获取成功", house)
|
||||
}
|
||||
|
||||
// ListPigHouses godoc
|
||||
@@ -110,16 +100,7 @@ func (c *PigFarmController) ListPigHouses(ctx echo.Context) error {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取列表失败", action, "业务逻辑失败", nil)
|
||||
}
|
||||
|
||||
var resp []dto.PigHouseResponse
|
||||
for _, house := range houses {
|
||||
resp = append(resp, dto.PigHouseResponse{
|
||||
ID: house.ID,
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
})
|
||||
}
|
||||
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", resp, action, "获取成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", houses, action, "获取成功", houses)
|
||||
}
|
||||
|
||||
// UpdatePigHouse godoc
|
||||
@@ -154,12 +135,7 @@ func (c *PigFarmController) UpdatePigHouse(ctx echo.Context) error {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
|
||||
}
|
||||
|
||||
resp := dto.PigHouseResponse{
|
||||
ID: house.ID,
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
}
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", resp, action, "更新成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", house, action, "更新成功", house)
|
||||
}
|
||||
|
||||
// DeletePigHouse godoc
|
||||
@@ -222,14 +198,7 @@ func (c *PigFarmController) CreatePen(ctx echo.Context) error {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪栏失败", action, "业务逻辑失败", req)
|
||||
}
|
||||
|
||||
resp := dto.PenResponse{
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
}
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "创建成功", resp, action, "创建成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "创建成功", pen, action, "创建成功", pen)
|
||||
}
|
||||
|
||||
// GetPen godoc
|
||||
@@ -312,15 +281,7 @@ func (c *PigFarmController) UpdatePen(ctx echo.Context) error {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
|
||||
}
|
||||
|
||||
resp := dto.PenResponse{
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
PigBatchID: pen.PigBatchID,
|
||||
}
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", resp, action, "更新成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", pen, action, "更新成功", pen)
|
||||
}
|
||||
|
||||
// DeletePen godoc
|
||||
@@ -388,13 +349,5 @@ func (c *PigFarmController) UpdatePenStatus(ctx echo.Context) error {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪栏状态失败", action, err.Error(), id)
|
||||
}
|
||||
|
||||
resp := dto.PenResponse{
|
||||
ID: pen.ID,
|
||||
PenNumber: pen.PenNumber,
|
||||
HouseID: pen.HouseID,
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
PigBatchID: pen.PigBatchID,
|
||||
}
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", resp, action, "更新成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", pen, action, "更新成功", pen)
|
||||
}
|
||||
|
||||
@@ -16,20 +16,20 @@ import (
|
||||
// PigFarmService 提供了猪场资产管理的业务逻辑
|
||||
type PigFarmService interface {
|
||||
// PigHouse methods
|
||||
CreatePigHouse(name, description string) (*models.PigHouse, error)
|
||||
GetPigHouseByID(id uint) (*models.PigHouse, error)
|
||||
ListPigHouses() ([]models.PigHouse, error)
|
||||
UpdatePigHouse(id uint, name, description string) (*models.PigHouse, error)
|
||||
CreatePigHouse(name, description string) (*dto.PigHouseResponse, error)
|
||||
GetPigHouseByID(id uint) (*dto.PigHouseResponse, error)
|
||||
ListPigHouses() ([]dto.PigHouseResponse, error)
|
||||
UpdatePigHouse(id uint, name, description string) (*dto.PigHouseResponse, error)
|
||||
DeletePigHouse(id uint) error
|
||||
|
||||
// Pen methods
|
||||
CreatePen(penNumber string, houseID uint, capacity int) (*models.Pen, error)
|
||||
CreatePen(penNumber string, houseID uint, capacity int) (*dto.PenResponse, 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)
|
||||
UpdatePen(id uint, penNumber string, houseID uint, capacity int, status models.PenStatus) (*dto.PenResponse, error)
|
||||
DeletePen(id uint) error
|
||||
// UpdatePenStatus 更新猪栏状态
|
||||
UpdatePenStatus(id uint, newStatus models.PenStatus) (*models.Pen, error)
|
||||
UpdatePenStatus(id uint, newStatus models.PenStatus) (*dto.PenResponse, error)
|
||||
}
|
||||
|
||||
type pigFarmService struct {
|
||||
@@ -60,24 +60,51 @@ func NewPigFarmService(farmRepository repository.PigFarmRepository,
|
||||
|
||||
// --- PigHouse Implementation ---
|
||||
|
||||
func (s *pigFarmService) CreatePigHouse(name, description string) (*models.PigHouse, error) {
|
||||
func (s *pigFarmService) CreatePigHouse(name, description string) (*dto.PigHouseResponse, error) {
|
||||
house := &models.PigHouse{
|
||||
Name: name,
|
||||
Description: description,
|
||||
}
|
||||
err := s.farmRepository.CreatePigHouse(house)
|
||||
return house, err
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.PigHouseResponse{
|
||||
ID: house.ID,
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *pigFarmService) GetPigHouseByID(id uint) (*models.PigHouse, error) {
|
||||
return s.farmRepository.GetPigHouseByID(id)
|
||||
func (s *pigFarmService) GetPigHouseByID(id uint) (*dto.PigHouseResponse, error) {
|
||||
house, err := s.farmRepository.GetPigHouseByID(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.PigHouseResponse{
|
||||
ID: house.ID,
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *pigFarmService) ListPigHouses() ([]models.PigHouse, error) {
|
||||
return s.farmRepository.ListPigHouses()
|
||||
func (s *pigFarmService) ListPigHouses() ([]dto.PigHouseResponse, error) {
|
||||
houses, err := s.farmRepository.ListPigHouses()
|
||||
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
|
||||
}
|
||||
|
||||
func (s *pigFarmService) UpdatePigHouse(id uint, name, description string) (*models.PigHouse, error) {
|
||||
func (s *pigFarmService) UpdatePigHouse(id uint, name, description string) (*dto.PigHouseResponse, error) {
|
||||
house := &models.PigHouse{
|
||||
Model: gorm.Model{ID: id},
|
||||
Name: name,
|
||||
@@ -91,7 +118,15 @@ func (s *pigFarmService) UpdatePigHouse(id uint, name, description string) (*mod
|
||||
return nil, ErrHouseNotFound
|
||||
}
|
||||
// 返回更新后的完整信息
|
||||
return s.farmRepository.GetPigHouseByID(id)
|
||||
updatedHouse, err := s.farmRepository.GetPigHouseByID(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.PigHouseResponse{
|
||||
ID: updatedHouse.ID,
|
||||
Name: updatedHouse.Name,
|
||||
Description: updatedHouse.Description,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *pigFarmService) DeletePigHouse(id uint) error {
|
||||
@@ -117,7 +152,7 @@ func (s *pigFarmService) DeletePigHouse(id uint) error {
|
||||
|
||||
// --- Pen Implementation ---
|
||||
|
||||
func (s *pigFarmService) CreatePen(penNumber string, houseID uint, capacity int) (*models.Pen, error) {
|
||||
func (s *pigFarmService) CreatePen(penNumber string, houseID uint, capacity int) (*dto.PenResponse, error) {
|
||||
// 业务逻辑:验证所属猪舍是否存在
|
||||
_, err := s.farmRepository.GetPigHouseByID(houseID)
|
||||
if err != nil {
|
||||
@@ -134,7 +169,16 @@ func (s *pigFarmService) CreatePen(penNumber string, houseID uint, capacity int)
|
||||
Status: models.PenStatusEmpty,
|
||||
}
|
||||
err = s.penRepository.CreatePen(pen)
|
||||
return pen, err
|
||||
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
|
||||
}
|
||||
|
||||
func (s *pigFarmService) GetPenByID(id uint) (*dto.PenResponse, error) {
|
||||
@@ -197,7 +241,7 @@ func (s *pigFarmService) ListPens() ([]*dto.PenResponse, error) {
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (s *pigFarmService) UpdatePen(id uint, penNumber string, houseID uint, capacity int, status models.PenStatus) (*models.Pen, error) {
|
||||
func (s *pigFarmService) UpdatePen(id uint, penNumber string, houseID uint, capacity int, status models.PenStatus) (*dto.PenResponse, error) {
|
||||
// 业务逻辑:验证所属猪舍是否存在
|
||||
_, err := s.farmRepository.GetPigHouseByID(houseID)
|
||||
if err != nil {
|
||||
@@ -222,7 +266,18 @@ func (s *pigFarmService) UpdatePen(id uint, penNumber string, houseID uint, capa
|
||||
return nil, ErrPenNotFound
|
||||
}
|
||||
// 返回更新后的完整信息
|
||||
return s.penRepository.GetPenByID(id)
|
||||
updatedPen, err := s.penRepository.GetPenByID(id)
|
||||
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
|
||||
}
|
||||
|
||||
func (s *pigFarmService) DeletePen(id uint) error {
|
||||
@@ -260,7 +315,7 @@ func (s *pigFarmService) DeletePen(id uint) error {
|
||||
}
|
||||
|
||||
// UpdatePenStatus 更新猪栏状态
|
||||
func (s *pigFarmService) UpdatePenStatus(id uint, newStatus models.PenStatus) (*models.Pen, error) {
|
||||
func (s *pigFarmService) UpdatePenStatus(id uint, newStatus models.PenStatus) (*dto.PenResponse, error) {
|
||||
var updatedPen *models.Pen
|
||||
err := s.uow.ExecuteInTransaction(func(tx *gorm.DB) error {
|
||||
pen, err := s.penRepository.GetPenByIDTx(tx, id)
|
||||
@@ -310,5 +365,12 @@ func (s *pigFarmService) UpdatePenStatus(id uint, newStatus models.PenStatus) (*
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return updatedPen, nil
|
||||
return &dto.PenResponse{
|
||||
ID: updatedPen.ID,
|
||||
PenNumber: updatedPen.PenNumber,
|
||||
HouseID: updatedPen.HouseID,
|
||||
Capacity: updatedPen.Capacity,
|
||||
Status: updatedPen.Status,
|
||||
PigBatchID: updatedPen.PigBatchID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user