jiangshuai
2023-09-27 1444c7c800fc042cd99213e901d6ad3722d0f03f
controllers/operation.go
@@ -6,7 +6,9 @@
   "github.com/gin-gonic/gin"
   "github.com/shopspring/decimal"
   "gorm.io/gorm"
   "sort"
   "strconv"
   "time"
   "wms/constvar"
   "wms/extend/code"
   "wms/extend/util"
@@ -46,13 +48,43 @@
      return
   }
   if CheckDetailsRepeat(params.Details) {
      util.ResponseFormat(c, code.RequestParamError, "明细中不能存在重复的产品")
      return
   }
   operationType, err := models.NewOperationTypeSearch().SetID(uint(params.OperationTypeId)).First()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, err.Error())
      return
   }
   params.Status = constvar.OperationStatus_Ready
   params.Number = strconv.FormatInt(time.Now().Unix(), 10)
   params.BaseOperationType = operationType.BaseOperationType
   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 CheckDetailsRepeat(details []*models.OperationDetails) bool {
   detailIDs := []string{}
   var tempID string
   for _, v := range details {
      detailIDs = append(detailIDs, v.ProductId)
   }
   sort.Strings(detailIDs)
   for _, v := range detailIDs {
      if v != tempID {
         tempID = v
      } else {
         return true
      }
   }
   return false
}
func (slf OperationController) FormatLocation(params *models.Operation) error {
@@ -113,9 +145,9 @@
      if v.ProductId == "" {
         return errors.New("productID为空")
      }
      if v.ProductName == "" {
         return errors.New("产品名称异常")
      }
      //if v.ProductName == "" {
      //   return errors.New("产品名称异常")
      //}
      if v.Amount.IsNegative() {
         return errors.New("产品数量出错")
      }
@@ -156,7 +188,6 @@
   }
   util.ResponseFormatListWithPage(c, code.Success, list, int(total), params.Page, params.PageSize)
}
func (slf OperationController) CheckListParams(params *request.OperationList) error {
@@ -193,6 +224,10 @@
   }
   if err := slf.CheckParams(params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, err.Error())
      return
   }
   if CheckDetailsRepeat(params.Details) {
      util.ResponseFormat(c, code.RequestParamError, "明细中不能存在重复的产品")
      return
   }
   if err := slf.FormatLocation(&params); err != nil {
@@ -318,12 +353,18 @@
               if value, ok := mapProdt[v.ID]; !ok {
                  return errors.New("产品种类异常")
               } else {
                  listProdt[k].Amount.Add(value)
                  listProdt[k].Amount = listProdt[k].Amount.Add(value)
                  if err := tx.Save(listProdt[k]).Error; err != nil {
                     return err
                  }
                  //TODO:出入库的finish和报废的finish都要增加对location_product_amount表数量的更新,因为此表有ProductCategory字段,所以operation_details表中要增加ProductCategoryId字段
                  //var locAmount models.LocationProductAmount
                  //if err := models.NewLocationProductAmountSearch().Orm.Table("wms_location_produt_amount").Joins("inner join wms_location_product on wms_location_produt.id=wms_location_produt_amount.location_product_id").Where("wms_location_produt.product_id=? and wms_location_produt.location_id=?",operation.ToLocationID,v.ID).First(&locAmount).Error;err!=nil {
                  //   return err
                  //}
               }
            }
         }
         if operationType.BaseOperationType == constvar.BaseOperationTypeOutgoing {
            for k, v := range listProdt {
@@ -333,7 +374,7 @@
                  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)
                  listProdt[k].Amount = listProdt[k].Amount.Sub(value)
                  if err := tx.Save(listProdt[k]).Error; err != nil {
                     return err
                  }
@@ -381,7 +422,5 @@
      util.ResponseFormat(c, code.RequestError, "查找失败:"+err.Error())
      return
   }
   util.ResponseFormatListWithPage(c, code.Success, list, int(total), params.Page, params.PageSize)
}