205 lines
6.0 KiB
Go
205 lines
6.0 KiB
Go
package system
|
||
|
||
import (
|
||
"strconv"
|
||
|
||
"git.echol.cn/loser/lckt/global"
|
||
"git.echol.cn/loser/lckt/model/common/response"
|
||
"git.echol.cn/loser/lckt/model/system/request"
|
||
"github.com/gin-gonic/gin"
|
||
"go.uber.org/zap"
|
||
)
|
||
|
||
type PayConfigApi struct{}
|
||
|
||
// GetPayConfigList 获取支付配置列表
|
||
// @Tags PayConfig
|
||
// @Summary 获取支付配置列表
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data query request.PayConfigSearch true "分页获取支付配置列表"
|
||
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
|
||
// @Router /payConfig/list [get]
|
||
func (p *PayConfigApi) GetPayConfigList(c *gin.Context) {
|
||
var req request.PayConfigSearch
|
||
if err := c.ShouldBindQuery(&req); err != nil {
|
||
response.FailWithMessage("参数错误: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
// 处理 enable 参数:如果参数不存在或为空字符串,设置为 nil(表示不筛选)
|
||
enableParam := c.Query("enable")
|
||
if enableParam == "" {
|
||
req.Enable = nil
|
||
}
|
||
|
||
if req.Page == 0 {
|
||
req.Page = 1
|
||
}
|
||
if req.PageSize == 0 {
|
||
req.PageSize = 10
|
||
}
|
||
|
||
list, total, err := payConfigService.GetPayConfigList(req)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取支付配置列表失败", zap.Error(err))
|
||
response.FailWithMessage("获取失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
response.OkWithDetailed(response.PageResult{
|
||
List: list,
|
||
Total: total,
|
||
Page: req.Page,
|
||
PageSize: req.PageSize,
|
||
}, "获取成功", c)
|
||
}
|
||
|
||
// GetPayConfigByID 根据ID获取支付配置
|
||
// @Tags PayConfig
|
||
// @Summary 根据ID获取支付配置
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param id path int true "支付配置ID"
|
||
// @Success 200 {object} response.Response{data=system.PayConfig,msg=string} "获取成功"
|
||
// @Router /payConfig/{id} [get]
|
||
func (p *PayConfigApi) GetPayConfigByID(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||
if err != nil {
|
||
response.FailWithMessage("参数错误", c)
|
||
return
|
||
}
|
||
|
||
config, err := payConfigService.GetPayConfigByID(uint(id))
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取支付配置失败", zap.Error(err))
|
||
response.FailWithMessage("获取失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
response.OkWithData(config, c)
|
||
}
|
||
|
||
// GetEnabledPayConfigs 获取启用的支付配置列表
|
||
// @Tags PayConfig
|
||
// @Summary 获取启用的支付配置列表
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Success 200 {object} response.Response{data=[]system.PayConfig,msg=string} "获取成功"
|
||
// @Router /payConfig/enabled [get]
|
||
func (p *PayConfigApi) GetEnabledPayConfigs(c *gin.Context) {
|
||
list, err := payConfigService.GetEnabledPayConfigs()
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取启用的支付配置失败", zap.Error(err))
|
||
response.FailWithMessage("获取失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
response.OkWithData(list, c)
|
||
}
|
||
|
||
// CreatePayConfig 创建支付配置
|
||
// @Tags PayConfig
|
||
// @Summary 创建支付配置
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data body request.PayConfigCreate true "创建支付配置"
|
||
// @Success 200 {object} response.Response{msg=string} "创建成功"
|
||
// @Router /payConfig [post]
|
||
func (p *PayConfigApi) CreatePayConfig(c *gin.Context) {
|
||
var req request.PayConfigCreate
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
response.FailWithMessage("参数错误: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
if err := payConfigService.CreatePayConfig(req); err != nil {
|
||
global.GVA_LOG.Error("创建支付配置失败", zap.Error(err))
|
||
response.FailWithMessage("创建失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
response.OkWithMessage("创建成功", c)
|
||
}
|
||
|
||
// UpdatePayConfig 更新支付配置
|
||
// @Tags PayConfig
|
||
// @Summary 更新支付配置
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data body request.PayConfigUpdate true "更新支付配置"
|
||
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
||
// @Router /payConfig [put]
|
||
func (p *PayConfigApi) UpdatePayConfig(c *gin.Context) {
|
||
var req request.PayConfigUpdate
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
response.FailWithMessage("参数错误: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
if err := payConfigService.UpdatePayConfig(req); err != nil {
|
||
global.GVA_LOG.Error("更新支付配置失败", zap.Error(err))
|
||
response.FailWithMessage("更新失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
response.OkWithMessage("更新成功", c)
|
||
}
|
||
|
||
// DeletePayConfig 删除支付配置
|
||
// @Tags PayConfig
|
||
// @Summary 删除支付配置
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param id path int true "支付配置ID"
|
||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||
// @Router /payConfig/{id} [delete]
|
||
func (p *PayConfigApi) DeletePayConfig(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||
if err != nil {
|
||
response.FailWithMessage("参数错误", c)
|
||
return
|
||
}
|
||
|
||
if err := payConfigService.DeletePayConfig(uint(id)); err != nil {
|
||
global.GVA_LOG.Error("删除支付配置失败", zap.Error(err))
|
||
response.FailWithMessage("删除失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
response.OkWithMessage("删除成功", c)
|
||
}
|
||
|
||
// TogglePayConfig 切换支付配置状态
|
||
// @Tags PayConfig
|
||
// @Summary 切换支付配置状态
|
||
// @Security ApiKeyAuth
|
||
// @accept application/json
|
||
// @Produce application/json
|
||
// @Param data body request.PayConfigToggle true "切换状态"
|
||
// @Success 200 {object} response.Response{msg=string} "操作成功"
|
||
// @Router /payConfig/toggle [post]
|
||
func (p *PayConfigApi) TogglePayConfig(c *gin.Context) {
|
||
var req request.PayConfigToggle
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
response.FailWithMessage("参数错误: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
if err := payConfigService.TogglePayConfig(req); err != nil {
|
||
global.GVA_LOG.Error("切换支付配置状态失败", zap.Error(err))
|
||
response.FailWithMessage("操作失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
response.OkWithMessage("操作成功", c)
|
||
}
|