重构配方类控制器

This commit is contained in:
2025-11-23 14:49:55 +08:00
parent 851682d579
commit 1b2e211bfa
12 changed files with 1245 additions and 1110 deletions

View File

@@ -1657,7 +1657,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-营养"
], ],
"summary": "获取营养种类列表", "summary": "获取营养种类列表",
"parameters": [ "parameters": [
@@ -1727,7 +1727,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-营养"
], ],
"summary": "创建营养种类", "summary": "创建营养种类",
"parameters": [ "parameters": [
@@ -1775,7 +1775,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-营养"
], ],
"summary": "获取营养种类详情", "summary": "获取营养种类详情",
"parameters": [ "parameters": [
@@ -1822,7 +1822,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-营养"
], ],
"summary": "更新营养种类", "summary": "更新营养种类",
"parameters": [ "parameters": [
@@ -1875,7 +1875,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-营养"
], ],
"summary": "删除营养种类", "summary": "删除营养种类",
"parameters": [ "parameters": [
@@ -1909,7 +1909,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "获取猪年龄阶段列表", "summary": "获取猪年龄阶段列表",
"parameters": [ "parameters": [
@@ -1973,7 +1973,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "创建猪年龄阶段", "summary": "创建猪年龄阶段",
"parameters": [ "parameters": [
@@ -2021,7 +2021,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "获取猪年龄阶段详情", "summary": "获取猪年龄阶段详情",
"parameters": [ "parameters": [
@@ -2068,7 +2068,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "更新猪年龄阶段", "summary": "更新猪年龄阶段",
"parameters": [ "parameters": [
@@ -2121,7 +2121,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "删除猪年龄阶段", "summary": "删除猪年龄阶段",
"parameters": [ "parameters": [
@@ -2155,7 +2155,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "获取猪品种列表", "summary": "获取猪品种列表",
"parameters": [ "parameters": [
@@ -2219,7 +2219,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "创建猪品种", "summary": "创建猪品种",
"parameters": [ "parameters": [
@@ -2267,7 +2267,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "获取猪品种详情", "summary": "获取猪品种详情",
"parameters": [ "parameters": [
@@ -2314,7 +2314,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "更新猪品种", "summary": "更新猪品种",
"parameters": [ "parameters": [
@@ -2367,7 +2367,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "删除猪品种", "summary": "删除猪品种",
"parameters": [ "parameters": [
@@ -2401,7 +2401,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "获取猪类型列表", "summary": "获取猪类型列表",
"parameters": [ "parameters": [
@@ -2483,7 +2483,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "创建猪类型", "summary": "创建猪类型",
"parameters": [ "parameters": [
@@ -2531,7 +2531,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "获取猪类型详情", "summary": "获取猪类型详情",
"parameters": [ "parameters": [
@@ -2578,7 +2578,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "更新猪类型", "summary": "更新猪类型",
"parameters": [ "parameters": [
@@ -2631,7 +2631,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "删除猪类型", "summary": "删除猪类型",
"parameters": [ "parameters": [
@@ -2668,7 +2668,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "全量更新猪类型的营养需求", "summary": "全量更新猪类型的营养需求",
"parameters": [ "parameters": [
@@ -2723,7 +2723,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-原料"
], ],
"summary": "获取原料列表", "summary": "获取原料列表",
"parameters": [ "parameters": [
@@ -2793,7 +2793,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-原料"
], ],
"summary": "创建原料", "summary": "创建原料",
"parameters": [ "parameters": [
@@ -2841,7 +2841,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-原料"
], ],
"summary": "获取原料详情", "summary": "获取原料详情",
"parameters": [ "parameters": [
@@ -2888,7 +2888,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-原料"
], ],
"summary": "更新原料", "summary": "更新原料",
"parameters": [ "parameters": [
@@ -2941,7 +2941,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-原料"
], ],
"summary": "删除原料", "summary": "删除原料",
"parameters": [ "parameters": [
@@ -2978,7 +2978,7 @@ const docTemplate = `{
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-原料"
], ],
"summary": "全量更新原料的营养成分", "summary": "全量更新原料的营养成分",
"parameters": [ "parameters": [
@@ -3202,7 +3202,6 @@ const docTemplate = `{
}, },
{ {
"enum": [ "enum": [
7,
-1, -1,
0, 0,
1, 1,
@@ -3212,12 +3211,12 @@ const docTemplate = `{
5, 5,
-1, -1,
5, 5,
6 6,
7
], ],
"type": "integer", "type": "integer",
"format": "int32", "format": "int32",
"x-enum-varnames": [ "x-enum-varnames": [
"_numLevels",
"DebugLevel", "DebugLevel",
"InfoLevel", "InfoLevel",
"WarnLevel", "WarnLevel",
@@ -3227,7 +3226,8 @@ const docTemplate = `{
"FatalLevel", "FatalLevel",
"_minLevel", "_minLevel",
"_maxLevel", "_maxLevel",
"InvalidLevel" "InvalidLevel",
"_numLevels"
], ],
"name": "level", "name": "level",
"in": "query" "in": "query"
@@ -9799,7 +9799,6 @@ const docTemplate = `{
"type": "integer", "type": "integer",
"format": "int32", "format": "int32",
"enum": [ "enum": [
7,
-1, -1,
0, 0,
1, 1,
@@ -9809,10 +9808,10 @@ const docTemplate = `{
5, 5,
-1, -1,
5, 5,
6 6,
7
], ],
"x-enum-varnames": [ "x-enum-varnames": [
"_numLevels",
"DebugLevel", "DebugLevel",
"InfoLevel", "InfoLevel",
"WarnLevel", "WarnLevel",
@@ -9822,7 +9821,8 @@ const docTemplate = `{
"FatalLevel", "FatalLevel",
"_minLevel", "_minLevel",
"_maxLevel", "_maxLevel",
"InvalidLevel" "InvalidLevel",
"_numLevels"
] ]
} }
}, },

View File

@@ -1649,7 +1649,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-营养"
], ],
"summary": "获取营养种类列表", "summary": "获取营养种类列表",
"parameters": [ "parameters": [
@@ -1719,7 +1719,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-营养"
], ],
"summary": "创建营养种类", "summary": "创建营养种类",
"parameters": [ "parameters": [
@@ -1767,7 +1767,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-营养"
], ],
"summary": "获取营养种类详情", "summary": "获取营养种类详情",
"parameters": [ "parameters": [
@@ -1814,7 +1814,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-营养"
], ],
"summary": "更新营养种类", "summary": "更新营养种类",
"parameters": [ "parameters": [
@@ -1867,7 +1867,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-营养"
], ],
"summary": "删除营养种类", "summary": "删除营养种类",
"parameters": [ "parameters": [
@@ -1901,7 +1901,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "获取猪年龄阶段列表", "summary": "获取猪年龄阶段列表",
"parameters": [ "parameters": [
@@ -1965,7 +1965,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "创建猪年龄阶段", "summary": "创建猪年龄阶段",
"parameters": [ "parameters": [
@@ -2013,7 +2013,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "获取猪年龄阶段详情", "summary": "获取猪年龄阶段详情",
"parameters": [ "parameters": [
@@ -2060,7 +2060,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "更新猪年龄阶段", "summary": "更新猪年龄阶段",
"parameters": [ "parameters": [
@@ -2113,7 +2113,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "删除猪年龄阶段", "summary": "删除猪年龄阶段",
"parameters": [ "parameters": [
@@ -2147,7 +2147,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "获取猪品种列表", "summary": "获取猪品种列表",
"parameters": [ "parameters": [
@@ -2211,7 +2211,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "创建猪品种", "summary": "创建猪品种",
"parameters": [ "parameters": [
@@ -2259,7 +2259,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "获取猪品种详情", "summary": "获取猪品种详情",
"parameters": [ "parameters": [
@@ -2306,7 +2306,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "更新猪品种", "summary": "更新猪品种",
"parameters": [ "parameters": [
@@ -2359,7 +2359,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "删除猪品种", "summary": "删除猪品种",
"parameters": [ "parameters": [
@@ -2393,7 +2393,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "获取猪类型列表", "summary": "获取猪类型列表",
"parameters": [ "parameters": [
@@ -2475,7 +2475,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "创建猪类型", "summary": "创建猪类型",
"parameters": [ "parameters": [
@@ -2523,7 +2523,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "获取猪类型详情", "summary": "获取猪类型详情",
"parameters": [ "parameters": [
@@ -2570,7 +2570,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "更新猪类型", "summary": "更新猪类型",
"parameters": [ "parameters": [
@@ -2623,7 +2623,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "删除猪类型", "summary": "删除猪类型",
"parameters": [ "parameters": [
@@ -2660,7 +2660,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-猪"
], ],
"summary": "全量更新猪类型的营养需求", "summary": "全量更新猪类型的营养需求",
"parameters": [ "parameters": [
@@ -2715,7 +2715,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-原料"
], ],
"summary": "获取原料列表", "summary": "获取原料列表",
"parameters": [ "parameters": [
@@ -2785,7 +2785,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-原料"
], ],
"summary": "创建原料", "summary": "创建原料",
"parameters": [ "parameters": [
@@ -2833,7 +2833,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-原料"
], ],
"summary": "获取原料详情", "summary": "获取原料详情",
"parameters": [ "parameters": [
@@ -2880,7 +2880,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-原料"
], ],
"summary": "更新原料", "summary": "更新原料",
"parameters": [ "parameters": [
@@ -2933,7 +2933,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-原料"
], ],
"summary": "删除原料", "summary": "删除原料",
"parameters": [ "parameters": [
@@ -2970,7 +2970,7 @@
"application/json" "application/json"
], ],
"tags": [ "tags": [
"饲料管理" "饲料管理-原料"
], ],
"summary": "全量更新原料的营养成分", "summary": "全量更新原料的营养成分",
"parameters": [ "parameters": [
@@ -3194,7 +3194,6 @@
}, },
{ {
"enum": [ "enum": [
7,
-1, -1,
0, 0,
1, 1,
@@ -3204,12 +3203,12 @@
5, 5,
-1, -1,
5, 5,
6 6,
7
], ],
"type": "integer", "type": "integer",
"format": "int32", "format": "int32",
"x-enum-varnames": [ "x-enum-varnames": [
"_numLevels",
"DebugLevel", "DebugLevel",
"InfoLevel", "InfoLevel",
"WarnLevel", "WarnLevel",
@@ -3219,7 +3218,8 @@
"FatalLevel", "FatalLevel",
"_minLevel", "_minLevel",
"_maxLevel", "_maxLevel",
"InvalidLevel" "InvalidLevel",
"_numLevels"
], ],
"name": "level", "name": "level",
"in": "query" "in": "query"
@@ -9791,7 +9791,6 @@
"type": "integer", "type": "integer",
"format": "int32", "format": "int32",
"enum": [ "enum": [
7,
-1, -1,
0, 0,
1, 1,
@@ -9801,10 +9800,10 @@
5, 5,
-1, -1,
5, 5,
6 6,
7
], ],
"x-enum-varnames": [ "x-enum-varnames": [
"_numLevels",
"DebugLevel", "DebugLevel",
"InfoLevel", "InfoLevel",
"WarnLevel", "WarnLevel",
@@ -9814,7 +9813,8 @@
"FatalLevel", "FatalLevel",
"_minLevel", "_minLevel",
"_maxLevel", "_maxLevel",
"InvalidLevel" "InvalidLevel",
"_numLevels"
] ]
} }
}, },

View File

@@ -2535,7 +2535,6 @@ definitions:
- PlanTypeFilterSystem - PlanTypeFilterSystem
zapcore.Level: zapcore.Level:
enum: enum:
- 7
- -1 - -1
- 0 - 0
- 1 - 1
@@ -2546,10 +2545,10 @@ definitions:
- -1 - -1
- 5 - 5
- 6 - 6
- 7
format: int32 format: int32
type: integer type: integer
x-enum-varnames: x-enum-varnames:
- _numLevels
- DebugLevel - DebugLevel
- InfoLevel - InfoLevel
- WarnLevel - WarnLevel
@@ -2560,6 +2559,7 @@ definitions:
- _minLevel - _minLevel
- _maxLevel - _maxLevel
- InvalidLevel - InvalidLevel
- _numLevels
info: info:
contact: contact:
email: divano@example.com email: divano@example.com
@@ -3618,7 +3618,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 获取营养种类列表 summary: 获取营养种类列表
tags: tags:
- 饲料管理 - 饲料管理-营养
post: post:
consumes: consumes:
- application/json - application/json
@@ -3646,7 +3646,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 创建营养种类 summary: 创建营养种类
tags: tags:
- 饲料管理 - 饲料管理-营养
/api/v1/feed/nutrients/{id}: /api/v1/feed/nutrients/{id}:
delete: delete:
description: 根据ID删除营养种类。 description: 根据ID删除营养种类。
@@ -3667,7 +3667,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 删除营养种类 summary: 删除营养种类
tags: tags:
- 饲料管理 - 饲料管理-营养
get: get:
description: 根据ID获取单个营养种类的详细信息。 description: 根据ID获取单个营养种类的详细信息。
parameters: parameters:
@@ -3692,7 +3692,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 获取营养种类详情 summary: 获取营养种类详情
tags: tags:
- 饲料管理 - 饲料管理-营养
put: put:
consumes: consumes:
- application/json - application/json
@@ -3725,7 +3725,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 更新营养种类 summary: 更新营养种类
tags: tags:
- 饲料管理 - 饲料管理-营养
/api/v1/feed/pig-age-stages: /api/v1/feed/pig-age-stages:
get: get:
description: 获取所有猪年龄阶段的列表,支持分页和过滤。 description: 获取所有猪年龄阶段的列表,支持分页和过滤。
@@ -3762,7 +3762,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 获取猪年龄阶段列表 summary: 获取猪年龄阶段列表
tags: tags:
- 饲料管理 - 饲料管理-猪
post: post:
consumes: consumes:
- application/json - application/json
@@ -3790,7 +3790,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 创建猪年龄阶段 summary: 创建猪年龄阶段
tags: tags:
- 饲料管理 - 饲料管理-猪
/api/v1/feed/pig-age-stages/{id}: /api/v1/feed/pig-age-stages/{id}:
delete: delete:
description: 根据ID删除猪年龄阶段。 description: 根据ID删除猪年龄阶段。
@@ -3811,7 +3811,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 删除猪年龄阶段 summary: 删除猪年龄阶段
tags: tags:
- 饲料管理 - 饲料管理-猪
get: get:
description: 根据ID获取单个猪年龄阶段的详细信息。 description: 根据ID获取单个猪年龄阶段的详细信息。
parameters: parameters:
@@ -3836,7 +3836,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 获取猪年龄阶段详情 summary: 获取猪年龄阶段详情
tags: tags:
- 饲料管理 - 饲料管理-猪
put: put:
consumes: consumes:
- application/json - application/json
@@ -3869,7 +3869,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 更新猪年龄阶段 summary: 更新猪年龄阶段
tags: tags:
- 饲料管理 - 饲料管理-猪
/api/v1/feed/pig-breeds: /api/v1/feed/pig-breeds:
get: get:
description: 获取所有猪品种的列表,支持分页和过滤。 description: 获取所有猪品种的列表,支持分页和过滤。
@@ -3906,7 +3906,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 获取猪品种列表 summary: 获取猪品种列表
tags: tags:
- 饲料管理 - 饲料管理-猪
post: post:
consumes: consumes:
- application/json - application/json
@@ -3934,7 +3934,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 创建猪品种 summary: 创建猪品种
tags: tags:
- 饲料管理 - 饲料管理-猪
/api/v1/feed/pig-breeds/{id}: /api/v1/feed/pig-breeds/{id}:
delete: delete:
description: 根据ID删除猪品种。 description: 根据ID删除猪品种。
@@ -3955,7 +3955,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 删除猪品种 summary: 删除猪品种
tags: tags:
- 饲料管理 - 饲料管理-猪
get: get:
description: 根据ID获取单个猪品种的详细信息。 description: 根据ID获取单个猪品种的详细信息。
parameters: parameters:
@@ -3980,7 +3980,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 获取猪品种详情 summary: 获取猪品种详情
tags: tags:
- 饲料管理 - 饲料管理-猪
put: put:
consumes: consumes:
- application/json - application/json
@@ -4013,7 +4013,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 更新猪品种 summary: 更新猪品种
tags: tags:
- 饲料管理 - 饲料管理-猪
/api/v1/feed/pig-types: /api/v1/feed/pig-types:
get: get:
description: 获取所有猪类型的列表,支持分页和过滤。 description: 获取所有猪类型的列表,支持分页和过滤。
@@ -4062,7 +4062,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 获取猪类型列表 summary: 获取猪类型列表
tags: tags:
- 饲料管理 - 饲料管理-猪
post: post:
consumes: consumes:
- application/json - application/json
@@ -4090,7 +4090,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 创建猪类型 summary: 创建猪类型
tags: tags:
- 饲料管理 - 饲料管理-猪
/api/v1/feed/pig-types/{id}: /api/v1/feed/pig-types/{id}:
delete: delete:
description: 根据ID删除猪类型。 description: 根据ID删除猪类型。
@@ -4111,7 +4111,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 删除猪类型 summary: 删除猪类型
tags: tags:
- 饲料管理 - 饲料管理-猪
get: get:
description: 根据ID获取单个猪类型的详细信息。 description: 根据ID获取单个猪类型的详细信息。
parameters: parameters:
@@ -4136,7 +4136,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 获取猪类型详情 summary: 获取猪类型详情
tags: tags:
- 饲料管理 - 饲料管理-猪
put: put:
consumes: consumes:
- application/json - application/json
@@ -4169,7 +4169,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 更新猪类型 summary: 更新猪类型
tags: tags:
- 饲料管理 - 饲料管理-猪
/api/v1/feed/pig-types/{id}/nutrient-requirements: /api/v1/feed/pig-types/{id}/nutrient-requirements:
put: put:
consumes: consumes:
@@ -4203,7 +4203,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 全量更新猪类型的营养需求 summary: 全量更新猪类型的营养需求
tags: tags:
- 饲料管理 - 饲料管理-猪
/api/v1/feed/raw-materials: /api/v1/feed/raw-materials:
get: get:
description: 获取所有原料的列表,支持分页和过滤。 description: 获取所有原料的列表,支持分页和过滤。
@@ -4244,7 +4244,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 获取原料列表 summary: 获取原料列表
tags: tags:
- 饲料管理 - 饲料管理-原料
post: post:
consumes: consumes:
- application/json - application/json
@@ -4272,7 +4272,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 创建原料 summary: 创建原料
tags: tags:
- 饲料管理 - 饲料管理-原料
/api/v1/feed/raw-materials/{id}: /api/v1/feed/raw-materials/{id}:
delete: delete:
description: 根据ID删除原料。 description: 根据ID删除原料。
@@ -4293,7 +4293,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 删除原料 summary: 删除原料
tags: tags:
- 饲料管理 - 饲料管理-原料
get: get:
description: 根据ID获取单个原料的详细信息。 description: 根据ID获取单个原料的详细信息。
parameters: parameters:
@@ -4318,7 +4318,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 获取原料详情 summary: 获取原料详情
tags: tags:
- 饲料管理 - 饲料管理-原料
put: put:
consumes: consumes:
- application/json - application/json
@@ -4351,7 +4351,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 更新原料 summary: 更新原料
tags: tags:
- 饲料管理 - 饲料管理-原料
/api/v1/feed/raw-materials/{id}/nutrients: /api/v1/feed/raw-materials/{id}/nutrients:
put: put:
consumes: consumes:
@@ -4385,7 +4385,7 @@ paths:
- BearerAuth: [] - BearerAuth: []
summary: 全量更新原料的营养成分 summary: 全量更新原料的营养成分
tags: tags:
- 饲料管理 - 饲料管理-原料
/api/v1/monitor/device-command-logs: /api/v1/monitor/device-command-logs:
get: get:
description: 根据提供的过滤条件,分页获取设备命令日志 description: 根据提供的过滤条件,分页获取设备命令日志
@@ -4484,7 +4484,6 @@ paths:
name: end_time name: end_time
type: string type: string
- enum: - enum:
- 7
- -1 - -1
- 0 - 0
- 1 - 1
@@ -4495,12 +4494,12 @@ paths:
- -1 - -1
- 5 - 5
- 6 - 6
- 7
format: int32 format: int32
in: query in: query
name: level name: level
type: integer type: integer
x-enum-varnames: x-enum-varnames:
- _numLevels
- DebugLevel - DebugLevel
- InfoLevel - InfoLevel
- WarnLevel - WarnLevel
@@ -4511,6 +4510,7 @@ paths:
- _minLevel - _minLevel
- _maxLevel - _maxLevel
- InvalidLevel - InvalidLevel
- _numLevels
- enum: - enum:
- 邮件 - 邮件
- 企业微信 - 企业微信

View File

@@ -57,6 +57,11 @@ type API struct {
healthController *health.Controller // 健康检查控制器实例 healthController *health.Controller // 健康检查控制器实例
alarmController *alarm.ThresholdAlarmController // 阈值告警控制器 alarmController *alarm.ThresholdAlarmController // 阈值告警控制器
feedController *feed.Controller // 饲料管理控制器实例 feedController *feed.Controller // 饲料管理控制器实例
nutrientController *feed.NutrientController // 营养控制器实例
pigAgeStageController *feed.PigAgeStageController // 猪龄阶段控制器实例
pigBreedController *feed.PigBreedController // 猪品种控制器实例
pigTypeController *feed.PigTypeController // 猪种类控制器实例
rawMaterialController *feed.RawMaterialController // 原料控制器实例
listenHandler webhook.ListenHandler // 设备上行事件监听器 listenHandler webhook.ListenHandler // 设备上行事件监听器
analysisTaskManager *domain_plan.AnalysisPlanTaskManager // 计划触发器管理器实例 analysisTaskManager *domain_plan.AnalysisPlanTaskManager // 计划触发器管理器实例
} }
@@ -98,24 +103,20 @@ func NewAPI(cfg config.ServerConfig,
auditService: auditService, auditService: auditService,
config: cfg, config: cfg,
listenHandler: listenHandler, listenHandler: listenHandler,
// 在 NewAPI 中初始化用户控制器,并将其作为 API 结构体的成员
userController: user.NewController(logs.AddCompName(baseCtx, "UserController"), userService), userController: user.NewController(logs.AddCompName(baseCtx, "UserController"), userService),
// 在 NewAPI 中初始化设备控制器,并将其作为 API 结构体的成员
deviceController: device.NewController(logs.AddCompName(baseCtx, "DeviceController"), deviceService), deviceController: device.NewController(logs.AddCompName(baseCtx, "DeviceController"), deviceService),
// 在 NewAPI 中初始化计划控制器,并将其作为 API 结构体的成员
planController: plan.NewController(logs.AddCompName(baseCtx, "PlanController"), planService), planController: plan.NewController(logs.AddCompName(baseCtx, "PlanController"), planService),
// 在 NewAPI 中初始化猪场管理控制器
pigFarmController: management.NewPigFarmController(logs.AddCompName(baseCtx, "PigFarmController"), pigFarmService), pigFarmController: management.NewPigFarmController(logs.AddCompName(baseCtx, "PigFarmController"), pigFarmService),
// 在 NewAPI 中初始化猪群控制器
pigBatchController: management.NewPigBatchController(logs.AddCompName(baseCtx, "PigBatchController"), pigBatchService), pigBatchController: management.NewPigBatchController(logs.AddCompName(baseCtx, "PigBatchController"), pigBatchService),
// 在 NewAPI 中初始化数据监控控制器
monitorController: monitor.NewController(logs.AddCompName(baseCtx, "MonitorController"), monitorService), monitorController: monitor.NewController(logs.AddCompName(baseCtx, "MonitorController"), monitorService),
// 在 NewAPI 中初始化健康检查控制器
healthController: health.NewController(logs.AddCompName(baseCtx, "HealthController")), healthController: health.NewController(logs.AddCompName(baseCtx, "HealthController")),
// 在 NewAPI 中初始化阈
alarmController: alarm.NewThresholdAlarmController(logs.AddCompName(baseCtx, "ThresholdAlarmController"), alarmService), alarmController: alarm.NewThresholdAlarmController(logs.AddCompName(baseCtx, "ThresholdAlarmController"), alarmService),
// 在 NewAPI 中初始化饲料管理控制器
feedController: feed.NewController(logs.AddCompName(baseCtx, "FeedController"), feedManagementService), feedController: feed.NewController(logs.AddCompName(baseCtx, "FeedController"), feedManagementService),
nutrientController: feed.NewNutrientController(logs.AddCompName(baseCtx, "NutrientController"), feedManagementService),
pigAgeStageController: feed.NewPigAgeStageController(logs.AddCompName(baseCtx, "PigAgeStageController"), feedManagementService),
pigBreedController: feed.NewPigBreedController(logs.AddCompName(baseCtx, "PigBreedController"), feedManagementService),
pigTypeController: feed.NewPigTypeController(logs.AddCompName(baseCtx, "PigTypeController"), feedManagementService),
rawMaterialController: feed.NewRawMaterialController(logs.AddCompName(baseCtx, "RawMaterialController"), feedManagementService),
} }
api.setupRoutes() // 设置所有路由 api.setupRoutes() // 设置所有路由

View File

@@ -217,41 +217,41 @@ func (a *API) setupRoutes() {
feedGroup := authGroup.Group("/feed") feedGroup := authGroup.Group("/feed")
{ {
// 营养种类 (Nutrient) 路由 // 营养种类 (Nutrient) 路由
feedGroup.POST("/nutrients", a.feedController.CreateNutrient) feedGroup.POST("/nutrients", a.nutrientController.CreateNutrient)
feedGroup.PUT("/nutrients/:id", a.feedController.UpdateNutrient) feedGroup.PUT("/nutrients/:id", a.nutrientController.UpdateNutrient)
feedGroup.DELETE("/nutrients/:id", a.feedController.DeleteNutrient) feedGroup.DELETE("/nutrients/:id", a.nutrientController.DeleteNutrient)
feedGroup.GET("/nutrients/:id", a.feedController.GetNutrient) feedGroup.GET("/nutrients/:id", a.nutrientController.GetNutrient)
feedGroup.GET("/nutrients", a.feedController.ListNutrients) feedGroup.GET("/nutrients", a.nutrientController.ListNutrients)
// 原料 (RawMaterial) 路由 // 原料 (RawMaterial) 路由
feedGroup.POST("/raw-materials", a.feedController.CreateRawMaterial) feedGroup.POST("/raw-materials", a.rawMaterialController.CreateRawMaterial)
feedGroup.PUT("/raw-materials/:id", a.feedController.UpdateRawMaterial) feedGroup.PUT("/raw-materials/:id", a.rawMaterialController.UpdateRawMaterial)
feedGroup.PUT("/raw-materials/:id/nutrients", a.feedController.UpdateRawMaterialNutrients) feedGroup.PUT("/raw-materials/:id/nutrients", a.rawMaterialController.UpdateRawMaterialNutrients)
feedGroup.DELETE("/raw-materials/:id", a.feedController.DeleteRawMaterial) feedGroup.DELETE("/raw-materials/:id", a.rawMaterialController.DeleteRawMaterial)
feedGroup.GET("/raw-materials/:id", a.feedController.GetRawMaterial) feedGroup.GET("/raw-materials/:id", a.rawMaterialController.GetRawMaterial)
feedGroup.GET("/raw-materials", a.feedController.ListRawMaterials) feedGroup.GET("/raw-materials", a.rawMaterialController.ListRawMaterials)
// 猪品种 (PigBreed) 路由 // 猪品种 (PigBreed) 路由
feedGroup.POST("/pig-breeds", a.feedController.CreatePigBreed) feedGroup.POST("/pig-breeds", a.pigBreedController.CreatePigBreed)
feedGroup.PUT("/pig-breeds/:id", a.feedController.UpdatePigBreed) feedGroup.PUT("/pig-breeds/:id", a.pigBreedController.UpdatePigBreed)
feedGroup.DELETE("/pig-breeds/:id", a.feedController.DeletePigBreed) feedGroup.DELETE("/pig-breeds/:id", a.pigBreedController.DeletePigBreed)
feedGroup.GET("/pig-breeds/:id", a.feedController.GetPigBreed) feedGroup.GET("/pig-breeds/:id", a.pigBreedController.GetPigBreed)
feedGroup.GET("/pig-breeds", a.feedController.ListPigBreeds) feedGroup.GET("/pig-breeds", a.pigBreedController.ListPigBreeds)
// 猪年龄阶段 (PigAgeStage) 路由 // 猪年龄阶段 (PigAgeStage) 路由
feedGroup.POST("/pig-age-stages", a.feedController.CreatePigAgeStage) feedGroup.POST("/pig-age-stages", a.pigAgeStageController.CreatePigAgeStage)
feedGroup.PUT("/pig-age-stages/:id", a.feedController.UpdatePigAgeStage) feedGroup.PUT("/pig-age-stages/:id", a.pigAgeStageController.UpdatePigAgeStage)
feedGroup.DELETE("/pig-age-stages/:id", a.feedController.DeletePigAgeStage) feedGroup.DELETE("/pig-age-stages/:id", a.pigAgeStageController.DeletePigAgeStage)
feedGroup.GET("/pig-age-stages/:id", a.feedController.GetPigAgeStage) feedGroup.GET("/pig-age-stages/:id", a.pigAgeStageController.GetPigAgeStage)
feedGroup.GET("/pig-age-stages", a.feedController.ListPigAgeStages) feedGroup.GET("/pig-age-stages", a.pigAgeStageController.ListPigAgeStages)
// 猪类型 (PigType) 路由 // 猪类型 (PigType) 路由
feedGroup.POST("/pig-types", a.feedController.CreatePigType) feedGroup.POST("/pig-types", a.pigTypeController.CreatePigType)
feedGroup.PUT("/pig-types/:id", a.feedController.UpdatePigType) feedGroup.PUT("/pig-types/:id", a.pigTypeController.UpdatePigType)
feedGroup.DELETE("/pig-types/:id", a.feedController.DeletePigType) feedGroup.DELETE("/pig-types/:id", a.pigTypeController.DeletePigType)
feedGroup.GET("/pig-types/:id", a.feedController.GetPigType) feedGroup.GET("/pig-types/:id", a.pigTypeController.GetPigType)
feedGroup.GET("/pig-types", a.feedController.ListPigTypes) feedGroup.GET("/pig-types", a.pigTypeController.ListPigTypes)
feedGroup.PUT("/pig-types/:id/nutrient-requirements", a.feedController.UpdatePigTypeNutrientRequirements) feedGroup.PUT("/pig-types/:id/nutrient-requirements", a.pigTypeController.UpdatePigTypeNutrientRequirements)
} }
logger.Debug("饲料管理相关接口注册成功 (需要认证和审计)") logger.Debug("饲料管理相关接口注册成功 (需要认证和审计)")
} }

View File

@@ -2,18 +2,11 @@ package feed
import ( import (
"context" "context"
"errors"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service" "git.huangwc.com/pig/pig-farm-controller/internal/app/service"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"github.com/labstack/echo/v4"
) )
// Controller 定义了饲料管理相关的控制器 // Controller 定义了饲料管理相关的控制器,作为各个子控制器的入口
type Controller struct { type Controller struct {
ctx context.Context ctx context.Context
feedManagementService service.FeedManagementService feedManagementService service.FeedManagementService
@@ -26,934 +19,3 @@ func NewController(ctx context.Context, feedManagementService service.FeedManage
feedManagementService: feedManagementService, feedManagementService: feedManagementService,
} }
} }
// --- 营养种类 (Nutrient) 接口方法实现 ---
// CreateNutrient godoc
// @Summary 创建营养种类
// @Description 创建一个新的营养种类。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param nutrient body dto.CreateNutrientRequest true "营养种类信息"
// @Success 200 {object} controller.Response{data=dto.NutrientResponse} "业务码为201代表创建成功"
// @Router /api/v1/feed/nutrients [post]
func (c *Controller) CreateNutrient(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateNutrient")
var req dto.CreateNutrientRequest
const actionType = "创建营养种类"
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreateNutrient(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建营养种类失败: %v", actionType, err)
if errors.Is(err, service.ErrNutrientNameConflict) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "营养种类名称已存在", req)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建营养种类失败: "+err.Error(), actionType, "服务层创建营养种类失败", req)
}
logger.Infof("%s: 营养种类创建成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "营养种类创建成功", resp, actionType, "营养种类创建成功", resp)
}
// UpdateNutrient godoc
// @Summary 更新营养种类
// @Description 根据ID更新营养种类信息。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "营养种类ID"
// @Param nutrient body dto.UpdateNutrientRequest true "更新后的营养种类信息"
// @Success 200 {object} controller.Response{data=dto.NutrientResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/nutrients/{id} [put]
func (c *Controller) UpdateNutrient(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateNutrient")
const actionType = "更新营养种类"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 营养种类ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的营养种类ID格式", actionType, "营养种类ID格式错误", idStr)
}
var req dto.UpdateNutrientRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdateNutrient(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新营养种类失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrNutrientNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "营养种类不存在", id)
}
if errors.Is(err, service.ErrNutrientNameConflict) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "营养种类名称已存在", req)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新营养种类失败: "+err.Error(), actionType, "服务层更新营养种类失败", req)
}
logger.Infof("%s: 营养种类更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "营养种类更新成功", resp, actionType, "营养种类更新成功", resp)
}
// DeleteNutrient godoc
// @Summary 删除营养种类
// @Description 根据ID删除营养种类。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param id path int true "营养种类ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Router /api/v1/feed/nutrients/{id} [delete]
func (c *Controller) DeleteNutrient(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteNutrient")
const actionType = "删除营养种类"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 营养种类ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的营养种类ID格式", actionType, "营养种类ID格式错误", idStr)
}
err = c.feedManagementService.DeleteNutrient(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除营养种类失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrNutrientNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "营养种类不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除营养种类失败: "+err.Error(), actionType, "服务层删除营养种类失败", id)
}
logger.Infof("%s: 营养种类删除成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "营养种类删除成功", nil, actionType, "营养种类删除成功", id)
}
// GetNutrient godoc
// @Summary 获取营养种类详情
// @Description 根据ID获取单个营养种类的详细信息。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param id path int true "营养种类ID"
// @Success 200 {object} controller.Response{data=dto.NutrientResponse} "业务码为200代表成功获取"
// @Router /api/v1/feed/nutrients/{id} [get]
func (c *Controller) GetNutrient(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetNutrient")
const actionType = "获取营养种类详情"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 营养种类ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的营养种类ID格式", actionType, "营养种类ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetNutrient(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取营养种类详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrNutrientNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "营养种类不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取营养种类详情失败: "+err.Error(), actionType, "服务层获取营养种类详情失败", id)
}
logger.Infof("%s: 获取营养种类详情成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取营养种类详情成功", resp, actionType, "获取营养种类详情成功", resp)
}
// ListNutrients godoc
// @Summary 获取营养种类列表
// @Description 获取所有营养种类的列表,支持分页和过滤。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListNutrientRequest false "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListNutrientResponse} "业务码为200代表成功获取列表"
// @Router /api/v1/feed/nutrients [get]
func (c *Controller) ListNutrients(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListNutrients")
const actionType = "获取营养种类列表"
var req dto.ListNutrientRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListNutrients(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取营养种类列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取营养种类列表失败: "+err.Error(), actionType, "服务层获取营养种类列表失败", nil)
}
logger.Infof("%s: 获取营养种类列表成功, 数量: %d", actionType, len(resp.List))
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取营养种类列表成功", resp, actionType, "获取营养种类列表成功", resp)
}
// --- 原料 (RawMaterial) 接口方法实现 ---
// CreateRawMaterial godoc
// @Summary 创建原料
// @Description 创建一个新的原料。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param rawMaterial body dto.CreateRawMaterialRequest true "原料信息"
// @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为201代表创建成功"
// @Router /api/v1/feed/raw-materials [post]
func (c *Controller) CreateRawMaterial(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateRawMaterial")
var req dto.CreateRawMaterialRequest
const actionType = "创建原料"
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreateRawMaterial(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建原料失败: %v", actionType, err)
if errors.Is(err, service.ErrRawMaterialNameConflict) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "原料名称已存在", req)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建原料失败: "+err.Error(), actionType, "服务层创建原料失败", req)
}
logger.Infof("%s: 原料创建成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "原料创建成功", resp, actionType, "原料创建成功", resp)
}
// UpdateRawMaterial godoc
// @Summary 更新原料
// @Description 根据ID更新原料信息。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "原料ID"
// @Param rawMaterial body dto.UpdateRawMaterialRequest true "更新后的原料信息"
// @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/raw-materials/{id} [put]
func (c *Controller) UpdateRawMaterial(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateRawMaterial")
const actionType = "更新原料"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 原料ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的原料ID格式", actionType, "原料ID格式错误", idStr)
}
var req dto.UpdateRawMaterialRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdateRawMaterial(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新原料失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrRawMaterialNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "原料不存在", id)
}
if errors.Is(err, service.ErrRawMaterialNameConflict) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "原料名称已存在", req)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新原料失败: "+err.Error(), actionType, "服务层更新原料失败", req)
}
logger.Infof("%s: 原料更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "原料更新成功", resp, actionType, "原料更新成功", resp)
}
// DeleteRawMaterial godoc
// @Summary 删除原料
// @Description 根据ID删除原料。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param id path int true "原料ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Router /api/v1/feed/raw-materials/{id} [delete]
func (c *Controller) DeleteRawMaterial(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteRawMaterial")
const actionType = "删除原料"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 原料ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的原料ID格式", actionType, "原料ID格式错误", idStr)
}
err = c.feedManagementService.DeleteRawMaterial(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除原料失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrRawMaterialNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "原料不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除原料失败: "+err.Error(), actionType, "服务层删除原料失败", id)
}
logger.Infof("%s: 原料删除成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "原料删除成功", nil, actionType, "原料删除成功", id)
}
// GetRawMaterial godoc
// @Summary 获取原料详情
// @Description 根据ID获取单个原料的详细信息。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param id path int true "原料ID"
// @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为200代表成功获取"
// @Router /api/v1/feed/raw-materials/{id} [get]
func (c *Controller) GetRawMaterial(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetRawMaterial")
const actionType = "获取原料详情"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 原料ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的原料ID格式", actionType, "原料ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetRawMaterial(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取原料详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrRawMaterialNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "原料不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取原料详情失败: "+err.Error(), actionType, "服务层获取原料详情失败", id)
}
logger.Infof("%s: 获取原料详情成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取原料详情成功", resp, actionType, "获取原料详情成功", resp)
}
// ListRawMaterials godoc
// @Summary 获取原料列表
// @Description 获取所有原料的列表,支持分页和过滤。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListRawMaterialRequest false "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListRawMaterialResponse} "业务码为200代表成功获取列表"
// @Router /api/v1/feed/raw-materials [get]
func (c *Controller) ListRawMaterials(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListRawMaterials")
const actionType = "获取原料列表"
var req dto.ListRawMaterialRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListRawMaterials(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取原料列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取原料列表失败: "+err.Error(), actionType, "服务层获取原料列表失败", nil)
}
logger.Infof("%s: 获取原料列表成功, 数量: %d", actionType, len(resp.List))
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取原料列表成功", resp, actionType, "获取原料列表成功", resp)
}
// UpdateRawMaterialNutrients godoc
// @Summary 全量更新原料的营养成分
// @Description 根据原料ID替换其所有的营养成分信息。这是一个覆盖操作。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "原料ID"
// @Param nutrients body dto.UpdateRawMaterialNutrientsRequest true "新的营养成分列表"
// @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/raw-materials/{id}/nutrients [put]
func (c *Controller) UpdateRawMaterialNutrients(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateRawMaterialNutrients")
const actionType = "更新原料营养成分"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 原料ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的原料ID格式", actionType, "原料ID格式错误", idStr)
}
var req dto.UpdateRawMaterialNutrientsRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdateRawMaterialNutrients(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新原料营养成分失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrRawMaterialNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "原料不存在", id)
}
// 这里可以根据未来可能从服务层返回的其他特定错误进行处理
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新原料营养成分失败: "+err.Error(), actionType, "服务层更新失败", req)
}
logger.Infof("%s: 原料营养成分更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "原料营养成分更新成功", resp, actionType, "原料营养成分更新成功", resp)
}
// --- 猪品种 (PigBreed) 接口方法实现 ---
// CreatePigBreed godoc
// @Summary 创建猪品种
// @Description 创建一个新的猪品种。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param pigBreed body dto.CreatePigBreedRequest true "猪品种信息"
// @Success 200 {object} controller.Response{data=dto.PigBreedResponse} "业务码为201代表创建成功"
// @Router /api/v1/feed/pig-breeds [post]
func (c *Controller) CreatePigBreed(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePigBreed")
var req dto.CreatePigBreedRequest
const actionType = "创建猪品种"
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreatePigBreed(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建猪品种失败: %v", actionType, err)
// 猪品种没有名称冲突的领域错误,这里直接返回内部错误
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪品种失败: "+err.Error(), actionType, "服务层创建猪品种失败", req)
}
logger.Infof("%s: 猪品种创建成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "猪品种创建成功", resp, actionType, "猪品种创建成功", resp)
}
// UpdatePigBreed godoc
// @Summary 更新猪品种
// @Description 根据ID更新猪品种信息。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "猪品种ID"
// @Param pigBreed body dto.UpdatePigBreedRequest true "更新后的猪品种信息"
// @Success 200 {object} controller.Response{data=dto.PigBreedResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/pig-breeds/{id} [put]
func (c *Controller) UpdatePigBreed(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePigBreed")
const actionType = "更新猪品种"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪品种ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪品种ID格式", actionType, "猪品种ID格式错误", idStr)
}
var req dto.UpdatePigBreedRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigBreed(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪品种失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigBreedNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪品种不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪品种失败: "+err.Error(), actionType, "服务层更新猪品种失败", req)
}
logger.Infof("%s: 猪品种更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪品种更新成功", resp, actionType, "猪品种更新成功", resp)
}
// DeletePigBreed godoc
// @Summary 删除猪品种
// @Description 根据ID删除猪品种。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param id path int true "猪品种ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Router /api/v1/feed/pig-breeds/{id} [delete]
func (c *Controller) DeletePigBreed(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePigBreed")
const actionType = "删除猪品种"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪品种ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪品种ID格式", actionType, "猪品种ID格式错误", idStr)
}
err = c.feedManagementService.DeletePigBreed(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除猪品种失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigBreedNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪品种不存在", id)
}
if errors.Is(err, service.ErrPigBreedInUse) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "猪品种正在被使用", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除猪品种失败: "+err.Error(), actionType, "服务层删除猪品种失败", id)
}
logger.Infof("%s: 猪品种删除成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪品种删除成功", nil, actionType, "猪品种删除成功", id)
}
// GetPigBreed godoc
// @Summary 获取猪品种详情
// @Description 根据ID获取单个猪品种的详细信息。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param id path int true "猪品种ID"
// @Success 200 {object} controller.Response{data=dto.PigBreedResponse} "业务码为200代表成功获取"
// @Router /api/v1/feed/pig-breeds/{id} [get]
func (c *Controller) GetPigBreed(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPigBreed")
const actionType = "获取猪品种详情"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪品种ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪品种ID格式", actionType, "猪品种ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetPigBreed(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取猪品种详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigBreedNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪品种不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪品种详情失败: "+err.Error(), actionType, "服务层获取猪品种详情失败", id)
}
logger.Infof("%s: 获取猪品种详情成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪品种详情成功", resp, actionType, "获取猪品种详情成功", resp)
}
// ListPigBreeds godoc
// @Summary 获取猪品种列表
// @Description 获取所有猪品种的列表,支持分页和过滤。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListPigBreedRequest false "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListPigBreedResponse} "业务码为200代表成功获取列表"
// @Router /api/v1/feed/pig-breeds [get]
func (c *Controller) ListPigBreeds(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigBreeds")
const actionType = "获取猪品种列表"
var req dto.ListPigBreedRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListPigBreeds(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取猪品种列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪品种列表失败: "+err.Error(), actionType, "服务层获取猪品种列表失败", nil)
}
logger.Infof("%s: 获取猪品种列表成功, 数量: %d", actionType, len(resp.List))
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪品种列表成功", resp, actionType, "获取猪品种列表成功", resp)
}
// --- 猪年龄阶段 (PigAgeStage) 接口方法实现 ---
// CreatePigAgeStage godoc
// @Summary 创建猪年龄阶段
// @Description 创建一个新的猪年龄阶段。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param pigAgeStage body dto.CreatePigAgeStageRequest true "猪年龄阶段信息"
// @Success 200 {object} controller.Response{data=dto.PigAgeStageResponse} "业务码为201代表创建成功"
// @Router /api/v1/feed/pig-age-stages [post]
func (c *Controller) CreatePigAgeStage(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePigAgeStage")
var req dto.CreatePigAgeStageRequest
const actionType = "创建猪年龄阶段"
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreatePigAgeStage(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建猪年龄阶段失败: %v", actionType, err)
// 猪年龄阶段没有名称冲突的领域错误,这里直接返回内部错误
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪年龄阶段失败: "+err.Error(), actionType, "服务层创建猪年龄阶段失败", req)
}
logger.Infof("%s: 猪年龄阶段创建成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "猪年龄阶段创建成功", resp, actionType, "猪年龄阶段创建成功", resp)
}
// UpdatePigAgeStage godoc
// @Summary 更新猪年龄阶段
// @Description 根据ID更新猪年龄阶段信息。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "猪年龄阶段ID"
// @Param pigAgeStage body dto.UpdatePigAgeStageRequest true "更新后的猪年龄阶段信息"
// @Success 200 {object} controller.Response{data=dto.PigAgeStageResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/pig-age-stages/{id} [put]
func (c *Controller) UpdatePigAgeStage(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePigAgeStage")
const actionType = "更新猪年龄阶段"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪年龄阶段ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪年龄阶段ID格式", actionType, "猪年龄阶段ID格式错误", idStr)
}
var req dto.UpdatePigAgeStageRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigAgeStage(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪年龄阶段失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigAgeStageNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪年龄阶段不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪年龄阶段失败: "+err.Error(), actionType, "服务层更新猪年龄阶段失败", req)
}
logger.Infof("%s: 猪年龄阶段更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪年龄阶段更新成功", resp, actionType, "猪年龄阶段更新成功", resp)
}
// DeletePigAgeStage godoc
// @Summary 删除猪年龄阶段
// @Description 根据ID删除猪年龄阶段。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param id path int true "猪年龄阶段ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Router /api/v1/feed/pig-age-stages/{id} [delete]
func (c *Controller) DeletePigAgeStage(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePigAgeStage")
const actionType = "删除猪年龄阶段"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪年龄阶段ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪年龄阶段ID格式", actionType, "猪年龄阶段ID格式错误", idStr)
}
err = c.feedManagementService.DeletePigAgeStage(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除猪年龄阶段失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigAgeStageNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪年龄阶段不存在", id)
}
if errors.Is(err, service.ErrPigAgeStageInUse) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "猪年龄阶段正在被使用", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除猪年龄阶段失败: "+err.Error(), actionType, "服务层删除猪年龄阶段失败", id)
}
logger.Infof("%s: 猪年龄阶段删除成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪年龄阶段删除成功", nil, actionType, "猪年龄阶段删除成功", id)
}
// GetPigAgeStage godoc
// @Summary 获取猪年龄阶段详情
// @Description 根据ID获取单个猪年龄阶段的详细信息。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param id path int true "猪年龄阶段ID"
// @Success 200 {object} controller.Response{data=dto.PigAgeStageResponse} "业务码为200代表成功获取"
// @Router /api/v1/feed/pig-age-stages/{id} [get]
func (c *Controller) GetPigAgeStage(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPigAgeStage")
const actionType = "获取猪年龄阶段详情"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪年龄阶段ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪年龄阶段ID格式", actionType, "猪年龄阶段ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetPigAgeStage(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取猪年龄阶段详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigAgeStageNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪年龄阶段不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪年龄阶段详情失败: "+err.Error(), actionType, "服务层获取猪年龄阶段详情失败", id)
}
logger.Infof("%s: 获取猪年龄阶段详情成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪年龄阶段详情成功", resp, actionType, "获取猪年龄阶段详情成功", resp)
}
// ListPigAgeStages godoc
// @Summary 获取猪年龄阶段列表
// @Description 获取所有猪年龄阶段的列表,支持分页和过滤。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListPigAgeStageRequest false "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListPigAgeStageResponse} "业务码为200代表成功获取列表"
// @Router /api/v1/feed/pig-age-stages [get]
func (c *Controller) ListPigAgeStages(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigAgeStages")
const actionType = "获取猪年龄阶段列表"
var req dto.ListPigAgeStageRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListPigAgeStages(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取猪年龄阶段列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪年龄阶段列表失败: "+err.Error(), actionType, "服务层获取猪年龄阶段列表失败", nil)
}
logger.Infof("%s: 获取猪年龄阶段列表成功, 数量: %d", actionType, len(resp.List))
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪年龄阶段列表成功", resp, actionType, "获取猪年龄阶段列表成功", resp)
}
// --- 猪类型 (PigType) 接口方法实现 ---
// CreatePigType godoc
// @Summary 创建猪类型
// @Description 创建一个新的猪类型。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param pigType body dto.CreatePigTypeRequest true "猪类型信息"
// @Success 200 {object} controller.Response{data=dto.PigTypeResponse} "业务码为201代表创建成功"
// @Router /api/v1/feed/pig-types [post]
func (c *Controller) CreatePigType(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePigType")
var req dto.CreatePigTypeRequest
const actionType = "创建猪类型"
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreatePigType(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建猪类型失败: %v", actionType, err)
if errors.Is(err, service.ErrPigBreedNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "关联猪品种不存在", req)
}
if errors.Is(err, service.ErrPigAgeStageNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "关联猪年龄阶段不存在", req)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪类型失败: "+err.Error(), actionType, "服务层创建猪类型失败", req)
}
logger.Infof("%s: 猪类型创建成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "猪类型创建成功", resp, actionType, "猪类型创建成功", resp)
}
// UpdatePigType godoc
// @Summary 更新猪类型
// @Description 根据ID更新猪类型信息。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "猪类型ID"
// @Param pigType body dto.UpdatePigTypeRequest true "更新后的猪类型信息"
// @Success 200 {object} controller.Response{data=dto.PigTypeResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/pig-types/{id} [put]
func (c *Controller) UpdatePigType(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePigType")
const actionType = "更新猪类型"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪类型ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪类型ID格式", actionType, "猪类型ID格式错误", idStr)
}
var req dto.UpdatePigTypeRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigType(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪类型失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪类型不存在", id)
}
if errors.Is(err, service.ErrPigBreedNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "关联猪品种不存在", req)
}
if errors.Is(err, service.ErrPigAgeStageNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "关联猪年龄阶段不存在", req)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪类型失败: "+err.Error(), actionType, "服务层更新猪类型失败", req)
}
logger.Infof("%s: 猪类型更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪类型更新成功", resp, actionType, "猪类型更新成功", resp)
}
// DeletePigType godoc
// @Summary 删除猪类型
// @Description 根据ID删除猪类型。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param id path int true "猪类型ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Router /api/v1/feed/pig-types/{id} [delete]
func (c *Controller) DeletePigType(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePigType")
const actionType = "删除猪类型"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪类型ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪类型ID格式", actionType, "猪类型ID格式错误", idStr)
}
err = c.feedManagementService.DeletePigType(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除猪类型失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪类型不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除猪类型失败: "+err.Error(), actionType, "服务层删除猪类型失败", id)
}
logger.Infof("%s: 猪类型删除成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪类型删除成功", nil, actionType, "猪类型删除成功", id)
}
// GetPigType godoc
// @Summary 获取猪类型详情
// @Description 根据ID获取单个猪类型的详细信息。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param id path int true "猪类型ID"
// @Success 200 {object} controller.Response{data=dto.PigTypeResponse} "业务码为200代表成功获取"
// @Router /api/v1/feed/pig-types/{id} [get]
func (c *Controller) GetPigType(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPigType")
const actionType = "获取猪类型详情"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪类型ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪类型ID格式", actionType, "猪类型ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetPigType(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取猪类型详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪类型不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪类型详情失败: "+err.Error(), actionType, "服务层获取猪类型详情失败", id)
}
logger.Infof("%s: 获取猪类型详情成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪类型详情成功", resp, actionType, "获取猪类型详情成功", resp)
}
// ListPigTypes godoc
// @Summary 获取猪类型列表
// @Description 获取所有猪类型的列表,支持分页和过滤。
// @Tags 饲料管理
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListPigTypeRequest false "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListPigTypeResponse} "业务码为200代表成功获取列表"
// @Router /api/v1/feed/pig-types [get]
func (c *Controller) ListPigTypes(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigTypes")
const actionType = "获取猪类型列表"
var req dto.ListPigTypeRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListPigTypes(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取猪类型列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪类型列表失败: "+err.Error(), actionType, "服务层获取猪类型列表失败", nil)
}
logger.Infof("%s: 获取猪类型列表成功, 数量: %d", actionType, len(resp.List))
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪类型列表成功", resp, actionType, "获取猪类型列表成功", resp)
}
// UpdatePigTypeNutrientRequirements godoc
// @Summary 全量更新猪类型的营养需求
// @Description 根据猪类型ID替换其所有的营养需求信息。这是一个覆盖操作。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "猪类型ID"
// @Param nutrientRequirements body dto.UpdatePigTypeNutrientRequirementsRequest true "新的营养需求列表"
// @Success 200 {object} controller.Response{data=dto.PigTypeResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/pig-types/{id}/nutrient-requirements [put]
func (c *Controller) UpdatePigTypeNutrientRequirements(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePigTypeNutrientRequirements")
const actionType = "更新猪类型营养需求"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪类型ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪类型ID格式", actionType, "猪类型ID格式错误", idStr)
}
var req dto.UpdatePigTypeNutrientRequirementsRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigTypeNutrientRequirements(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪类型营养需求失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪类型不存在", id)
}
// 这里可以根据未来可能从服务层返回的其他特定错误进行处理
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪类型营养需求失败: "+err.Error(), actionType, "服务层更新失败", req)
}
logger.Infof("%s: 猪类型营养需求更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪类型营养需求更新成功", resp, actionType, "猪类型营养需求更新成功", resp)
}

View File

@@ -0,0 +1,195 @@
package feed
import (
"context"
"errors"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"github.com/labstack/echo/v4"
)
// NutrientController 定义了营养种类相关的控制器
type NutrientController struct {
ctx context.Context
feedManagementService service.FeedManagementService
}
// NewNutrientController 创建一个新的 NutrientController 实例
func NewNutrientController(ctx context.Context, feedManagementService service.FeedManagementService) *NutrientController {
return &NutrientController{
ctx: ctx,
feedManagementService: feedManagementService,
}
}
// CreateNutrient godoc
// @Summary 创建营养种类
// @Description 创建一个新的营养种类。
// @Tags 饲料管理-营养
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param nutrient body dto.CreateNutrientRequest true "营养种类信息"
// @Success 200 {object} controller.Response{data=dto.NutrientResponse} "业务码为201代表创建成功"
// @Router /api/v1/feed/nutrients [post]
func (c *NutrientController) CreateNutrient(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateNutrient")
var req dto.CreateNutrientRequest
const actionType = "创建营养种类"
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreateNutrient(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建营养种类失败: %v", actionType, err)
if errors.Is(err, service.ErrNutrientNameConflict) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "营养种类名称已存在", req)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建营养种类失败: "+err.Error(), actionType, "服务层创建营养种类失败", req)
}
logger.Infof("%s: 营养种类创建成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "营养种类创建成功", resp, actionType, "营养种类创建成功", resp)
}
// UpdateNutrient godoc
// @Summary 更新营养种类
// @Description 根据ID更新营养种类信息。
// @Tags 饲料管理-营养
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "营养种类ID"
// @Param nutrient body dto.UpdateNutrientRequest true "更新后的营养种类信息"
// @Success 200 {object} controller.Response{data=dto.NutrientResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/nutrients/{id} [put]
func (c *NutrientController) UpdateNutrient(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateNutrient")
const actionType = "更新营养种类"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 营养种类ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的营养种类ID格式", actionType, "营养种类ID格式错误", idStr)
}
var req dto.UpdateNutrientRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdateNutrient(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新营养种类失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrNutrientNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "营养种类不存在", id)
}
if errors.Is(err, service.ErrNutrientNameConflict) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "营养种类名称已存在", req)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新营养种类失败: "+err.Error(), actionType, "服务层更新营养种类失败", req)
}
logger.Infof("%s: 营养种类更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "营养种类更新成功", resp, actionType, "营养种类更新成功", resp)
}
// DeleteNutrient godoc
// @Summary 删除营养种类
// @Description 根据ID删除营养种类。
// @Tags 饲料管理-营养
// @Security BearerAuth
// @Produce json
// @Param id path int true "营养种类ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Router /api/v1/feed/nutrients/{id} [delete]
func (c *NutrientController) DeleteNutrient(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteNutrient")
const actionType = "删除营养种类"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 营养种类ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的营养种类ID格式", actionType, "营养种类ID格式错误", idStr)
}
err = c.feedManagementService.DeleteNutrient(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除营养种类失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrNutrientNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "营养种类不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除营养种类失败: "+err.Error(), actionType, "服务层删除营养种类失败", id)
}
logger.Infof("%s: 营养种类删除成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "营养种类删除成功", nil, actionType, "营养种类删除成功", id)
}
// GetNutrient godoc
// @Summary 获取营养种类详情
// @Description 根据ID获取单个营养种类的详细信息。
// @Tags 饲料管理-营养
// @Security BearerAuth
// @Produce json
// @Param id path int true "营养种类ID"
// @Success 200 {object} controller.Response{data=dto.NutrientResponse} "业务码为200代表成功获取"
// @Router /api/v1/feed/nutrients/{id} [get]
func (c *NutrientController) GetNutrient(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetNutrient")
const actionType = "获取营养种类详情"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 营养种类ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的营养种类ID格式", actionType, "营养种类ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetNutrient(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取营养种类详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrNutrientNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "营养种类不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取营养种类详情失败: "+err.Error(), actionType, "服务层获取营养种类详情失败", id)
}
logger.Infof("%s: 获取营养种类详情成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取营养种类详情成功", resp, actionType, "获取营养种类详情成功", resp)
}
// ListNutrients godoc
// @Summary 获取营养种类列表
// @Description 获取所有营养种类的列表,支持分页和过滤。
// @Tags 饲料管理-营养
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListNutrientRequest false "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListNutrientResponse} "业务码为200代表成功获取列表"
// @Router /api/v1/feed/nutrients [get]
func (c *NutrientController) ListNutrients(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListNutrients")
const actionType = "获取营养种类列表"
var req dto.ListNutrientRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListNutrients(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取营养种类列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取营养种类列表失败: "+err.Error(), actionType, "服务层获取营养种类列表失败", nil)
}
logger.Infof("%s: 获取营养种类列表成功, 数量: %d", actionType, len(resp.List))
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取营养种类列表成功", resp, actionType, "获取营养种类列表成功", resp)
}

View File

@@ -0,0 +1,193 @@
package feed
import (
"context"
"errors"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"github.com/labstack/echo/v4"
)
// PigAgeStageController 定义了猪年龄阶段相关的控制器
type PigAgeStageController struct {
ctx context.Context
feedManagementService service.FeedManagementService
}
// NewPigAgeStageController 创建一个新的 PigAgeStageController 实例
func NewPigAgeStageController(ctx context.Context, feedManagementService service.FeedManagementService) *PigAgeStageController {
return &PigAgeStageController{
ctx: ctx,
feedManagementService: feedManagementService,
}
}
// CreatePigAgeStage godoc
// @Summary 创建猪年龄阶段
// @Description 创建一个新的猪年龄阶段。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param pigAgeStage body dto.CreatePigAgeStageRequest true "猪年龄阶段信息"
// @Success 200 {object} controller.Response{data=dto.PigAgeStageResponse} "业务码为201代表创建成功"
// @Router /api/v1/feed/pig-age-stages [post]
func (c *PigAgeStageController) CreatePigAgeStage(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePigAgeStage")
var req dto.CreatePigAgeStageRequest
const actionType = "创建猪年龄阶段"
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreatePigAgeStage(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建猪年龄阶段失败: %v", actionType, err)
// 猪年龄阶段没有名称冲突的领域错误,这里直接返回内部错误
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪年龄阶段失败: "+err.Error(), actionType, "服务层创建猪年龄阶段失败", req)
}
logger.Infof("%s: 猪年龄阶段创建成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "猪年龄阶段创建成功", resp, actionType, "猪年龄阶段创建成功", resp)
}
// UpdatePigAgeStage godoc
// @Summary 更新猪年龄阶段
// @Description 根据ID更新猪年龄阶段信息。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "猪年龄阶段ID"
// @Param pigAgeStage body dto.UpdatePigAgeStageRequest true "更新后的猪年龄阶段信息"
// @Success 200 {object} controller.Response{data=dto.PigAgeStageResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/pig-age-stages/{id} [put]
func (c *PigAgeStageController) UpdatePigAgeStage(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePigAgeStage")
const actionType = "更新猪年龄阶段"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪年龄阶段ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪年龄阶段ID格式", actionType, "猪年龄阶段ID格式错误", idStr)
}
var req dto.UpdatePigAgeStageRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigAgeStage(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪年龄阶段失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigAgeStageNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪年龄阶段不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪年龄阶段失败: "+err.Error(), actionType, "服务层更新猪年龄阶段失败", req)
}
logger.Infof("%s: 猪年龄阶段更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪年龄阶段更新成功", resp, actionType, "猪年龄阶段更新成功", resp)
}
// DeletePigAgeStage godoc
// @Summary 删除猪年龄阶段
// @Description 根据ID删除猪年龄阶段。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Produce json
// @Param id path int true "猪年龄阶段ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Router /api/v1/feed/pig-age-stages/{id} [delete]
func (c *PigAgeStageController) DeletePigAgeStage(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePigAgeStage")
const actionType = "删除猪年龄阶段"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪年龄阶段ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪年龄阶段ID格式", actionType, "猪年龄阶段ID格式错误", idStr)
}
err = c.feedManagementService.DeletePigAgeStage(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除猪年龄阶段失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigAgeStageNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪年龄阶段不存在", id)
}
if errors.Is(err, service.ErrPigAgeStageInUse) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "猪年龄阶段正在被使用", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除猪年龄阶段失败: "+err.Error(), actionType, "服务层删除猪年龄阶段失败", id)
}
logger.Infof("%s: 猪年龄阶段删除成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪年龄阶段删除成功", nil, actionType, "猪年龄阶段删除成功", id)
}
// GetPigAgeStage godoc
// @Summary 获取猪年龄阶段详情
// @Description 根据ID获取单个猪年龄阶段的详细信息。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Produce json
// @Param id path int true "猪年龄阶段ID"
// @Success 200 {object} controller.Response{data=dto.PigAgeStageResponse} "业务码为200代表成功获取"
// @Router /api/v1/feed/pig-age-stages/{id} [get]
func (c *PigAgeStageController) GetPigAgeStage(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPigAgeStage")
const actionType = "获取猪年龄阶段详情"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪年龄阶段ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪年龄阶段ID格式", actionType, "猪年龄阶段ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetPigAgeStage(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取猪年龄阶段详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigAgeStageNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪年龄阶段不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪年龄阶段详情失败: "+err.Error(), actionType, "服务层获取猪年龄阶段详情失败", id)
}
logger.Infof("%s: 获取猪年龄阶段详情成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪年龄阶段详情成功", resp, actionType, "获取猪年龄阶段详情成功", resp)
}
// ListPigAgeStages godoc
// @Summary 获取猪年龄阶段列表
// @Description 获取所有猪年龄阶段的列表,支持分页和过滤。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListPigAgeStageRequest false "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListPigAgeStageResponse} "业务码为200代表成功获取列表"
// @Router /api/v1/feed/pig-age-stages [get]
func (c *PigAgeStageController) ListPigAgeStages(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigAgeStages")
const actionType = "获取猪年龄阶段列表"
var req dto.ListPigAgeStageRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListPigAgeStages(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取猪年龄阶段列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪年龄阶段列表失败: "+err.Error(), actionType, "服务层获取猪年龄阶段列表失败", nil)
}
logger.Infof("%s: 获取猪年龄阶段列表成功, 数量: %d", actionType, len(resp.List))
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪年龄阶段列表成功", resp, actionType, "获取猪年龄阶段列表成功", resp)
}

View File

@@ -0,0 +1,193 @@
package feed
import (
"context"
"errors"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"github.com/labstack/echo/v4"
)
// PigBreedController 定义了猪品种相关的控制器
type PigBreedController struct {
ctx context.Context
feedManagementService service.FeedManagementService
}
// NewPigBreedController 创建一个新的 PigBreedController 实例
func NewPigBreedController(ctx context.Context, feedManagementService service.FeedManagementService) *PigBreedController {
return &PigBreedController{
ctx: ctx,
feedManagementService: feedManagementService,
}
}
// CreatePigBreed godoc
// @Summary 创建猪品种
// @Description 创建一个新的猪品种。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param pigBreed body dto.CreatePigBreedRequest true "猪品种信息"
// @Success 200 {object} controller.Response{data=dto.PigBreedResponse} "业务码为201代表创建成功"
// @Router /api/v1/feed/pig-breeds [post]
func (c *PigBreedController) CreatePigBreed(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePigBreed")
var req dto.CreatePigBreedRequest
const actionType = "创建猪品种"
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreatePigBreed(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建猪品种失败: %v", actionType, err)
// 猪品种没有名称冲突的领域错误,这里直接返回内部错误
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪品种失败: "+err.Error(), actionType, "服务层创建猪品种失败", req)
}
logger.Infof("%s: 猪品种创建成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "猪品种创建成功", resp, actionType, "猪品种创建成功", resp)
}
// UpdatePigBreed godoc
// @Summary 更新猪品种
// @Description 根据ID更新猪品种信息。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "猪品种ID"
// @Param pigBreed body dto.UpdatePigBreedRequest true "更新后的猪品种信息"
// @Success 200 {object} controller.Response{data=dto.PigBreedResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/pig-breeds/{id} [put]
func (c *PigBreedController) UpdatePigBreed(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePigBreed")
const actionType = "更新猪品种"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪品种ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪品种ID格式", actionType, "猪品种ID格式错误", idStr)
}
var req dto.UpdatePigBreedRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigBreed(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪品种失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigBreedNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪品种不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪品种失败: "+err.Error(), actionType, "服务层更新猪品种失败", req)
}
logger.Infof("%s: 猪品种更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪品种更新成功", resp, actionType, "猪品种更新成功", resp)
}
// DeletePigBreed godoc
// @Summary 删除猪品种
// @Description 根据ID删除猪品种。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Produce json
// @Param id path int true "猪品种ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Router /api/v1/feed/pig-breeds/{id} [delete]
func (c *PigBreedController) DeletePigBreed(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePigBreed")
const actionType = "删除猪品种"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪品种ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪品种ID格式", actionType, "猪品种ID格式错误", idStr)
}
err = c.feedManagementService.DeletePigBreed(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除猪品种失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigBreedNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪品种不存在", id)
}
if errors.Is(err, service.ErrPigBreedInUse) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "猪品种正在被使用", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除猪品种失败: "+err.Error(), actionType, "服务层删除猪品种失败", id)
}
logger.Infof("%s: 猪品种删除成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪品种删除成功", nil, actionType, "猪品种删除成功", id)
}
// GetPigBreed godoc
// @Summary 获取猪品种详情
// @Description 根据ID获取单个猪品种的详细信息。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Produce json
// @Param id path int true "猪品种ID"
// @Success 200 {object} controller.Response{data=dto.PigBreedResponse} "业务码为200代表成功获取"
// @Router /api/v1/feed/pig-breeds/{id} [get]
func (c *PigBreedController) GetPigBreed(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPigBreed")
const actionType = "获取猪品种详情"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪品种ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪品种ID格式", actionType, "猪品种ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetPigBreed(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取猪品种详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigBreedNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪品种不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪品种详情失败: "+err.Error(), actionType, "服务层获取猪品种详情失败", id)
}
logger.Infof("%s: 获取猪品种详情成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪品种详情成功", resp, actionType, "获取猪品种详情成功", resp)
}
// ListPigBreeds godoc
// @Summary 获取猪品种列表
// @Description 获取所有猪品种的列表,支持分页和过滤。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListPigBreedRequest false "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListPigBreedResponse} "业务码为200代表成功获取列表"
// @Router /api/v1/feed/pig-breeds [get]
func (c *PigBreedController) ListPigBreeds(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigBreeds")
const actionType = "获取猪品种列表"
var req dto.ListPigBreedRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListPigBreeds(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取猪品种列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪品种列表失败: "+err.Error(), actionType, "服务层获取猪品种列表失败", nil)
}
logger.Infof("%s: 获取猪品种列表成功, 数量: %d", actionType, len(resp.List))
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪品种列表成功", resp, actionType, "获取猪品种列表成功", resp)
}

View File

@@ -0,0 +1,243 @@
package feed
import (
"context"
"errors"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"github.com/labstack/echo/v4"
)
// PigTypeController 定义了猪类型相关的控制器
type PigTypeController struct {
ctx context.Context
feedManagementService service.FeedManagementService
}
// NewPigTypeController 创建一个新的 PigTypeController 实例
func NewPigTypeController(ctx context.Context, feedManagementService service.FeedManagementService) *PigTypeController {
return &PigTypeController{
ctx: ctx,
feedManagementService: feedManagementService,
}
}
// CreatePigType godoc
// @Summary 创建猪类型
// @Description 创建一个新的猪类型。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param pigType body dto.CreatePigTypeRequest true "猪类型信息"
// @Success 200 {object} controller.Response{data=dto.PigTypeResponse} "业务码为201代表创建成功"
// @Router /api/v1/feed/pig-types [post]
func (c *PigTypeController) CreatePigType(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePigType")
var req dto.CreatePigTypeRequest
const actionType = "创建猪类型"
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreatePigType(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建猪类型失败: %v", actionType, err)
if errors.Is(err, service.ErrPigBreedNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "关联猪品种不存在", req)
}
if errors.Is(err, service.ErrPigAgeStageNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "关联猪年龄阶段不存在", req)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪类型失败: "+err.Error(), actionType, "服务层创建猪类型失败", req)
}
logger.Infof("%s: 猪类型创建成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "猪类型创建成功", resp, actionType, "猪类型创建成功", resp)
}
// UpdatePigType godoc
// @Summary 更新猪类型
// @Description 根据ID更新猪类型信息。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "猪类型ID"
// @Param pigType body dto.UpdatePigTypeRequest true "更新后的猪类型信息"
// @Success 200 {object} controller.Response{data=dto.PigTypeResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/pig-types/{id} [put]
func (c *PigTypeController) UpdatePigType(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePigType")
const actionType = "更新猪类型"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪类型ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪类型ID格式", actionType, "猪类型ID格式错误", idStr)
}
var req dto.UpdatePigTypeRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigType(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪类型失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪类型不存在", id)
}
if errors.Is(err, service.ErrPigBreedNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "关联猪品种不存在", req)
}
if errors.Is(err, service.ErrPigAgeStageNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "关联猪年龄阶段不存在", req)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪类型失败: "+err.Error(), actionType, "服务层更新猪类型失败", req)
}
logger.Infof("%s: 猪类型更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪类型更新成功", resp, actionType, "猪类型更新成功", resp)
}
// DeletePigType godoc
// @Summary 删除猪类型
// @Description 根据ID删除猪类型。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Produce json
// @Param id path int true "猪类型ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Router /api/v1/feed/pig-types/{id} [delete]
func (c *PigTypeController) DeletePigType(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePigType")
const actionType = "删除猪类型"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪类型ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪类型ID格式", actionType, "猪类型ID格式错误", idStr)
}
err = c.feedManagementService.DeletePigType(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除猪类型失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪类型不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除猪类型失败: "+err.Error(), actionType, "服务层删除猪类型失败", id)
}
logger.Infof("%s: 猪类型删除成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪类型删除成功", nil, actionType, "猪类型删除成功", id)
}
// GetPigType godoc
// @Summary 获取猪类型详情
// @Description 根据ID获取单个猪类型的详细信息。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Produce json
// @Param id path int true "猪类型ID"
// @Success 200 {object} controller.Response{data=dto.PigTypeResponse} "业务码为200代表成功获取"
// @Router /api/v1/feed/pig-types/{id} [get]
func (c *PigTypeController) GetPigType(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPigType")
const actionType = "获取猪类型详情"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪类型ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪类型ID格式", actionType, "猪类型ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetPigType(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取猪类型详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪类型不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪类型详情失败: "+err.Error(), actionType, "服务层获取猪类型详情失败", id)
}
logger.Infof("%s: 获取猪类型详情成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪类型详情成功", resp, actionType, "获取猪类型详情成功", resp)
}
// ListPigTypes godoc
// @Summary 获取猪类型列表
// @Description 获取所有猪类型的列表,支持分页和过滤。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListPigTypeRequest false "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListPigTypeResponse} "业务码为200代表成功获取列表"
// @Router /api/v1/feed/pig-types [get]
func (c *PigTypeController) ListPigTypes(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigTypes")
const actionType = "获取猪类型列表"
var req dto.ListPigTypeRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListPigTypes(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取猪类型列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪类型列表失败: "+err.Error(), actionType, "服务层获取猪类型列表失败", nil)
}
logger.Infof("%s: 获取猪类型列表成功, 数量: %d", actionType, len(resp.List))
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪类型列表成功", resp, actionType, "获取猪类型列表成功", resp)
}
// UpdatePigTypeNutrientRequirements godoc
// @Summary 全量更新猪类型的营养需求
// @Description 根据猪类型ID替换其所有的营养需求信息。这是一个覆盖操作。
// @Tags 饲料管理-猪
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "猪类型ID"
// @Param nutrientRequirements body dto.UpdatePigTypeNutrientRequirementsRequest true "新的营养需求列表"
// @Success 200 {object} controller.Response{data=dto.PigTypeResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/pig-types/{id}/nutrient-requirements [put]
func (c *PigTypeController) UpdatePigTypeNutrientRequirements(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePigTypeNutrientRequirements")
const actionType = "更新猪类型营养需求"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪类型ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪类型ID格式", actionType, "猪类型ID格式错误", idStr)
}
var req dto.UpdatePigTypeNutrientRequirementsRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigTypeNutrientRequirements(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪类型营养需求失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪类型不存在", id)
}
// 这里可以根据未来可能从服务层返回的其他特定错误进行处理
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪类型营养需求失败: "+err.Error(), actionType, "服务层更新失败", req)
}
logger.Infof("%s: 猪类型营养需求更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪类型营养需求更新成功", resp, actionType, "猪类型营养需求更新成功", resp)
}

View File

@@ -0,0 +1,237 @@
package feed
import (
"context"
"errors"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"github.com/labstack/echo/v4"
)
// RawMaterialController 定义了原料相关的控制器
type RawMaterialController struct {
ctx context.Context
feedManagementService service.FeedManagementService
}
// NewRawMaterialController 创建一个新的 RawMaterialController 实例
func NewRawMaterialController(ctx context.Context, feedManagementService service.FeedManagementService) *RawMaterialController {
return &RawMaterialController{
ctx: ctx,
feedManagementService: feedManagementService,
}
}
// CreateRawMaterial godoc
// @Summary 创建原料
// @Description 创建一个新的原料。
// @Tags 饲料管理-原料
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param rawMaterial body dto.CreateRawMaterialRequest true "原料信息"
// @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为201代表创建成功"
// @Router /api/v1/feed/raw-materials [post]
func (c *RawMaterialController) CreateRawMaterial(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateRawMaterial")
var req dto.CreateRawMaterialRequest
const actionType = "创建原料"
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreateRawMaterial(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建原料失败: %v", actionType, err)
if errors.Is(err, service.ErrRawMaterialNameConflict) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "原料名称已存在", req)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建原料失败: "+err.Error(), actionType, "服务层创建原料失败", req)
}
logger.Infof("%s: 原料创建成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "原料创建成功", resp, actionType, "原料创建成功", resp)
}
// UpdateRawMaterial godoc
// @Summary 更新原料
// @Description 根据ID更新原料信息。
// @Tags 饲料管理-原料
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "原料ID"
// @Param rawMaterial body dto.UpdateRawMaterialRequest true "更新后的原料信息"
// @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/raw-materials/{id} [put]
func (c *RawMaterialController) UpdateRawMaterial(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateRawMaterial")
const actionType = "更新原料"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 原料ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的原料ID格式", actionType, "原料ID格式错误", idStr)
}
var req dto.UpdateRawMaterialRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdateRawMaterial(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新原料失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrRawMaterialNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "原料不存在", id)
}
if errors.Is(err, service.ErrRawMaterialNameConflict) {
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "原料名称已存在", req)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新原料失败: "+err.Error(), actionType, "服务层更新原料失败", req)
}
logger.Infof("%s: 原料更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "原料更新成功", resp, actionType, "原料更新成功", resp)
}
// DeleteRawMaterial godoc
// @Summary 删除原料
// @Description 根据ID删除原料。
// @Tags 饲料管理-原料
// @Security BearerAuth
// @Produce json
// @Param id path int true "原料ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Router /api/v1/feed/raw-materials/{id} [delete]
func (c *RawMaterialController) DeleteRawMaterial(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteRawMaterial")
const actionType = "删除原料"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 原料ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的原料ID格式", actionType, "原料ID格式错误", idStr)
}
err = c.feedManagementService.DeleteRawMaterial(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除原料失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrRawMaterialNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "原料不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除原料失败: "+err.Error(), actionType, "服务层删除原料失败", id)
}
logger.Infof("%s: 原料删除成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "原料删除成功", nil, actionType, "原料删除成功", id)
}
// GetRawMaterial godoc
// @Summary 获取原料详情
// @Description 根据ID获取单个原料的详细信息。
// @Tags 饲料管理-原料
// @Security BearerAuth
// @Produce json
// @Param id path int true "原料ID"
// @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为200代表成功获取"
// @Router /api/v1/feed/raw-materials/{id} [get]
func (c *RawMaterialController) GetRawMaterial(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetRawMaterial")
const actionType = "获取原料详情"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 原料ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的原料ID格式", actionType, "原料ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetRawMaterial(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取原料详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrRawMaterialNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "原料不存在", id)
}
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取原料详情失败: "+err.Error(), actionType, "服务层获取原料详情失败", id)
}
logger.Infof("%s: 获取原料详情成功, ID: %d", actionType, id)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取原料详情成功", resp, actionType, "获取原料详情成功", resp)
}
// ListRawMaterials godoc
// @Summary 获取原料列表
// @Description 获取所有原料的列表,支持分页和过滤。
// @Tags 饲料管理-原料
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListRawMaterialRequest false "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListRawMaterialResponse} "业务码为200代表成功获取列表"
// @Router /api/v1/feed/raw-materials [get]
func (c *RawMaterialController) ListRawMaterials(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListRawMaterials")
const actionType = "获取原料列表"
var req dto.ListRawMaterialRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 查询参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListRawMaterials(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取原料列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取原料列表失败: "+err.Error(), actionType, "服务层获取原料列表失败", nil)
}
logger.Infof("%s: 获取原料列表成功, 数量: %d", actionType, len(resp.List))
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取原料列表成功", resp, actionType, "获取原料列表成功", resp)
}
// UpdateRawMaterialNutrients godoc
// @Summary 全量更新原料的营养成分
// @Description 根据原料ID替换其所有的营养成分信息。这是一个覆盖操作。
// @Tags 饲料管理-原料
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "原料ID"
// @Param nutrients body dto.UpdateRawMaterialNutrientsRequest true "新的营养成分列表"
// @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/raw-materials/{id}/nutrients [put]
func (c *RawMaterialController) UpdateRawMaterialNutrients(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateRawMaterialNutrients")
const actionType = "更新原料营养成分"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 原料ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的原料ID格式", actionType, "原料ID格式错误", idStr)
}
var req dto.UpdateRawMaterialNutrientsRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdateRawMaterialNutrients(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新原料营养成分失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrRawMaterialNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "原料不存在", id)
}
// 这里可以根据未来可能从服务层返回的其他特定错误进行处理
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新原料营养成分失败: "+err.Error(), actionType, "服务层更新失败", req)
}
logger.Infof("%s: 原料营养成分更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "原料营养成分更新成功", resp, actionType, "原料营养成分更新成功", resp)
}

View File

@@ -49,6 +49,11 @@ internal/app/controller/alarm/threshold_alarm_controller.go
internal/app/controller/auth_utils.go internal/app/controller/auth_utils.go
internal/app/controller/device/device_controller.go internal/app/controller/device/device_controller.go
internal/app/controller/feed/feed_controller.go internal/app/controller/feed/feed_controller.go
internal/app/controller/feed/nutrient_controller.go
internal/app/controller/feed/pig_age_stage_controller.go
internal/app/controller/feed/pig_breed_controller.go
internal/app/controller/feed/pig_type_controller.go
internal/app/controller/feed/raw_material_controller.go
internal/app/controller/health/health_controller.go internal/app/controller/health/health_controller.go
internal/app/controller/management/controller_helpers.go internal/app/controller/management/controller_helpers.go
internal/app/controller/management/pig_batch_controller.go internal/app/controller/management/pig_batch_controller.go
@@ -111,6 +116,12 @@ internal/domain/plan/analysis_plan_task_manager.go
internal/domain/plan/plan_execution_manager.go internal/domain/plan/plan_execution_manager.go
internal/domain/plan/plan_service.go internal/domain/plan/plan_service.go
internal/domain/plan/task.go internal/domain/plan/task.go
internal/domain/recipe/nutrient_service.go
internal/domain/recipe/pig_age_stage_service.go
internal/domain/recipe/pig_breed_service.go
internal/domain/recipe/pig_type_service.go
internal/domain/recipe/raw_material_service.go
internal/domain/recipe/recipe_core_service.go
internal/domain/recipe/recipe_service.go internal/domain/recipe/recipe_service.go
internal/domain/task/alarm_notification_task.go internal/domain/task/alarm_notification_task.go
internal/domain/task/area_threshold_check_task.go internal/domain/task/area_threshold_check_task.go