feat: 等级配置

This commit is contained in:
Blizzard
2026-02-11 11:37:35 +08:00
parent 6be24bdbda
commit 4ccab96ca2
12 changed files with 272 additions and 18 deletions
+2
View File
@@ -9,6 +9,7 @@ type ApiGroup struct {
WikiClassApi WikiClassApi
WikiApi WikiApi
OcrApi OcrApi
LevelConfigApi
} }
var ( var (
@@ -18,4 +19,5 @@ var (
wikiClassService = service.GroupApp.PlantServiceGroup.WikiClassService wikiClassService = service.GroupApp.PlantServiceGroup.WikiClassService
wikiService = service.GroupApp.PlantServiceGroup.WikiService wikiService = service.GroupApp.PlantServiceGroup.WikiService
ocrService = service.GroupApp.PlantServiceGroup.OcrService ocrService = service.GroupApp.PlantServiceGroup.OcrService
levelConfigService = service.GroupApp.PlantServiceGroup.LevelConfigService
) )
+101
View File
@@ -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)
}
+22
View File
@@ -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 // 钻石
)
+2
View File
@@ -52,6 +52,8 @@ func MigrateTable() {
plant.Wiki{}, //百科植物 plant.Wiki{}, //百科植物
plant.ClassifyRecord{}, //植物识别记录 plant.ClassifyRecord{}, //植物识别记录
plant.LevelConfig{}, //等级配置
) )
if err != nil { if err != nil {
global.Logger.Error("Migrate table failed,err:", zap.Error(err)) global.Logger.Error("Migrate table failed,err:", zap.Error(err))
+1
View File
@@ -55,6 +55,7 @@ func Routers() {
plantGroup.InitWikiClassRouter(NeedAuthGroup) //百科分类 plantGroup.InitWikiClassRouter(NeedAuthGroup) //百科分类
plantGroup.InitWikiRouter(NeedAuthGroup) //百科 plantGroup.InitWikiRouter(NeedAuthGroup) //百科
plantGroup.InitOcrRouter(NeedAuthGroup) // ocr识别 plantGroup.InitOcrRouter(NeedAuthGroup) // ocr识别
plantGroup.InitLevelConfigRouter(NeedAuthGroup) //等级配置
} }
+15
View File
@@ -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., "解锁智能诊断")
}
+18
View File
@@ -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"` // 徽章图标地址
}
+12
View File
@@ -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., "解锁智能诊断")
}
+2
View File
@@ -9,6 +9,7 @@ type RouterGroup struct {
WikiClassRouter WikiClassRouter
WikiRouter WikiRouter
OcrRouter OcrRouter
LevelConfigRouter
} }
// 初始化路由 // 初始化路由
@@ -19,4 +20,5 @@ var (
wikiClassApi = v1.ApiGroupApp.PlantApiGroup.WikiClassApi wikiClassApi = v1.ApiGroupApp.PlantApiGroup.WikiClassApi
wikiApi = v1.ApiGroupApp.PlantApiGroup.WikiApi wikiApi = v1.ApiGroupApp.PlantApiGroup.WikiApi
ocrApi = v1.ApiGroupApp.PlantApiGroup.OcrApi ocrApi = v1.ApiGroupApp.PlantApiGroup.OcrApi
levelConfigApi = v1.ApiGroupApp.PlantApiGroup.LevelConfigApi
) )
+16
View File
@@ -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)
}
}
+1
View File
@@ -7,4 +7,5 @@ type ServiceGroup struct {
WikiClassService WikiClassService
WikiService WikiService
OcrService OcrService
LevelConfigService
} }
+62
View File
@@ -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
}