init
This commit is contained in:
383
server/internal/handler/admin_banner.go
Normal file
383
server/internal/handler/admin_banner.go
Normal file
@@ -0,0 +1,383 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"dianshang/internal/model"
|
||||
"dianshang/internal/service"
|
||||
"dianshang/pkg/response"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AdminBannerHandler 管理员轮播图处理器
|
||||
type AdminBannerHandler struct {
|
||||
bannerService *service.BannerService
|
||||
}
|
||||
|
||||
// NewAdminBannerHandler 创建管理员轮播图处理器
|
||||
func NewAdminBannerHandler(bannerService *service.BannerService) *AdminBannerHandler {
|
||||
return &AdminBannerHandler{
|
||||
bannerService: bannerService,
|
||||
}
|
||||
}
|
||||
|
||||
// GetBannerList 获取轮播图列表
|
||||
func (h *AdminBannerHandler) GetBannerList(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
|
||||
statusStr := c.Query("status")
|
||||
|
||||
var status *int
|
||||
if statusStr != "" {
|
||||
statusInt, err := strconv.Atoi(statusStr)
|
||||
if err == nil {
|
||||
status = &statusInt
|
||||
}
|
||||
}
|
||||
|
||||
banners, total, err := h.bannerService.GetBannerList(page, pageSize, status)
|
||||
if err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
response.Page(c, banners, total, page, pageSize)
|
||||
}
|
||||
|
||||
// GetBannerDetail 获取轮播图详情
|
||||
func (h *AdminBannerHandler) GetBannerDetail(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "轮播图ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
banner, err := h.bannerService.GetBannerByID(uint(id))
|
||||
if err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, banner)
|
||||
}
|
||||
|
||||
// CreateBanner 创建轮播图
|
||||
func (h *AdminBannerHandler) CreateBanner(c *gin.Context) {
|
||||
var req struct {
|
||||
Title string `json:"title" binding:"required"`
|
||||
Image string `json:"image" binding:"required"`
|
||||
LinkType int `json:"link_type"`
|
||||
LinkValue string `json:"link_value"`
|
||||
Sort int `json:"sort"`
|
||||
Status int `json:"status"`
|
||||
StartTime *time.Time `json:"start_time"`
|
||||
EndTime *time.Time `json:"end_time"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
banner := &model.Banner{
|
||||
Title: req.Title,
|
||||
Image: req.Image,
|
||||
LinkType: req.LinkType,
|
||||
LinkValue: req.LinkValue,
|
||||
Sort: req.Sort,
|
||||
Status: req.Status,
|
||||
StartTime: req.StartTime,
|
||||
EndTime: req.EndTime,
|
||||
}
|
||||
|
||||
if err := h.bannerService.CreateBanner(banner); err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, banner)
|
||||
}
|
||||
|
||||
// UpdateBanner 更新轮播图
|
||||
func (h *AdminBannerHandler) UpdateBanner(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "轮播图ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Title string `json:"title"`
|
||||
Image string `json:"image"`
|
||||
LinkType int `json:"link_type"`
|
||||
LinkValue string `json:"link_value"`
|
||||
Sort int `json:"sort"`
|
||||
Status int `json:"status"`
|
||||
StartTime *time.Time `json:"start_time"`
|
||||
EndTime *time.Time `json:"end_time"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 获取现有的轮播图
|
||||
banner, err := h.bannerService.GetBannerByID(uint(id))
|
||||
if err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
// 更新字段
|
||||
if req.Title != "" {
|
||||
banner.Title = req.Title
|
||||
}
|
||||
if req.Image != "" {
|
||||
banner.Image = req.Image
|
||||
}
|
||||
if req.LinkType != 0 {
|
||||
banner.LinkType = req.LinkType
|
||||
}
|
||||
if req.LinkValue != "" {
|
||||
banner.LinkValue = req.LinkValue
|
||||
}
|
||||
if req.Sort != 0 {
|
||||
banner.Sort = req.Sort
|
||||
}
|
||||
banner.Status = req.Status
|
||||
if req.StartTime != nil {
|
||||
banner.StartTime = req.StartTime
|
||||
}
|
||||
if req.EndTime != nil {
|
||||
banner.EndTime = req.EndTime
|
||||
}
|
||||
|
||||
if err := h.bannerService.UpdateBanner(uint(id), banner); err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, banner)
|
||||
}
|
||||
|
||||
// DeleteBanner 删除轮播图
|
||||
func (h *AdminBannerHandler) DeleteBanner(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "轮播图ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.bannerService.DeleteBanner(uint(id)); err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// UpdateBannerStatus 更新轮播图状态
|
||||
func (h *AdminBannerHandler) UpdateBannerStatus(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "轮播图ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Status int `json:"status" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.bannerService.UpdateBannerStatus(uint(id), req.Status); err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// BatchUpdateBannerStatus 批量更新轮播图状态
|
||||
func (h *AdminBannerHandler) BatchUpdateBannerStatus(c *gin.Context) {
|
||||
var req struct {
|
||||
IDs []uint `json:"ids" binding:"required"`
|
||||
Status int `json:"status" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.bannerService.BatchUpdateBannerStatus(req.IDs, req.Status); err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// BatchDeleteBanners 批量删除轮播图
|
||||
func (h *AdminBannerHandler) BatchDeleteBanners(c *gin.Context) {
|
||||
var req struct {
|
||||
IDs []uint `json:"ids" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "轮播图ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.bannerService.BatchDeleteBanners(req.IDs); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// GetBannerStatistics 获取轮播图统计
|
||||
func (h *AdminBannerHandler) GetBannerStatistics(c *gin.Context) {
|
||||
statistics, err := h.bannerService.GetBannerStatistics()
|
||||
if err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, statistics)
|
||||
}
|
||||
|
||||
// GetBannersByStatus 根据状态获取轮播图
|
||||
func (h *AdminBannerHandler) GetBannersByStatus(c *gin.Context) {
|
||||
statusStr := c.Param("status")
|
||||
status, err := strconv.Atoi(statusStr)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "状态格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
banners, err := h.bannerService.GetBannersByStatus(status)
|
||||
if err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, banners)
|
||||
}
|
||||
|
||||
// BatchUpdateBannerSort 批量更新轮播图排序
|
||||
func (h *AdminBannerHandler) BatchUpdateBannerSort(c *gin.Context) {
|
||||
var req struct {
|
||||
Items []struct {
|
||||
ID uint `json:"id" binding:"required"`
|
||||
Sort int `json:"sort" binding:"required"`
|
||||
} `json:"items" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
for _, item := range req.Items {
|
||||
if err := h.bannerService.UpdateBannerSort(item.ID, item.Sort); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, "更新排序失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// GetBannersByDateRange 根据日期范围获取轮播图
|
||||
func (h *AdminBannerHandler) GetBannersByDateRange(c *gin.Context) {
|
||||
startDateStr := c.Query("start_date")
|
||||
endDateStr := c.Query("end_date")
|
||||
|
||||
if startDateStr == "" || endDateStr == "" {
|
||||
response.BadRequest(c, "开始日期和结束日期不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 解析日期字符串
|
||||
startDate, err := time.Parse("2006-01-02", startDateStr)
|
||||
if err != nil {
|
||||
response.BadRequest(c, "开始日期格式错误,应为YYYY-MM-DD")
|
||||
return
|
||||
}
|
||||
|
||||
endDate, err := time.Parse("2006-01-02", endDateStr)
|
||||
if err != nil {
|
||||
response.BadRequest(c, "结束日期格式错误,应为YYYY-MM-DD")
|
||||
return
|
||||
}
|
||||
|
||||
banners, err := h.bannerService.GetBannersByDateRange(startDate, endDate)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, banners)
|
||||
}
|
||||
|
||||
// CleanExpiredBanners 清理过期轮播图
|
||||
func (h *AdminBannerHandler) CleanExpiredBanners(c *gin.Context) {
|
||||
// 先获取过期轮播图统计
|
||||
stats, err := h.bannerService.GetBannerStatistics()
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
expiredCount := 0
|
||||
if expired, ok := stats["expired"].(int); ok {
|
||||
expiredCount = expired
|
||||
}
|
||||
|
||||
// 执行清理
|
||||
err = h.bannerService.CleanExpiredBanners()
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, map[string]interface{}{
|
||||
"cleaned_count": expiredCount,
|
||||
"message": "清理完成",
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateBannerSort 更新轮播图排序
|
||||
func (h *AdminBannerHandler) UpdateBannerSort(c *gin.Context) {
|
||||
var req struct {
|
||||
Items []struct {
|
||||
ID uint `json:"id" binding:"required"`
|
||||
Sort int `json:"sort" binding:"required"`
|
||||
} `json:"items" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
for _, item := range req.Items {
|
||||
if err := h.bannerService.UpdateBannerSort(item.ID, item.Sort); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
response.Success(c, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user