wanjianli
2023-09-18 690dd891f8ee47b6036eb87c239275490ee02b7f
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
package controllers
 
import (
    "errors"
    "github.com/gin-gonic/gin"
    "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, "参数解析失败,数据类型错误")
    }
    if err := structx.AssignTo(reqParams, params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "数据转换错误")
    }
    if err := slf.CheckParams(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
    }
    if err := models.NewOperationSearch().Create(&params); err != nil {
        logx.Errorf("Operation create err: %v", err)
        util.ResponseFormat(c, code.SaveFail, "添加失败:"+err.Error())
    }
    util.ResponseFormat(c, code.Success, "添加成功")
 
}
 
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.FromLocationId == 0 {
        return errors.New("请选择源位置")
    }
 
    if params.ToLocationId == 0 {
        return errors.New("请选择目标位置")
    }
 
    if params.OperationDate.IsZero() {
        return errors.New("请选择安排日期")
    }
 
    if len(params.Details) <= 0 {
        return errors.New("请添加明细信息")
    }
 
    //检查明细部分
    for _, v := range params.Details {
        if v.ProductId == 0 {
            return errors.New("productID为0")
        }
        if v.ProductName == "" {
            return errors.New("产品名称异常")
        }
        if v.Quantity.IsNegative() {
            return errors.New("产品数量出错")
        }
        if v.FinishQuantity.IsNegative() {
            return errors.New("产品数量出错")
        }
    }
 
    return nil
}