package models
|
|
import (
|
"fmt"
|
"github.com/shopspring/decimal"
|
"gorm.io/gorm"
|
"srm/global"
|
)
|
|
type (
|
OutsourcingMaterialApply struct {
|
gorm.Model
|
OutsourcingOrderNumber string `json:"outsourcingOrderNumber" gorm:"type:varchar(255);comment:委外订单编码"`
|
MaterialNumber string `json:"materialNumber" gorm:"type:varchar(191);comment:物料编码"`
|
MaterialName string `json:"materialName" gorm:"type:varchar(191);comment:物料名称"`
|
Unit string `json:"unit" gorm:"type:varchar(100);comment:单位"`
|
Specs string `gorm:"type:varchar(191);comment:物料规格" json:"specs"`
|
Type string `gorm:"type:varchar(191);comment:物料型号" json:"type"`
|
Amount decimal.Decimal `gorm:"type:decimal(35,18);comment:数量" json:"amount"`
|
}
|
OutsourcingMaterialApplySearch struct {
|
OutsourcingMaterialApply
|
PageNum int
|
PageSize int
|
Orm *gorm.DB
|
}
|
)
|
|
func (slf OutsourcingMaterialApply) TableName() string {
|
return "outsourcing_material_apply"
|
}
|
|
func NewOutsourcingMaterialApplySearch() *OutsourcingMaterialApplySearch {
|
return &OutsourcingMaterialApplySearch{Orm: global.GVA_DB}
|
}
|
|
func (slf *OutsourcingMaterialApplySearch) SetOrm(tx *gorm.DB) *OutsourcingMaterialApplySearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *OutsourcingMaterialApplySearch) SetPage(page, size int) *OutsourcingMaterialApplySearch {
|
slf.PageNum, slf.PageSize = page, size
|
return slf
|
}
|
|
func (slf *OutsourcingMaterialApplySearch) SetOutsourcingOrderNumber(number string) *OutsourcingMaterialApplySearch {
|
slf.OutsourcingOrderNumber = number
|
return slf
|
}
|
|
func (slf *OutsourcingMaterialApplySearch) build() *gorm.DB {
|
var db = slf.Orm.Table(slf.TableName())
|
|
if slf.OutsourcingOrderNumber != "" {
|
db = db.Where("outsourcing_order_number = ?", slf.OutsourcingOrderNumber)
|
}
|
|
return db
|
}
|
|
// CreateBatch 批量插入
|
func (slf *OutsourcingMaterialApplySearch) CreateBatch(records []*OutsourcingMaterialApply) error {
|
var db = slf.build()
|
|
if err := db.Create(&records).Error; err != nil {
|
return fmt.Errorf("create batch err: %v, records: %+v", err, records)
|
}
|
|
return nil
|
}
|
|
func (slf *OutsourcingMaterialApplySearch) Find() ([]*OutsourcingMaterialApply, int64, error) {
|
var (
|
records = make([]*OutsourcingMaterialApply, 0)
|
total int64
|
db = slf.build()
|
)
|
|
if err := db.Count(&total).Error; err != nil {
|
return records, total, fmt.Errorf("find count err: %v", err)
|
}
|
if slf.PageNum*slf.PageSize > 0 {
|
db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
|
}
|
if err := db.Find(&records).Error; err != nil {
|
return records, total, fmt.Errorf("find records err: %v", err)
|
}
|
|
return records, total, nil
|
}
|
|
func (slf *OutsourcingMaterialApplySearch) FindNotTotal() ([]*OutsourcingMaterialApply, error) {
|
var (
|
records = make([]*OutsourcingMaterialApply, 0)
|
db = slf.build()
|
)
|
|
if err := db.Find(&records).Error; err != nil {
|
return records, fmt.Errorf("find records err: %v", err)
|
}
|
|
return records, nil
|
}
|