zhangqian
2024-06-05 b327b91db1c7015845ad293e8ccc48c4611819c6
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package controllers
 
import (
    "errors"
    "fmt"
    "github.com/gin-gonic/gin"
    "github.com/spf13/cast"
    "gorm.io/gorm"
    "strconv"
    "strings"
    "wms/constvar"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/pkg/logx"
    "wms/pkg/structx"
    "wms/request"
)
 
type WarehouseController struct{}
 
// Add
// @Tags      仓库
// @Summary   添加仓库
// @Produce   application/json
// @Param     object  body  request.AddWarehouse true  "仓库信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/warehouse/warehouse [post]
func (slf WarehouseController) Add(c *gin.Context) {
    var reqParams request.AddWarehouse
    var params models.Warehouse
    if err := c.BindJSON(&reqParams); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if err := structx.AssignTo(reqParams, &params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "数据转换错误")
        return
    }
 
    if err := slf.ParamsCheck(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    //创建视图
    view := &models.Location{
        Name:      params.Code,
        JointName: params.Code,
        Type:      constvar.LocationTypeView,
    }
    id, err := models.NewLocationSearch().CreateReturnId(view)
    if err != nil {
        util.ResponseFormat(c, code.SaveFail, "视图创建失败")
        return
    }
    //创建默认位置
    location := &models.Location{
        Name:              "默认位置",
        JointName:         params.Code + "/默认位置",
        Type:              constvar.LocationTypeInternal,
        ReplenishLocation: true,
        ParentId:          id,
    }
    locationId, err := models.NewLocationSearch().CreateReturnId(location)
    if err != nil {
        util.ResponseFormat(c, code.SaveFail, "位置创建失败")
        return
    }
    params.LocationId = locationId
    err = models.WithTransaction(func(tx *gorm.DB) error {
        err := models.NewWarehouseSearch().SetOrm(tx).Create(&params)
        if err != nil {
            return err
        }
        //创建三个默认操作类型
        var types []*models.OperationType
        inType := &models.OperationType{
            Name:              params.Name + "-入库",
            BaseOperationType: constvar.BaseOperationTypeIncoming,
            WarehouseId:       params.Id,
        }
        types = append(types, inType)
        outType := &models.OperationType{
            Name:              params.Name + "-出库",
            BaseOperationType: constvar.BaseOperationTypeOutgoing,
            WarehouseId:       params.Id,
        }
        types = append(types, outType)
        internalType := &models.OperationType{
            Name:              params.Name + "-内部调拨",
            BaseOperationType: constvar.BaseOperationTypeInternal,
            WarehouseId:       params.Id,
        }
        types = append(types, internalType)
        err = models.NewOperationTypeSearch().SetOrm(tx).CreateBatch(types)
        return err
    })
    if err != nil {
        _ = models.NewLocationSearch().SetID(locationId).Delete()
        logx.Errorf("warehouse create err: %v", err)
        util.ResponseFormat(c, code.SaveFail, "插入失败")
        return
    }
 
    util.ResponseFormat(c, code.Success, "添加成功")
}
 
// UpdateWarehouse
// @Tags      仓库
// @Summary   编辑仓库
// @Produce   application/json
// @Param     object  body  models.Warehouse true  "仓库信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/warehouse/updateWarehouse [post]
func (slf WarehouseController) UpdateWarehouse(c *gin.Context) {
    var params models.Warehouse
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("参数解析失败: %v"+err.Error()))
        return
    }
    if err := slf.ParamsCheck(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    warehouse, err := models.NewWarehouseSearch().SetID(params.Id).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "仓库不存在")
        return
    }
    err = models.WithTransaction(func(db *gorm.DB) error {
        //更新作业类型名称
        if params.Name != warehouse.Name {
            m := make(map[string]interface{})
            m["name"] = params.Name + "-入库"
            err := models.NewOperationTypeSearch().SetOrm(db).SetBaseOperationType(constvar.BaseOperationTypeIncoming).SetWarehouseId(params.Id).UpdateByMap(m)
            if err != nil {
                return err
            }
            m["name"] = params.Name + "-出库"
            err = models.NewOperationTypeSearch().SetOrm(db).SetBaseOperationType(constvar.BaseOperationTypeOutgoing).SetWarehouseId(params.Id).UpdateByMap(m)
            if err != nil {
                return err
            }
            m["name"] = params.Name + "-内部调拨"
            err = models.NewOperationTypeSearch().SetOrm(db).SetBaseOperationType(constvar.BaseOperationTypeInternal).SetWarehouseId(params.Id).UpdateByMap(m)
            if err != nil {
                return err
            }
        }
        err = models.NewWarehouseSearch().SetID(params.Id).Update(&params)
        return err
    })
 
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "修改失败")
        return
    }
 
    util.ResponseFormat(c, code.UpdateSuccess, "更新成功")
}
 
