liujiandao
2023-09-23 79eca4ef507f418b88a0817f43a9ea25750a818a
controllers/operation.go
@@ -7,6 +7,7 @@
   "github.com/shopspring/decimal"
   "gorm.io/gorm"
   "strconv"
   "time"
   "wms/constvar"
   "wms/extend/code"
   "wms/extend/util"
@@ -47,6 +48,7 @@
   }
   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())
@@ -128,13 +130,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,14 +145,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)
}
func (slf OperationController) CheckListParams(params *request.OperationList) error {
@@ -293,11 +299,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.quantity 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 +314,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 = 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.quantity 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 = listProdt[k].Amount.Sub(value)
                  if err := tx.Save(listProdt[k]).Error; err != nil {
                     return err
                  }
               }
            }
         }
      }
      return nil
   }); err != nil {
@@ -332,3 +350,37 @@
   }
   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)
}