You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

211 lines
7.5 KiB

package controller
import (
"gofaster/internal/auth/service"
"gofaster/internal/shared/response"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
// FrontendRouteController 前台路由控制器
type FrontendRouteController struct {
frontendRouteService *service.FrontendRouteService
logger *zap.Logger
}
// NewFrontendRouteController 创建前台路由控制器实例
func NewFrontendRouteController(frontendRouteService *service.FrontendRouteService, logger *zap.Logger) *FrontendRouteController {
return &FrontendRouteController{
frontendRouteService: frontendRouteService,
logger: logger,
}
}
// SyncFrontendRoute 同步单个前台路由
// @Summary 同步单个前台路由
// @Description 同步单个前台路由及其后台路由关联
// @Tags 前台路由
// @Accept json
// @Produce json
// @Param route body map[string]interface{} true "前台路由数据"
// @Success 200 {object} response.Response
// @Router /api/frontend-routes/sync [post]
func (c *FrontendRouteController) SyncFrontendRoute(ctx *gin.Context) {
var routeData map[string]interface{}
if err := ctx.ShouldBindJSON(&routeData); err != nil {
c.logger.Error("解析前台路由数据失败", zap.Error(err))
response.Error(ctx, http.StatusBadRequest, "请求参数错误", err.Error())
return
}
if err := c.frontendRouteService.SyncFrontendRoute(routeData); err != nil {
c.logger.Error("同步前台路由失败", zap.Error(err))
response.Error(ctx, http.StatusInternalServerError, "同步前台路由失败", err.Error())
return
}
response.Success(ctx, "前台路由同步成功", nil)
}
// BatchSyncFrontendRoutes 批量同步前台路由
// @Summary 批量同步前台路由
// @Description 批量同步前台路由及其后台路由关联
// @Tags 前台路由
// @Accept json
// @Produce json
// @Param routes body []map[string]interface{} true "前台路由数据列表"
// @Success 200 {object} response.Response
// @Router /api/frontend-routes/batch-sync [post]
func (c *FrontendRouteController) BatchSyncFrontendRoutes(ctx *gin.Context) {
var routesData []map[string]interface{}
if err := ctx.ShouldBindJSON(&routesData); err != nil {
c.logger.Error("解析前台路由数据失败", zap.Error(err))
response.Error(ctx, http.StatusBadRequest, "请求参数错误", err.Error())
return
}
if err := c.frontendRouteService.BatchSyncFrontendRoutes(routesData); err != nil {
c.logger.Error("批量同步前台路由失败", zap.Error(err))
response.Error(ctx, http.StatusInternalServerError, "批量同步前台路由失败", err.Error())
return
}
response.Success(ctx, "批量同步前台路由成功", nil)
}
// GetFrontendRoutes 获取前台路由列表
// @Summary 获取前台路由列表
// @Description 获取所有前台路由列表
// @Tags 前台路由
// @Produce json
// @Success 200 {object} response.Response
// @Router /api/frontend-routes [get]
func (c *FrontendRouteController) GetFrontendRoutes(ctx *gin.Context) {
routes, err := c.frontendRouteService.GetFrontendRoutes()
if err != nil {
c.logger.Error("获取前台路由列表失败", zap.Error(err))
response.Error(ctx, http.StatusInternalServerError, "获取前台路由列表失败", err.Error())
return
}
response.Success(ctx, "获取前台路由列表成功", routes)
}
// GetFrontendRouteByID 根据ID获取前台路由
// @Summary 根据ID获取前台路由
// @Description 根据ID获取前台路由详情
// @Tags 前台路由
// @Produce json
// @Param id path int true "前台路由ID"
// @Success 200 {object} response.Response
// @Router /api/frontend-routes/{id} [get]
func (c *FrontendRouteController) GetFrontendRouteByID(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.logger.Error("解析前台路由ID失败", zap.Error(err))
response.Error(ctx, http.StatusBadRequest, "无效的前台路由ID", err.Error())
return
}
route, err := c.frontendRouteService.GetFrontendRouteByID(uint(id))
if err != nil {
c.logger.Error("获取前台路由失败", zap.Error(err))
response.Error(ctx, http.StatusInternalServerError, "获取前台路由失败", err.Error())
return
}
response.Success(ctx, "获取前台路由成功", route)
}
// GetFrontendRoutesByModule 根据模块获取前台路由
// @Summary 根据模块获取前台路由
// @Description 根据模块获取前台路由列表
// @Tags 前台路由
// @Produce json
// @Param module query string true "模块名称"
// @Success 200 {object} response.Response
// @Router /api/frontend-routes/by-module [get]
func (c *FrontendRouteController) GetFrontendRoutesByModule(ctx *gin.Context) {
module := ctx.Query("module")
if module == "" {
c.logger.Error("模块参数为空")
response.Error(ctx, http.StatusBadRequest, "模块参数不能为空", "module parameter is required")
return
}
routes, err := c.frontendRouteService.GetFrontendRoutesByModule(module)
if err != nil {
c.logger.Error("根据模块获取前台路由失败", zap.Error(err))
response.Error(ctx, http.StatusInternalServerError, "根据模块获取前台路由失败", err.Error())
return
}
response.Success(ctx, "根据模块获取前台路由成功", routes)
}
// GetRouteRelations 获取路由关联关系
// @Summary 获取路由关联关系
// @Description 获取前台路由与后台路由的关联关系
// @Tags 前台路由
// @Produce json
// @Success 200 {object} response.Response
// @Router /api/frontend-routes/relations [get]
func (c *FrontendRouteController) GetRouteRelations(ctx *gin.Context) {
relations, err := c.frontendRouteService.GetRouteRelations()
if err != nil {
c.logger.Error("获取路由关联关系失败", zap.Error(err))
response.Error(ctx, http.StatusInternalServerError, "获取路由关联关系失败", err.Error())
return
}
response.Success(ctx, "获取路由关联关系成功", relations)
}
// GetRouteRelationsByFrontendRouteID 根据前台路由ID获取关联关系
// @Summary 根据前台路由ID获取关联关系
// @Description 根据前台路由ID获取其与后台路由的关联关系
// @Tags 前台路由
// @Produce json
// @Param id path int true "前台路由ID"
// @Success 200 {object} response.Response
// @Router /api/frontend-routes/{id}/relations [get]
func (c *FrontendRouteController) GetRouteRelationsByFrontendRouteID(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.logger.Error("解析前台路由ID失败", zap.Error(err))
response.Error(ctx, http.StatusBadRequest, "无效的前台路由ID", err.Error())
return
}
relations, err := c.frontendRouteService.GetRouteRelationsByFrontendRouteID(uint(id))
if err != nil {
c.logger.Error("获取前台路由关联关系失败", zap.Error(err))
response.Error(ctx, http.StatusInternalServerError, "获取前台路由关联关系失败", err.Error())
return
}
response.Success(ctx, "获取前台路由关联关系成功", relations)
}
// GetStats 获取统计信息
// @Summary 获取前台路由统计信息
// @Description 获取前台路由和路由关联的统计信息
// @Tags 前台路由
// @Produce json
// @Success 200 {object} response.Response
// @Router /api/frontend-routes/stats [get]
func (c *FrontendRouteController) GetStats(ctx *gin.Context) {
stats, err := c.frontendRouteService.GetStats()
if err != nil {
c.logger.Error("获取前台路由统计信息失败", zap.Error(err))
response.Error(ctx, http.StatusInternalServerError, "获取前台路由统计信息失败", err.Error())
return
}
response.Success(ctx, "获取前台路由统计信息成功", stats)
}