原料模型增加参考价
This commit is contained in:
@@ -1,4 +1,10 @@
|
|||||||
# 资源地址
|
# 资源地址
|
||||||
|
|
||||||
1. 你可以访问 http://localhost:8080/ 进入我的前端界面, 前端项目是另一个项目, 但接入的是当前项目对应的后端平台, 如果需要登录账号密码都是huang
|
1. 你可以访问 http://localhost:8080/ 进入我的前端界面, 前端项目是另一个项目, 但接入的是当前项目对应的后端平台, 如果需要登录账号密码都是huang
|
||||||
2. 项目根目录有project_structure.txt, 你需要先阅读此文件了解项目目录结构
|
2. 你可以阅读 config/config.yml 了解我的配置信息, 包括数据库的连接地址和账号密码, 本平台监听的端口等, 后端的swagger界面在 http://localhost:8086/swagger/index.html
|
||||||
|
3. 项目根目录有project_structure.txt, 你需要先阅读此文件了解项目目录结构
|
||||||
|
|
||||||
|
# 权限管理
|
||||||
|
|
||||||
|
1. 我授权你执行数据库的所有查询类sql
|
||||||
|
2. 我授权你操作浏览器访问我的项目swagger文档地址和前端项目, 并允许你进行任何操作
|
||||||
@@ -34,7 +34,7 @@ func NewRawMaterialController(ctx context.Context, feedManagementService service
|
|||||||
// @Security BearerAuth
|
// @Security BearerAuth
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param rawMaterial body dto.CreateRawMaterialRequest true "原料信息"
|
// @Param rawMaterial body dto.CreateRawMaterialRequest true "原料信息,包含名称、描述和参考价格"
|
||||||
// @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为201代表创建成功"
|
// @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为201代表创建成功"
|
||||||
// @Router /api/v1/feed/raw-materials [post]
|
// @Router /api/v1/feed/raw-materials [post]
|
||||||
func (c *RawMaterialController) CreateRawMaterial(ctx echo.Context) error {
|
func (c *RawMaterialController) CreateRawMaterial(ctx echo.Context) error {
|
||||||
@@ -67,7 +67,7 @@ func (c *RawMaterialController) CreateRawMaterial(ctx echo.Context) error {
|
|||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param id path int true "原料ID"
|
// @Param id path int true "原料ID"
|
||||||
// @Param rawMaterial body dto.UpdateRawMaterialRequest true "更新后的原料信息"
|
// @Param rawMaterial body dto.UpdateRawMaterialRequest true "更新后的原料信息,包含名称、描述和参考价格"
|
||||||
// @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为200代表更新成功"
|
// @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为200代表更新成功"
|
||||||
// @Router /api/v1/feed/raw-materials/{id} [put]
|
// @Router /api/v1/feed/raw-materials/{id} [put]
|
||||||
func (c *RawMaterialController) UpdateRawMaterial(ctx echo.Context) error {
|
func (c *RawMaterialController) UpdateRawMaterial(ctx echo.Context) error {
|
||||||
@@ -172,7 +172,7 @@ func (c *RawMaterialController) GetRawMaterial(ctx echo.Context) error {
|
|||||||
// @Tags 饲料管理-原料
|
// @Tags 饲料管理-原料
|
||||||
// @Security BearerAuth
|
// @Security BearerAuth
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param query query dto.ListRawMaterialRequest false "查询参数"
|
// @Param query query dto.ListRawMaterialRequest false "查询参数,支持按名称、营养名称、参考价格范围过滤"
|
||||||
// @Success 200 {object} controller.Response{data=dto.ListRawMaterialResponse} "业务码为200代表成功获取列表"
|
// @Success 200 {object} controller.Response{data=dto.ListRawMaterialResponse} "业务码为200代表成功获取列表"
|
||||||
// @Router /api/v1/feed/raw-materials [get]
|
// @Router /api/v1/feed/raw-materials [get]
|
||||||
func (c *RawMaterialController) ListRawMaterials(ctx echo.Context) error {
|
func (c *RawMaterialController) ListRawMaterials(ctx echo.Context) error {
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ func ConvertRawMaterialToDTO(rm *models.RawMaterial) *RawMaterialResponse {
|
|||||||
ID: rm.ID,
|
ID: rm.ID,
|
||||||
Name: rm.Name,
|
Name: rm.Name,
|
||||||
Description: rm.Description,
|
Description: rm.Description,
|
||||||
|
ReferencePrice: rm.ReferencePrice,
|
||||||
RawMaterialNutrients: rawMaterialNutrientDTOs,
|
RawMaterialNutrients: rawMaterialNutrientDTOs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,14 +52,16 @@ type ListNutrientResponse struct {
|
|||||||
|
|
||||||
// CreateRawMaterialRequest 创建原料的请求体
|
// CreateRawMaterialRequest 创建原料的请求体
|
||||||
type CreateRawMaterialRequest struct {
|
type CreateRawMaterialRequest struct {
|
||||||
Name string `json:"name" validate:"required,max=100"` // 原料名称
|
Name string `json:"name" validate:"required,max=100"` // 原料名称
|
||||||
Description string `json:"description" validate:"max=255"` // 描述
|
Description string `json:"description" validate:"max=255"` // 描述
|
||||||
|
ReferencePrice float32 `json:"reference_price"` // 参考价格(kg/元)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRawMaterialRequest 更新原料的请求体
|
// UpdateRawMaterialRequest 更新原料的请求体
|
||||||
type UpdateRawMaterialRequest struct {
|
type UpdateRawMaterialRequest struct {
|
||||||
Name string `json:"name" validate:"required,max=100"` // 原料名称
|
Name string `json:"name" validate:"required,max=100"` // 原料名称
|
||||||
Description string `json:"description" validate:"max=255"` // 描述
|
Description string `json:"description" validate:"max=255"` // 描述
|
||||||
|
ReferencePrice float32 `json:"reference_price"` // 参考价格(kg/元)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RawMaterialNutrientDTO 原料营养素响应体
|
// RawMaterialNutrientDTO 原料营养素响应体
|
||||||
@@ -75,16 +77,19 @@ type RawMaterialResponse struct {
|
|||||||
ID uint32 `json:"id"`
|
ID uint32 `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
|
ReferencePrice float32 `json:"reference_price"` // 参考价格(kg/元)
|
||||||
RawMaterialNutrients []RawMaterialNutrientDTO `json:"raw_material_nutrients"` // 关联的营养素信息
|
RawMaterialNutrients []RawMaterialNutrientDTO `json:"raw_material_nutrients"` // 关联的营养素信息
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListRawMaterialRequest 定义了获取原料列表的请求参数
|
// ListRawMaterialRequest 定义了获取原料列表的请求参数
|
||||||
type ListRawMaterialRequest struct {
|
type ListRawMaterialRequest struct {
|
||||||
Page int `json:"page" query:"page"` // 页码
|
Page int `json:"page" query:"page"` // 页码
|
||||||
PageSize int `json:"page_size" query:"page_size"` // 每页数量
|
PageSize int `json:"page_size" query:"page_size"` // 每页数量
|
||||||
Name *string `json:"name" query:"name"` // 按原料名称模糊查询
|
Name *string `json:"name" query:"name"` // 按原料名称模糊查询
|
||||||
NutrientName *string `json:"nutrient_name" query:"nutrient_name"` // 按营养名称模糊查询
|
NutrientName *string `json:"nutrient_name" query:"nutrient_name"` // 按营养名称模糊查询
|
||||||
OrderBy string `json:"order_by" query:"order_by"` // 排序字段,例如 "id DESC"
|
MinReferencePrice *float32 `json:"min_reference_price" query:"min_reference_price"` // 参考价格最小值
|
||||||
|
MaxReferencePrice *float32 `json:"max_reference_price" query:"max_reference_price"` // 参考价格最大值
|
||||||
|
OrderBy string `json:"order_by" query:"order_by"` // 排序字段,例如 "id DESC"
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListRawMaterialResponse 是获取原料列表的响应结构
|
// ListRawMaterialResponse 是获取原料列表的响应结构
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func NewRawMaterialService(ctx context.Context, recipeSvc recipe.Service) RawMat
|
|||||||
func (s *rawMaterialServiceImpl) CreateRawMaterial(ctx context.Context, req *dto.CreateRawMaterialRequest) (*dto.RawMaterialResponse, error) {
|
func (s *rawMaterialServiceImpl) CreateRawMaterial(ctx context.Context, req *dto.CreateRawMaterialRequest) (*dto.RawMaterialResponse, error) {
|
||||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CreateRawMaterial")
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CreateRawMaterial")
|
||||||
|
|
||||||
rawMaterial, err := s.recipeSvc.CreateRawMaterial(serviceCtx, req.Name, req.Description)
|
rawMaterial, err := s.recipeSvc.CreateRawMaterial(serviceCtx, req.Name, req.Description, req.ReferencePrice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, recipe.ErrRawMaterialNameConflict) {
|
if errors.Is(err, recipe.ErrRawMaterialNameConflict) {
|
||||||
return nil, ErrRawMaterialNameConflict
|
return nil, ErrRawMaterialNameConflict
|
||||||
@@ -61,7 +61,7 @@ func (s *rawMaterialServiceImpl) CreateRawMaterial(ctx context.Context, req *dto
|
|||||||
func (s *rawMaterialServiceImpl) UpdateRawMaterial(ctx context.Context, id uint32, req *dto.UpdateRawMaterialRequest) (*dto.RawMaterialResponse, error) {
|
func (s *rawMaterialServiceImpl) UpdateRawMaterial(ctx context.Context, id uint32, req *dto.UpdateRawMaterialRequest) (*dto.RawMaterialResponse, error) {
|
||||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "UpdateRawMaterial")
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "UpdateRawMaterial")
|
||||||
|
|
||||||
rawMaterial, err := s.recipeSvc.UpdateRawMaterial(serviceCtx, id, req.Name, req.Description)
|
rawMaterial, err := s.recipeSvc.UpdateRawMaterial(serviceCtx, id, req.Name, req.Description, req.ReferencePrice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, recipe.ErrRawMaterialNotFound) {
|
if errors.Is(err, recipe.ErrRawMaterialNotFound) {
|
||||||
return nil, ErrRawMaterialNotFound
|
return nil, ErrRawMaterialNotFound
|
||||||
@@ -106,9 +106,11 @@ func (s *rawMaterialServiceImpl) ListRawMaterials(ctx context.Context, req *dto.
|
|||||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "ListRawMaterials")
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "ListRawMaterials")
|
||||||
|
|
||||||
opts := repository.RawMaterialListOptions{
|
opts := repository.RawMaterialListOptions{
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
NutrientName: req.NutrientName,
|
NutrientName: req.NutrientName,
|
||||||
OrderBy: req.OrderBy,
|
MinReferencePrice: req.MinReferencePrice,
|
||||||
|
MaxReferencePrice: req.MaxReferencePrice,
|
||||||
|
OrderBy: req.OrderBy,
|
||||||
}
|
}
|
||||||
rawMaterials, total, err := s.recipeSvc.ListRawMaterials(serviceCtx, opts, req.Page, req.PageSize)
|
rawMaterials, total, err := s.recipeSvc.ListRawMaterials(serviceCtx, opts, req.Page, req.PageSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ var (
|
|||||||
|
|
||||||
// RawMaterialService 定义了原料领域的核心业务服务接口
|
// RawMaterialService 定义了原料领域的核心业务服务接口
|
||||||
type RawMaterialService interface {
|
type RawMaterialService interface {
|
||||||
CreateRawMaterial(ctx context.Context, name, description string) (*models.RawMaterial, error)
|
CreateRawMaterial(ctx context.Context, name, description string, referencePrice float32) (*models.RawMaterial, error)
|
||||||
UpdateRawMaterial(ctx context.Context, id uint32, name, description string) (*models.RawMaterial, error)
|
UpdateRawMaterial(ctx context.Context, id uint32, name, description string, referencePrice float32) (*models.RawMaterial, error)
|
||||||
DeleteRawMaterial(ctx context.Context, id uint32) error
|
DeleteRawMaterial(ctx context.Context, id uint32) error
|
||||||
GetRawMaterial(ctx context.Context, id uint32) (*models.RawMaterial, error)
|
GetRawMaterial(ctx context.Context, id uint32) (*models.RawMaterial, error)
|
||||||
ListRawMaterials(ctx context.Context, opts repository.RawMaterialListOptions, page, pageSize int) ([]models.RawMaterial, int64, error)
|
ListRawMaterials(ctx context.Context, opts repository.RawMaterialListOptions, page, pageSize int) ([]models.RawMaterial, int64, error)
|
||||||
@@ -55,7 +55,7 @@ func NewRawMaterialService(ctx context.Context, uow repository.UnitOfWork, rawMa
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateRawMaterial 实现了创建原料的核心业务逻辑
|
// CreateRawMaterial 实现了创建原料的核心业务逻辑
|
||||||
func (s *rawMaterialServiceImpl) CreateRawMaterial(ctx context.Context, name, description string) (*models.RawMaterial, error) {
|
func (s *rawMaterialServiceImpl) CreateRawMaterial(ctx context.Context, name, description string, referencePrice float32) (*models.RawMaterial, error) {
|
||||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CreateRawMaterial")
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "CreateRawMaterial")
|
||||||
|
|
||||||
// 检查名称是否已存在
|
// 检查名称是否已存在
|
||||||
@@ -68,8 +68,9 @@ func (s *rawMaterialServiceImpl) CreateRawMaterial(ctx context.Context, name, de
|
|||||||
}
|
}
|
||||||
|
|
||||||
rawMaterial := &models.RawMaterial{
|
rawMaterial := &models.RawMaterial{
|
||||||
Name: name,
|
Name: name,
|
||||||
Description: description,
|
Description: description,
|
||||||
|
ReferencePrice: referencePrice,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.rawMaterialRepo.CreateRawMaterial(serviceCtx, rawMaterial); err != nil {
|
if err := s.rawMaterialRepo.CreateRawMaterial(serviceCtx, rawMaterial); err != nil {
|
||||||
@@ -80,7 +81,7 @@ func (s *rawMaterialServiceImpl) CreateRawMaterial(ctx context.Context, name, de
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRawMaterial 实现了更新原料的核心业务逻辑
|
// UpdateRawMaterial 实现了更新原料的核心业务逻辑
|
||||||
func (s *rawMaterialServiceImpl) UpdateRawMaterial(ctx context.Context, id uint32, name, description string) (*models.RawMaterial, error) {
|
func (s *rawMaterialServiceImpl) UpdateRawMaterial(ctx context.Context, id uint32, name, description string, referencePrice float32) (*models.RawMaterial, error) {
|
||||||
serviceCtx := logs.AddFuncName(ctx, s.ctx, "UpdateRawMaterial")
|
serviceCtx := logs.AddFuncName(ctx, s.ctx, "UpdateRawMaterial")
|
||||||
|
|
||||||
// 检查要更新的实体是否存在
|
// 检查要更新的实体是否存在
|
||||||
@@ -105,6 +106,7 @@ func (s *rawMaterialServiceImpl) UpdateRawMaterial(ctx context.Context, id uint3
|
|||||||
|
|
||||||
rawMaterial.Name = name
|
rawMaterial.Name = name
|
||||||
rawMaterial.Description = description
|
rawMaterial.Description = description
|
||||||
|
rawMaterial.ReferencePrice = referencePrice
|
||||||
|
|
||||||
if err := s.rawMaterialRepo.UpdateRawMaterial(serviceCtx, rawMaterial); err != nil {
|
if err := s.rawMaterialRepo.UpdateRawMaterial(serviceCtx, rawMaterial); err != nil {
|
||||||
return nil, fmt.Errorf("更新原料失败: %w", err)
|
return nil, fmt.Errorf("更新原料失败: %w", err)
|
||||||
|
|||||||
@@ -21,8 +21,9 @@ const (
|
|||||||
// RawMaterial 代表一种原料的静态定义,是系统中的原料字典。
|
// RawMaterial 代表一种原料的静态定义,是系统中的原料字典。
|
||||||
type RawMaterial struct {
|
type RawMaterial struct {
|
||||||
Model
|
Model
|
||||||
Name string `gorm:"size:100;not null;comment:原料名称"`
|
Name string `gorm:"size:100;not null;comment:原料名称"`
|
||||||
Description string `gorm:"size:255;comment:描述"`
|
Description string `gorm:"size:255;comment:描述"`
|
||||||
|
ReferencePrice float32 `gorm:"comment:参考价格(kg/元)"`
|
||||||
// RawMaterialNutrients 关联此原料的所有营养素含量信息
|
// RawMaterialNutrients 关联此原料的所有营养素含量信息
|
||||||
RawMaterialNutrients []RawMaterialNutrient `gorm:"foreignKey:RawMaterialID"`
|
RawMaterialNutrients []RawMaterialNutrient `gorm:"foreignKey:RawMaterialID"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,9 +14,11 @@ import (
|
|||||||
|
|
||||||
// RawMaterialListOptions 定义了查询原料列表时的筛选条件
|
// RawMaterialListOptions 定义了查询原料列表时的筛选条件
|
||||||
type RawMaterialListOptions struct {
|
type RawMaterialListOptions struct {
|
||||||
Name *string
|
Name *string
|
||||||
NutrientName *string
|
NutrientName *string
|
||||||
OrderBy string
|
MinReferencePrice *float32 // 参考价格最小值
|
||||||
|
MaxReferencePrice *float32 // 参考价格最大值
|
||||||
|
OrderBy string
|
||||||
}
|
}
|
||||||
|
|
||||||
// StockLogListOptions 定义了查询库存日志列表时的筛选条件
|
// StockLogListOptions 定义了查询库存日志列表时的筛选条件
|
||||||
@@ -111,6 +113,14 @@ func (r *gormRawMaterialRepository) ListRawMaterials(ctx context.Context, opts R
|
|||||||
db = db.Where("id IN (?)", subQuery)
|
db = db.Where("id IN (?)", subQuery)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 筛选参考价格
|
||||||
|
if opts.MinReferencePrice != nil {
|
||||||
|
db = db.Where("reference_price >= ?", *opts.MinReferencePrice)
|
||||||
|
}
|
||||||
|
if opts.MaxReferencePrice != nil {
|
||||||
|
db = db.Where("reference_price <= ?", *opts.MaxReferencePrice)
|
||||||
|
}
|
||||||
|
|
||||||
// 首先计算总数
|
// 首先计算总数
|
||||||
if err := db.Count(&total).Error; err != nil {
|
if err := db.Count(&total).Error; err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
@@ -133,8 +143,9 @@ func (r *gormRawMaterialRepository) UpdateRawMaterial(ctx context.Context, rawMa
|
|||||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateRawMaterial")
|
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateRawMaterial")
|
||||||
// 使用 map 更新以避免 GORM 的零值问题,并确保只更新指定字段
|
// 使用 map 更新以避免 GORM 的零值问题,并确保只更新指定字段
|
||||||
updateData := map[string]interface{}{
|
updateData := map[string]interface{}{
|
||||||
"name": rawMaterial.Name,
|
"name": rawMaterial.Name,
|
||||||
"description": rawMaterial.Description,
|
"description": rawMaterial.Description,
|
||||||
|
"reference_price": rawMaterial.ReferencePrice,
|
||||||
}
|
}
|
||||||
result := r.db.WithContext(repoCtx).Model(&models.RawMaterial{}).Where("id = ?", rawMaterial.ID).Updates(updateData)
|
result := r.db.WithContext(repoCtx).Model(&models.RawMaterial{}).Where("id = ?", rawMaterial.ID).Updates(updateData)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user