171 lines
3.8 KiB
Go
171 lines
3.8 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"hepa-calc-admin-api/api/dao"
|
|
"hepa-calc-admin-api/api/dto"
|
|
"hepa-calc-admin-api/api/model"
|
|
"hepa-calc-admin-api/api/requests"
|
|
"hepa-calc-admin-api/api/responses"
|
|
"hepa-calc-admin-api/global"
|
|
"hepa-calc-admin-api/utils"
|
|
"strconv"
|
|
)
|
|
|
|
type SystemSingle struct{}
|
|
|
|
// GetSystemSingleList 获取单项配置列表
|
|
func (b *SystemSingle) GetSystemSingleList(c *gin.Context) {
|
|
// 获取数据
|
|
systemSingleDao := dao.SystemSingleDao{}
|
|
SystemSingles, err := systemSingleDao.GetSystemSingleListSearch()
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
g := dto.GetSystemSingleListDto(SystemSingles)
|
|
|
|
responses.OkWithData(g, c)
|
|
}
|
|
|
|
// GetSystemSingle 获取单项配置详情
|
|
func (b *SystemSingle) GetSystemSingle(c *gin.Context) {
|
|
id := c.Param("system_single_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
systemSingleId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 获取数据
|
|
systemSingleDao := dao.SystemSingleDao{}
|
|
SystemSingle, err := systemSingleDao.GetSystemSingleById(systemSingleId)
|
|
if err != nil {
|
|
responses.FailWithMessage("数据异常", c)
|
|
return
|
|
}
|
|
|
|
// 处理返回值
|
|
g := dto.GetSystemSingleDto(SystemSingle)
|
|
|
|
responses.OkWithData(g, c)
|
|
}
|
|
|
|
// PutSystemSingle 修改会员配置
|
|
func (b *SystemSingle) PutSystemSingle(c *gin.Context) {
|
|
SystemSingleRequest := requests.SystemSingleRequest{}
|
|
req := SystemSingleRequest.PutSystemSingle
|
|
if err := c.ShouldBindJSON(&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("system_single_id")
|
|
if id == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 将 id 转换为 int64 类型
|
|
systemSingleId, err := strconv.ParseInt(id, 10, 64)
|
|
if err != nil {
|
|
responses.Fail(c)
|
|
return
|
|
}
|
|
|
|
// 获取问题数据
|
|
SystemSingleDao := dao.SystemSingleDao{}
|
|
systemSingle, err := SystemSingleDao.GetSystemSingleById(systemSingleId)
|
|
if err != nil {
|
|
responses.FailWithMessage("数据异常", c)
|
|
return
|
|
}
|
|
|
|
// 修改值
|
|
systemSingleData := make(map[string]interface{})
|
|
|
|
// 首次购买价格
|
|
if req.FirstTimePrice != systemSingle.FirstTimePrice {
|
|
systemSingleData["first_time_price"] = req.FirstTimePrice
|
|
}
|
|
|
|
// 购买后有效天数
|
|
if req.ValidDays != systemSingle.ValidDays {
|
|
systemSingleData["valid_days"] = req.ValidDays
|
|
}
|
|
|
|
// 开始事务
|
|
tx := global.Db.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
if len(systemSingleData) > 0 {
|
|
err = SystemSingleDao.EditSystemSingleById(tx, systemSingle.SystemSingleId, systemSingleData)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage("操作失败", c)
|
|
return
|
|
}
|
|
}
|
|
|
|
tx.Commit()
|
|
responses.Ok(c)
|
|
}
|
|
|
|
// AddSystemSingle 新增会员配置
|
|
func (b *SystemSingle) AddSystemSingle(c *gin.Context) {
|
|
SystemSingleRequest := requests.SystemSingleRequest{}
|
|
req := SystemSingleRequest.AddSystemSingle
|
|
if err := c.ShouldBindJSON(&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()
|
|
}
|
|
}()
|
|
|
|
systemSingle := &model.SystemSingle{
|
|
FirstTimePrice: req.FirstTimePrice,
|
|
ValidDays: req.ValidDays,
|
|
}
|
|
|
|
systemSingleDao := dao.SystemSingleDao{}
|
|
systemSingle, err := systemSingleDao.AddSystemSingle(tx, systemSingle)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
responses.FailWithMessage("操作失败", c)
|
|
return
|
|
}
|
|
|
|
tx.Commit()
|
|
responses.Ok(c)
|
|
}
|