599 lines
15 KiB
Go
599 lines
15 KiB
Go
package service
|
||
|
||
import (
|
||
"strconv"
|
||
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"github.com/gogf/gf/v2/net/ghttp"
|
||
"github.com/gogf/gf/v2/os/gtime"
|
||
"nl-video-api/internal/dao"
|
||
"nl-video-api/internal/model/entity"
|
||
"nl-video-api/utility/response"
|
||
)
|
||
|
||
// VipLevelService VIP等级服务
|
||
type VipLevelService struct{}
|
||
|
||
var VipLevel = &VipLevelService{}
|
||
|
||
// GetList 获取VIP等级列表
|
||
func (s *VipLevelService) GetList(r *ghttp.Request) {
|
||
// 获取请求参数
|
||
pageStr := r.Get("page", "1").String()
|
||
pageSizeStr := r.Get("page_size", "10").String()
|
||
|
||
page, err := strconv.Atoi(pageStr)
|
||
if err != nil || page < 1 {
|
||
page = 1
|
||
}
|
||
|
||
pageSize, err := strconv.Atoi(pageSizeStr)
|
||
if err != nil || pageSize < 1 {
|
||
pageSize = 10
|
||
}
|
||
|
||
// 获取总数
|
||
total, err := dao.VipLevel.Ctx(r.Context()).Count()
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "获取VIP等级总数失败")
|
||
return
|
||
}
|
||
|
||
// 获取VIP等级列表
|
||
var vipLevels []*entity.VipLevel
|
||
err = dao.VipLevel.Ctx(r.Context()).
|
||
Page(page, pageSize).
|
||
OrderAsc("level").
|
||
Scan(&vipLevels)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "获取VIP等级列表失败")
|
||
return
|
||
}
|
||
|
||
// 构建返回数据
|
||
var vipLevelItems []g.Map
|
||
for _, vipLevel := range vipLevels {
|
||
vipLevelItems = append(vipLevelItems, g.Map{
|
||
"id": vipLevel.Id,
|
||
"name": vipLevel.Name,
|
||
"level": vipLevel.Level,
|
||
"price": vipLevel.Price,
|
||
"duration": vipLevel.Duration,
|
||
"description": vipLevel.Description,
|
||
"privileges": vipLevel.Privileges,
|
||
"status": vipLevel.Status,
|
||
"created_at": vipLevel.CreatedAt.Unix(),
|
||
"updated_at": vipLevel.UpdatedAt.Unix(),
|
||
})
|
||
}
|
||
|
||
response.Success(r, g.Map{
|
||
"list": vipLevelItems,
|
||
"total": total,
|
||
"page": page,
|
||
"page_size": pageSize,
|
||
})
|
||
}
|
||
|
||
// GetDetail 获取VIP等级详情
|
||
func (s *VipLevelService) GetDetail(r *ghttp.Request) {
|
||
// 获取请求参数
|
||
idStr := r.Get("id").String()
|
||
if idStr == "" {
|
||
response.Error(r, response.CodeInvalidParam, "VIP等级ID不能为空")
|
||
return
|
||
}
|
||
|
||
id, err := strconv.Atoi(idStr)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "VIP等级ID格式错误")
|
||
return
|
||
}
|
||
|
||
// 获取VIP等级详情
|
||
var vipLevel entity.VipLevel
|
||
err = dao.VipLevel.Ctx(r.Context()).Where("id", id).Scan(&vipLevel)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "获取VIP等级详情失败")
|
||
return
|
||
}
|
||
|
||
if vipLevel.Id == 0 {
|
||
response.Error(r, response.CodeNotFound, "VIP等级不存在")
|
||
return
|
||
}
|
||
|
||
response.Success(r, g.Map{
|
||
"id": vipLevel.Id,
|
||
"name": vipLevel.Name,
|
||
"level": vipLevel.Level,
|
||
"price": vipLevel.Price,
|
||
"duration": vipLevel.Duration,
|
||
"description": vipLevel.Description,
|
||
"privileges": vipLevel.Privileges,
|
||
"status": vipLevel.Status,
|
||
"created_at": vipLevel.CreatedAt.Unix(),
|
||
"updated_at": vipLevel.UpdatedAt.Unix(),
|
||
})
|
||
}
|
||
|
||
// AdminGetList 管理员获取VIP等级列表
|
||
func (s *VipLevelService) AdminGetList(r *ghttp.Request) {
|
||
// 获取请求参数
|
||
keyword := r.Get("keyword").String()
|
||
statusStr := r.Get("status").String()
|
||
pageStr := r.Get("page", "1").String()
|
||
pageSizeStr := r.Get("page_size", "10").String()
|
||
|
||
page, err := strconv.Atoi(pageStr)
|
||
if err != nil || page < 1 {
|
||
page = 1
|
||
}
|
||
|
||
pageSize, err := strconv.Atoi(pageSizeStr)
|
||
if err != nil || pageSize < 1 {
|
||
pageSize = 10
|
||
}
|
||
|
||
// 构建查询条件
|
||
query := dao.VipLevel.Ctx(r.Context())
|
||
|
||
if keyword != "" {
|
||
query = query.Where("name LIKE ?", "%"+keyword+"%")
|
||
}
|
||
if statusStr != "" {
|
||
status, err := strconv.Atoi(statusStr)
|
||
if err == nil {
|
||
query = query.Where("status", status)
|
||
}
|
||
}
|
||
|
||
// 获取总数
|
||
total, err := query.Count()
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "获取VIP等级总数失败")
|
||
return
|
||
}
|
||
|
||
// 获取VIP等级列表
|
||
var vipLevels []*entity.VipLevel
|
||
err = query.Page(page, pageSize).OrderAsc("level").Scan(&vipLevels)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "获取VIP等级列表失败")
|
||
return
|
||
}
|
||
|
||
// 构建返回数据
|
||
var vipLevelItems []g.Map
|
||
for _, vipLevel := range vipLevels {
|
||
vipLevelItems = append(vipLevelItems, g.Map{
|
||
"id": vipLevel.Id,
|
||
"name": vipLevel.Name,
|
||
"level": vipLevel.Level,
|
||
"price": vipLevel.Price,
|
||
"duration": vipLevel.Duration,
|
||
"description": vipLevel.Description,
|
||
"privileges": vipLevel.Privileges,
|
||
"status": vipLevel.Status,
|
||
"created_at": vipLevel.CreatedAt.Unix(),
|
||
"updated_at": vipLevel.UpdatedAt.Unix(),
|
||
})
|
||
}
|
||
|
||
response.Success(r, g.Map{
|
||
"list": vipLevelItems,
|
||
"total": total,
|
||
"page": page,
|
||
"page_size": pageSize,
|
||
})
|
||
}
|
||
|
||
// AdminCreate 管理员创建VIP等级
|
||
func (s *VipLevelService) AdminCreate(r *ghttp.Request) {
|
||
// 获取请求参数
|
||
name := r.Get("name").String()
|
||
levelStr := r.Get("level").String()
|
||
priceStr := r.Get("price").String()
|
||
durationStr := r.Get("duration").String()
|
||
description := r.Get("description").String()
|
||
privileges := r.Get("privileges").String()
|
||
statusStr := r.Get("status", "1").String()
|
||
|
||
if name == "" {
|
||
response.Error(r, response.CodeInvalidParam, "VIP等级名称不能为空")
|
||
return
|
||
}
|
||
|
||
level, err := strconv.Atoi(levelStr)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "VIP等级格式错误")
|
||
return
|
||
}
|
||
|
||
price, err := strconv.ParseFloat(priceStr, 64)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "价格格式错误")
|
||
return
|
||
}
|
||
|
||
duration, err := strconv.Atoi(durationStr)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "有效期格式错误")
|
||
return
|
||
}
|
||
|
||
status, err := strconv.Atoi(statusStr)
|
||
if err != nil {
|
||
status = 1
|
||
}
|
||
|
||
// 检查等级是否已存在
|
||
count, err := dao.VipLevel.Ctx(r.Context()).Where("level", level).Count()
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "系统错误")
|
||
return
|
||
}
|
||
if count > 0 {
|
||
response.Error(r, response.CodeInvalidParam, "该等级已存在")
|
||
return
|
||
}
|
||
|
||
// 创建VIP等级
|
||
_, err = dao.VipLevel.Ctx(r.Context()).Data(g.Map{
|
||
"name": name,
|
||
"level": level,
|
||
"price": price,
|
||
"duration": duration,
|
||
"description": description,
|
||
"privileges": privileges,
|
||
"status": status,
|
||
"created_at": gtime.Now(),
|
||
"updated_at": gtime.Now(),
|
||
}).Insert()
|
||
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "创建VIP等级失败")
|
||
return
|
||
}
|
||
|
||
response.Success(r, "创建成功")
|
||
}
|
||
|
||
// AdminUpdate 管理员更新VIP等级
|
||
func (s *VipLevelService) AdminUpdate(r *ghttp.Request) {
|
||
// 获取请求参数
|
||
idStr := r.Get("id").String()
|
||
name := r.Get("name").String()
|
||
levelStr := r.Get("level").String()
|
||
priceStr := r.Get("price").String()
|
||
durationStr := r.Get("duration").String()
|
||
description := r.Get("description").String()
|
||
privileges := r.Get("privileges").String()
|
||
statusStr := r.Get("status").String()
|
||
|
||
if idStr == "" {
|
||
response.Error(r, response.CodeInvalidParam, "VIP等级ID不能为空")
|
||
return
|
||
}
|
||
|
||
id, err := strconv.Atoi(idStr)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "VIP等级ID格式错误")
|
||
return
|
||
}
|
||
|
||
// 检查VIP等级是否存在
|
||
var existVipLevel entity.VipLevel
|
||
err = dao.VipLevel.Ctx(r.Context()).Where("id", id).Scan(&existVipLevel)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "系统错误")
|
||
return
|
||
}
|
||
if existVipLevel.Id == 0 {
|
||
response.Error(r, response.CodeNotFound, "VIP等级不存在")
|
||
return
|
||
}
|
||
|
||
// 构建更新数据
|
||
updateData := g.Map{
|
||
"updated_at": gtime.Now(),
|
||
}
|
||
|
||
if name != "" {
|
||
updateData["name"] = name
|
||
}
|
||
if levelStr != "" {
|
||
level, err := strconv.Atoi(levelStr)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "VIP等级格式错误")
|
||
return
|
||
}
|
||
// 检查等级是否已被其他记录使用
|
||
count, err := dao.VipLevel.Ctx(r.Context()).Where("level", level).Where("id !=", id).Count()
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "系统错误")
|
||
return
|
||
}
|
||
if count > 0 {
|
||
response.Error(r, response.CodeInvalidParam, "该等级已存在")
|
||
return
|
||
}
|
||
updateData["level"] = level
|
||
}
|
||
if priceStr != "" {
|
||
price, err := strconv.ParseFloat(priceStr, 64)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "价格格式错误")
|
||
return
|
||
}
|
||
updateData["price"] = price
|
||
}
|
||
if durationStr != "" {
|
||
duration, err := strconv.Atoi(durationStr)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "有效期格式错误")
|
||
return
|
||
}
|
||
updateData["duration"] = duration
|
||
}
|
||
if description != "" {
|
||
updateData["description"] = description
|
||
}
|
||
if privileges != "" {
|
||
updateData["privileges"] = privileges
|
||
}
|
||
if statusStr != "" {
|
||
status, err := strconv.Atoi(statusStr)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "状态格式错误")
|
||
return
|
||
}
|
||
updateData["status"] = status
|
||
}
|
||
|
||
// 更新VIP等级
|
||
_, err = dao.VipLevel.Ctx(r.Context()).Where("id", id).Data(updateData).Update()
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "更新VIP等级失败")
|
||
return
|
||
}
|
||
|
||
response.Success(r, "更新成功")
|
||
}
|
||
|
||
// AdminDelete 管理员删除VIP等级
|
||
func (s *VipLevelService) AdminDelete(r *ghttp.Request) {
|
||
// 获取请求参数
|
||
idStr := r.Get("id").String()
|
||
if idStr == "" {
|
||
response.Error(r, response.CodeInvalidParam, "VIP等级ID不能为空")
|
||
return
|
||
}
|
||
|
||
id, err := strconv.Atoi(idStr)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "VIP等级ID格式错误")
|
||
return
|
||
}
|
||
|
||
// 检查VIP等级是否存在
|
||
count, err := dao.VipLevel.Ctx(r.Context()).Where("id", id).Count()
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "系统错误")
|
||
return
|
||
}
|
||
if count == 0 {
|
||
response.Error(r, response.CodeNotFound, "VIP等级不存在")
|
||
return
|
||
}
|
||
|
||
// 检查是否有用户正在使用该VIP等级
|
||
userCount, err := g.DB().Model("nl_user").Where("vip_level", id).Count()
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "系统错误")
|
||
return
|
||
}
|
||
if userCount > 0 {
|
||
response.Error(r, response.CodeInvalidParam, "该VIP等级正在被用户使用,无法删除")
|
||
return
|
||
}
|
||
|
||
// 删除VIP等级
|
||
_, err = dao.VipLevel.Ctx(r.Context()).Where("id", id).Delete()
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "删除VIP等级失败")
|
||
return
|
||
}
|
||
|
||
response.Success(r, "删除成功")
|
||
}
|
||
|
||
// AdminBatchDelete 管理员批量删除VIP等级
|
||
func (s *VipLevelService) AdminBatchDelete(r *ghttp.Request) {
|
||
// 获取请求参数
|
||
var ids []int
|
||
err := r.Parse(&ids)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "参数格式错误")
|
||
return
|
||
}
|
||
|
||
if len(ids) == 0 {
|
||
response.Error(r, response.CodeInvalidParam, "请选择要删除的VIP等级")
|
||
return
|
||
}
|
||
|
||
// 检查是否有用户正在使用这些VIP等级
|
||
userCount, err := g.DB().Model("nl_user").WhereIn("vip_level", ids).Count()
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "系统错误")
|
||
return
|
||
}
|
||
if userCount > 0 {
|
||
response.Error(r, response.CodeInvalidParam, "选中的VIP等级中有正在被用户使用的,无法删除")
|
||
return
|
||
}
|
||
|
||
// 批量删除
|
||
_, err = dao.VipLevel.Ctx(r.Context()).WhereIn("id", ids).Delete()
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "批量删除失败")
|
||
return
|
||
}
|
||
|
||
response.Success(r, "删除成功")
|
||
}
|
||
|
||
// AdminUpdateStatus 管理员更新VIP等级状态
|
||
func (s *VipLevelService) AdminUpdateStatus(r *ghttp.Request) {
|
||
// 获取请求参数
|
||
idStr := r.Get("id").String()
|
||
statusStr := r.Get("status").String()
|
||
|
||
if idStr == "" {
|
||
response.Error(r, response.CodeInvalidParam, "VIP等级ID不能为空")
|
||
return
|
||
}
|
||
|
||
if statusStr == "" {
|
||
response.Error(r, response.CodeInvalidParam, "状态不能为空")
|
||
return
|
||
}
|
||
|
||
id, err := strconv.Atoi(idStr)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "VIP等级ID格式错误")
|
||
return
|
||
}
|
||
|
||
status, err := strconv.Atoi(statusStr)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInvalidParam, "状态格式错误")
|
||
return
|
||
}
|
||
|
||
// 检查VIP等级是否存在
|
||
count, err := dao.VipLevel.Ctx(r.Context()).Where("id", id).Count()
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "系统错误")
|
||
return
|
||
}
|
||
if count == 0 {
|
||
response.Error(r, response.CodeNotFound, "VIP等级不存在")
|
||
return
|
||
}
|
||
|
||
// 更新状态
|
||
_, err = dao.VipLevel.Ctx(r.Context()).Where("id", id).Data(g.Map{
|
||
"status": status,
|
||
"updated_at": gtime.Now(),
|
||
}).Update()
|
||
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "更新状态失败")
|
||
return
|
||
}
|
||
|
||
response.Success(r, "更新成功")
|
||
}
|
||
|
||
// GetActiveList 获取启用的VIP等级列表
|
||
func (s *VipLevelService) GetActiveList(r *ghttp.Request) {
|
||
// 获取启用的VIP等级列表
|
||
var vipLevels []*entity.VipLevel
|
||
err := dao.VipLevel.Ctx(r.Context()).
|
||
Where("status", 1).
|
||
OrderAsc("level").
|
||
Scan(&vipLevels)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "获取VIP等级列表失败")
|
||
return
|
||
}
|
||
|
||
// 构建返回数据
|
||
var vipLevelItems []g.Map
|
||
for _, vipLevel := range vipLevels {
|
||
vipLevelItems = append(vipLevelItems, g.Map{
|
||
"id": vipLevel.Id,
|
||
"name": vipLevel.Name,
|
||
"level": vipLevel.Level,
|
||
"price": vipLevel.Price,
|
||
"duration": vipLevel.Duration,
|
||
"description": vipLevel.Description,
|
||
"privileges": vipLevel.Privileges,
|
||
})
|
||
}
|
||
|
||
response.Success(r, vipLevelItems)
|
||
}
|
||
|
||
// GetUserVipInfo 获取用户VIP信息
|
||
func (s *VipLevelService) GetUserVipInfo(r *ghttp.Request) {
|
||
// 从上下文获取用户ID
|
||
userIdValue := r.Context().Value("user_id")
|
||
if userIdValue == nil {
|
||
response.Error(r, response.CodeUnauthorized, "请先登录")
|
||
return
|
||
}
|
||
userId := userIdValue.(uint)
|
||
|
||
// 获取用户信息
|
||
var user entity.NlUser
|
||
err := g.DB().Model("nl_user").Where("id", userId).Scan(&user)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "获取用户信息失败")
|
||
return
|
||
}
|
||
|
||
if user.Id == 0 {
|
||
response.Error(r, response.CodeNotFound, "用户不存在")
|
||
return
|
||
}
|
||
|
||
// 获取VIP等级信息
|
||
var vipLevel entity.VipLevel
|
||
if user.VipLevel > 0 {
|
||
dao.VipLevel.Ctx(r.Context()).Where("id", user.VipLevel).Scan(&vipLevel)
|
||
}
|
||
|
||
response.Success(r, g.Map{
|
||
"user_id": user.Id,
|
||
"vip_level_id": user.VipLevel,
|
||
"vip_level_name": vipLevel.Name,
|
||
"vip_expire_at": user.VipExpireTime,
|
||
"is_vip": user.VipLevel > 0 && user.VipExpireTime > int(gtime.Now().Unix()),
|
||
})
|
||
}
|
||
|
||
// GetAll 获取所有VIP等级
|
||
func (s *VipLevelService) GetAll(r *ghttp.Request) {
|
||
// 获取所有VIP等级列表
|
||
var vipLevels []*entity.VipLevel
|
||
err := dao.VipLevel.Ctx(r.Context()).
|
||
OrderAsc("level").
|
||
Scan(&vipLevels)
|
||
if err != nil {
|
||
response.Error(r, response.CodeInternalError, "获取VIP等级列表失败")
|
||
return
|
||
}
|
||
|
||
// 构建返回数据
|
||
var vipLevelItems []g.Map
|
||
for _, vipLevel := range vipLevels {
|
||
vipLevelItems = append(vipLevelItems, g.Map{
|
||
"id": vipLevel.Id,
|
||
"name": vipLevel.Name,
|
||
"level": vipLevel.Level,
|
||
"price": vipLevel.Price,
|
||
"duration": vipLevel.Duration,
|
||
"description": vipLevel.Description,
|
||
"privileges": vipLevel.Privileges,
|
||
"status": vipLevel.Status,
|
||
"created_at": vipLevel.CreatedAt.Unix(),
|
||
"updated_at": vipLevel.UpdatedAt.Unix(),
|
||
})
|
||
}
|
||
|
||
response.Success(r, vipLevelItems)
|
||
}
|