jiangshuai
2023-09-22 f89bbcf77dae0465e829ca6f7548cd36ef57aaa6
controllers/operation.go
@@ -128,13 +128,12 @@
// @Tags      入库/出库
// @Summary   入库/出库列表
// @Produce   application/json
// @Accept     json
// @Param     object  query  request.OperationList true  "参数"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/operation/operation [get]
// @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.ShouldBindQuery(&params); err != nil {
   if err := c.BindJSON(&params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
      return
   }
@@ -144,13 +143,19 @@
   }
   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.ResponseFormatList(c, code.Success, list, int(total))
   util.ResponseFormatListWithPage(c, code.Success, list, int(total), params.Page, params.PageSize)
}
@@ -293,11 +298,7 @@
      if err := models.NewOperationSearch().SetOrm(tx).SetID(id).Update(&models.Operation{Status: constvar.OperationStatus_Finish}); err != nil {
         return err
      }
      if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming {
         if err := models.NewMaterialSearch().Orm.Exec("update material INNER JOIN wms_operation_details on wms_operation_details.product_id=material.id INNER JOIN wms_operation on wms_operation.id=wms_operation_details.operation_id set material.amount=material.amount + wms_operation_details.amount where wms_operation.id=?", id).Error; err != nil {
            return err
         }
      } else if operationType.BaseOperationType == constvar.BaseOperationTypeOutgoing {
      if operationType.BaseOperationType != constvar.BaseOperationTypeInternal {
         var listProdtId []string
         var listProdt []*models.Material
         mapProdt := make(map[string]decimal.Decimal)
@@ -312,18 +313,34 @@
         if err := models.NewMaterialSearch().Orm.Where("id IN ?", listProdtId).Find(&listProdt).Error; err != nil {
            return err
         }
         for _, 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()))
         if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming {
            for k, v := range listProdt {
               if value, ok := mapProdt[v.ID]; !ok {
                  return errors.New("产品种类异常")
               } else {
                  listProdt[k].Amount.Add(value)
                  if err := tx.Save(listProdt[k]).Error; err != nil {
                     return err
                  }
               }
            }
         }
         if err := models.NewMaterialSearch().Orm.Exec("update material INNER JOIN wms_operation_details on wms_operation_details.product_id=material.id INNER JOIN wms_operation on wms_operation.id=wms_operation_details.operation_id set material.amount=material.amount - wms_operation_details.amount where wms_operation.id=?", id).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.Sub(value)
                  if err := tx.Save(listProdt[k]).Error; err != nil {
                     return err
                  }
               }
            }
         }
      }
      return nil
   }); err != nil {
@@ -332,3 +349,39 @@
   }
   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)
}