Compare commits
9 Commits
1b5f715dec
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c2c6577064 | |||
| 260c7d054c | |||
| d25933cf26 | |||
| 4aa56441ce | |||
| de68151539 | |||
| 04b46d8025 | |||
| bc4355cad5 | |||
| 968d996a9b | |||
| d6e5d89768 |
@@ -3,6 +3,7 @@
|
||||
1. 你可以访问 http://localhost:8080/ 进入我的前端界面, 前端项目是另一个项目, 但接入的是当前项目对应的后端平台, 如果需要登录账号密码都是huang
|
||||
2. 你可以阅读 config/config.yml 了解我的配置信息, 包括数据库的连接地址和账号密码, 本平台监听的端口等, 后端的swagger界面在 http://localhost:8086/swagger/index.html
|
||||
3. 项目根目录有project_structure.txt, 你需要先阅读此文件了解项目目录结构
|
||||
4. 项目中有config/presets-data目录, 里面有一些预设数据, 可以间接看作数据库内的数据, 在测试环境中他们一般会和数据库的数据保持一致
|
||||
|
||||
# 权限管理
|
||||
|
||||
|
||||
11
Makefile
11
Makefile
@@ -10,10 +10,15 @@ help:
|
||||
@echo " build Build the application"
|
||||
@echo " clean Clean generated files"
|
||||
@echo " test Run all tests"
|
||||
@echo " swag Generate swagger docs"
|
||||
@echo " help Show this help message"
|
||||
@echo " swag Generate Swagger docs"
|
||||
@echo " proto Generate protobuf files"
|
||||
@echo " lint Lint the code"
|
||||
@echo " lint Lint the code"
|
||||
@echo " dev Run in development mode with hot-reload"
|
||||
@echo " mcp-chrome Start the Google Chrome MCP server"
|
||||
@echo " mcp-pgsql Start the PostgreSQL MCP server"
|
||||
@echo " tree Generate the project file structure list"
|
||||
@echo " gemini Start the gemini-cli"
|
||||
@echo " help Show this help message"
|
||||
|
||||
# 运行应用
|
||||
.PHONY: run
|
||||
|
||||
@@ -46,6 +46,20 @@ http://git.huangwc.com/pig/pig-farm-controller/issues/66
|
||||
7. 简单查看功能
|
||||
- 两个配方对比页面(营养+成本对比)
|
||||
|
||||
# 实现总结
|
||||
|
||||
## 实现内容
|
||||
|
||||
实现库存和原料和营养和猪营养需求的管理, 支持根据库存和已录入原料和猪营养需求生成配方
|
||||
|
||||
## TODO
|
||||
|
||||
1. 发酵料管理考虑到发酵目前没有自动化流程, 不好追踪, 遂暂时不做
|
||||
2. 目前的价格是根据原料的参考价设置的, 后续应当实现一个在服务供平台采集参考价, 以及使用原料采购价计算
|
||||
3. 原料应该加上膨润土等, 比如膨润土的黄曲霉素含量应该是负数以表示减少饲料里的含量
|
||||
4. 饲料保质期考虑到批次间管理暂时不方便, 等可以实现同一原料先进先出后再实现
|
||||
5. 暂时不支持指定原料列表然后自动生成, 也不支持告诉用户当前生成不出是为什么, 等以后再做
|
||||
|
||||
# 完成事项
|
||||
|
||||
1. 定义原料表, 营养表, 原料营养表, 原料库存变更表
|
||||
@@ -260,7 +260,7 @@ func (a *API) setupRoutes() {
|
||||
feedGroup.GET("/recipes/:id", a.recipeController.GetRecipe)
|
||||
feedGroup.GET("/recipes", a.recipeController.ListRecipes)
|
||||
feedGroup.POST("/recipes/generate-from-all-materials/:pig_type_id", a.recipeController.GenerateFromAllMaterials)
|
||||
feedGroup.POST("/recipes/generate-prioritized-stock/:pig_type_id", a.recipeController.GenerateFromAllMaterials)
|
||||
feedGroup.POST("/recipes/generate-prioritized-stock/:pig_type_id", a.recipeController.GenerateRecipeWithPrioritizedStockRawMaterials)
|
||||
}
|
||||
logger.Debug("饲料管理相关接口注册成功 (需要认证和审计)")
|
||||
|
||||
|
||||
@@ -223,7 +223,7 @@ func (g *GeneralDeviceService) Collect(ctx context.Context, areaControllerID uin
|
||||
logger.Errorf("创建待采集请求失败 (CorrelationID: %s): %v", correlationID, err)
|
||||
return err
|
||||
}
|
||||
logger.Infof("成功创建待采集请求 (CorrelationID: %s, DeviceID: %d)", correlationID, areaController.ID)
|
||||
logger.Debugf("成功创建待采集请求 (CorrelationID: %s, DeviceID: %d)", correlationID, areaController.ID)
|
||||
|
||||
// 5. 构建最终的空中载荷
|
||||
batchCmd := &proto.BatchCollectCommand{
|
||||
@@ -240,12 +240,12 @@ func (g *GeneralDeviceService) Collect(ctx context.Context, areaControllerID uin
|
||||
logger.Errorf("序列化采集指令失败 (CorrelationID: %s): %v", correlationID, err)
|
||||
return err
|
||||
}
|
||||
logger.Infof("构造空中载荷成功: networkID: %v, payload: %v", networkID, instruction)
|
||||
logger.Debugf("构造空中载荷成功: networkID: %v, payload: %v", networkID, instruction)
|
||||
if _, err := g.comm.Send(serviceCtx, networkID, payload); err != nil {
|
||||
logger.DPanicf("待采集请求 (CorrelationID: %s) 已创建,但发送到设备失败: %v。数据可能不一致!", correlationID, err)
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Infof("成功将采集请求 (CorrelationID: %s) 发送到设备 %s", correlationID, networkID)
|
||||
logger.Debugf("成功将采集请求 (CorrelationID: %s) 发送到设备 %s", correlationID, networkID)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -86,6 +86,7 @@ func (r *recipeServiceImpl) GenerateRecipeWithAllRawMaterials(ctx context.Contex
|
||||
}
|
||||
|
||||
// 4. 丰富配方描述:计算并添加参考价格信息
|
||||
recipe.Name = fmt.Sprintf("%s - 使用所有已知原料", recipe.Name)
|
||||
|
||||
// 填充 RecipeIngredients 中的 RawMaterial 字段,以便后续计算成本
|
||||
rawMaterialMap := make(map[uint32]models.RawMaterial)
|
||||
@@ -120,7 +121,7 @@ func (r *recipeServiceImpl) GenerateRecipeWithAllRawMaterials(ctx context.Contex
|
||||
if recipe, err = r.CreateRecipe(serviceCtx, recipe); err != nil {
|
||||
return nil, fmt.Errorf("保存生成的配方失败: %w", err)
|
||||
}
|
||||
logger.Infof("成功生成配方: %+v", recipe)
|
||||
logger.Infof("成功生成配方: 配方名称: %v | 配方简介: %v", recipe.Name, recipe.Description)
|
||||
|
||||
// 6. 返回创建的配方 (现在它应该已经有了ID)
|
||||
return recipe, nil
|
||||
@@ -168,7 +169,8 @@ func (r *recipeServiceImpl) GenerateRecipeWithPrioritizedStockRawMaterials(ctx c
|
||||
for _, mat := range stockMaterials {
|
||||
adjustedMat := mat // 复制一份
|
||||
// 大幅调低有库存原料的参考价格,诱导生成器优先使用
|
||||
adjustedMat.ReferencePrice = 0.01 // 设置一个非常小的价格
|
||||
// TODO 按理说应该尽量优先使用已有原料, 但如果搭配后购买缺失原料花的钱还不如不用已有原料的另一个组合钱少怎么办
|
||||
adjustedMat.ReferencePrice = adjustedMat.ReferencePrice * 0.1
|
||||
materialsForGeneration = append(materialsForGeneration, adjustedMat)
|
||||
logger.Debugf("原料 '%s' (ID: %d) 有库存,生成配方时参考价格调整为 %.2f", mat.Name, mat.ID, adjustedMat.ReferencePrice)
|
||||
}
|
||||
@@ -184,6 +186,8 @@ func (r *recipeServiceImpl) GenerateRecipeWithPrioritizedStockRawMaterials(ctx c
|
||||
}
|
||||
|
||||
// 5. 丰富配方描述:计算并添加参考价格信息
|
||||
recipe.Name = fmt.Sprintf("%s - 优先使用库存已有原料", recipe.Name)
|
||||
|
||||
// 注意:这里需要使用原始的、未调整价格的原料信息来计算最终的参考价格
|
||||
// rawMaterialMap 从 allOriginalMaterials 构建,确保使用原始价格
|
||||
rawMaterialMap := make(map[uint32]models.RawMaterial)
|
||||
@@ -201,7 +205,7 @@ func (r *recipeServiceImpl) GenerateRecipeWithPrioritizedStockRawMaterials(ctx c
|
||||
}
|
||||
|
||||
referencePrice := recipe.CalculateReferencePricePerKilogram() / 100
|
||||
recipe.Description = fmt.Sprintf("%s 计算时预估成本: %.2f元/kg。", recipe.Description, referencePrice)
|
||||
recipe.Description = fmt.Sprintf("使用 %v 种有库存原料和 %v 种无库存原料计算的库存原料优先使用的配方。 计算时预估成本: %.2f元/kg。", len(stockMaterials), len(noStockMaterials), referencePrice)
|
||||
|
||||
// 如果 totalPercentage 小于 100%,说明填充料被使用,这是符合预期的。
|
||||
// 此时需要在描述中说明需要添加的廉价填充料的百分比。
|
||||
@@ -216,7 +220,7 @@ func (r *recipeServiceImpl) GenerateRecipeWithPrioritizedStockRawMaterials(ctx c
|
||||
|
||||
return nil, fmt.Errorf("保存生成的配方失败: %w", err)
|
||||
}
|
||||
logger.Infof("成功生成优先使用库存原料的配方: %+v", recipe)
|
||||
logger.Infof("成功生成优先使用库存原料的配方: 配方名称: %v | 配方简介: %v", recipe.Name, recipe.Description)
|
||||
|
||||
// 7. 返回创建的配方
|
||||
return recipe, nil
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/config"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
|
||||
@@ -18,7 +18,7 @@ type RawMaterialListOptions struct {
|
||||
NutrientName *string
|
||||
MinReferencePrice *float32 // 参考价格最小值
|
||||
MaxReferencePrice *float32 // 参考价格最大值
|
||||
HasStock *bool // 是否只查询有库存的原料
|
||||
HasStock *bool
|
||||
OrderBy string
|
||||
}
|
||||
|
||||
@@ -123,20 +123,27 @@ func (r *gormRawMaterialRepository) ListRawMaterials(ctx context.Context, opts R
|
||||
db = db.Where("reference_price <= ?", *opts.MaxReferencePrice)
|
||||
}
|
||||
|
||||
// 筛选有库存的原料
|
||||
if opts.HasStock != nil && *opts.HasStock {
|
||||
// 筛选有/无库存的原料
|
||||
if opts.HasStock != nil {
|
||||
// 内部子查询:生成带有 rn 的结果集,GORM 会自动为 models.RawMaterialStockLog 添加 deleted_at IS NULL
|
||||
rankedLogsQuery := r.db.Model(&models.RawMaterialStockLog{}).
|
||||
Select("raw_material_id, after_quantity, ROW_NUMBER() OVER(PARTITION BY raw_material_id ORDER BY happened_at DESC, id DESC) as rn")
|
||||
|
||||
// 外部子查询:从 ranked_logs 中筛选 rn=1 且 after_quantity > 0 的 raw_material_id
|
||||
// GORM 会将 rankedLogsQuery 作为一个子查询嵌入到 FROM 子句中
|
||||
// 外部子查询:从 ranked_logs 中筛选 rn=1 的 raw_material_id
|
||||
latestStockLogSubQuery := r.db.Table("(?) as ranked_logs", rankedLogsQuery).
|
||||
Select("raw_material_id").
|
||||
Where("rn = 1 AND after_quantity > 0")
|
||||
Where("rn = 1").
|
||||
Where("after_quantity > 0")
|
||||
|
||||
if *opts.HasStock {
|
||||
// 筛选有库存的原料 (ID 在有正库存的集合中)
|
||||
db = db.Where("id IN (?)", latestStockLogSubQuery)
|
||||
} else {
|
||||
// 筛选无库存的原料 (ID 不在有正库存的集合中)
|
||||
// 包含了最新库存为0 和 没有库存日志的原料。
|
||||
db = db.Where("id NOT IN (?)", latestStockLogSubQuery)
|
||||
}
|
||||
|
||||
// 将这个子查询直接应用到主查询的 WHERE id IN (?) 条件中
|
||||
db = db.Where("id IN (?)", latestStockLogSubQuery)
|
||||
}
|
||||
|
||||
// 首先计算总数
|
||||
|
||||
@@ -266,7 +266,7 @@ func (t *LoRaMeshUartPassthroughTransport) executeSend(ctx context.Context, req
|
||||
frame.WriteByte(currentChunk) // 当前包序号
|
||||
frame.Write(chunk) // 数据块
|
||||
|
||||
logger.Infof("构建LoRa数据包: %v", frame.Bytes())
|
||||
logger.Debugf("构建LoRa数据包: %v", frame.Bytes())
|
||||
_, err := t.port.Write(frame.Bytes())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("写入串口失败: %w", err)
|
||||
|
||||
@@ -37,7 +37,7 @@ design/archive/2025-11-05-provide-logger-with-mothed/task-webhook.md
|
||||
design/archive/2025-11-06-health-check-routing/index.md
|
||||
design/archive/2025-11-06-system-plan-continuously-triggered/index.md
|
||||
design/archive/2025-11-10-exceeding-threshold-alarm/index.md
|
||||
design/recipe-management/index.md
|
||||
design/archive/2025-11-29-recipe-management/index.md
|
||||
docs/docs.go
|
||||
docs/swagger.json
|
||||
docs/swagger.yaml
|
||||
|
||||
Reference in New Issue
Block a user