diff --git a/api/v1/enter.go b/api/v1/enter.go index 9ea2c73..7120c18 100644 --- a/api/v1/enter.go +++ b/api/v1/enter.go @@ -7,6 +7,7 @@ import ( "git.echol.cn/loser/lckt/api/v1/example" "git.echol.cn/loser/lckt/api/v1/system" "git.echol.cn/loser/lckt/api/v1/user" + "git.echol.cn/loser/lckt/api/v1/vip" ) var ApiGroupApp = new(ApiGroup) @@ -18,4 +19,5 @@ type ApiGroup struct { BotApiGroup bot.ApiGroup ArticleApiGroup article.ApiGroup UserApiGroup user.APPUserApi + VipApiGroup vip.ApiGroup } diff --git a/api/v1/vip/enter.go b/api/v1/vip/enter.go new file mode 100644 index 0000000..5cc81f2 --- /dev/null +++ b/api/v1/vip/enter.go @@ -0,0 +1,7 @@ +package vip + +import "git.echol.cn/loser/lckt/service" + +type ApiGroup struct{ VipApi } + +var vipService = service.ServiceGroupApp.VipServiceGroup.VipService diff --git a/api/v1/vip/vip.go b/api/v1/vip/vip.go new file mode 100644 index 0000000..f0e33aa --- /dev/null +++ b/api/v1/vip/vip.go @@ -0,0 +1,88 @@ +package vip + +import ( + "git.echol.cn/loser/lckt/model/common/request" + "git.echol.cn/loser/lckt/model/common/response" + "git.echol.cn/loser/lckt/model/vip" + "github.com/gin-gonic/gin" +) + +type VipApi struct{} + +// GetVipList 获取会员列表 +func (v *VipApi) GetVipList(ctx *gin.Context) { + var p request.PageInfo + if err := ctx.ShouldBind(&p); err != nil { + response.FailWithMessage("参数有误", ctx) + return + } + + vipList, total, err := vipService.GetVipList(p) + if err != nil { + response.FailWithMessage("获取VIP列表失败", ctx) + return + } + response.OkWithDetailed( + response.PageResult{ + List: vipList, + Total: total, + Page: p.Page, + PageSize: p.PageSize, + }, + "获取VIP列表成功", + ctx, + ) +} + +// Create 创建会员 +func (v *VipApi) Create(ctx *gin.Context) { + var p vip.Vip + if err := ctx.ShouldBind(&p); err != nil { + response.FailWithMessage("参数有误", ctx) + return + } + if err := vipService.CreateVip(p); err != nil { + response.FailWithMessage("创建会员失败", ctx) + return + } + response.OkWithMessage("创建会员成功", ctx) +} + +// Update 更新会员 +func (v *VipApi) Update(ctx *gin.Context) { + var p vip.Vip + if err := ctx.ShouldBind(&p); err != nil { + response.FailWithMessage("参数有误", ctx) + return + } + if err := vipService.UpdateVip(p); err != nil { + response.FailWithMessage("更新会员失败", ctx) + return + } + response.OkWithMessage("更新会员成功", ctx) +} + +// Delete 删除会员 +func (v *VipApi) Delete(ctx *gin.Context) { + var p vip.Vip + if err := ctx.ShouldBind(&p); err != nil { + response.FailWithMessage("参数有误", ctx) + return + } + if err := vipService.DeleteVip(p); err != nil { + response.FailWithMessage("删除会员失败", ctx) + return + } + response.OkWithMessage("删除会员成功", ctx) +} + +// GetVipById 获取会员详情 +func (v *VipApi) GetVipById(ctx *gin.Context) { + id := ctx.Param("id") + vip, err := vipService.GetVipById(id) + if err != nil { + response.FailWithMessage("获取会员详情失败", ctx) + return + } + response.OkWithDetailed(vip, "获取会员详情成功", ctx) +} diff --git a/initialize/gorm_biz.go b/initialize/gorm_biz.go index 11c21b4..e88a681 100644 --- a/initialize/gorm_biz.go +++ b/initialize/gorm_biz.go @@ -6,6 +6,7 @@ import ( "git.echol.cn/loser/lckt/model/bot" "git.echol.cn/loser/lckt/model/category" "git.echol.cn/loser/lckt/model/user" + "git.echol.cn/loser/lckt/model/vip" ) func bizModel() error { @@ -15,6 +16,7 @@ func bizModel() error { bot.Bot{}, article.Article{}, user.User{}, + vip.Vip{}, ) if err != nil { return err diff --git a/initialize/router_biz.go b/initialize/router_biz.go index d9141ad..ffda167 100644 --- a/initialize/router_biz.go +++ b/initialize/router_biz.go @@ -30,4 +30,8 @@ func initBizRouter(routers ...*gin.RouterGroup) { userRouter.InitUserRouter(privateGroup, publicGroup) userRouter.InitAppUserRouter(publicGroup) } + { + vipRouter := router.RouterGroupApp.Vip + vipRouter.InitVipRouter(privateGroup, publicGroup) + } } diff --git a/model/vip/vip.go b/model/vip/vip.go new file mode 100644 index 0000000..82c329d --- /dev/null +++ b/model/vip/vip.go @@ -0,0 +1,16 @@ +package vip + +import "git.echol.cn/loser/lckt/global" + +type Vip struct { + global.GVA_MODEL + Name string `json:"name" form:"name" gorm:"comment:会员名称"` // 会员名称 + Level int `json:"level" form:"level" gorm:"comment:会员等级 1 Vip 2 Svip"` // 会员等级 + Price float64 `json:"price" form:"price" gorm:"comment:会员价格"` // 会员价格 + Expiration int64 `json:"expiration" form:"expiration" gorm:"comment:会员有效期"` // 会员过期时间 +} + +// TableName 设置表名 +func (Vip) TableName() string { + return "lckt_vip" +} diff --git a/router/enter.go b/router/enter.go index 60dfc04..391caf6 100644 --- a/router/enter.go +++ b/router/enter.go @@ -7,6 +7,7 @@ import ( "git.echol.cn/loser/lckt/router/example" "git.echol.cn/loser/lckt/router/system" "git.echol.cn/loser/lckt/router/user" + "git.echol.cn/loser/lckt/router/vip" ) var RouterGroupApp = new(RouterGroup) @@ -18,4 +19,5 @@ type RouterGroup struct { Bot bot.RouterGroup Article article.RouterGroup User user.UserRouter + Vip vip.VipRouter } diff --git a/router/vip/enter.go b/router/vip/enter.go new file mode 100644 index 0000000..008b088 --- /dev/null +++ b/router/vip/enter.go @@ -0,0 +1,7 @@ +package vip + +import api "git.echol.cn/loser/lckt/api/v1" + +type RouterGroup struct{ VipRouter } + +var vipApi = api.ApiGroupApp.VipApiGroup diff --git a/router/vip/vip.go b/router/vip/vip.go new file mode 100644 index 0000000..bb8f3d8 --- /dev/null +++ b/router/vip/vip.go @@ -0,0 +1,23 @@ +package vip + +import ( + "git.echol.cn/loser/lckt/middleware" + "github.com/gin-gonic/gin" +) + +type VipRouter struct{} + +// InitVipRouter 初始化会员路由 +func (s *VipRouter) InitVipRouter(Router *gin.RouterGroup, PublicRouter *gin.RouterGroup) { + vipRouter := Router.Group("vip").Use(middleware.OperationRecord()) + vipNoAuthRouter := PublicRouter.Group("vip").Use(middleware.OperationRecord()) + { + vipRouter.POST("", vipApi.Create) // 获取用户列表 + vipRouter.DELETE("", vipApi.Delete) // 更新用户余额 + vipRouter.PUT("", vipApi.Update) // 注册 + } + { + vipNoAuthRouter.GET(":id", vipApi.GetVipById) // 根据获取vip信息 + vipNoAuthRouter.GET("list", vipApi.GetVipList) // 获取会员列表 + } +} diff --git a/service/enter.go b/service/enter.go index 01f1564..1a94396 100644 --- a/service/enter.go +++ b/service/enter.go @@ -7,6 +7,7 @@ import ( "git.echol.cn/loser/lckt/service/example" "git.echol.cn/loser/lckt/service/system" "git.echol.cn/loser/lckt/service/user" + "git.echol.cn/loser/lckt/service/vip" ) var ServiceGroupApp = new(ServiceGroup) @@ -18,4 +19,5 @@ type ServiceGroup struct { BotServiceGroup bot.ServiceGroup ArticleGroup article.ServiceGroup UserServiceGroup user.ServiceGroup + VipServiceGroup vip.ServiceGroup } diff --git a/service/vip/enter.go b/service/vip/enter.go new file mode 100644 index 0000000..5dcd77c --- /dev/null +++ b/service/vip/enter.go @@ -0,0 +1,3 @@ +package vip + +type ServiceGroup struct{ VipService } diff --git a/service/vip/vip.go b/service/vip/vip.go new file mode 100644 index 0000000..c43e6f4 --- /dev/null +++ b/service/vip/vip.go @@ -0,0 +1,70 @@ +package vip + +import ( + "git.echol.cn/loser/lckt/global" + "git.echol.cn/loser/lckt/model/common/request" + "git.echol.cn/loser/lckt/model/vip" + "go.uber.org/zap" +) + +type VipService struct{} + +// GetVipList 获取会员列表 +func (v *VipService) GetVipList(p request.PageInfo) (vipList []vip.Vip, total int64, err error) { + limit := p.PageSize + offset := p.PageSize * (p.Page - 1) + // 创建db + db := global.GVA_DB.Model(&vip.Vip{}) + + err = db.Count(&total).Error + if err != nil { + global.GVA_LOG.Error("获取会员列表失败", zap.Error(err)) + return + } + + if limit != 0 { + db = db.Limit(limit).Offset(offset) + } + err = db.Find(&vipList).Error + if err != nil { + global.GVA_LOG.Error("获取会员列表失败", zap.Error(err)) + return + } + return +} + +// CreateVip 创建会员 +func (v *VipService) CreateVip(vip vip.Vip) (err error) { + err = global.GVA_DB.Create(&vip).Error + if err != nil { + return + } + return +} + +// UpdateVip 更新会员 +func (v *VipService) UpdateVip(vip vip.Vip) (err error) { + err = global.GVA_DB.Save(&vip).Error + if err != nil { + return + } + return +} + +// DeleteVip 删除会员 +func (v *VipService) DeleteVip(vip vip.Vip) (err error) { + err = global.GVA_DB.Delete(&vip).Error + if err != nil { + return + } + return +} + +// GetVipById 获取会员详情 +func (v *VipService) GetVipById(id string) (vip vip.Vip, err error) { + err = global.GVA_DB.Where("id = ?", id).First(&vip).Error + if err != nil { + return + } + return +}