2025-03-07 17:23:50 +08:00

298 lines
6.3 KiB
Go

package controller
import (
"case-admin-api/api/dao"
"case-admin-api/api/dto"
"case-admin-api/api/model"
"case-admin-api/api/requests"
"case-admin-api/api/responses"
"case-admin-api/global"
"case-admin-api/utils"
"github.com/gin-gonic/gin"
"strconv"
)
type Platform struct{}
// GetPlatformPage 获取列表-分页
func (b *Platform) GetPlatformPage(c *gin.Context) {
PlatformRequest := requests.PlatformRequest{}
req := PlatformRequest.GetPlatformPage
if err := c.ShouldBind(&req); err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 参数验证
if err := global.Validate.Struct(req); err != nil {
responses.FailWithMessage(utils.Translate(err), c)
return
}
if req.Page == 0 {
req.Page = 1
}
if req.PageSize == 0 {
req.PageSize = 20
}
// 获取数据
PlatformDao := dao.PlatformDao{}
Platforms, total, err := PlatformDao.GetPlatformPageSearch(req, req.Page, req.PageSize)
if err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 处理返回值
g := dto.GetPlatformListDto(Platforms)
result := make(map[string]interface{})
result["page"] = req.Page
result["page_size"] = req.PageSize
result["total"] = total
result["data"] = g
responses.OkWithData(result, c)
}
// GetPlatformList 获取列表
func (b *Platform) GetPlatformList(c *gin.Context) {
PlatformRequest := requests.PlatformRequest{}
req := PlatformRequest.GetPlatformList
if err := c.ShouldBind(&req); err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 参数验证
if err := global.Validate.Struct(req); err != nil {
responses.FailWithMessage(utils.Translate(err), c)
return
}
// 获取数据
PlatformDao := dao.PlatformDao{}
Platforms, err := PlatformDao.GetPlatformListSearch(req)
if err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 处理返回值
g := dto.GetPlatformListDto(Platforms)
responses.OkWithData(g, c)
}
// GetPlatform 获取详情
func (b *Platform) GetPlatform(c *gin.Context) {
id := c.Param("platform_id")
if id == "" {
responses.FailWithMessage("缺少参数", c)
return
}
// 将 id 转换为 int64 类型
platformId, err := strconv.ParseInt(id, 10, 64)
if err != nil {
responses.Fail(c)
return
}
PlatformDao := dao.PlatformDao{}
platform, err := PlatformDao.GetPlatformById(platformId)
if err != nil {
responses.FailWithMessage("数据异常", c)
return
}
g := dto.GetPlatformDto(platform)
responses.OkWithData(g, c)
}
// PutPlatformStatus 操作状态
func (b *Platform) PutPlatformStatus(c *gin.Context) {
PlatformRequest := requests.PlatformRequest{}
req := PlatformRequest.PutPlatformStatus
if err := c.ShouldBind(&req); err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 参数验证
if err := global.Validate.Struct(req); err != nil {
responses.FailWithMessage(utils.Translate(err), c)
return
}
id := c.Param("platform_id")
if id == "" {
responses.FailWithMessage("缺少参数", c)
return
}
// 将 id 转换为 int64 类型
platformId, err := strconv.ParseInt(id, 10, 64)
if err != nil {
responses.Fail(c)
return
}
PlatformDao := dao.PlatformDao{}
platform, err := PlatformDao.GetPlatformById(platformId)
if err != nil {
responses.FailWithMessage("数据异常", c)
return
}
// 检测状态
if platform.PlatformStatus == req.PlatformStatus {
responses.Ok(c)
return
}
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
PlatformData := make(map[string]interface{})
PlatformData["platform_status"] = req.PlatformStatus
err = PlatformDao.EditPlatformById(tx, platformId, PlatformData)
if err != nil {
tx.Rollback()
responses.FailWithMessage("操作失败", c)
return
}
tx.Commit()
responses.Ok(c)
}
// AddPlatform 新增
func (b *Platform) AddPlatform(c *gin.Context) {
PlatformRequest := requests.PlatformRequest{}
req := PlatformRequest.AddPlatform
if err := c.ShouldBind(&req); err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 参数验证
if err := global.Validate.Struct(req); err != nil {
responses.FailWithMessage(utils.Translate(err), c)
return
}
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
platform := &model.Platform{
PlatformName: req.PlatformName,
PlatformStatus: req.PlatformStatus,
PlatformKey: req.PlatformKey,
PlatformSecret: req.PlatformSecret,
}
PlatformDao := dao.PlatformDao{}
platform, err := PlatformDao.AddPlatform(tx, platform)
if err != nil {
tx.Rollback()
responses.FailWithMessage(err.Error(), c)
return
}
tx.Commit()
responses.Ok(c)
}
// PutPlatform 修改
func (b *Platform) PutPlatform(c *gin.Context) {
PlatformRequest := requests.PlatformRequest{}
req := PlatformRequest.PutPlatform
if err := c.ShouldBind(&req); err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
// 参数验证
if err := global.Validate.Struct(req); err != nil {
responses.FailWithMessage(utils.Translate(err), c)
return
}
id := c.Param("platform_id")
if id == "" {
responses.FailWithMessage("缺少参数", c)
return
}
// 将 id 转换为 int64 类型
platformId, err := strconv.ParseInt(id, 10, 64)
if err != nil {
responses.Fail(c)
return
}
PlatformDao := dao.PlatformDao{}
platform, err := PlatformDao.GetPlatformById(platformId)
if err != nil {
responses.FailWithMessage("数据异常", c)
return
}
// 修改值
PlatformData := make(map[string]interface{})
// 平台名称
if req.PlatformName != platform.PlatformName {
PlatformData["platform_name"] = req.PlatformName
}
// 平台状态
if req.PlatformStatus != platform.PlatformStatus {
PlatformData["platform_status"] = req.PlatformStatus
}
// 平台标识
if req.PlatformKey != platform.PlatformKey {
PlatformData["platform_key"] = req.PlatformKey
}
// 平台密钥
if req.PlatformSecret != platform.PlatformSecret {
PlatformData["platform_secret"] = req.PlatformSecret
}
if len(PlatformData) > 0 {
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
err = PlatformDao.EditPlatformById(tx, platformId, PlatformData)
if err != nil {
tx.Rollback()
responses.FailWithMessage("操作失败", c)
return
}
tx.Commit()
}
responses.Ok(c)
}