zhangqian
2024-04-16 71288e85afbfc7ac12d5060feb1d29aa139513d6
models/operation_details.go
@@ -12,8 +12,8 @@
   OperationDetails struct {
      WmsModel
      Id          int    `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
      OperationID int    `json:"operationId" gorm:"type:int;not null;comment:操作记录id"`      //操作id
      ProductId   string `json:"productId" gorm:"type:varchar(191);not null;comment:产品id"` //产品id
      OperationID int    `json:"operationId" gorm:"index;type:int;not null;comment:操作记录id"` //操作id
      ProductId   string `json:"productId" gorm:"type:varchar(191);not null;comment:产品id"`  //产品id
      //ProductName string          `json:"productName" gorm:"type:varchar(255);not null;comment:产品名称"` //产品名称
      Amount decimal.Decimal `json:"amount" gorm:"type:decimal(20,2);not null;comment:数量"` //数量
      //Unit        string          `json:"unit" gorm:"type:varchar(31);comment:单位"`                    //单位
@@ -29,12 +29,14 @@
   OperationDetailsSearch struct {
      OperationDetails
      Order    string
      PageNum  int
      PageSize int
      Keyword  string
      Orm      *gorm.DB
      Preload  bool
      Order        string
      PageNum      int
      PageSize     int
      Keyword      string
      Orm          *gorm.DB
      Preload      bool
      OperationIDs []int
      Fields       string
   }
)
@@ -81,6 +83,16 @@
   return slf
}
func (slf *OperationDetailsSearch) SetOperationIds(operationIds []int) *OperationDetailsSearch {
   slf.OperationIDs = operationIds
   return slf
}
func (slf *OperationDetailsSearch) SetFields(fields string) *OperationDetailsSearch {
   slf.Fields = fields
   return slf
}
func (slf *OperationDetailsSearch) SetProductId(productId string) *OperationDetailsSearch {
   slf.ProductId = productId
   return slf
@@ -109,6 +121,14 @@
   }
   if slf.Preload {
      db = db.Preload("Product")
   }
   if len(slf.OperationIDs) > 0 {
      db = db.Where("operation_id in ?", slf.OperationIDs)
   }
   if slf.Fields != "" {
      db = db.Select(slf.Fields)
   }
   return db
@@ -274,3 +294,14 @@
   return records, nil
}
func (slf *OperationDetailsSearch) GroupSum(groupField string, sumField string) ([]*GroupSum, error) {
   var (
      db     = slf.build()
      result = make([]*GroupSum, 0)
   )
   if err := db.Select("sum(" + sumField + ") as sum, " + groupField + " as class").Group(groupField).Scan(&result).Error; err != nil {
      return nil, fmt.Errorf("select group err: %v", err)
   }
   return result, nil
}