feat(knowledge): 饲养阶段风险提示接口与种子数据(#13)

This commit is contained in:
weijuesen
2026-08-12 14:44:41 +08:00
parent 9c8388d220
commit 6d9cfc7aa9
3 changed files with 169 additions and 0 deletions
+36
View File
@@ -28,9 +28,45 @@ func RegisterKnowledgeRoutes(rg *gin.RouterGroup, db *gorm.DB, s3 *service.S3Ser
rg.PATCH("/knowledge/articles/:id", write, updateArticle(db))
rg.DELETE("/knowledge/articles/:id", write, deleteArticle(db))
rg.GET("/knowledge/stage-hints", read, listStageHints(db))
registerKnowledgeImageRoute(rg, db, s3, imageBucket)
}
// listStageHints 饲养阶段风险提示(?stage= 过滤;GET 天然幂等)
func listStageHints(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
hints, err := model.StageHintsByStage(c.Query("stage"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
resp := make([]gin.H, 0, len(hints))
for _, h := range hints {
diseases := make([]gin.H, 0, len(h.Diseases))
for _, dh := range h.Diseases {
id := ""
var d model.Disease
if db.Where("name = ?", dh.Name).First(&d).Error == nil {
id = d.ID
}
diseases = append(diseases, gin.H{
"id": id,
"name": dh.Name,
"reason": dh.Reason,
})
}
resp = append(resp, gin.H{
"stage": h.Stage,
"label": h.Label,
"summary": h.Summary,
"diseases": diseases,
})
}
c.JSON(http.StatusOK, resp)
}
}
// listDiseases 蚕病百科列表(支持 category/keyword/enabled 过滤)
func listDiseases(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {