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(¶ms); 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 }