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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package controllers
 
import (
    "context"
    "errors"
    "fmt"
    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
    "strconv"
    "wms/constvar"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/pkg/logx"
    "wms/proto/init_client"
    "wms/proto/user"
    "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
    }
 
    _, err := models.NewMiniDictSearch().SetType(params.Type).SetName(params.Name).First()
    if !errors.Is(err, gorm.ErrRecordNotFound) {
        util.ResponseFormat(c, code.NameExistedError, "名称已存在")
        return
    }
 
    autoCode, _ := getAutoCode(params.Type)
    record := models.MiniDict{
        Type:      params.Type,
        Code:      autoCode,
        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,
        Code:      params.Code,
        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
    }
 
    list, total, err := models.NewMiniDictSearch().SetKeyword(params.Keyword).SetType(params.Type).Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
 
    util.ResponseFormatList(c, code.Success, list, int(total))
}
 
// GetUserList
// @Tags      数据字典
// @Summary   获取用户列表
// @Produce   application/json
// @Param        object    query        user.GetUserRequest    true    "参数"
// @Success    200        {object}    util.ResponseList{data=[]user.GetUserRequest}    "成功"
// @Router    /api-wms/v1/dict/getUserList [get]
func (slf DictController) GetUserList(c *gin.Context) {
    if init_client.AdminConn == nil {
        util.ResponseFormat(c, code.InternalError, "请先配置Admin的grpc服务信息")
    }
    query := new(user.GetUserRequest)
    if c.Query("id") != "" {
        query.Id = c.Query("id")
    }
    if c.Query("userName") != "" {
        query.UserName = c.Query("userName")
    }
    if c.Query("nickName") != "" {
        query.NickName = c.Query("nickName")
    }
    if c.Query("parentId") != "" {
        query.ParentId = c.Query("parentId")
    }
    if userType, err := strconv.Atoi(c.Query("userType")); err == nil && userType > 0 {
        query.UserType = int32(userType)
    }
 
    cli := user.NewUserServiceClient(init_client.AdminConn)
    list, err := cli.GetUserList(context.Background(), query)
    if err != nil {
        util.ResponseFormat(c, code.InternalError, "内部错误")
        logx.Error("grpc调用失败, GetPersonnelList err : " + err.Error())
        return
    }
    util.ResponseFormat(c, code.Success, list.List)
}
 
// GetAutoCode 获取字典自动编码
func getAutoCode(dictType constvar.MiniDictType) (string, error) {
 
    var prefix string
    switch constvar.MiniDictType(dictType) {
    case constvar.StorageType:
        prefix = "IN0"
    case constvar.StockoutType:
        prefix = "OUT"
    case constvar.TransferType:
        prefix = "TF0"
    case constvar.TakeStockType:
        prefix = "TS0"
    case constvar.DisuseType:
        prefix = "DIS"
    case constvar.ProductSource:
        prefix = "PS0"
    default:
        return "", errors.New("编码规则不存在")
    }
    id, err := models.NewMiniDictSearch().SetType(constvar.MiniDictType(dictType)).MaxAutoIncr()
    if err != nil {
        return "", errors.New("获取最大值失败")
    }
 
    strMaxAutoIncr := strconv.Itoa(id + 1)
    count := 5 - len(strMaxAutoIncr)
    for i := 0; i < count; i++ {
        strMaxAutoIncr = "0" + strMaxAutoIncr
    }
 
    return fmt.Sprintf("%v%v", prefix, strMaxAutoIncr), nil
}