zhangqian
2024-08-01 fc3313955a083c9480e4ea74398f72f9ba6addcd
models/location_product_amount.go
@@ -1,6 +1,7 @@
package models
import (
   "encoding/json"
   "fmt"
   "github.com/shopspring/decimal"
   "gorm.io/gorm"
@@ -21,7 +22,9 @@
      ProductId         string          `json:"productId" gorm:"type:varchar(191);not null;comment:产品id"` //产品id
      Product           Material        `json:"product" gorm:"foreignKey:ProductId;references:ID"`
      Amount            decimal.Decimal `json:"amount" gorm:"type:decimal(20,2);not null;comment:库存数量"` //库存数量
      CreateDate        string          `json:"createDate" gorm:"type:varchar(63);comment:日期"`          //日期
      MoreUnitList      []UnitItems     `json:"amountMoreUnits" gorm:"-"`                               //在库数量多单位
      MoreUnitValue     string          `json:"-" gorm:"type:varchar(255);comment:多单位值"`
      CreateDate        string          `json:"createDate" gorm:"type:varchar(63);comment:日期"` //日期
   }
   LocationProductAmountSearch struct {
@@ -33,11 +36,13 @@
      Orm      *gorm.DB
      Preload  bool
      //LocationProductIds []int
      LocationIds []int
      ProductIds  []string
      Ids         []int
      Query       string
      Fields      string
      LocationIds     []int
      ProductIds      []string
      Ids             []int
      Query           string
      Fields          string
      CategoryIds     []int
      JoinedMaterials bool
   }
   LocationProductAmountWithOperation struct {
@@ -62,6 +67,37 @@
func (slf *LocationProductAmount) TableName() string {
   return "wms_location_product_amount"
}
func (slf *LocationProductAmount) AfterFind(tx *gorm.DB) (err error) {
   if slf.MoreUnitValue != "" {
      var arr []UnitItems
      err := json.Unmarshal([]byte(slf.MoreUnitValue), &arr)
      if err != nil {
         return err
      }
      slf.MoreUnitList = arr
   }
   return
}
func (slf *LocationProductAmount) BeforeCreate(tx *gorm.DB) (err error) {
   if len(slf.MoreUnitList) != 0 {
      items := make([]UnitItems, 0)
      for k, item := range slf.MoreUnitList {
         if item.Unit != "" && !item.Amount.IsZero() {
            items = append(items, slf.MoreUnitList[k])
         }
      }
      str, err := json.Marshal(items)
      if err != nil {
         return err
      }
      slf.MoreUnitValue = string(str)
   }
   return
}
func NewLocationProductAmountSearch() *LocationProductAmountSearch {
@@ -138,6 +174,11 @@
   return slf
}
func (slf *LocationProductAmountSearch) SetCategoryIds(ids []int) *LocationProductAmountSearch {
   slf.CategoryIds = ids
   return slf
}
func (slf *LocationProductAmountSearch) build() *gorm.DB {
   var db = slf.Orm.Model(&LocationProductAmount{})
@@ -149,6 +190,10 @@
   }
   if slf.Keyword != "" {
      if !slf.JoinedMaterials {
         db = db.Joins("left join material on wms_location_product_amount.product_id = material.id")
         slf.JoinedMaterials = true
      }
      db = db.Joins("left join wms_location on wms_location_product_amount.location_id = wms_location.id").
         Joins("left join material on wms_location_product_amount.product_id = material.id").
         Joins("left join wms_product_category on wms_location_product_amount.product_category_id = wms_product_category.id").
@@ -190,6 +235,14 @@
   if slf.Fields != "" {
      db = db.Select(slf.Fields)
   }
   if len(slf.CategoryIds) > 0 {
      if !slf.JoinedMaterials {
         db = db.Joins("left join material on wms_location_product_amount.product_id = material.id")
         slf.JoinedMaterials = true
      }
      db = db.Where("material.category_id in ?", slf.CategoryIds)
   }
   return db
@@ -281,6 +334,19 @@
   return records, nil
}
func (slf *LocationProductAmountSearch) FindAll() ([]*LocationProductAmount, error) {
   var (
      records = make([]*LocationProductAmount, 0)
      db      = slf.build()
   )
   if err := db.Find(&records).Error; err != nil {
      return records, fmt.Errorf("find records err: %v", err)
   }
   return records, nil
}
func (slf *LocationProductAmountSearch) FirstRes() (*LocationProductAmount, *gorm.DB) {
   var (
      record = new(LocationProductAmount)