单独修改猪圈状态

This commit is contained in:
2025-10-04 01:28:05 +08:00
parent 5403be5e7d
commit 8d9e4286b0
5 changed files with 147 additions and 17 deletions

View File

@@ -9,7 +9,6 @@ import (
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// --- 控制器定义 ---
@@ -80,7 +79,7 @@ func (c *PigFarmController) GetPigHouse(ctx *gin.Context) {
house, err := c.service.GetPigHouseByID(uint(id))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if errors.Is(err, service.ErrHouseNotFound) {
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
return
}
@@ -151,7 +150,7 @@ func (c *PigFarmController) UpdatePigHouse(ctx *gin.Context) {
house, err := c.service.UpdatePigHouse(uint(id), req.Name, req.Description)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if errors.Is(err, service.ErrHouseNotFound) {
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
return
}
@@ -185,10 +184,15 @@ func (c *PigFarmController) DeletePigHouse(ctx *gin.Context) {
}
if err := c.service.DeletePigHouse(uint(id)); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if errors.Is(err, service.ErrHouseNotFound) {
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
return
}
// 检查是否是业务逻辑错误
if errors.Is(err, service.ErrHouseContainsPens) {
controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
return
}
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败", action, "业务逻辑失败", id)
return
@@ -218,6 +222,11 @@ func (c *PigFarmController) CreatePen(ctx *gin.Context) {
pen, err := c.service.CreatePen(req.PenNumber, req.HouseID, req.Capacity)
if err != nil {
// 检查是否是业务逻辑错误
if errors.Is(err, service.ErrHouseNotFound) {
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), req)
return
}
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪栏失败", action, "业务逻辑失败", req)
return
@@ -252,7 +261,7 @@ func (c *PigFarmController) GetPen(ctx *gin.Context) {
pen, err := c.service.GetPenByID(uint(id))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if errors.Is(err, service.ErrPenNotFound) {
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
return
}
@@ -329,10 +338,11 @@ func (c *PigFarmController) UpdatePen(ctx *gin.Context) {
pen, err := c.service.UpdatePen(uint(id), req.PenNumber, req.HouseID, req.Capacity, req.Status)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if errors.Is(err, service.ErrPenNotFound) {
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
return
}
// 其他业务逻辑错误可以在这里添加处理
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
return
@@ -366,10 +376,15 @@ func (c *PigFarmController) DeletePen(ctx *gin.Context) {
}
if err := c.service.DeletePen(uint(id)); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if errors.Is(err, service.ErrPenNotFound) {
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
return
}
// 检查是否是业务逻辑错误
if errors.Is(err, service.ErrPenInUse) {
controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
return
}
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败", action, "业务逻辑失败", id)
return
@@ -377,3 +392,52 @@ func (c *PigFarmController) DeletePen(ctx *gin.Context) {
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "删除成功", nil, action, "删除成功", id)
}
// UpdatePenStatus godoc
// @Summary 更新猪栏状态
// @Description 更新指定猪栏的当前状态
// @Tags 猪场管理
// @Accept json
// @Produce json
// @Param id path int true "猪栏ID"
// @Param body body dto.UpdatePenStatusRequest true "新的猪栏状态"
// @Success 200 {object} controller.Response{data=dto.PenResponse} "更新成功"
// @Router /api/v1/pens/{id}/status [put]
func (c *PigFarmController) UpdatePenStatus(ctx *gin.Context) {
const action = "更新猪栏状态"
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
if err != nil {
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
return
}
var req dto.UpdatePenStatusRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
return
}
pen, err := c.service.UpdatePenStatus(uint(id), req.Status)
if err != nil {
if errors.Is(err, service.ErrPenNotFound) {
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), id)
return
} else if errors.Is(err, service.ErrPenStatusInvalidForOccupiedPen) || errors.Is(err, service.ErrPenStatusInvalidForUnoccupiedPen) {
controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
return
}
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪栏状态失败", action, err.Error(), id)
return
}
resp := dto.PenResponse{
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)
}