yinbentan
2024-07-16 93c71a94d77ffe1f36654decc0bada0a2ac27c88
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
package controllers
 
import (
    "errors"
    "github.com/gin-gonic/gin"
    "strconv"
    "strings"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/request"
)
 
type LocationController struct {
}
 
// AddLocation
// @Tags      位置
// @Summary   添加位置信息
// @Produce   application/json
// @Param     object  body  models.Location true  "位置信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/location/addLocation [post]
func (slf LocationController) AddLocation(c *gin.Context) {
    var params models.Location
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
        return
    }
    if err := slf.CheckLocation(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err)
        return
    }
 
    if params.WarehouseId == 0 {
        util.ResponseFormat(c, code.RequestParamError, "仓库ID参数缺失")
        return
    }
 
    if params.ParentId != 0 {
        //查询上级名称
        first, err := models.NewLocationSearch().SetID(params.ParentId).First()
        if err != nil {
            util.ResponseFormat(c, code.RequestParamError, "查询上级名称失败")
            return
        }
        params.JointName = first.JointName + "/" + params.Name
        if first.WarehouseId != 0 {
            params.WarehouseId = first.WarehouseId
        } else {
            //根据仓库缩写查询仓库
            houseCode := strings.Split(first.JointName, "/")[0]
            warehouse, err := models.NewWarehouseSearch().SetCode(houseCode).First()
            if err != nil {
                util.ResponseFormat(c, code.RequestParamError, err)
                return
            }
            params.WarehouseId = warehouse.Id
        }
 
    } else {
        params.JointName = params.Name
    }
 
    err := models.NewLocationSearch().Create(&params)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "创建失败")
        return
    }
    util.ResponseFormat(c, code.Success, "创建成功")
}
 
// GetLocationList
// @Tags      位置
// @Summary   获取位置列表
// @Produce   application/json
// @Param     object  body  request.GetLocationList true  "查询参数"
// @Success   200 {object} util.ResponseList{data=[]models.Location}    "成功"
// @Router    /api-wms/v1/location/getLocationList [post]
func (slf LocationController) GetLocationList(c *gin.Context) {
    var params request.GetLocationList
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    search := models.NewLocationSearch()
    if params.PageInfo.Check() {
        search.SetPage(params.Page, params.PageSize)
    }
    list, total, err := search.SetKeyword(params.KeyWord).SetType(params.Type).SetJointName(params.JointName).
        SetIsScrapLocation(params.IsScrapLocation).SetOrder("created_at desc").
        SetWarehouseId(params.WarehouseId).
        Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
 
    util.ResponseFormatList(c, code.Success, list, int(total))
}
 
// GetLocationTreeList
// @Tags      位置
// @Summary   获取位置列表树
// @Produce   application/json
// @Success   200 {object} util.ResponseList{data=[]models.Location}    "成功"
// @Router    /api-wms/v1/location/getLocationTreeList [get]
func (slf LocationController) GetLocationTreeList(c *gin.Context) {
    all, err := models.NewLocationSearch().SetType(3).FindAll()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
    var tree []*models.Location
    m := make(map[int]*models.Location)
    for _, location := range all {
        m[location.Id] = location
    }
    for _, location := range all {
        if location.ParentId == 0 {
            tree = append(tree, location)
        } else {
            if _, ok := m[location.ParentId]; !ok {
                tree = append(tree, location)
                continue
            }
            if m[location.ParentId].Children == nil {
                m[location.ParentId].Children = make([]*models.Location, 0)
            }
 
            m[location.ParentId].Children = append(m[location.ParentId].Children, location)
        }
    }
 
    util.ResponseFormat(c, code.Success, tree)
}
 
// GetLocationDetails
// @Tags      位置
// @Summary   获取位置详情
// @Produce   application/json
// @Param        id    path        string            true    "id"  "查询参数"
// @Success   200 {object} util.Response{data=models.Location}    "成功"
// @Router    /api-wms/v1/location/getLocationDetails/{id} [get]
func (slf LocationController) GetLocationDetails(c *gin.Context) {
    id, _ := strconv.Atoi(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "无效id")
        return
    }
    location, err := models.NewLocationSearch().SetID(id).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
    util.ResponseFormat(c, code.Success, location)
}
 
// UpdateLocation
// @Tags      位置
// @Summary   修改位置
// @Produce   application/json
// @Param     object  body  models.Location true  "产品信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/location/updateLocation [post]
func (slf LocationController) UpdateLocation(c *gin.Context) {
    var params models.Location
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if err := slf.CheckLocation(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err)
        return
    }
    //index := strings.LastIndex(params.JointName, "/")
    //if index > 0 {
    //    jn := params.JointName[:index]
    //    oldName := params.JointName[index+1:]
    //    if oldName != params.Name {
    //        params.JointName = jn + "/" + params.Name
    //    }
    //}
    if params.ParentId != 0 {
        //查询上级名称
        first, err := models.NewLocationSearch().SetID(params.ParentId).First()
        if err != nil {
            util.ResponseFormat(c, code.RequestParamError, "查询上级名称失败")
            return
        }
        params.JointName = first.JointName + "/" + params.Name
    } else {
        params.JointName = strings.Split(params.JointName, "/")[0] + "/" + params.Name
    }
    err := models.NewLocationSearch().Update(&params)
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "位置信息更新失败")
        return
    }
    util.ResponseFormat(c, code.Success, "更新成功")
}
 
// DeleteLocation
// @Tags      位置
// @Summary   删除位置
// @Produce   application/json
// @Param        id    path        string            true    "id"  "查询参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/location/deleteLocation/{id} [delete]
func (slf LocationController) DeleteLocation(c *gin.Context) {
    id, _ := strconv.Atoi(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "无效id")
        return
    }
    err := models.NewLocationSearch().SetID(id).Delete()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "删除失败")
        return
    }
    util.ResponseFormat(c, code.Success, "删除成功")
}
 
func (slf LocationController) CheckLocation(params models.Location) error {
    if params.Name == "" {
        return errors.New("名称不能为空")
    }
    if params.Type == 0 {
        return errors.New("请选择正确的位置类型")
    }
    return nil
}