liujiandao
2023-10-09 2716487d13f3521adeb25a48aa07a89f885d32c7
controllers/location_product_amount.go
@@ -12,6 +12,7 @@
   "wms/extend/util"
   "wms/models"
   "wms/request"
   "wms/response"
)
type LocationProductAmountController struct {
@@ -41,6 +42,7 @@
   search.Orm = search.Orm.
      Table("wms_location_product_amount").
      Select(`
               wms_location_product_amount.id as location_product_amount_id,
               wms_location.id as location_id,
               wms_location.name as location_name,
               material.id as product_id,
@@ -49,14 +51,13 @@
               material.unit,
               wms_location_product_amount.create_date,
               wms_operation_details.amount as adjust_amount,
               wms_operation.id as operation_id`).
               wms_operation.id as operation_id,
               wms_operation.status`).
      InnerJoins("inner join wms_location_product on wms_location_product.id=wms_location_product_amount.location_product_id").
      InnerJoins("inner join material on material.id=wms_location_product.product_id").
      InnerJoins("inner join wms_location on wms_location.id=wms_location_product.location_id").
      InnerJoins("inner join wms_operation_details on wms_operation_details.product_id=wms_location_product.product_id").
      InnerJoins("inner join material on material.id=wms_operation_details.product_id").
      InnerJoins("inner join wms_operation on wms_operation.id=wms_operation_details.operation_id").
      Where("wms_operation.base_operation_type=?", constvar.BaseOperationTypeAdjust)
      Joins("left join wms_operation_details on wms_operation_details.product_id=wms_location_product.product_id").
      Joins("left join wms_operation on wms_operation.id=wms_operation_details.operation_id and wms_operation.base_operation_type=? and wms_operation.status=?", constvar.BaseOperationTypeAdjust, constvar.OperationStatus_Ready)
   var (
      records = make([]*models.LocationProductAmountWithOperation, 0)
      total   int64
@@ -70,12 +71,14 @@
   if params.Page*params.PageSize > 0 {
      search.Orm = search.Orm.Offset((params.Page - 1) * params.PageSize).Limit(params.PageSize)
   }
   if err := search.Orm.Find(&records).Error; err != nil {
   if err := search.Orm.Order("wms_operation.created_at desc").Find(&records).Error; err != nil {
      util.ResponseFormat(c, code.RequestError, fmt.Errorf("find count err: %v", err))
      return
   }
   for k, v := range records {
      records[k].DifferenceAmount = v.Amount.Sub(v.AdjustAmount)
      if v.OperationId != 0 {
         records[k].DifferenceAmount = v.Amount.Add(v.AdjustAmount)
      }
   }
   util.ResponseFormatListWithPage(c, code.Success, records, int(total), params.Page, params.PageSize)
}
@@ -121,6 +124,11 @@
      util.ResponseFormat(c, code.RequestError, "不存在该上架规则,无法进行库存盘点")
      return
   }
   existAmount, err := models.NewLocationProductAmountSearch().SetLocationProductId(locProduct.Id).First()
   if err != nil {
      util.ResponseFormat(c, code.RequestError, err.Error())
      return
   }
   location, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeAdjust)).First()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, err.Error())
@@ -128,11 +136,15 @@
   }
   //reqParams.AdjustAmount = reqParams.Amount.Sub(reqParams.DifferenceAmount)
   reqParams.AdjustAmount = reqParams.DifferenceAmount.Sub(reqParams.Amount)
   locAmount := models.LocationProductAmount{
      LocationProductId: locProduct.Id,
      Amount:            decimal.NewFromInt(0),
      CreateDate:        time.Now().Format("2006-01-02 15:04:05"),
   var locAmount models.LocationProductAmount
   if existAmount.Id == 0 {
      locAmount = models.LocationProductAmount{
         LocationProductId: locProduct.Id,
         Amount:            decimal.NewFromInt(0),
         CreateDate:        time.Now().Format("2006-01-02 15:04:05"),
      }
   }
   detail := &models.OperationDetails{
      ProductId: reqParams.ProductId,
      Amount:    reqParams.AdjustAmount,
@@ -148,20 +160,14 @@
      ToLocationID:      reqParams.LocationId,
   }
   //if reqParams.AdjustAmount.GreaterThanOrEqual(decimal.NewFromInt(0)) {
   //   operation.FromLocationID = Location.Id
   //   operation.ToLocationID = reqParams.LocationId
   //} else {
   //   operation.FromLocationID = reqParams.LocationId
   //   operation.ToLocationID = Location.Id
   //}
   if err := models.WithTransaction(func(tx *gorm.DB) error {
      if err := models.NewOperationSearch().SetOrm(tx).Create(&operation); err != nil {
         return err
      }
      if err := models.NewLocationProductAmountSearch().SetOrm(tx).Create(&locAmount); err != nil {
         return err
      if existAmount.Id == 0 {
         if err := models.NewLocationProductAmountSearch().SetOrm(tx).Create(&locAmount); err != nil {
            return err
         }
      }
      return nil
   }); err != nil {
@@ -172,7 +178,33 @@
   util.ResponseFormat(c, code.Success, "添加成功")
}
// Add
// GetRuleList
// @Tags      库存盘点
// @Summary   获取上架规则
// @Produce   application/json
// @Param     object  body  request.GetRuleList true  "查询信息"
// @Success   200 {object} util.ResponseList{data=[]response.RuleList}   "成功"
// @Router    /api-wms/v1/locationProductAmount/getRuleList [post]
func (slf LocationProductAmountController) GetRuleList(c *gin.Context) {
   var params request.GetRuleList
   if err := c.BindJSON(&params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
      return
   }
   list, err := models.NewLocationProductSearch().SetProductId(params.ProductId).SetLocationId(params.LocationId).Find()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, err.Error())
      return
   }
   var ruleList response.RuleList
   for _, l := range list {
      ruleList.LocationList = append(ruleList.LocationList, l.Location)
      ruleList.ProductList = append(ruleList.ProductList, l.Product)
   }
   util.ResponseFormat(c, code.Success, ruleList)
}
// Update
// @Tags      库存盘点
// @Summary   修改库存盘点信息
// @Produce   application/json