重构配方类服务层

This commit is contained in:
2025-11-23 15:16:45 +08:00
parent 1b2e211bfa
commit 1200f36d14
16 changed files with 815 additions and 705 deletions

View File

@@ -1,21 +0,0 @@
package feed
import (
"context"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
)
// Controller 定义了饲料管理相关的控制器,作为各个子控制器的入口
type Controller struct {
ctx context.Context
feedManagementService service.FeedManagementService
}
// NewController 创建一个新的 Controller 实例
func NewController(ctx context.Context, feedManagementService service.FeedManagementService) *Controller {
return &Controller{
ctx: ctx,
feedManagementService: feedManagementService,
}
}

View File

@@ -15,15 +15,15 @@ import (
// NutrientController 定义了营养种类相关的控制器
type NutrientController struct {
ctx context.Context
feedManagementService service.FeedManagementService
ctx context.Context
nutrientService service.NutrientService
}
// NewNutrientController 创建一个新的 NutrientController 实例
func NewNutrientController(ctx context.Context, feedManagementService service.FeedManagementService) *NutrientController {
func NewNutrientController(ctx context.Context, feedManagementService service.NutrientService) *NutrientController {
return &NutrientController{
ctx: ctx,
feedManagementService: feedManagementService,
ctx: ctx,
nutrientService: feedManagementService,
}
}
@@ -46,7 +46,7 @@ func (c *NutrientController) CreateNutrient(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreateNutrient(reqCtx, &req)
resp, err := c.nutrientService.CreateNutrient(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建营养种类失败: %v", actionType, err)
if errors.Is(err, service.ErrNutrientNameConflict) {
@@ -86,7 +86,7 @@ func (c *NutrientController) UpdateNutrient(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdateNutrient(reqCtx, uint32(id), &req)
resp, err := c.nutrientService.UpdateNutrient(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新营养种类失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrNutrientNotFound) {
@@ -121,7 +121,7 @@ func (c *NutrientController) DeleteNutrient(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的营养种类ID格式", actionType, "营养种类ID格式错误", idStr)
}
err = c.feedManagementService.DeleteNutrient(reqCtx, uint32(id))
err = c.nutrientService.DeleteNutrient(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除营养种类失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrNutrientNotFound) {
@@ -153,7 +153,7 @@ func (c *NutrientController) GetNutrient(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的营养种类ID格式", actionType, "营养种类ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetNutrient(reqCtx, uint32(id))
resp, err := c.nutrientService.GetNutrient(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取营养种类详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrNutrientNotFound) {
@@ -184,7 +184,7 @@ func (c *NutrientController) ListNutrients(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListNutrients(reqCtx, &req)
resp, err := c.nutrientService.ListNutrients(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取营养种类列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取营养种类列表失败: "+err.Error(), actionType, "服务层获取营养种类列表失败", nil)

View File

@@ -15,15 +15,15 @@ import (
// PigAgeStageController 定义了猪年龄阶段相关的控制器
type PigAgeStageController struct {
ctx context.Context
feedManagementService service.FeedManagementService
ctx context.Context
pigAgeStageService service.PigAgeStageService
}
// NewPigAgeStageController 创建一个新的 PigAgeStageController 实例
func NewPigAgeStageController(ctx context.Context, feedManagementService service.FeedManagementService) *PigAgeStageController {
func NewPigAgeStageController(ctx context.Context, feedManagementService service.PigAgeStageService) *PigAgeStageController {
return &PigAgeStageController{
ctx: ctx,
feedManagementService: feedManagementService,
ctx: ctx,
pigAgeStageService: feedManagementService,
}
}
@@ -46,7 +46,7 @@ func (c *PigAgeStageController) CreatePigAgeStage(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreatePigAgeStage(reqCtx, &req)
resp, err := c.pigAgeStageService.CreatePigAgeStage(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建猪年龄阶段失败: %v", actionType, err)
// 猪年龄阶段没有名称冲突的领域错误,这里直接返回内部错误
@@ -84,7 +84,7 @@ func (c *PigAgeStageController) UpdatePigAgeStage(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigAgeStage(reqCtx, uint32(id), &req)
resp, err := c.pigAgeStageService.UpdatePigAgeStage(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪年龄阶段失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigAgeStageNotFound) {
@@ -116,7 +116,7 @@ func (c *PigAgeStageController) DeletePigAgeStage(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪年龄阶段ID格式", actionType, "猪年龄阶段ID格式错误", idStr)
}
err = c.feedManagementService.DeletePigAgeStage(reqCtx, uint32(id))
err = c.pigAgeStageService.DeletePigAgeStage(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除猪年龄阶段失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigAgeStageNotFound) {
@@ -151,7 +151,7 @@ func (c *PigAgeStageController) GetPigAgeStage(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪年龄阶段ID格式", actionType, "猪年龄阶段ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetPigAgeStage(reqCtx, uint32(id))
resp, err := c.pigAgeStageService.GetPigAgeStage(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取猪年龄阶段详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigAgeStageNotFound) {
@@ -182,7 +182,7 @@ func (c *PigAgeStageController) ListPigAgeStages(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListPigAgeStages(reqCtx, &req)
resp, err := c.pigAgeStageService.ListPigAgeStages(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取猪年龄阶段列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪年龄阶段列表失败: "+err.Error(), actionType, "服务层获取猪年龄阶段列表失败", nil)

View File

@@ -15,15 +15,15 @@ import (
// PigBreedController 定义了猪品种相关的控制器
type PigBreedController struct {
ctx context.Context
feedManagementService service.FeedManagementService
ctx context.Context
pigBreedService service.PigBreedService
}
// NewPigBreedController 创建一个新的 PigBreedController 实例
func NewPigBreedController(ctx context.Context, feedManagementService service.FeedManagementService) *PigBreedController {
func NewPigBreedController(ctx context.Context, feedManagementService service.PigBreedService) *PigBreedController {
return &PigBreedController{
ctx: ctx,
feedManagementService: feedManagementService,
ctx: ctx,
pigBreedService: feedManagementService,
}
}
@@ -46,7 +46,7 @@ func (c *PigBreedController) CreatePigBreed(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreatePigBreed(reqCtx, &req)
resp, err := c.pigBreedService.CreatePigBreed(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建猪品种失败: %v", actionType, err)
// 猪品种没有名称冲突的领域错误,这里直接返回内部错误
@@ -84,7 +84,7 @@ func (c *PigBreedController) UpdatePigBreed(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigBreed(reqCtx, uint32(id), &req)
resp, err := c.pigBreedService.UpdatePigBreed(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪品种失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigBreedNotFound) {
@@ -116,7 +116,7 @@ func (c *PigBreedController) DeletePigBreed(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪品种ID格式", actionType, "猪品种ID格式错误", idStr)
}
err = c.feedManagementService.DeletePigBreed(reqCtx, uint32(id))
err = c.pigBreedService.DeletePigBreed(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除猪品种失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigBreedNotFound) {
@@ -151,7 +151,7 @@ func (c *PigBreedController) GetPigBreed(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪品种ID格式", actionType, "猪品种ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetPigBreed(reqCtx, uint32(id))
resp, err := c.pigBreedService.GetPigBreed(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取猪品种详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigBreedNotFound) {
@@ -182,7 +182,7 @@ func (c *PigBreedController) ListPigBreeds(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListPigBreeds(reqCtx, &req)
resp, err := c.pigBreedService.ListPigBreeds(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取猪品种列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪品种列表失败: "+err.Error(), actionType, "服务层获取猪品种列表失败", nil)

View File

@@ -15,15 +15,15 @@ import (
// PigTypeController 定义了猪类型相关的控制器
type PigTypeController struct {
ctx context.Context
feedManagementService service.FeedManagementService
ctx context.Context
typeService service.PigTypeService
}
// NewPigTypeController 创建一个新的 PigTypeController 实例
func NewPigTypeController(ctx context.Context, feedManagementService service.FeedManagementService) *PigTypeController {
func NewPigTypeController(ctx context.Context, feedManagementService service.PigTypeService) *PigTypeController {
return &PigTypeController{
ctx: ctx,
feedManagementService: feedManagementService,
ctx: ctx,
typeService: feedManagementService,
}
}
@@ -46,7 +46,7 @@ func (c *PigTypeController) CreatePigType(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreatePigType(reqCtx, &req)
resp, err := c.typeService.CreatePigType(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建猪类型失败: %v", actionType, err)
if errors.Is(err, service.ErrPigBreedNotFound) {
@@ -89,7 +89,7 @@ func (c *PigTypeController) UpdatePigType(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigType(reqCtx, uint32(id), &req)
resp, err := c.typeService.UpdatePigType(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪类型失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {
@@ -127,7 +127,7 @@ func (c *PigTypeController) DeletePigType(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪类型ID格式", actionType, "猪类型ID格式错误", idStr)
}
err = c.feedManagementService.DeletePigType(reqCtx, uint32(id))
err = c.typeService.DeletePigType(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除猪类型失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {
@@ -159,7 +159,7 @@ func (c *PigTypeController) GetPigType(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪类型ID格式", actionType, "猪类型ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetPigType(reqCtx, uint32(id))
resp, err := c.typeService.GetPigType(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取猪类型详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {
@@ -190,7 +190,7 @@ func (c *PigTypeController) ListPigTypes(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListPigTypes(reqCtx, &req)
resp, err := c.typeService.ListPigTypes(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取猪类型列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪类型列表失败: "+err.Error(), actionType, "服务层获取猪类型列表失败", nil)
@@ -228,7 +228,7 @@ func (c *PigTypeController) UpdatePigTypeNutrientRequirements(ctx echo.Context)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigTypeNutrientRequirements(reqCtx, uint32(id), &req)
resp, err := c.typeService.UpdatePigTypeNutrientRequirements(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪类型营养需求失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {

View File

@@ -15,15 +15,15 @@ import (
// RawMaterialController 定义了原料相关的控制器
type RawMaterialController struct {
ctx context.Context
feedManagementService service.FeedManagementService
ctx context.Context
rawMaterialService service.RawMaterialService
}
// NewRawMaterialController 创建一个新的 RawMaterialController 实例
func NewRawMaterialController(ctx context.Context, feedManagementService service.FeedManagementService) *RawMaterialController {
func NewRawMaterialController(ctx context.Context, feedManagementService service.RawMaterialService) *RawMaterialController {
return &RawMaterialController{
ctx: ctx,
feedManagementService: feedManagementService,
ctx: ctx,
rawMaterialService: feedManagementService,
}
}
@@ -46,7 +46,7 @@ func (c *RawMaterialController) CreateRawMaterial(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.CreateRawMaterial(reqCtx, &req)
resp, err := c.rawMaterialService.CreateRawMaterial(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层创建原料失败: %v", actionType, err)
if errors.Is(err, service.ErrRawMaterialNameConflict) {
@@ -86,7 +86,7 @@ func (c *RawMaterialController) UpdateRawMaterial(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdateRawMaterial(reqCtx, uint32(id), &req)
resp, err := c.rawMaterialService.UpdateRawMaterial(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新原料失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrRawMaterialNotFound) {
@@ -121,7 +121,7 @@ func (c *RawMaterialController) DeleteRawMaterial(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的原料ID格式", actionType, "原料ID格式错误", idStr)
}
err = c.feedManagementService.DeleteRawMaterial(reqCtx, uint32(id))
err = c.rawMaterialService.DeleteRawMaterial(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层删除原料失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrRawMaterialNotFound) {
@@ -153,7 +153,7 @@ func (c *RawMaterialController) GetRawMaterial(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的原料ID格式", actionType, "原料ID格式错误", idStr)
}
resp, err := c.feedManagementService.GetRawMaterial(reqCtx, uint32(id))
resp, err := c.rawMaterialService.GetRawMaterial(reqCtx, uint32(id))
if err != nil {
logger.Errorf("%s: 服务层获取原料详情失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrRawMaterialNotFound) {
@@ -184,7 +184,7 @@ func (c *RawMaterialController) ListRawMaterials(ctx echo.Context) error {
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "查询参数绑定失败", req)
}
resp, err := c.feedManagementService.ListRawMaterials(reqCtx, &req)
resp, err := c.rawMaterialService.ListRawMaterials(reqCtx, &req)
if err != nil {
logger.Errorf("%s: 服务层获取原料列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取原料列表失败: "+err.Error(), actionType, "服务层获取原料列表失败", nil)
@@ -222,7 +222,7 @@ func (c *RawMaterialController) UpdateRawMaterialNutrients(ctx echo.Context) err
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdateRawMaterialNutrients(reqCtx, uint32(id), &req)
resp, err := c.rawMaterialService.UpdateRawMaterialNutrients(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新原料营养成分失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrRawMaterialNotFound) {