func (slf WarehouseController) ParamsCheck(params models.Warehouse) (err error) {
    var oldRecord *models.Warehouse
    if params.Id != 0 {
        oldRecord, err = models.NewWarehouseSearch().SetID(params.Id).First()
        if err == gorm.ErrRecordNotFound {
            return errors.New("记录不存在")
        }
    }
    //更新位置信息
    if oldRecord != nil && params.Code != oldRecord.Code {
        locations, err := models.NewLocationSearch().SetJointName(oldRecord.Code).FindNotTotal()
        if err != nil {
            return errors.New("查询位置信息失败")
        }
 
        err = models.WithTransaction(func(db *gorm.DB) error {
            for _, location := range locations {
                m := make(map[string]interface{})
                index := strings.Index(location.JointName, "/")
                if index > 0 {
                    m["joint_name"] = params.Code + location.JointName[index:]
                } else {
                    m["joint_name"] = params.Code
                }
                err = models.NewLocationSearch().SetOrm(db).SetID(location.Id).UpdateByMap(m)
                if err != nil {
                    return err
                }
            }
            return nil
        })
        if err != nil {
            return errors.New("更新位置信息失败")
        }
    }
    if oldRecord == nil {
        record, err := models.NewWarehouseSearch().SetCode(params.Code).First()
        if record != nil && record.Code == params.Code {
            fmt.Println(err)
            return errors.New("仓库编号重复")
        }
    }
    if params.Code == "" {
        return errors.New("缩写不能为空")
    }
    return nil
}
 
// List
// @Tags      仓库
// @Summary   查询仓库列表
// @Produce   application/json
// @Param     object  query    request.GetWarehouseList true  "查询参数"
// @Success   200   {object}  util.ResponseList{data=[]models.Warehouse}  "成功"
// @Router    /api-wms/v1/warehouse/warehouse [get]
func (slf WarehouseController) List(c *gin.Context) {
    var params request.GetWarehouseList
    if err := c.ShouldBindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    list, total, err := models.NewWarehouseSearch().SetPage(params.Page, params.PageSize).SetKeyword(params.Keyword).SetOrder("id desc").Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
    //获取位置信息
    ids := make([]int, 0)
    for _, warehouse := range list {
        ids = append(ids, warehouse.LocationId)
    }
    locations, err := models.NewLocationSearch().SetIds(ids).FindNotTotal()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "位置信息查找失败")
        return
    }
    for _, warehouse := range list {
        for _, location := range locations {
            if warehouse.LocationId == location.Id {
                warehouse.WarehouseLocation = warehouse.Code + "/" + location.Name
                break
            }
        }
    }
 
    util.ResponseFormatList(c, code.Success, list, cast.ToInt(total))
}
 
// Delete
// @Tags      仓库
// @Summary   删除仓库
// @Produce   application/json
// @Param     id  path string true  "仓库id"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/warehouse/warehouse/{id} [delete]
func (slf WarehouseController) Delete(c *gin.Context) {
    id, _ := strconv.Atoi(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
 
    first, err := models.NewWarehouseSearch().SetID(id).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "获取仓库信息失败")
        return
    }
    err = models.WithTransaction(func(tx *gorm.DB) error {
        //删除位置信息
        err = models.NewLocationSearch().SetOrm(tx).SetJointName(first.Code).Delete()
        if err != nil {
            return err
        }
        //删除操作类型
        err = models.NewOperationTypeSearch().SetOrm(tx).SetWarehouseId(first.Id).Delete()
        if err != nil {
            return err
        }
        //删除仓库
        err = models.NewWarehouseSearch().SetOrm(tx).SetID(id).Delete()
        return err
    })
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "删除仓库失败")
        return
    }
 
    util.ResponseFormat(c, code.UpdateSuccess, "删除成功")
}
 
// GetWarehouseDetails
// @Tags      仓库
// @Summary   获取仓库详情
// @Produce   application/json
// @Param     id  path string true  "仓库id"
// @Success   200 {object} util.Response{data=models.Warehouse}    "成功"
// @Router    /api-wms/v1/warehouse/getWarehouseDetails/{id} [get]
func (slf WarehouseController) GetWarehouseDetails(c *gin.Context) {
    id, _ := strconv.Atoi(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
 
    first, err := models.NewWarehouseSearch().SetID(id).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "获取仓库信息失败")
        return
    }
    util.ResponseFormat(c, code.UpdateSuccess, first)
}