jiangshuai
2023-09-23 db425e3e5022111c8b776fa18f453ed04623deb2
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package controllers
 
import (
    "errors"
    "fmt"
    "github.com/gin-gonic/gin"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "strconv"
    "time"
    "wms/constvar"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/pkg/logx"
    "wms/pkg/structx"
    "wms/request"
)
 
type OperationController struct {
}
 
// Add
// @Tags      入库/出库
// @Summary   添加入库/出库
// @Produce   application/json
// @Param     object  body  request.AddOperation true  "入库/出库信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/operation/operation [post]
func (slf OperationController) Add(c *gin.Context) {
    var reqParams request.AddOperation
    var params models.Operation
    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, "数据转换错误"+err.Error())
        return
    }
    if err := slf.CheckParams(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    if err := slf.FormatLocation(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    params.Status = constvar.OperationStatus_Ready
    params.Number = strconv.FormatInt(time.Now().Unix(), 10)
    if err := models.NewOperationSearch().Create(&params); err != nil {
        logx.Errorf("Operation create err: %v", err)
        util.ResponseFormat(c, code.SaveFail, "添加失败:"+err.Error())
        return
    }
    util.ResponseFormat(c, code.Success, "添加成功")
}
 
func (slf OperationController) FormatLocation(params *models.Operation) error {
    operationType, err := models.NewOperationTypeSearch().SetID(uint(params.OperationTypeId)).First()
    if err != nil {
        return err
    }
    if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming {
        if location, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeVendor)).First(); err != nil {
            return err
        } else {
            params.FromLocationID = location.Id
        }
        if params.ToLocationID == 0 {
            return errors.New("请选择目标位置")
        }
    }
    if operationType.BaseOperationType == constvar.BaseOperationTypeOutgoing {
        if location, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeCustomer)).First(); err != nil {
            return err
        } else {
            params.ToLocationID = location.Id
        }
        if params.FromLocationID == 0 {
            return errors.New("请选择源位置")
        }
    }
    if operationType.BaseOperationType == constvar.BaseOperationTypeInternal {
        if params.ToLocationID == 0 {
            return errors.New("请选择目标位置")
        }
        if params.FromLocationID == 0 {
            return errors.New("请选择源位置")
        }
    }
    return nil
}
 
func (slf OperationController) CheckParams(params models.Operation) error {
    if params.SourceNumber == "" {
        return errors.New("请填入源单号")
    }
 
    if params.OperationTypeId == 0 {
        return errors.New("operationTypeId为0")
    }
 
    if params.OperationDate == "" {
        return errors.New("请选择安排日期")
    }
 
    if len(params.Details) <= 0 {
        return errors.New("请添加明细信息")
    }
 
    //检查明细部分
    for _, v := range params.Details {
        if v.ProductId == "" {
            return errors.New("productID为空")
        }
        if v.ProductName == "" {
            return errors.New("产品名称异常")
        }
        if v.Amount.IsNegative() {
            return errors.New("产品数量出错")
        }
    }
 
    return nil
}
 
// List
// @Tags      入库/出库
// @Summary   入库/出库列表
// @Produce   application/json
// @Param         object  body  request.OperationList true  "查询参数"
// @Success   200 {object} util.Response    "成功"
// @Router    /api-wms/v1/operation/list [post]
func (slf OperationController) List(c *gin.Context) {
    var params request.OperationList
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
        return
    }
    if err := slf.CheckListParams(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    search := models.NewOperationSearch()
    search.SetPage(params.Page, params.PageSize)
    if params.Number != "" {
        search.SetNumber(params.Number)
    }
    if params.SourceNumber != "" {
        search.SetSourceNumber(params.SourceNumber)
    }
    list, total, err := search.SetOperationTypeId(params.OperationTypeId).SetPreload(true).SetOrder("created_at desc").Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestError, "查找失败:"+err.Error())
        return
    }
 
    util.ResponseFormatListWithPage(c, code.Success, list, int(total), params.Page, params.PageSize)
}
 
func (slf OperationController) CheckListParams(params *request.OperationList) error {
    if !params.PageInfo.Check() {
        return errors.New("数据分页信息错误")
    }
    if params.OperationTypeId == 0 {
        return errors.New("operationTypeId为0")
    }
    return nil
}
 
// Update
// @Tags      入库/出库
// @Summary   修改入库/出库信息
// @Produce   application/json
// @Param     object  body request.UpdateOperation true  "入库信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/operation/update [post]
func (slf OperationController) Update(c *gin.Context) {
    var reqParams request.UpdateOperation
    var params models.Operation
    if err := c.BindJSON(&reqParams); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
        return
    }
    if reqParams.Status != constvar.OperationStatus_Ready {
        util.ResponseFormat(c, code.RequestParamError, "该入库信息已完成,无法进行修改")
        return
    }
    if err := structx.AssignTo(reqParams, &params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "数据转换错误"+err.Error())
        return
    }
    if err := slf.CheckParams(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    if err := slf.FormatLocation(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    if err := models.WithTransaction(func(tx *gorm.DB) error {
        if err := models.NewOperationDetailsSearch().SetOrm(tx).SetOperationId(params.Id).Delete(); err != nil {
            return err
        }
        operationSearch := models.NewOperationSearch().SetOrm(tx)
        if err := operationSearch.Orm.Model(&params).Association("Details").Replace(params.Details); err != nil {
            return err
        }
        if err := models.NewOperationSearch().SetOrm(tx).SetID(params.Id).Save(&params); err != nil {
            return err
        }
        return nil
    }); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "修改失败:"+err.Error())
        return
    }
 
    util.ResponseFormat(c, code.Success, "修改成功")
}
 
