实现修改批次绑定的猪栏
This commit is contained in:
@@ -188,3 +188,48 @@ func (c *PigBatchController) ListPigBatches(ctx *gin.Context) {
|
||||
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", respDTOs, action, "获取成功", respDTOs)
|
||||
}
|
||||
|
||||
// UpdatePigBatchPens godoc
|
||||
// @Summary 更新猪批次关联的猪栏
|
||||
// @Description 更新指定猪批次当前关联的猪栏列表
|
||||
// @Tags 猪批次管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Param body body dto.PigBatchUpdatePensRequest true "猪批次关联的猪栏ID列表"
|
||||
// @Success 200 {object} controller.Response "更新成功"
|
||||
// @Failure 400 {object} controller.Response "请求参数错误或无效的ID格式"
|
||||
// @Failure 404 {object} controller.Response "猪批次或猪栏不存在"
|
||||
// @Failure 409 {object} controller.Response "业务逻辑冲突 (如猪栏已被占用)"
|
||||
// @Failure 500 {object} controller.Response "内部服务器错误"
|
||||
// @Router /api/v1/pig-batches/{id}/pens [put]
|
||||
func (c *PigBatchController) UpdatePigBatchPens(ctx *gin.Context) {
|
||||
const action = "更新猪批次关联猪栏"
|
||||
batchID, 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.PigBatchUpdatePensRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
err = c.service.UpdatePigBatchPens(uint(batchID), req.PenIDs)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrPigBatchNotFound) || errors.Is(err, service.ErrPenNotFound) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), batchID)
|
||||
return
|
||||
} else if errors.Is(err, service.ErrPigBatchNotActive) || errors.Is(err, service.ErrPenOccupiedByOtherBatch) || errors.Is(err, service.ErrPenStatusInvalidForAllocation) || errors.Is(err, service.ErrPenNotAssociatedWithBatch) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), batchID)
|
||||
return
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪批次关联猪栏失败", action, err.Error(), batchID)
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", nil, action, "更新成功", batchID)
|
||||
}
|
||||
|
||||
@@ -229,7 +229,7 @@ func (c *PigFarmController) CreatePen(ctx *gin.Context) {
|
||||
HouseID: pen.HouseID,
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
PigBatchID: pen.PigBatchID,
|
||||
PigBatchID: *pen.PigBatchID,
|
||||
}
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "创建成功", resp, action, "创建成功", resp)
|
||||
}
|
||||
@@ -267,7 +267,7 @@ func (c *PigFarmController) GetPen(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)
|
||||
}
|
||||
@@ -296,7 +296,7 @@ func (c *PigFarmController) ListPens(ctx *gin.Context) {
|
||||
HouseID: pen.HouseID,
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
PigBatchID: pen.PigBatchID,
|
||||
PigBatchID: *pen.PigBatchID,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -344,7 +344,7 @@ func (c *PigFarmController) UpdatePen(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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user