配方增删改查服务层和控制器
This commit is contained in:
@@ -198,3 +198,84 @@ func ConvertPigTypeListToDTO(pigTypes []models.PigType, total int64, page, pageS
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ConvertRecipeToDto 将 models.Recipe 转换为 RecipeResponse DTO
|
||||
func ConvertRecipeToDto(recipe *models.Recipe) *RecipeResponse {
|
||||
if recipe == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
ingredients := make([]RecipeIngredientDto, len(recipe.RecipeIngredients))
|
||||
for i, ri := range recipe.RecipeIngredients {
|
||||
ingredients[i] = RecipeIngredientDto{
|
||||
RawMaterialID: ri.RawMaterialID,
|
||||
Percentage: ri.Percentage,
|
||||
}
|
||||
}
|
||||
|
||||
return &RecipeResponse{
|
||||
ID: recipe.ID,
|
||||
Name: recipe.Name,
|
||||
Description: recipe.Description,
|
||||
RecipeIngredients: ingredients,
|
||||
}
|
||||
}
|
||||
|
||||
// ConvertRecipeListToDTO 将 []models.Recipe 转换为 ListRecipeResponse DTO
|
||||
func ConvertRecipeListToDTO(recipes []models.Recipe, total int64, page, pageSize int) *ListRecipeResponse {
|
||||
recipeDTOs := make([]RecipeResponse, len(recipes))
|
||||
for i, r := range recipes {
|
||||
recipeDTOs[i] = *ConvertRecipeToDto(&r)
|
||||
}
|
||||
|
||||
return &ListRecipeResponse{
|
||||
List: recipeDTOs,
|
||||
Pagination: PaginationDTO{
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
Total: total,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ConvertCreateRecipeRequestToModel 将 CreateRecipeRequest DTO 转换为 models.Recipe 模型
|
||||
func ConvertCreateRecipeRequestToModel(req *CreateRecipeRequest) *models.Recipe {
|
||||
if req == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
ingredients := make([]models.RecipeIngredient, len(req.RecipeIngredients))
|
||||
for i, ri := range req.RecipeIngredients {
|
||||
ingredients[i] = models.RecipeIngredient{
|
||||
RawMaterialID: ri.RawMaterialID,
|
||||
Percentage: ri.Percentage,
|
||||
}
|
||||
}
|
||||
|
||||
return &models.Recipe{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
RecipeIngredients: ingredients,
|
||||
}
|
||||
}
|
||||
|
||||
// ConvertUpdateRecipeRequestToModel 将 UpdateRecipeRequest DTO 转换为 models.Recipe 模型
|
||||
func ConvertUpdateRecipeRequestToModel(req *UpdateRecipeRequest) *models.Recipe {
|
||||
if req == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
ingredients := make([]models.RecipeIngredient, len(req.RecipeIngredients))
|
||||
for i, ri := range req.RecipeIngredients {
|
||||
ingredients[i] = models.RecipeIngredient{
|
||||
RawMaterialID: ri.RawMaterialID,
|
||||
Percentage: ri.Percentage,
|
||||
}
|
||||
}
|
||||
|
||||
return &models.Recipe{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
RecipeIngredients: ingredients,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,3 +274,49 @@ type PigNutrientRequirementItem struct {
|
||||
MinRequirement float32 `json:"min_requirement" validate:"gte=0"` // 最低营养需求量
|
||||
MaxRequirement float32 `json:"max_requirement" validate:"gte=0"` // 最高营养需求量
|
||||
}
|
||||
|
||||
// =============================================================================================================
|
||||
// 配方 (Recipe) 相关 DTO
|
||||
// =============================================================================================================
|
||||
|
||||
// RecipeIngredientDto 代表配方中的一个原料及其百分比
|
||||
type RecipeIngredientDto struct {
|
||||
RawMaterialID uint32 `json:"raw_material_id" validate:"required"` // 原料ID
|
||||
Percentage float32 `json:"percentage" validate:"gte=0,lte=1"` // 原料在配方中的百分比 (0-1之间)
|
||||
}
|
||||
|
||||
// CreateRecipeRequest 创建配方的请求体
|
||||
type CreateRecipeRequest struct {
|
||||
Name string `json:"name" validate:"required,max=100"` // 配方名称
|
||||
Description string `json:"description" validate:"max=255"` // 配方描述
|
||||
RecipeIngredients []RecipeIngredientDto `json:"recipe_ingredients" validate:"dive"` // 配方原料组成
|
||||
}
|
||||
|
||||
// UpdateRecipeRequest 更新配方的请求体
|
||||
type UpdateRecipeRequest struct {
|
||||
Name string `json:"name" validate:"required,max=100"` // 配方名称
|
||||
Description string `json:"description" validate:"max=255"` // 配方描述
|
||||
RecipeIngredients []RecipeIngredientDto `json:"recipe_ingredients" validate:"dive"` // 配方原料组成
|
||||
}
|
||||
|
||||
// RecipeResponse 配方响应体
|
||||
type RecipeResponse struct {
|
||||
ID uint32 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
RecipeIngredients []RecipeIngredientDto `json:"recipe_ingredients"`
|
||||
}
|
||||
|
||||
// ListRecipeRequest 定义了获取配方列表的请求参数
|
||||
type ListRecipeRequest struct {
|
||||
Page int `json:"page" query:"page"` // 页码
|
||||
PageSize int `json:"page_size" query:"page_size"` // 每页数量
|
||||
Name *string `json:"name" query:"name"` // 按名称模糊查询
|
||||
OrderBy string `json:"order_by" query:"order_by"` // 排序字段,例如 "id DESC"
|
||||
}
|
||||
|
||||
// ListRecipeResponse 是获取配方列表的响应结构
|
||||
type ListRecipeResponse struct {
|
||||
List []RecipeResponse `json:"list"`
|
||||
Pagination PaginationDTO `json:"pagination"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user