// DeleteDevice
//
//    @Tags        入库/出库
//    @Summary    删除入库/出库信息
//    @Produce    application/json
//    @Param        id    path        int            true    "id"
//    @Success    200    {object}    util.Response    "成功"
//    @Router        /api-wms/v1/operation/operation/{id} [delete]
func (slf OperationController) Delete(c *gin.Context) {
    id, err := strconv.Atoi(c.Param("id"))
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "错误的id值")
        return
    }
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "id为0")
        return
    }
    operation, err := models.NewOperationSearch().SetID(id).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "未找到相关出入库信息:"+err.Error())
        return
    }
    if operation.Status != constvar.OperationStatus_Ready {
        util.ResponseFormat(c, code.RequestError, "该入库信息无法进行删除")
        return
    }
    if err := models.WithTransaction(func(tx *gorm.DB) error {
        if err := models.NewOperationDetailsSearch().SetOrm(tx).SetOperationId(id).Delete(); err != nil {
            return err
        }
        if err := models.NewOperationSearch().SetOrm(tx).SetID(id).Delete(); err != nil {
            return err
        }
        return nil
    }); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "修改失败:"+err.Error())
        return
    }
    util.ResponseFormat(c, code.Success, "删除成功")
}
 
// DeleteDevice
//
//    @Tags        入库/出库
//    @Summary    更改记录状态
//    @Produce    application/json
//    @Param        id    path        int            true    "id"
//    @Success    200    {object}    util.Response    "成功"
//    @Router        /api-wms/v1/operation/finish/{id} [put]
func (slf OperationController) Finish(c *gin.Context) {
    id, err := strconv.Atoi(c.Param("id"))
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "错误的id值")
        return
    }
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "id为0")
        return
    }
    operation, err := models.NewOperationSearch().SetID(id).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "未找到相关出入库信息:"+err.Error())
        return
    }
    if operation.Status != constvar.OperationStatus_Ready {
        util.ResponseFormat(c, code.RequestError, "该出入库信息无法完成")
        return
    }
    operationType, err := models.NewOperationTypeSearch().SetID(uint(operation.OperationTypeId)).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestError, err.Error())
        return
    }
    if err := models.WithTransaction(func(tx *gorm.DB) error {
 
        if err := models.NewOperationSearch().SetOrm(tx).SetID(id).Update(&models.Operation{Status: constvar.OperationStatus_Finish}); err != nil {
            return err
        }
        if operationType.BaseOperationType != constvar.BaseOperationTypeInternal {
            var listProdtId []string
            var listProdt []*models.Material
            mapProdt := make(map[string]decimal.Decimal)
            listDetails, err := models.NewOperationDetailsSearch().SetOperationId(operation.Id).FindAll()
            if err != nil {
                return err
            }
            for _, v := range listDetails {
                listProdtId = append(listProdtId, v.ProductId)
                mapProdt[v.ProductId] = v.Amount
            }
            if err := models.NewMaterialSearch().Orm.Where("id IN ?", listProdtId).Find(&listProdt).Error; err != nil {
                return err
            }
            if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming {
                for k, v := range listProdt {
                    if value, ok := mapProdt[v.ID]; !ok {
                        return errors.New("产品种类异常")
                    } else {
                        listProdt[k].Amount = listProdt[k].Amount.Add(value)
                        if err := tx.Save(listProdt[k]).Error; err != nil {
                            return err
                        }
                    }
                }
            }
            if operationType.BaseOperationType == constvar.BaseOperationTypeOutgoing {
                for k, v := range listProdt {
                    if value, ok := mapProdt[v.ID]; !ok {
                        return errors.New("产品种类异常")
                    } else {
                        if v.Amount.LessThan(value) {
                            return errors.New(fmt.Sprintf("产品:%v,库存:%v,出库:%v,数量不够,无法完成出库操作", v.Name, v.Amount.String(), value.String()))
                        }
                        listProdt[k].Amount = listProdt[k].Amount.Sub(value)
                        if err := tx.Save(listProdt[k]).Error; err != nil {
                            return err
                        }
                    }
                }
            }
 
        }
        return nil
    }); err != nil {
        util.ResponseFormat(c, code.RequestError, err.Error())
        return
    }
    util.ResponseFormat(c, code.Success, "操作成功")
}
 
// ListAll
// @Tags      入库/出库
// @Summary   调拨
// @Produce   application/json
// @Param     object  body  request.OperationAllList true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/operation/listAll [post]
func (slf OperationController) ListAll(c *gin.Context) {
    var params request.OperationAllList
    if err := c.BindJSON(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
        return
    }
    if !params.PageInfo.Check() {
        util.ResponseFormat(c, code.RequestParamError, "数据分页信息错误")
        return
    }
    search := models.NewOperationSearch()
    search.SetPage(params.Page, params.PageSize)
    search.SetPage(params.Page, params.PageSize)
    if params.Number != "" {
        search.SetNumber(params.Number)
    }
    if params.SourceNumber != "" {
        search.SetSourceNumber(params.SourceNumber)
    }
    list, total, err := search.SetPreload(true).SetOrder("created_at desc").Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestError, "查找失败:"+err.Error())
        return
    }
    util.ResponseFormatListWithPage(c, code.Success, list, int(total), params.Page, params.PageSize)
}