411 lines
10 KiB
Go
411 lines
10 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"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"
|
|
)
|
|
|
|
// BannerService Banner服务
|
|
type BannerService struct{}
|
|
|
|
// NewBannerService 创建Banner服务实例
|
|
func NewBannerService() *BannerService {
|
|
return &BannerService{}
|
|
}
|
|
|
|
// 定义请求和响应结构体
|
|
type AdminBannerListReq struct {
|
|
Page int `json:"page"`
|
|
Size int `json:"size"`
|
|
Sort int `json:"sort"`
|
|
Status int `json:"status"`
|
|
Keyword string `json:"keyword"`
|
|
Category string `json:"category"`
|
|
}
|
|
|
|
type AdminBannerListRes struct {
|
|
List []AdminBannerItem `json:"list"`
|
|
Total int `json:"total"`
|
|
Page int `json:"page"`
|
|
Size int `json:"size"`
|
|
}
|
|
|
|
type AdminBannerItem struct {
|
|
Id uint `json:"id"`
|
|
Title string `json:"title"`
|
|
ImageUrl string `json:"image_url"`
|
|
LinkUrl string `json:"link_url"`
|
|
Sort int `json:"sort"`
|
|
Status int `json:"status"`
|
|
Description string `json:"description"`
|
|
CreatedAt int64 `json:"created_at"`
|
|
UpdatedAt int64 `json:"updated_at"`
|
|
}
|
|
|
|
type AdminBannerDetailReq struct {
|
|
Id uint `json:"id"`
|
|
}
|
|
|
|
type AdminBannerDetailRes struct {
|
|
Banner AdminBannerDetail `json:"banner"`
|
|
}
|
|
|
|
type AdminBannerDetail struct {
|
|
Id uint `json:"id"`
|
|
Title string `json:"title"`
|
|
ImageUrl string `json:"image_url"`
|
|
LinkUrl string `json:"link_url"`
|
|
Sort int `json:"sort"`
|
|
Status int `json:"status"`
|
|
Description string `json:"description"`
|
|
CreatedAt int64 `json:"created_at"`
|
|
UpdatedAt int64 `json:"updated_at"`
|
|
}
|
|
|
|
type AdminBannerCreateReq struct {
|
|
Title string `json:"title"`
|
|
ImageUrl string `json:"image_url"`
|
|
LinkUrl string `json:"link_url"`
|
|
Sort int `json:"sort"`
|
|
Status int `json:"status"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
type AdminBannerUpdateReq struct {
|
|
Id uint `json:"id"`
|
|
Title string `json:"title"`
|
|
ImageUrl string `json:"image_url"`
|
|
LinkUrl string `json:"link_url"`
|
|
Sort int `json:"sort"`
|
|
Status int `json:"status"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
type AdminBannerDeleteReq struct {
|
|
Id uint `json:"id"`
|
|
}
|
|
|
|
type AdminBannerUpdateStatusReq struct {
|
|
Id uint `json:"id"`
|
|
Status int `json:"status"`
|
|
}
|
|
|
|
type AdminBannerBatchDeleteReq struct {
|
|
Ids []uint `json:"ids"`
|
|
}
|
|
|
|
// UserGetList 用户获取轮播图列表
|
|
func (s *BannerService) UserGetList(r *ghttp.Request) {
|
|
// 获取请求参数
|
|
page := r.Get("page", 1).Int()
|
|
size := r.Get("size", 10).Int()
|
|
sort := r.Get("sort", 0).Int()
|
|
|
|
// 构建查询条件
|
|
query := dao.Banner.Ctx(r.Context()).Where("status", 1) // 只显示启用的轮播图
|
|
|
|
// 排序筛选
|
|
if sort > 0 {
|
|
query = query.Where("sort", sort)
|
|
}
|
|
|
|
// 获取总数
|
|
total, err := query.Count()
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "获取轮播图总数失败")
|
|
return
|
|
}
|
|
|
|
// 获取列表数据
|
|
var banners []entity.Banner
|
|
err = query.Order("sort ASC, id DESC").
|
|
Limit((page-1)*size, size).
|
|
Scan(&banners)
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "获取轮播图列表失败")
|
|
return
|
|
}
|
|
|
|
// 转换为响应格式
|
|
list := make([]g.Map, 0, len(banners))
|
|
for _, banner := range banners {
|
|
list = append(list, g.Map{
|
|
"id": banner.Id,
|
|
"title": banner.Title,
|
|
"image_url": banner.ImageUrl,
|
|
"link_url": banner.LinkUrl,
|
|
"sort": banner.Sort,
|
|
"status": banner.Status,
|
|
"created_at": banner.CreatedAt.Unix(),
|
|
"updated_at": banner.UpdatedAt.Unix(),
|
|
})
|
|
}
|
|
|
|
response.Success(r, g.Map{
|
|
"list": list,
|
|
"total": total,
|
|
"page": page,
|
|
"size": size,
|
|
})
|
|
}
|
|
|
|
// UserGetDetail 用户获取轮播图详情
|
|
func (s *BannerService) UserGetDetail(r *ghttp.Request) {
|
|
id := r.Get("id").Uint()
|
|
if id == 0 {
|
|
response.Error(r, response.CodeInvalidParam, "轮播图ID不能为空")
|
|
return
|
|
}
|
|
|
|
var banner entity.Banner
|
|
err := dao.Banner.Ctx(r.Context()).Where("id", id).Where("status", 1).Scan(&banner)
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "获取轮播图详情失败")
|
|
return
|
|
}
|
|
if banner.Id == 0 {
|
|
response.Error(r, response.CodeNotFound, "轮播图不存在")
|
|
return
|
|
}
|
|
|
|
response.Success(r, g.Map{
|
|
"id": banner.Id,
|
|
"title": banner.Title,
|
|
"image_url": banner.ImageUrl,
|
|
"link_url": banner.LinkUrl,
|
|
"sort": banner.Sort,
|
|
"status": banner.Status,
|
|
"created_at": banner.CreatedAt.Unix(),
|
|
"updated_at": banner.UpdatedAt.Unix(),
|
|
})
|
|
}
|
|
|
|
// AdminCreate 管理员创建轮播图
|
|
func (s *BannerService) AdminCreate(r *ghttp.Request) {
|
|
// 获取请求参数
|
|
title := r.Get("title").String()
|
|
imageUrl := r.Get("image_url").String()
|
|
linkUrl := r.Get("link_url").String()
|
|
sort := r.Get("sort", 0).Int()
|
|
status := r.Get("status", 1).Int()
|
|
|
|
// 参数验证
|
|
if title == "" {
|
|
response.Error(r, response.CodeInvalidParam, "标题不能为空")
|
|
return
|
|
}
|
|
if imageUrl == "" {
|
|
response.Error(r, response.CodeInvalidParam, "图片地址不能为空")
|
|
return
|
|
}
|
|
|
|
// 创建轮播图
|
|
data := &entity.Banner{
|
|
Title: title,
|
|
ImageUrl: imageUrl,
|
|
LinkUrl: linkUrl,
|
|
Sort: sort,
|
|
Status: status,
|
|
CreatedAt: gtime.Now(),
|
|
UpdatedAt: gtime.Now(),
|
|
}
|
|
|
|
id, err := dao.Banner.Ctx(r.Context()).Data(data).InsertAndGetId()
|
|
if err != nil {
|
|
response.Error(r, response.CodeInternalError, "创建轮播图失败")
|
|
return
|
|
}
|
|
|
|
response.Success(r, g.Map{
|
|
"id": uint(id),
|
|
})
|
|
}
|
|
|
|
// AdminUpdate 管理员更新轮播图
|
|
func (s *BannerService) AdminUpdate(ctx context.Context, req *AdminBannerUpdateReq) error {
|
|
// 检查轮播图是否存在
|
|
count, err := dao.Banner.Ctx(ctx).Where("id", req.Id).Count()
|
|
if err != nil {
|
|
return gerror.New("检查轮播图失败")
|
|
}
|
|
if count == 0 {
|
|
return gerror.New("轮播图不存在")
|
|
}
|
|
|
|
// 更新轮播图
|
|
data := g.Map{
|
|
"title": req.Title,
|
|
"image_url": req.ImageUrl,
|
|
"link_url": req.LinkUrl,
|
|
"sort": req.Sort,
|
|
"status": req.Status,
|
|
"description": req.Description,
|
|
"updated_at": gtime.Now(),
|
|
}
|
|
|
|
_, err = dao.Banner.Ctx(ctx).Where("id", req.Id).Data(data).Update()
|
|
if err != nil {
|
|
return gerror.New("更新轮播图失败")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// AdminDelete 管理员删除轮播图
|
|
func (s *BannerService) AdminDelete(ctx context.Context, req *AdminBannerDeleteReq) error {
|
|
// 检查轮播图是否存在
|
|
count, err := dao.Banner.Ctx(ctx).Where("id", req.Id).Count()
|
|
if err != nil {
|
|
return gerror.New("检查轮播图失败")
|
|
}
|
|
if count == 0 {
|
|
return gerror.New("轮播图不存在")
|
|
}
|
|
|
|
// 删除轮播图
|
|
_, err = dao.Banner.Ctx(ctx).Where("id", req.Id).Delete()
|
|
if err != nil {
|
|
return gerror.New("删除轮播图失败")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// AdminGetDetail 管理员获取轮播图详情
|
|
func (s *BannerService) AdminGetDetail(ctx context.Context, req *AdminBannerDetailReq) (*AdminBannerDetailRes, error) {
|
|
var banner entity.Banner
|
|
err := dao.Banner.Ctx(ctx).Where("id", req.Id).Scan(&banner)
|
|
if err != nil {
|
|
return nil, gerror.New("获取轮播图详情失败")
|
|
}
|
|
if banner.Id == 0 {
|
|
return nil, gerror.New("轮播图不存在")
|
|
}
|
|
|
|
return &AdminBannerDetailRes{
|
|
Banner: AdminBannerDetail{
|
|
Id: banner.Id,
|
|
Title: banner.Title,
|
|
ImageUrl: banner.ImageUrl,
|
|
LinkUrl: banner.LinkUrl,
|
|
Sort: banner.Sort,
|
|
Status: banner.Status,
|
|
CreatedAt: banner.CreatedAt.Unix(),
|
|
UpdatedAt: banner.UpdatedAt.Unix(),
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// AdminGetList 管理员获取轮播图列表
|
|
func (s *BannerService) AdminGetList(ctx context.Context, req *AdminBannerListReq) (*AdminBannerListRes, error) {
|
|
// 设置默认值
|
|
if req.Page <= 0 {
|
|
req.Page = 1
|
|
}
|
|
if req.Size <= 0 {
|
|
req.Size = 10
|
|
}
|
|
|
|
// 构建查询条件
|
|
query := dao.Banner.Ctx(ctx)
|
|
|
|
// 状态筛选
|
|
if req.Status >= 0 {
|
|
query = query.Where("status", req.Status)
|
|
}
|
|
|
|
// 排序筛选
|
|
if req.Sort > 0 {
|
|
query = query.Where("sort", req.Sort)
|
|
}
|
|
|
|
// 关键词搜索
|
|
if req.Keyword != "" {
|
|
keyword := "%" + req.Keyword + "%"
|
|
query = query.Where("title LIKE ?", keyword)
|
|
}
|
|
|
|
// 获取总数
|
|
total, err := query.Count()
|
|
if err != nil {
|
|
return nil, gerror.New("获取轮播图总数失败")
|
|
}
|
|
|
|
// 获取列表数据
|
|
var banners []entity.Banner
|
|
err = query.Order("sort ASC, id DESC").
|
|
Limit((req.Page-1)*req.Size, req.Size).
|
|
Scan(&banners)
|
|
if err != nil {
|
|
return nil, gerror.New("获取轮播图列表失败")
|
|
}
|
|
|
|
// 转换为响应格式
|
|
list := make([]AdminBannerItem, 0, len(banners))
|
|
for _, banner := range banners {
|
|
list = append(list, AdminBannerItem{
|
|
Id: banner.Id,
|
|
Title: banner.Title,
|
|
ImageUrl: banner.ImageUrl,
|
|
LinkUrl: banner.LinkUrl,
|
|
Sort: banner.Sort,
|
|
Status: banner.Status,
|
|
Description: "", // 实体中没有此字段,设为空
|
|
CreatedAt: banner.CreatedAt.Unix(),
|
|
UpdatedAt: banner.UpdatedAt.Unix(),
|
|
})
|
|
}
|
|
|
|
return &AdminBannerListRes{
|
|
List: list,
|
|
Total: total,
|
|
Page: req.Page,
|
|
Size: req.Size,
|
|
}, nil
|
|
}
|
|
|
|
// AdminUpdateStatus 管理员更新轮播图状态
|
|
func (s *BannerService) AdminUpdateStatus(ctx context.Context, req *AdminBannerUpdateStatusReq) error {
|
|
// 检查轮播图是否存在
|
|
count, err := dao.Banner.Ctx(ctx).Where("id", req.Id).Count()
|
|
if err != nil {
|
|
return gerror.New("检查轮播图失败")
|
|
}
|
|
if count == 0 {
|
|
return gerror.New("轮播图不存在")
|
|
}
|
|
|
|
// 更新状态
|
|
_, err = dao.Banner.Ctx(ctx).Where("id", req.Id).Data(g.Map{
|
|
"status": req.Status,
|
|
"updated_at": gtime.Now(),
|
|
}).Update()
|
|
if err != nil {
|
|
return gerror.New("更新轮播图状态失败")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// AdminBatchDelete 管理员批量删除轮播图
|
|
func (s *BannerService) AdminBatchDelete(ctx context.Context, req *AdminBannerBatchDeleteReq) error {
|
|
if len(req.Ids) == 0 {
|
|
return gerror.New("请选择要删除的轮播图")
|
|
}
|
|
|
|
// 批量删除
|
|
_, err := dao.Banner.Ctx(ctx).WhereIn("id", req.Ids).Delete()
|
|
if err != nil {
|
|
return gerror.New("批量删除轮播图失败")
|
|
}
|
|
|
|
return nil
|
|
} |