diff --git a/api/v1/plant/enter.go b/api/v1/plant/enter.go index 217c7b1..6566f6b 100644 --- a/api/v1/plant/enter.go +++ b/api/v1/plant/enter.go @@ -9,13 +9,15 @@ type ApiGroup struct { WikiClassApi WikiApi OcrApi + LevelConfigApi } var ( - plantService = service.GroupApp.PlantServiceGroup.MyPlantService - topicService = service.GroupApp.PlantServiceGroup.TopicService - postService = service.GroupApp.PlantServiceGroup.PostService - wikiClassService = service.GroupApp.PlantServiceGroup.WikiClassService - wikiService = service.GroupApp.PlantServiceGroup.WikiService - ocrService = service.GroupApp.PlantServiceGroup.OcrService + plantService = service.GroupApp.PlantServiceGroup.MyPlantService + topicService = service.GroupApp.PlantServiceGroup.TopicService + postService = service.GroupApp.PlantServiceGroup.PostService + wikiClassService = service.GroupApp.PlantServiceGroup.WikiClassService + wikiService = service.GroupApp.PlantServiceGroup.WikiService + ocrService = service.GroupApp.PlantServiceGroup.OcrService + levelConfigService = service.GroupApp.PlantServiceGroup.LevelConfigService ) diff --git a/api/v1/plant/level_config.go b/api/v1/plant/level_config.go new file mode 100644 index 0000000..772807c --- /dev/null +++ b/api/v1/plant/level_config.go @@ -0,0 +1,101 @@ +package plant + +import ( + "sundynix-go/global" + "sundynix-go/model/commom/response" + plantReq "sundynix-go/model/plant/request" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" +) + +type LevelConfigApi struct{} + +// AddLevelConfig 添加等级配置 +// @Tags 等级配置 +// @Summary 添加等级配置 +// @Security BearerAuth +// @accept json +// @Produce application/json +// @Param data body plantReq.CreateLevelConf true "添加等级配置" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}" +// @Router /config/level/add [post] +func (a *LevelConfigApi) AddLevelConfig(c *gin.Context) { + var req plantReq.CreateLevelConf + err := c.ShouldBindJSON(&req) + if err != nil { + response.FailWithMsg("请求参数错误", c) + return + } + err = levelConfigService.AddLevelConfig(req) + if err != nil { + global.Logger.Error("添加等级配置失败", zap.Error(err)) + response.FailWithMsg("添加等级配置失败", c) + return + } + response.OkWithMsg("添加等级配置成功", c) +} + +// UpdateLevelConfig 修改等级配置 +// @Tags 等级配置 +// @Summary 修改等级配置 +// @Security BearerAuth +// @accept json +// @Produce application/json +// @Param data body plantReq.UpdateLevelConf true "修改等级配置" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}" +// @Router /config/level/update [post] +func (a *LevelConfigApi) UpdateLevelConfig(c *gin.Context) { + var req plantReq.UpdateLevelConf + err := c.ShouldBindJSON(&req) + if err != nil { + response.FailWithMsg("请求参数错误", c) + return + } + err = levelConfigService.UpdateLevelConfig(req) + if err != nil { + global.Logger.Error("修改等级配置失败", zap.Error(err)) + response.FailWithMsg("修改等级配置失败", c) + return + } + response.OkWithMsg("修改等级配置成功", c) +} + +// LevelConfigList 等级配置列表 +// @Tags 等级配置 +// @Summary 等级配置列表 +// @Security BearerAuth +// @Produce application/json +// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}" +// @Router /config/level/list [get] +func (a *LevelConfigApi) LevelConfigList(c *gin.Context) { + list, err := levelConfigService.LevelConfigList() + if err != nil { + global.Logger.Error("查询等级配置列表失败", zap.Error(err)) + response.FailWithMsg("查询等级配置列表失败", c) + return + } + response.OkWithData(response.ListResult{ + List: list, + }, c) + +} + +// LevelConfigDetail 等级配置详情 +// @Tags 等级配置 +// @Summary 等级配置详情 +// @Security BearerAuth +// @Produce application/json +// @Param id query string true "等级配置id" +// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}" +// @Router /config/level/detail [get] +func (a *LevelConfigApi) LevelConfigDetail(c *gin.Context) { + id := c.Query("id") + levelConfig, err := levelConfigService.LevelConfigDetail(id) + if err != nil { + global.Logger.Error("查询等级配置详情失败", zap.Error(err)) + response.FailWithMsg("查询等级配置详情失败", c) + return + } + response.OkWithData(levelConfig, c) +} diff --git a/config/constans.go b/config/constans.go new file mode 100644 index 0000000..1ce60ba --- /dev/null +++ b/config/constans.go @@ -0,0 +1,22 @@ +package config + +// ActionType 定义养护行为类型 +type ActionType string + +const ( + ActionWater ActionType = "WATER" // 浇水 + ActionFertilize ActionType = "FERTILIZE" // 施肥 + ActionPrune ActionType = "PRUNE" // 修剪 + ActionPhoto ActionType = "PHOTO" // 拍照 + ActionLogin ActionType = "LOGIN" // 每日签到 +) + +// BadgeTier 定义徽章的稀有度 +type BadgeTier int + +const ( + TierBronze BadgeTier = 1 // 铜 + TierSilver BadgeTier = 2 // 银 + TierGold BadgeTier = 3 // 金 + TierDiamond BadgeTier = 4 // 钻石 +) diff --git a/initialize/gorm.go b/initialize/gorm.go index efee43b..c725171 100644 --- a/initialize/gorm.go +++ b/initialize/gorm.go @@ -52,6 +52,8 @@ func MigrateTable() { plant.Wiki{}, //百科植物 plant.ClassifyRecord{}, //植物识别记录 + plant.LevelConfig{}, //等级配置 + ) if err != nil { global.Logger.Error("Migrate table failed,err:", zap.Error(err)) diff --git a/initialize/router.go b/initialize/router.go index 031ed1e..7a70d79 100644 --- a/initialize/router.go +++ b/initialize/router.go @@ -49,12 +49,13 @@ func Routers() { { //需要鉴权的路由 - plantGroup.InitPlantRouter(NeedAuthGroup) // 植物相关 - plantGroup.InitTopicRouter(NeedAuthGroup) // 帖子话题 - plantGroup.InitPostRouter(NeedAuthGroup) // 帖子相关 - plantGroup.InitWikiClassRouter(NeedAuthGroup) //百科分类 - plantGroup.InitWikiRouter(NeedAuthGroup) //百科 - plantGroup.InitOcrRouter(NeedAuthGroup) // ocr识别 + plantGroup.InitPlantRouter(NeedAuthGroup) // 植物相关 + plantGroup.InitTopicRouter(NeedAuthGroup) // 帖子话题 + plantGroup.InitPostRouter(NeedAuthGroup) // 帖子相关 + plantGroup.InitWikiClassRouter(NeedAuthGroup) //百科分类 + plantGroup.InitWikiRouter(NeedAuthGroup) //百科 + plantGroup.InitOcrRouter(NeedAuthGroup) // ocr识别 + plantGroup.InitLevelConfigRouter(NeedAuthGroup) //等级配置 } diff --git a/model/plant/request/level.go b/model/plant/request/level.go new file mode 100644 index 0000000..772cc0e --- /dev/null +++ b/model/plant/request/level.go @@ -0,0 +1,15 @@ +package request + +type CreateLevelConf struct { + Level int `json:"level"` // 等级数值 + Title string `json:"title"` // 等级称号 (e.g., "萌芽园丁") + MinSunlight int64 `json:"minSunlight"` // 达到该等级所需的最小阳光值 + Perks string `json:"perks"` // 解锁权益描述 (e.g., "解锁智能诊断") +} +type UpdateLevelConf struct { + Id string `json:"id" binding:"required"` + Level int `json:"level"` // 等级数值 + Title string `json:"title"` // 等级称号 (e.g., "萌芽园丁") + MinSunlight int64 `json:"minSunlight"` // 达到该等级所需的最小阳光值 + Perks string `json:"perks"` // 解锁权益描述 (e.g., "解锁智能诊断") +} diff --git a/model/plant/sys_badge_config.go b/model/plant/sys_badge_config.go new file mode 100644 index 0000000..12ca847 --- /dev/null +++ b/model/plant/sys_badge_config.go @@ -0,0 +1,18 @@ +package plant + +import ( + "sundynix-go/config" + "sundynix-go/global" +) + +// BadgeConfig 徽章配置 +type BadgeConfig struct { + global.BaseModel + GroupId string `json:"group_id"` // 徽章组 (用于系列徽章,如浇水铜/银/金) + Name string `json:"name"` // 徽章名称 + Description string `json:"description"` // 描述 + Tier config.BadgeTier `json:"tier"` // 稀有度 + TargetAction config.ActionType `json:"target_action"` // 关联的行为 + TargetCount int `json:"target_count"` // 需要完成该行为的次数 + IconURL string `json:"icon_url"` // 徽章图标地址 +} diff --git a/model/plant/sys_level_config.go b/model/plant/sys_level_config.go new file mode 100644 index 0000000..957f0b3 --- /dev/null +++ b/model/plant/sys_level_config.go @@ -0,0 +1,12 @@ +package plant + +import "sundynix-go/global" + +// LevelConfig 等级配置 +type LevelConfig struct { + global.BaseModel + Level int `json:"level" gorm:"column:level"` // 等级数值 + Title string `json:"title" gorm:"column:title"` // 等级称号 (e.g., "萌芽园丁") + MinSunlight int64 `json:"minSunlight" gorm:"column:min_sunlight"` // 达到该等级所需的最小阳光值 + Perks string `json:"perks" gorm:"column:column:perks"` // 解锁权益描述 (e.g., "解锁智能诊断") +} diff --git a/router/plant/enter.go b/router/plant/enter.go index 35814fa..7cc3fa4 100644 --- a/router/plant/enter.go +++ b/router/plant/enter.go @@ -9,14 +9,16 @@ type RouterGroup struct { WikiClassRouter WikiRouter OcrRouter + LevelConfigRouter } // 初始化路由 var ( - myPlantApi = v1.ApiGroupApp.PlantApiGroup.MyPlantApi - topicApi = v1.ApiGroupApp.PlantApiGroup.TopicApi - postApi = v1.ApiGroupApp.PlantApiGroup.PostApi - wikiClassApi = v1.ApiGroupApp.PlantApiGroup.WikiClassApi - wikiApi = v1.ApiGroupApp.PlantApiGroup.WikiApi - ocrApi = v1.ApiGroupApp.PlantApiGroup.OcrApi + myPlantApi = v1.ApiGroupApp.PlantApiGroup.MyPlantApi + topicApi = v1.ApiGroupApp.PlantApiGroup.TopicApi + postApi = v1.ApiGroupApp.PlantApiGroup.PostApi + wikiClassApi = v1.ApiGroupApp.PlantApiGroup.WikiClassApi + wikiApi = v1.ApiGroupApp.PlantApiGroup.WikiApi + ocrApi = v1.ApiGroupApp.PlantApiGroup.OcrApi + levelConfigApi = v1.ApiGroupApp.PlantApiGroup.LevelConfigApi ) diff --git a/router/plant/level_config_router.go b/router/plant/level_config_router.go new file mode 100644 index 0000000..3934f52 --- /dev/null +++ b/router/plant/level_config_router.go @@ -0,0 +1,16 @@ +package plant + +import "github.com/gin-gonic/gin" + +type LevelConfigRouter struct{} + +func (c *OcrRouter) InitLevelConfigRouter(Router *gin.RouterGroup) { + levelConfigRouter := Router.Group("config/level") + { + levelConfigRouter.POST("/add", levelConfigApi.AddLevelConfig) + levelConfigRouter.POST("/update", levelConfigApi.UpdateLevelConfig) + levelConfigRouter.GET("/list", levelConfigApi.LevelConfigList) + levelConfigRouter.GET("/detail", levelConfigApi.LevelConfigDetail) + } + +} diff --git a/service/plant/enter.go b/service/plant/enter.go index 411de13..4f8963b 100644 --- a/service/plant/enter.go +++ b/service/plant/enter.go @@ -7,4 +7,5 @@ type ServiceGroup struct { WikiClassService WikiService OcrService + LevelConfigService } diff --git a/service/plant/level_config.go b/service/plant/level_config.go new file mode 100644 index 0000000..870f8f3 --- /dev/null +++ b/service/plant/level_config.go @@ -0,0 +1,62 @@ +package plant + +import ( + "errors" + "sundynix-go/global" + "sundynix-go/model/plant" + plantReq "sundynix-go/model/plant/request" + + "gorm.io/gorm" +) + +type LevelConfigService struct{} + +var LevelConfigServiceApp = new(LevelConfigService) + +// AddLevelConfig 添加等级配置 +func (s *LevelConfigService) AddLevelConfig(req plantReq.CreateLevelConf) error { + var exist plant.LevelConfig + err := global.DB.Where("level = ?", req.Level).First(&exist).Error + if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + return errors.New("配置已经存在") + } + conf := plant.LevelConfig{ + Level: req.Level, + MinSunlight: req.MinSunlight, + Perks: req.Perks, + Title: req.Title, + } + err = global.DB.Create(&conf).Error + if err != nil { + return err + } + return nil +} + +// UpdateLevelConfig 修改等级配置 +func (s *LevelConfigService) UpdateLevelConfig(req plantReq.UpdateLevelConf) error { + updateMap := map[string]interface{}{ + "level": req.Level, + "min_sunlight": req.MinSunlight, + "perks": req.Perks, + "title": req.Title, + } + return global.DB.Model(&plant.LevelConfig{}).Where("id = ?", req.Id).Updates(updateMap).Error +} + +// LevelConfigList 获取等级配置列表 +func (s *LevelConfigService) LevelConfigList() ([]plant.LevelConfig, error) { + var list []plant.LevelConfig + err := global.DB.Order("level asc").Find(&list).Error + return list, err +} + +// LevelConfigDetail 获取等级配置详情 +func (s *LevelConfigService) LevelConfigDetail(id string) (plant.LevelConfig, error) { + var config plant.LevelConfig + err := global.DB.Where("id = ?", id).First(&config).Error + if err != nil { + return config, err + } + return config, nil +}