selfcheer
2024-07-19 1572f45e72cc0fa15c029f9ee2a08474104435e6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package v1
 
import (
    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
    "srm/constvar"
    "srm/model"
    "srm/utils"
    "srm/utils/code"
)
 
type DictController struct{}
 
type MiniDict struct {
    Name      string `gorm:"type:varchar(191);not null;comment:名称" json:"name"`
    IsDefault bool   `gorm:"type:tinyint(1);comment:是否默认" json:"isDefault"`
    Value     string `gorm:"type:varchar(191);;comment:值" json:"value"`
}
 
type SaveMiniDict struct {
    Type constvar.MiniDictType `gorm:"type:int(11);comment:字典类型" json:"type"`
    List []*MiniDict           `json:"list"`
}
 
type GetMiniDictList struct {
    Type constvar.MiniDictType `gorm:"type:int(11);comment:字典类型" json:"type"`
}
 
// SaveMiniDict
// @Tags        数据字典
// @Summary    更新迷你字典
// @Produce    application/json
// @Param        object    body        SaveMiniDict    true    "参数"
// @Param         Authorization    header string true "token"
// @Success    200        {object}    utils.Response            "成功"
// @Router        /dict/saveMiniDict [post]
func (slf DictController) SaveMiniDict(c *gin.Context) {
    var params SaveMiniDict
    if err := c.BindJSON(&params); err != nil {
        utils.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
 
    if !params.Type.Valid() {
        utils.ResponseFormat(c, code.RequestParamError, "字典类型错误")
        return
    }
 
    var records []*model.MiniDict
    for _, v := range params.List {
        if len(v.Name) == 0 {
            utils.ResponseFormat(c, code.RequestParamError, "名称为空")
            return
        }
 
        records = append(records, &model.MiniDict{
            Type:      params.Type,
            Name:      v.Name,
            IsDefault: v.IsDefault,
            Value:     v.Value,
        })
    }
 
    err := model.WithTransaction(func(tx *gorm.DB) error {
        err := model.NewMiniDictSearch().SetOrm(tx).SetType(params.Type).Delete()
        if err != nil {
            return err
        }
 
        err = model.NewMiniDictSearch().SetOrm(tx).CreateBatch(records)
        if err != nil {
            return err
        }
        return nil
    })
    if err != nil {
        utils.ResponseFormat(c, code.RequestParamError, "保存失败")
        return
    }
 
    utils.ResponseFormat(c, code.Success, "添加成功")
}
 
// GetMiniDictList
// @Tags        数据字典
// @Summary    获取迷你字典列表
// @Produce    application/json
// @Param        object    body    GetMiniDictList    true    "参数"
// @Param         Authorization    header string true "token"
// @Success    200    {object}    utils.ResponseList{data=[]model.MiniDict}    "成功"
// @Router        /dict/getMiniDictList [post]
func (slf DictController) GetMiniDictList(c *gin.Context) {
    var params GetMiniDictList
    if err := c.BindJSON(&params); err != nil {
        utils.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
 
    if !params.Type.Valid() {
        utils.ResponseFormat(c, code.RequestParamError, "字典类型错误")
        return
    }
 
    list, total, err := model.NewMiniDictSearch().SetType(params.Type).Find()
    if err != nil {
        utils.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
 
    utils.ResponseFormatList(c, code.Success, list, int(total))
}