143 lines
3.3 KiB
Go
143 lines
3.3 KiB
Go
package system
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"kra/internal/biz/system"
|
|
"kra/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type DictionaryApi struct{}
|
|
|
|
// CreateDictionaryRequest 创建字典请求
|
|
type CreateDictionaryRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Type string `json:"type" binding:"required"`
|
|
Status *bool `json:"status"`
|
|
Desc string `json:"desc"`
|
|
}
|
|
|
|
// UpdateDictionaryRequest 更新字典请求
|
|
type UpdateDictionaryRequest struct {
|
|
ID uint `json:"ID" binding:"required"`
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Status *bool `json:"status"`
|
|
Desc string `json:"desc"`
|
|
}
|
|
|
|
// GetDictionaryListRequest 获取字典列表请求
|
|
type GetDictionaryListRequest struct {
|
|
Page int `json:"page"`
|
|
PageSize int `json:"pageSize"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// CreateSysDictionary 创建字典
|
|
func (d *DictionaryApi) CreateSysDictionary(c *gin.Context) {
|
|
var req CreateDictionaryRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
dict := &system.Dictionary{
|
|
Name: req.Name,
|
|
Type: req.Type,
|
|
Status: req.Status,
|
|
Desc: req.Desc,
|
|
}
|
|
|
|
if err := dictionaryUsecase.CreateDictionary(c, dict); err != nil {
|
|
response.FailWithMessage("创建失败: "+err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("创建成功", c)
|
|
}
|
|
|
|
// DeleteSysDictionary 删除字典
|
|
func (d *DictionaryApi) DeleteSysDictionary(c *gin.Context) {
|
|
idStr := c.Query("ID")
|
|
if idStr == "" {
|
|
response.FailWithMessage("参数错误", c)
|
|
return
|
|
}
|
|
|
|
id, err := strconv.ParseUint(idStr, 10, 64)
|
|
if err != nil {
|
|
response.FailWithMessage("参数错误", c)
|
|
return
|
|
}
|
|
|
|
if err := dictionaryUsecase.DeleteDictionary(c, uint(id)); err != nil {
|
|
response.FailWithMessage("删除失败: "+err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("删除成功", c)
|
|
}
|
|
|
|
// UpdateSysDictionary 更新字典
|
|
func (d *DictionaryApi) UpdateSysDictionary(c *gin.Context) {
|
|
var req UpdateDictionaryRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
dict := &system.Dictionary{
|
|
ID: req.ID,
|
|
Name: req.Name,
|
|
Type: req.Type,
|
|
Status: req.Status,
|
|
Desc: req.Desc,
|
|
}
|
|
|
|
if err := dictionaryUsecase.UpdateDictionary(c, dict); err != nil {
|
|
response.FailWithMessage("更新失败: "+err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("更新成功", c)
|
|
}
|
|
|
|
// FindSysDictionary 根据ID或type获取字典
|
|
func (d *DictionaryApi) FindSysDictionary(c *gin.Context) {
|
|
idStr := c.Query("ID")
|
|
dictType := c.Query("type")
|
|
|
|
var id uint
|
|
if idStr != "" {
|
|
idUint, err := strconv.ParseUint(idStr, 10, 64)
|
|
if err != nil {
|
|
response.FailWithMessage("参数错误", c)
|
|
return
|
|
}
|
|
id = uint(idUint)
|
|
}
|
|
|
|
dict, err := dictionaryUsecase.GetDictionary(c, dictType, id, nil)
|
|
if err != nil {
|
|
response.FailWithMessage("获取失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithDetailed(gin.H{"resysDictionary": dict}, "获取成功", c)
|
|
}
|
|
|
|
// GetSysDictionaryList 获取字典列表
|
|
func (d *DictionaryApi) GetSysDictionaryList(c *gin.Context) {
|
|
name := c.Query("name")
|
|
|
|
list, err := dictionaryUsecase.GetDictionaryList(c, name)
|
|
if err != nil {
|
|
response.FailWithMessage("获取失败", c)
|
|
return
|
|
}
|
|
|
|
response.OkWithDetailed(gin.H{"list": list}, "获取成功", c)
|
|
}
|