yinbentan
2024-07-08 f4508a84236a4aff1c7b5bfa17a14a8ff95728ba
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package controllers
 
import (
    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
    "strconv"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/pkg/logx"
    "wms/request"
)
 
type DictController struct{}
 
// AddMiniDict
//
//    @Tags        数据字典
//    @Summary    添加字典信息
//    @Produce    application/json
//    @Param        object    body        request.AddMiniDict    true    "参数"
//    @Success    200        {object}    util.Response        "成功"
//    @Router        /api-wms/v1/dict/add [post]
func (slf DictController) AddMiniDict(c *gin.Context) {
    var params request.AddMiniDict
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
 
    if !params.Type.Valid() {
        util.ResponseFormat(c, code.RequestParamError, "字典类型错误")
        return
    }
 
    if len(params.Name) == 0 {
        util.ResponseFormat(c, code.RequestParamError, "名称为空")
        return
    }
 
    record := models.MiniDict{
        Type:      params.Type,
        Name:      params.Name,
        Value:     params.Value,
        IsDefault: params.IsDefault,
    }
 
    if err := models.NewMiniDictSearch().Create(&record); err != nil {
        logx.Errorf("MiniDict add err: %v", err)
        util.ResponseFormat(c, code.RequestParamError, "添加失败")
        return
    }
    util.ResponseFormat(c, code.Success, "添加成功")
}
 
// EditMiniDict
//
//    @Tags        数据字典
//    @Summary    编辑字典信息
//    @Produce    application/json
//    @Param        object    body        request.EditMiniDict    true    "参数"
//    @Success    200        {object}    util.Response            "成功"
//    @Router        /api-wms/v1/dict/edit [post]
func (slf DictController) EditMiniDict(c *gin.Context) {
    var params request.EditMiniDict
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
 
    if params.ID == 0 {
        util.ResponseFormat(c, code.RequestParamError, "无效ID")
        return
    }
 
    if !params.Type.Valid() {
        util.ResponseFormat(c, code.RequestParamError, "字典类型错误")
        return
    }
 
    if len(params.Name) == 0 {
        util.ResponseFormat(c, code.RequestParamError, "名称为空")
        return
    }
 
    record := models.MiniDict{
        Type:      params.Type,
        Name:      params.Name,
        Value:     params.Value,
        IsDefault: params.IsDefault,
    }
    record.ID = uint(params.ID)
 
    if err := models.NewMiniDictSearch().SetID(uint(params.ID)).Save(&record); err != nil {
        logx.Errorf("MiniDict edit err: %v", err)
        util.ResponseFormat(c, code.RequestParamError, "编辑失败")
        return
    }
    util.ResponseFormat(c, code.Success, "编辑成功")
}
 
// DeleteMiniDict
//
//    @Tags        数据字典
//    @Summary    删除字典信息
//    @Param        id    path        string            true    "id"
//    @Success    200    {object}    util.Response    "成功"
//    @Router        /api-wms/v1/dict/delete/{id} [delete]
func (slf DictController) DeleteMiniDict(c *gin.Context) {
    id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "无效ID")
        return
    }
    err := models.NewMiniDictSearch().SetID(uint(id)).Delete()
    if err != nil {
        logx.Errorf("MiniDict delete err: %v", err)
        util.ResponseFormat(c, code.RequestParamError, "删除失败")
        return
    }
    util.ResponseFormat(c, code.Success, "删除成功")
}
 
// SaveMiniDict
//
//    @Tags        数据字典
//    @Summary    批量更新迷你字典(会删除原数据)
//    @Produce    application/json
//    @Param        object    body        request.SaveMiniDict    true    "参数"
//    @Success    200        {object}    util.Response            "成功"
//    @Router        /api-wms/v1/dict/save [post]
func (slf DictController) SaveMiniDict(c *gin.Context) {
    var params request.SaveMiniDict
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
 
    if !params.Type.Valid() {
        util.ResponseFormat(c, code.RequestParamError, "字典类型错误")
        return
    }
 
    var records []*models.MiniDict
    for _, v := range params.List {
        if len(v.Name) == 0 {
            util.ResponseFormat(c, code.RequestParamError, "名称为空")
            return
        }
 
        records = append(records, &models.MiniDict{
            Type:      params.Type,
            Name:      v.Name,
            IsDefault: v.IsDefault,
            Value:     v.Value,
        })
    }
 
    err := models.WithTransaction(func(tx *gorm.DB) error {
        err := models.NewMiniDictSearch().SetOrm(tx).SetType(params.Type).Delete()
        if err != nil {
            return err
        }
 
        err = models.NewMiniDictSearch().SetOrm(tx).CreateBatch(records)
        if err != nil {
            return err
        }
        return nil
    })
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "保存失败")
        return
    }
 
    util.ResponseFormat(c, code.Success, "添加成功")
}
 
// GetMiniDictList
//
//    @Tags        数据字典
//    @Summary    获取字典信息列表
//    @Produce    application/json
//     @Param        object    body        request.GetMiniDictList    true    "参数"
//    @Success    200        {object}    util.ResponseList{data=[]models.MiniDict}    "成功"
//    @Router        /api-wms/v1/dict/getDictList [post]
func (slf DictController) GetMiniDictList(c *gin.Context) {
    var params request.GetMiniDictList
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
 
    //if !params.Type.Valid() {
    //    util.ResponseFormat(c, code.RequestParamError, "字典类型错误")
    //    return
    //}
    dictSearch := models.NewMiniDictSearch()
    if params.Type.Valid() {
        dictSearch.SetType(params.Type)
    }
 
    list, total, err := dictSearch.Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
 
    util.ResponseFormatList(c, code.Success, list, int(total))
}