2025-07-11 02:30:57 +08:00
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"net/http"
|
2025-07-17 14:08:52 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/ctwj/panResManage/db/converter"
|
|
|
|
|
|
"github.com/ctwj/panResManage/db/dto"
|
|
|
|
|
|
"github.com/ctwj/panResManage/db/repo"
|
|
|
|
|
|
"github.com/ctwj/panResManage/utils"
|
2025-07-11 02:30:57 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// SystemConfigHandler 系统配置处理器
|
|
|
|
|
|
type SystemConfigHandler struct {
|
|
|
|
|
|
systemConfigRepo repo.SystemConfigRepository
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewSystemConfigHandler 创建系统配置处理器
|
|
|
|
|
|
func NewSystemConfigHandler(systemConfigRepo repo.SystemConfigRepository) *SystemConfigHandler {
|
|
|
|
|
|
return &SystemConfigHandler{
|
|
|
|
|
|
systemConfigRepo: systemConfigRepo,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetConfig 获取系统配置
|
|
|
|
|
|
func (h *SystemConfigHandler) GetConfig(c *gin.Context) {
|
|
|
|
|
|
config, err := h.systemConfigRepo.GetOrCreateDefault()
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "获取系统配置失败", http.StatusInternalServerError)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
configResponse := converter.SystemConfigToResponse(config)
|
2025-07-11 17:45:16 +08:00
|
|
|
|
SuccessResponse(c, configResponse)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateConfig 更新系统配置
|
|
|
|
|
|
func (h *SystemConfigHandler) UpdateConfig(c *gin.Context) {
|
|
|
|
|
|
var req dto.SystemConfigRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "请求参数错误", http.StatusBadRequest)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 验证参数
|
|
|
|
|
|
if req.SiteTitle == "" {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "网站标题不能为空", http.StatusBadRequest)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if req.AutoProcessInterval < 1 || req.AutoProcessInterval > 1440 {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "自动处理间隔必须在1-1440分钟之间", http.StatusBadRequest)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if req.PageSize < 10 || req.PageSize > 500 {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "每页显示数量必须在10-500之间", http.StatusBadRequest)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 转换为实体
|
|
|
|
|
|
config := converter.RequestToSystemConfig(&req)
|
|
|
|
|
|
if config == nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "数据转换失败", http.StatusInternalServerError)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存配置
|
|
|
|
|
|
err := h.systemConfigRepo.Upsert(config)
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "保存系统配置失败", http.StatusInternalServerError)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 返回更新后的配置
|
|
|
|
|
|
updatedConfig, err := h.systemConfigRepo.FindFirst()
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "获取更新后的配置失败", http.StatusInternalServerError)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
configResponse := converter.SystemConfigToResponse(updatedConfig)
|
2025-07-11 17:45:16 +08:00
|
|
|
|
SuccessResponse(c, configResponse)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetSystemConfig 获取系统配置(使用全局repoManager)
|
|
|
|
|
|
func GetSystemConfig(c *gin.Context) {
|
|
|
|
|
|
config, err := repoManager.SystemConfigRepository.GetOrCreateDefault()
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "获取系统配置失败", http.StatusInternalServerError)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
configResponse := converter.SystemConfigToResponse(config)
|
2025-07-11 17:45:16 +08:00
|
|
|
|
SuccessResponse(c, configResponse)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateSystemConfig 更新系统配置(使用全局repoManager)
|
|
|
|
|
|
func UpdateSystemConfig(c *gin.Context) {
|
|
|
|
|
|
var req dto.SystemConfigRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "请求参数错误", http.StatusBadRequest)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 验证参数
|
|
|
|
|
|
if req.SiteTitle == "" {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "网站标题不能为空", http.StatusBadRequest)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if req.AutoProcessInterval < 1 || req.AutoProcessInterval > 1440 {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "自动处理间隔必须在1-1440分钟之间", http.StatusBadRequest)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if req.PageSize < 10 || req.PageSize > 500 {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "每页显示数量必须在10-500之间", http.StatusBadRequest)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 转换为实体
|
|
|
|
|
|
config := converter.RequestToSystemConfig(&req)
|
|
|
|
|
|
if config == nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "数据转换失败", http.StatusInternalServerError)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存配置
|
|
|
|
|
|
err := repoManager.SystemConfigRepository.Upsert(config)
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "保存系统配置失败", http.StatusInternalServerError)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 17:45:16 +08:00
|
|
|
|
// 根据配置更新定时任务状态(错误不影响配置保存)
|
2025-07-12 21:23:23 +08:00
|
|
|
|
scheduler := utils.GetGlobalScheduler(
|
|
|
|
|
|
repoManager.HotDramaRepository,
|
|
|
|
|
|
repoManager.ReadyResourceRepository,
|
|
|
|
|
|
repoManager.ResourceRepository,
|
|
|
|
|
|
repoManager.SystemConfigRepository,
|
2025-07-15 12:50:24 +08:00
|
|
|
|
repoManager.PanRepository,
|
2025-07-12 21:23:23 +08:00
|
|
|
|
)
|
2025-07-11 17:45:16 +08:00
|
|
|
|
if scheduler != nil {
|
2025-07-15 12:50:24 +08:00
|
|
|
|
scheduler.UpdateSchedulerStatus(req.AutoFetchHotDramaEnabled, req.AutoProcessReadyResources)
|
2025-07-11 17:45:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 02:30:57 +08:00
|
|
|
|
// 返回更新后的配置
|
|
|
|
|
|
updatedConfig, err := repoManager.SystemConfigRepository.FindFirst()
|
|
|
|
|
|
if err != nil {
|
2025-07-11 17:45:16 +08:00
|
|
|
|
ErrorResponse(c, "获取更新后的配置失败", http.StatusInternalServerError)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
configResponse := converter.SystemConfigToResponse(updatedConfig)
|
2025-07-11 17:45:16 +08:00
|
|
|
|
SuccessResponse(c, configResponse)
|
2025-07-11 02:30:57 +08:00
|
|
|
|
}
|