实现修改原料营养信息

This commit is contained in:
2025-11-22 16:44:22 +08:00
parent f81635f997
commit 9aea487537
11 changed files with 411 additions and 165 deletions

View File

@@ -22,8 +22,6 @@ func ConvertNutrientToDTO(nutrient *models.Nutrient) *NutrientResponse {
return &NutrientResponse{
ID: nutrient.ID,
CreatedAt: nutrient.CreatedAt,
UpdatedAt: nutrient.UpdatedAt,
Name: nutrient.Name,
Description: nutrient.Description,
RawMaterials: rawMaterials,
@@ -57,8 +55,6 @@ func ConvertRawMaterialToDTO(rm *models.RawMaterial) *RawMaterialResponse {
for i, rmn := range rm.RawMaterialNutrients {
rawMaterialNutrientDTOs[i] = RawMaterialNutrientDTO{
ID: rmn.ID,
CreatedAt: rmn.CreatedAt,
UpdatedAt: rmn.UpdatedAt,
NutrientID: rmn.NutrientID,
Nutrient: rmn.Nutrient.Name, // 假设 Nutrient 已经被预加载
Value: rmn.Value,
@@ -67,8 +63,6 @@ func ConvertRawMaterialToDTO(rm *models.RawMaterial) *RawMaterialResponse {
return &RawMaterialResponse{
ID: rm.ID,
CreatedAt: rm.CreatedAt,
UpdatedAt: rm.UpdatedAt,
Name: rm.Name,
Description: rm.Description,
RawMaterialNutrients: rawMaterialNutrientDTOs,
@@ -99,8 +93,6 @@ func ConvertPigBreedToDTO(breed *models.PigBreed) *PigBreedResponse {
}
return &PigBreedResponse{
ID: breed.ID,
CreatedAt: breed.CreatedAt,
UpdatedAt: breed.UpdatedAt,
Name: breed.Name,
Description: breed.Description,
ParentInfo: breed.ParentInfo,
@@ -134,8 +126,6 @@ func ConvertPigAgeStageToDTO(ageStage *models.PigAgeStage) *PigAgeStageResponse
}
return &PigAgeStageResponse{
ID: ageStage.ID,
CreatedAt: ageStage.CreatedAt,
UpdatedAt: ageStage.UpdatedAt,
Name: ageStage.Name,
Description: ageStage.Description,
}
@@ -168,8 +158,6 @@ func ConvertPigTypeToDTO(pt *models.PigType) *PigTypeResponse {
for i, pnr := range pt.PigNutrientRequirements {
pigNutrientRequirementDTOs[i] = PigNutrientRequirementDTO{
ID: pnr.ID,
CreatedAt: pnr.CreatedAt,
UpdatedAt: pnr.UpdatedAt,
NutrientID: pnr.NutrientID,
NutrientName: pnr.Nutrient.Name, // 假设 Nutrient 已经被预加载
MinRequirement: pnr.MinRequirement,
@@ -179,8 +167,6 @@ func ConvertPigTypeToDTO(pt *models.PigType) *PigTypeResponse {
return &PigTypeResponse{
ID: pt.ID,
CreatedAt: pt.CreatedAt,
UpdatedAt: pt.UpdatedAt,
BreedID: pt.BreedID,
BreedName: pt.Breed.Name, // 假设 Breed 已经被预加载
AgeStageID: pt.AgeStageID,

View File

@@ -1,9 +1,5 @@
package dto
import (
"time"
)
// =============================================================================================================
// 营养种类 (Nutrient) 相关 DTO
// =============================================================================================================
@@ -30,8 +26,6 @@ type NutrientRawMaterialDTO struct {
// NutrientResponse 营养种类响应体
type NutrientResponse struct {
ID uint32 `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Description string `json:"description"`
RawMaterials []NutrientRawMaterialDTO `json:"raw_materials"` // 包含此营养的原料列表
@@ -70,19 +64,15 @@ type UpdateRawMaterialRequest struct {
// RawMaterialNutrientDTO 原料营养素响应体
type RawMaterialNutrientDTO struct {
ID uint32 `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
NutrientID uint32 `json:"nutrient_id"`
Nutrient string `json:"nutrient_name"` // 营养素名称
Value float32 `json:"value"` // 营养价值含量
ID uint32 `json:"id"`
NutrientID uint32 `json:"nutrient_id"`
Nutrient string `json:"nutrient_name"` // 营养素名称
Value float32 `json:"value"` // 营养价值含量
}
// RawMaterialResponse 原料响应体
type RawMaterialResponse struct {
ID uint32 `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Description string `json:"description"`
RawMaterialNutrients []RawMaterialNutrientDTO `json:"raw_material_nutrients"` // 关联的营养素信息
@@ -103,6 +93,17 @@ type ListRawMaterialResponse struct {
Pagination PaginationDTO `json:"pagination"`
}
// UpdateRawMaterialNutrientsRequest 更新原料营养成分的请求体
type UpdateRawMaterialNutrientsRequest struct {
Nutrients []RawMaterialNutrientItem `json:"nutrients" validate:"required,dive"`
}
// RawMaterialNutrientItem 代表一个营养成分及其含量
type RawMaterialNutrientItem struct {
NutrientID uint32 `json:"nutrient_id" validate:"required"` // 营养素ID
Value float32 `json:"value" validate:"gte=0"` // 含量值必须大于等于0
}
// =============================================================================================================
// 猪品种 (PigBreed) 相关 DTO
// =============================================================================================================
@@ -129,15 +130,13 @@ type UpdatePigBreedRequest struct {
// PigBreedResponse 猪品种响应体
type PigBreedResponse struct {
ID uint32 `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Description string `json:"description"`
ParentInfo string `json:"parent_info"`
AppearanceFeatures string `json:"appearance_features"`
BreedAdvantages string `json:"breed_advantages"`
BreedDisadvantages string `json:"breed_disadvantages"`
ID uint32 `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
ParentInfo string `json:"parent_info"`
AppearanceFeatures string `json:"appearance_features"`
BreedAdvantages string `json:"breed_advantages"`
BreedDisadvantages string `json:"breed_disadvantages"`
}
// ListPigBreedRequest 定义了获取猪品种列表的请求参数
@@ -172,11 +171,9 @@ type UpdatePigAgeStageRequest struct {
// PigAgeStageResponse 猪年龄阶段响应体
type PigAgeStageResponse struct {
ID uint32 `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Description string `json:"description"`
ID uint32 `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
// ListPigAgeStageRequest 定义了获取猪年龄阶段列表的请求参数
@@ -225,20 +222,16 @@ type UpdatePigTypeRequest struct {
// PigNutrientRequirementDTO 猪营养需求响应体
type PigNutrientRequirementDTO struct {
ID uint32 `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
NutrientID uint32 `json:"nutrient_id"`
NutrientName string `json:"nutrient_name"` // 营养素名称
MinRequirement float32 `json:"min_requirement"` // 最低营养需求量
MaxRequirement float32 `json:"max_requirement"` // 最高营养需求量
ID uint32 `json:"id"`
NutrientID uint32 `json:"nutrient_id"`
NutrientName string `json:"nutrient_name"` // 营养素名称
MinRequirement float32 `json:"min_requirement"` // 最低营养需求量
MaxRequirement float32 `json:"max_requirement"` // 最高营养需求量
}
// PigTypeResponse 猪类型响应体
type PigTypeResponse struct {
ID uint32 `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BreedID uint32 `json:"breed_id"`
BreedName string `json:"breed_name"` // 猪品种名称
AgeStageID uint32 `json:"age_stage_id"`