Compare commits
2 Commits
5403be5e7d
...
740e14e6cc
| Author | SHA1 | Date | |
|---|---|---|---|
| 740e14e6cc | |||
| 8d9e4286b0 |
102
docs/docs.go
102
docs/docs.go
@@ -807,6 +807,59 @@ const docTemplate = `{
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/pens/{id}/status": {
|
||||
"put": {
|
||||
"description": "更新指定猪栏的当前状态",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"猪场管理"
|
||||
],
|
||||
"summary": "更新猪栏状态",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "integer",
|
||||
"description": "猪栏ID",
|
||||
"name": "id",
|
||||
"in": "path",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"description": "新的猪栏状态",
|
||||
"name": "body",
|
||||
"in": "body",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"$ref": "#/definitions/dto.UpdatePenStatusRequest"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "更新成功",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/controller.Response"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"$ref": "#/definitions/dto.PenResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/pig-batches": {
|
||||
"get": {
|
||||
"description": "获取所有猪批次的列表,支持按活跃状态筛选",
|
||||
@@ -1136,7 +1189,7 @@ const docTemplate = `{
|
||||
}
|
||||
},
|
||||
"409": {
|
||||
"description": "业务逻辑冲突 (如猪栏已被占用)",
|
||||
"description": "业务逻辑冲突 (如猪栏已被使用)",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/controller.Response"
|
||||
}
|
||||
@@ -1937,8 +1990,7 @@ const docTemplate = `{
|
||||
"required": [
|
||||
"capacity",
|
||||
"house_id",
|
||||
"pen_number",
|
||||
"status"
|
||||
"pen_number"
|
||||
],
|
||||
"properties": {
|
||||
"capacity": {
|
||||
@@ -1949,9 +2001,6 @@ const docTemplate = `{
|
||||
},
|
||||
"pen_number": {
|
||||
"type": "string"
|
||||
},
|
||||
"status": {
|
||||
"$ref": "#/definitions/models.PenStatus"
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -2616,7 +2665,44 @@ const docTemplate = `{
|
||||
"type": "string"
|
||||
},
|
||||
"status": {
|
||||
"$ref": "#/definitions/models.PenStatus"
|
||||
"description": "添加oneof校验",
|
||||
"enum": [
|
||||
"空闲",
|
||||
"使用中",
|
||||
"病猪栏",
|
||||
"康复栏",
|
||||
"清洗消毒",
|
||||
"维修中"
|
||||
],
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/models.PenStatus"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"dto.UpdatePenStatusRequest": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"status"
|
||||
],
|
||||
"properties": {
|
||||
"status": {
|
||||
"enum": [
|
||||
"空闲",
|
||||
"使用中",
|
||||
"病猪栏",
|
||||
"康复栏",
|
||||
"清洗消毒",
|
||||
"维修中"
|
||||
],
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/models.PenStatus"
|
||||
}
|
||||
],
|
||||
"example": "病猪栏"
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -2693,7 +2779,7 @@ const docTemplate = `{
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"空闲",
|
||||
"占用",
|
||||
"使用中",
|
||||
"病猪栏",
|
||||
"康复栏",
|
||||
"清洗消毒",
|
||||
|
||||
@@ -796,6 +796,59 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/pens/{id}/status": {
|
||||
"put": {
|
||||
"description": "更新指定猪栏的当前状态",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"猪场管理"
|
||||
],
|
||||
"summary": "更新猪栏状态",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "integer",
|
||||
"description": "猪栏ID",
|
||||
"name": "id",
|
||||
"in": "path",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"description": "新的猪栏状态",
|
||||
"name": "body",
|
||||
"in": "body",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"$ref": "#/definitions/dto.UpdatePenStatusRequest"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "更新成功",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/controller.Response"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"$ref": "#/definitions/dto.PenResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/pig-batches": {
|
||||
"get": {
|
||||
"description": "获取所有猪批次的列表,支持按活跃状态筛选",
|
||||
@@ -1125,7 +1178,7 @@
|
||||
}
|
||||
},
|
||||
"409": {
|
||||
"description": "业务逻辑冲突 (如猪栏已被占用)",
|
||||
"description": "业务逻辑冲突 (如猪栏已被使用)",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/controller.Response"
|
||||
}
|
||||
@@ -1926,8 +1979,7 @@
|
||||
"required": [
|
||||
"capacity",
|
||||
"house_id",
|
||||
"pen_number",
|
||||
"status"
|
||||
"pen_number"
|
||||
],
|
||||
"properties": {
|
||||
"capacity": {
|
||||
@@ -1938,9 +1990,6 @@
|
||||
},
|
||||
"pen_number": {
|
||||
"type": "string"
|
||||
},
|
||||
"status": {
|
||||
"$ref": "#/definitions/models.PenStatus"
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -2605,7 +2654,44 @@
|
||||
"type": "string"
|
||||
},
|
||||
"status": {
|
||||
"$ref": "#/definitions/models.PenStatus"
|
||||
"description": "添加oneof校验",
|
||||
"enum": [
|
||||
"空闲",
|
||||
"使用中",
|
||||
"病猪栏",
|
||||
"康复栏",
|
||||
"清洗消毒",
|
||||
"维修中"
|
||||
],
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/models.PenStatus"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"dto.UpdatePenStatusRequest": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"status"
|
||||
],
|
||||
"properties": {
|
||||
"status": {
|
||||
"enum": [
|
||||
"空闲",
|
||||
"使用中",
|
||||
"病猪栏",
|
||||
"康复栏",
|
||||
"清洗消毒",
|
||||
"维修中"
|
||||
],
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/models.PenStatus"
|
||||
}
|
||||
],
|
||||
"example": "病猪栏"
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -2682,7 +2768,7 @@
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"空闲",
|
||||
"占用",
|
||||
"使用中",
|
||||
"病猪栏",
|
||||
"康复栏",
|
||||
"清洗消毒",
|
||||
|
||||
@@ -132,13 +132,10 @@ definitions:
|
||||
type: integer
|
||||
pen_number:
|
||||
type: string
|
||||
status:
|
||||
$ref: '#/definitions/models.PenStatus'
|
||||
required:
|
||||
- capacity
|
||||
- house_id
|
||||
- pen_number
|
||||
- status
|
||||
type: object
|
||||
dto.CreatePigHouseRequest:
|
||||
properties:
|
||||
@@ -578,13 +575,38 @@ definitions:
|
||||
pen_number:
|
||||
type: string
|
||||
status:
|
||||
$ref: '#/definitions/models.PenStatus'
|
||||
allOf:
|
||||
- $ref: '#/definitions/models.PenStatus'
|
||||
description: 添加oneof校验
|
||||
enum:
|
||||
- 空闲
|
||||
- 使用中
|
||||
- 病猪栏
|
||||
- 康复栏
|
||||
- 清洗消毒
|
||||
- 维修中
|
||||
required:
|
||||
- capacity
|
||||
- house_id
|
||||
- pen_number
|
||||
- status
|
||||
type: object
|
||||
dto.UpdatePenStatusRequest:
|
||||
properties:
|
||||
status:
|
||||
allOf:
|
||||
- $ref: '#/definitions/models.PenStatus'
|
||||
enum:
|
||||
- 空闲
|
||||
- 使用中
|
||||
- 病猪栏
|
||||
- 康复栏
|
||||
- 清洗消毒
|
||||
- 维修中
|
||||
example: 病猪栏
|
||||
required:
|
||||
- status
|
||||
type: object
|
||||
dto.UpdatePigHouseRequest:
|
||||
properties:
|
||||
description:
|
||||
@@ -634,7 +656,7 @@ definitions:
|
||||
models.PenStatus:
|
||||
enum:
|
||||
- 空闲
|
||||
- 占用
|
||||
- 使用中
|
||||
- 病猪栏
|
||||
- 康复栏
|
||||
- 清洗消毒
|
||||
@@ -1268,6 +1290,38 @@ paths:
|
||||
summary: 更新猪栏
|
||||
tags:
|
||||
- 猪场管理
|
||||
/api/v1/pens/{id}/status:
|
||||
put:
|
||||
consumes:
|
||||
- application/json
|
||||
description: 更新指定猪栏的当前状态
|
||||
parameters:
|
||||
- description: 猪栏ID
|
||||
in: path
|
||||
name: id
|
||||
required: true
|
||||
type: integer
|
||||
- description: 新的猪栏状态
|
||||
in: body
|
||||
name: body
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/dto.UpdatePenStatusRequest'
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: 更新成功
|
||||
schema:
|
||||
allOf:
|
||||
- $ref: '#/definitions/controller.Response'
|
||||
- properties:
|
||||
data:
|
||||
$ref: '#/definitions/dto.PenResponse'
|
||||
type: object
|
||||
summary: 更新猪栏状态
|
||||
tags:
|
||||
- 猪场管理
|
||||
/api/v1/pig-batches:
|
||||
get:
|
||||
description: 获取所有猪批次的列表,支持按活跃状态筛选
|
||||
@@ -1473,7 +1527,7 @@ paths:
|
||||
schema:
|
||||
$ref: '#/definitions/controller.Response'
|
||||
"409":
|
||||
description: 业务逻辑冲突 (如猪栏已被占用)
|
||||
description: 业务逻辑冲突 (如猪栏已被使用)
|
||||
schema:
|
||||
$ref: '#/definitions/controller.Response'
|
||||
"500":
|
||||
|
||||
@@ -222,6 +222,7 @@ func (a *API) setupRoutes() {
|
||||
penGroup.GET("/:id", a.pigFarmController.GetPen)
|
||||
penGroup.PUT("/:id", a.pigFarmController.UpdatePen)
|
||||
penGroup.DELETE("/:id", a.pigFarmController.DeletePen)
|
||||
penGroup.PUT("/:id/status", a.pigFarmController.UpdatePenStatus)
|
||||
}
|
||||
a.logger.Info("猪圈相关接口注册成功 (需要认证和审计)")
|
||||
|
||||
|
||||
@@ -200,7 +200,7 @@ func (c *PigBatchController) ListPigBatches(ctx *gin.Context) {
|
||||
// @Success 200 {object} controller.Response "更新成功"
|
||||
// @Failure 400 {object} controller.Response "请求参数错误或无效的ID格式"
|
||||
// @Failure 404 {object} controller.Response "猪批次或猪栏不存在"
|
||||
// @Failure 409 {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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -33,10 +33,9 @@ type UpdatePigHouseRequest struct {
|
||||
|
||||
// CreatePenRequest 定义了创建猪栏的请求结构
|
||||
type CreatePenRequest struct {
|
||||
PenNumber string `json:"pen_number" binding:"required"`
|
||||
HouseID uint `json:"house_id" binding:"required"`
|
||||
Capacity int `json:"capacity" binding:"required"`
|
||||
Status models.PenStatus `json:"status" binding:"required"`
|
||||
PenNumber string `json:"pen_number" binding:"required"`
|
||||
HouseID uint `json:"house_id" binding:"required"`
|
||||
Capacity int `json:"capacity" binding:"required"`
|
||||
}
|
||||
|
||||
// UpdatePenRequest 定义了更新猪栏的请求结构
|
||||
@@ -44,5 +43,10 @@ type UpdatePenRequest struct {
|
||||
PenNumber string `json:"pen_number" binding:"required"`
|
||||
HouseID uint `json:"house_id" binding:"required"`
|
||||
Capacity int `json:"capacity" binding:"required"`
|
||||
Status models.PenStatus `json:"status" binding:"required"`
|
||||
Status models.PenStatus `json:"status" binding:"required,oneof=空闲 使用中 病猪栏 康复栏 清洗消毒 维修中"` // 添加oneof校验
|
||||
}
|
||||
|
||||
// UpdatePenStatusRequest 定义了更新猪栏状态的请求结构
|
||||
type UpdatePenStatusRequest struct {
|
||||
Status models.PenStatus `json:"status" binding:"required,oneof=空闲 使用中 病猪栏 康复栏 清洗消毒 维修中" example:"病猪栏"`
|
||||
}
|
||||
|
||||
@@ -16,8 +16,7 @@ var (
|
||||
ErrPigBatchNotFound = errors.New("指定的猪批次不存在")
|
||||
ErrPigBatchActive = errors.New("活跃的猪批次不能被删除")
|
||||
ErrPigBatchNotActive = errors.New("猪批次不处于活跃状态,无法修改关联猪栏")
|
||||
ErrPenNotFound = errors.New("指定的猪栏不存在")
|
||||
ErrPenOccupiedByOtherBatch = errors.New("猪栏已被其他批次占用")
|
||||
ErrPenOccupiedByOtherBatch = errors.New("猪栏已被其他批次使用")
|
||||
ErrPenStatusInvalidForAllocation = errors.New("猪栏状态不允许分配")
|
||||
ErrPenNotAssociatedWithBatch = errors.New("猪栏未与该批次关联")
|
||||
)
|
||||
@@ -258,7 +257,7 @@ func (s *pigBatchService) UpdatePigBatchPens(batchID uint, desiredPenIDs []uint)
|
||||
updates := make(map[string]interface{})
|
||||
updates["pig_batch_id"] = nil // 总是将 PigBatchID 设为 nil
|
||||
|
||||
// 只有当猪栏当前状态是“占用”时,才将其状态改回“空闲”
|
||||
// 只有当猪栏当前状态是“使用中”时,才将其状态改回“空闲”
|
||||
if currentPen.Status == models.PenStatusOccupied {
|
||||
updates["status"] = models.PenStatusEmpty
|
||||
}
|
||||
@@ -280,17 +279,17 @@ func (s *pigBatchService) UpdatePigBatchPens(batchID uint, desiredPenIDs []uint)
|
||||
return fmt.Errorf("获取猪栏 %d 信息失败: %w", penID, err)
|
||||
}
|
||||
|
||||
// 验证:猪栏必须是“空闲”状态且未被任何批次占用,才能被分配
|
||||
// 验证:猪栏必须是“空闲”状态且未被任何批次使用,才能被分配
|
||||
if actualPen.Status != models.PenStatusEmpty {
|
||||
return fmt.Errorf("猪栏 %s 状态为 %s,无法分配: %w", actualPen.PenNumber, actualPen.Status, ErrPenStatusInvalidForAllocation)
|
||||
}
|
||||
if actualPen.PigBatchID != nil {
|
||||
return fmt.Errorf("猪栏 %s 已被其他批次 %d 占用,无法分配: %w", actualPen.PenNumber, *actualPen.PigBatchID, ErrPenOccupiedByOtherBatch)
|
||||
return fmt.Errorf("猪栏 %s 已被其他批次 %d 使用,无法分配: %w", actualPen.PenNumber, *actualPen.PigBatchID, ErrPenOccupiedByOtherBatch)
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"pig_batch_id": &batchID, // 将 PigBatchID 设为当前批次ID的指针
|
||||
"status": models.PenStatusOccupied, // 分配后,状态变为“占用”
|
||||
"status": models.PenStatusOccupied, // 分配后,状态变为“使用中”
|
||||
}
|
||||
if err := s.pigFarmRepo.UpdatePenFields(tx, penID, updates); err != nil {
|
||||
s.logger.Errorf("更新猪批次猪栏失败: 添加猪栏 %d 失败: %v", penID, err)
|
||||
|
||||
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
@@ -11,9 +12,12 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrHouseContainsPens = errors.New("无法删除包含猪栏的猪舍")
|
||||
ErrHouseNotFound = errors.New("指定的猪舍不存在")
|
||||
ErrPenInUse = errors.New("猪栏正在被活跃批次使用,无法删除")
|
||||
ErrHouseContainsPens = errors.New("无法删除包含猪栏的猪舍")
|
||||
ErrHouseNotFound = errors.New("指定的猪舍不存在")
|
||||
ErrPenInUse = errors.New("猪栏正在被活跃批次使用,无法删除")
|
||||
ErrPenNotFound = errors.New("指定的猪栏不存在")
|
||||
ErrPenStatusInvalidForOccupiedPen = errors.New("猪栏已被批次使用,无法设置为非使用中状态")
|
||||
ErrPenStatusInvalidForUnoccupiedPen = errors.New("猪栏未被批次使用,无法设置为使用中状态")
|
||||
)
|
||||
|
||||
// PigFarmService 提供了猪场资产管理的业务逻辑
|
||||
@@ -31,18 +35,22 @@ type PigFarmService interface {
|
||||
ListPens() ([]models.Pen, error)
|
||||
UpdatePen(id uint, penNumber string, houseID uint, capacity int, status models.PenStatus) (*models.Pen, error)
|
||||
DeletePen(id uint) error
|
||||
// UpdatePenStatus 更新猪栏状态
|
||||
UpdatePenStatus(id uint, newStatus models.PenStatus) (*models.Pen, error)
|
||||
}
|
||||
|
||||
type pigFarmService struct {
|
||||
logger *logs.Logger
|
||||
repo repository.PigFarmRepository
|
||||
uow repository.UnitOfWork // 工作单元,用于事务管理
|
||||
}
|
||||
|
||||
// NewPigFarmService 创建一个新的 PigFarmService 实例
|
||||
func NewPigFarmService(repo repository.PigFarmRepository, logger *logs.Logger) PigFarmService {
|
||||
func NewPigFarmService(repo repository.PigFarmRepository, uow repository.UnitOfWork, logger *logs.Logger) PigFarmService {
|
||||
return &pigFarmService{
|
||||
logger: logger,
|
||||
repo: repo,
|
||||
uow: uow,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,3 +202,57 @@ func (s *pigFarmService) DeletePen(id uint) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdatePenStatus 更新猪栏状态
|
||||
func (s *pigFarmService) UpdatePenStatus(id uint, newStatus models.PenStatus) (*models.Pen, error) {
|
||||
var updatedPen *models.Pen
|
||||
err := s.uow.ExecuteInTransaction(func(tx *gorm.DB) error {
|
||||
pen, err := s.repo.GetPenByIDTx(tx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrPenNotFound
|
||||
}
|
||||
s.logger.Errorf("更新猪栏状态失败: 获取猪栏 %d 信息错误: %v", id, err)
|
||||
return fmt.Errorf("获取猪栏 %d 信息失败: %w", id, err)
|
||||
}
|
||||
|
||||
// 业务逻辑:根据猪栏的 PigBatchID 和当前状态,判断是否允许设置为 newStatus
|
||||
if pen.PigBatchID != nil && *pen.PigBatchID != 0 { // 猪栏已被批次使用
|
||||
if newStatus == models.PenStatusEmpty { // 猪栏已被批次使用,不能直接设置为空闲
|
||||
return ErrPenStatusInvalidForOccupiedPen
|
||||
}
|
||||
} else { // 猪栏未被批次使用 (PigBatchID == nil)
|
||||
if newStatus == models.PenStatusOccupied { // 猪栏未被批次使用,不能设置为使用中
|
||||
return ErrPenStatusInvalidForUnoccupiedPen
|
||||
}
|
||||
}
|
||||
|
||||
// 如果新状态与旧状态相同,则无需更新
|
||||
if pen.Status == newStatus {
|
||||
updatedPen = pen // 返回原始猪栏,因为没有实际更新
|
||||
return nil
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{
|
||||
"status": newStatus,
|
||||
}
|
||||
|
||||
if err := s.repo.UpdatePenFields(tx, id, updates); err != nil {
|
||||
s.logger.Errorf("更新猪栏 %d 状态失败: %v", id, err)
|
||||
return fmt.Errorf("更新猪栏 %d 状态失败: %w", id, err)
|
||||
}
|
||||
|
||||
// 获取更新后的猪栏信息
|
||||
updatedPen, err = s.repo.GetPenByIDTx(tx, id)
|
||||
if err != nil {
|
||||
s.logger.Errorf("更新猪栏状态后获取猪栏 %d 信息失败: %v", id, err)
|
||||
return fmt.Errorf("更新猪栏状态后获取猪栏 %d 信息失败: %w", id, err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return updatedPen, nil
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ func NewApplication(configPath string) (*Application, error) {
|
||||
unitOfWork := repository.NewGormUnitOfWork(storage.GetDB(), logger)
|
||||
|
||||
// --- 业务逻辑处理器初始化 ---
|
||||
pigFarmService := service.NewPigFarmService(pigFarmRepo, logger)
|
||||
pigFarmService := service.NewPigFarmService(pigFarmRepo, unitOfWork, logger)
|
||||
pigBatchService := service.NewPigBatchService(pigBatchRepo, pigFarmRepo, unitOfWork, logger)
|
||||
|
||||
// 初始化审计服务
|
||||
|
||||
@@ -21,7 +21,7 @@ type PenStatus string
|
||||
|
||||
const (
|
||||
PenStatusEmpty PenStatus = "空闲"
|
||||
PenStatusOccupied PenStatus = "占用"
|
||||
PenStatusOccupied PenStatus = "使用中"
|
||||
PenStatusSickPen PenStatus = "病猪栏"
|
||||
PenStatusRecovering PenStatus = "康复栏"
|
||||
PenStatusCleaning PenStatus = "清洗消毒"
|
||||
|
||||
Reference in New Issue
Block a user