This commit is contained in:
2026-08-15 01:12:04 +08:00
parent f63acae353
commit 6698e59c3a
45 changed files with 725 additions and 415 deletions
+86 -55
View File
@@ -19,54 +19,50 @@ func NewDictionary(service *service.SettingsService) *Dictionary {
func (h *Dictionary) Create(c *gin.Context) {
var req dto.DictionaryRequest
if c.ShouldBindJSON(&req) != nil || req.Name == "" || req.Type == "" {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.CreateDictionaryRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "创建失败:"+err.Error())
created, err := h.service.CreateDictionaryRequest(c.Request.Context(), &req)
if err != nil {
httpx.Fail(c, "创建失败")
return
}
httpx.OK(c)
httpx.Write(c, httpx.CodeSuccess, created, "创建成功")
}
func (h *Dictionary) Update(c *gin.Context) {
var req dto.DictionaryRequest
if c.ShouldBindJSON(&req) != nil || req.ID == 0 {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.UpdateDictionaryRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "更新失败")
return
}
httpx.OK(c)
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "更新成功")
}
func (h *Dictionary) Delete(c *gin.Context) {
var req dto.DictionaryRequest
if c.ShouldBindJSON(&req) != nil || req.ID == 0 {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.DeleteDictionary(c.Request.Context(), req.ID); err != nil {
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
httpx.OK(c)
if err := h.service.DeleteDictionary(c.Request.Context(), req.ID); err != nil {
httpx.Fail(c, "删除失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
}
func (h *Dictionary) Find(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
var status *bool
if raw, exists := c.GetQuery("status"); exists {
value, parseErr := strconv.ParseBool(raw)
if parseErr != nil {
httpx.Fail(c, parseErr.Error())
return
}
status = &value
var req dto.DictionaryRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
item, err := h.service.Dictionary(c.Request.Context(), uint(id), c.Query("type"), status, true)
item, err := h.service.Dictionary(c.Request.Context(), req.ID, req.Type, req.Status, true)
if err != nil {
httpx.Fail(c, "查询失败")
httpx.Fail(c, "字典未创建或未开启")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{"resysDictionary": item}, "查询成功")
@@ -82,8 +78,16 @@ func (h *Dictionary) List(details bool) gin.HandlerFunc {
}
}
func (h *Dictionary) Export(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
item, err := h.service.ExportDictionary(c.Request.Context(), uint(id))
var req dto.DictionaryRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if req.ID == 0 {
httpx.Fail(c, "字典ID不能为空")
return
}
item, err := h.service.ExportDictionary(c.Request.Context(), req.ID)
if err != nil {
httpx.Fail(c, "导出失败")
return
@@ -103,56 +107,60 @@ func (h *Dictionary) Export(c *gin.Context) {
}
func (h *Dictionary) Import(c *gin.Context) {
var req dto.ImportDictionaryRequest
if c.ShouldBindJSON(&req) != nil {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.ImportDictionaryJSON(c.Request.Context(), req.JSON); err != nil {
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
httpx.OK(c)
if err := h.service.ImportDictionaryJSON(c.Request.Context(), req.JSON); err != nil {
httpx.Fail(c, "导入失败: "+err.Error())
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "导入成功")
}
func (h *Dictionary) CreateDetail(c *gin.Context) {
var req dto.DictionaryDetailRequest
if c.ShouldBindJSON(&req) != nil || req.DictionaryID == 0 {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.CreateDictionaryDetailRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, "创建失败")
return
}
httpx.OK(c)
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "创建成功")
}
func (h *Dictionary) UpdateDetail(c *gin.Context) {
var req dto.DictionaryDetailRequest
if c.ShouldBindJSON(&req) != nil || req.ID == 0 {
httpx.Fail(c, "参数错误")
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
if err := h.service.UpdateDictionaryDetailRequest(c.Request.Context(), &req); err != nil {
httpx.Fail(c, err.Error())
httpx.Fail(c, "更新失败")
return
}
httpx.OK(c)
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "更新成功")
}
func (h *Dictionary) DeleteDetail(c *gin.Context) {
var req dto.DictionaryDetailRequest
if c.ShouldBindJSON(&req) != nil || req.ID == 0 {
httpx.Fail(c, "参数错误")
return
}
if err := h.service.DeleteDictionaryDetail(c.Request.Context(), req.ID); err != nil {
if err := c.ShouldBindJSON(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
httpx.OK(c)
if err := h.service.DeleteDictionaryDetail(c.Request.Context(), req.ID); err != nil {
httpx.Fail(c, "删除失败")
return
}
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "删除成功")
}
func (h *Dictionary) FindDetail(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
item, err := h.service.DictionaryDetail(c.Request.Context(), uint(id))
var req dto.DictionaryDetailRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
item, err := h.service.DictionaryDetail(c.Request.Context(), req.ID)
if err != nil {
httpx.Fail(c, "查询失败")
return
@@ -197,31 +205,54 @@ func (h *Dictionary) Details(c *gin.Context) {
}
func (h *Dictionary) Tree(byType bool) gin.HandlerFunc {
return func(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("sysDictionaryID"), 10, 64)
rawID := c.Query("sysDictionaryID")
id, parseErr := strconv.ParseUint(rawID, 10, 64)
typ := ""
if byType {
typ = c.Query("type")
if typ == "" {
httpx.Fail(c, "字典类型不能为空")
return
}
} else if rawID == "" {
httpx.Fail(c, "字典ID不能为空")
return
} else if parseErr != nil {
httpx.Fail(c, "字典ID格式错误")
return
}
items, err := h.service.DictionaryTree(c.Request.Context(), uint(id), typ)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, items, "获取成功")
httpx.Write(c, httpx.CodeSuccess, gin.H{"list": items}, "获取成功")
}
}
func (h *Dictionary) DetailsByParent(c *gin.Context) {
parentID, _ := strconv.ParseUint(c.Query("parentID"), 10, 64)
dictionaryID, _ := strconv.ParseUint(c.Query("sysDictionaryID"), 10, 64)
items, err := h.service.DictionaryDetailsByParent(c.Request.Context(), uint(dictionaryID), uint(parentID))
var req dto.DictionaryDetailsByParentRequest
if err := c.ShouldBindQuery(&req); err != nil {
httpx.Fail(c, err.Error())
return
}
items, err := h.service.DictionaryDetailsByParent(c.Request.Context(), req.DictionaryID, req.ParentID, req.IncludeChildren)
if err != nil {
httpx.Fail(c, "获取失败")
return
}
httpx.Write(c, httpx.CodeSuccess, items, "获取成功")
httpx.Write(c, httpx.CodeSuccess, gin.H{"list": items}, "获取成功")
}
func (h *Dictionary) Path(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
rawID := c.Query("id")
if rawID == "" {
httpx.Fail(c, "字典详情ID不能为空")
return
}
id, parseErr := strconv.ParseUint(rawID, 10, 64)
if parseErr != nil {
httpx.Fail(c, "字典详情ID格式错误")
return
}
item, err := h.service.DictionaryDetail(c.Request.Context(), uint(id))
if err != nil {
httpx.Fail(c, "获取失败")