package models
|
|
import (
|
"fmt"
|
"github.com/shopspring/decimal"
|
"gorm.io/gorm"
|
"outsourcing/pkg/mysqlx"
|
)
|
|
type (
|
// OutsourcingOrderProduct 委外订单产品表
|
OutsourcingOrderProduct struct {
|
BaseModelInt
|
OutsourcingOrderID uint `gorm:"index;not null;comment:委外订单ID" json:"outsourcingOrderID"`
|
EnterpriseID uint `gorm:"type:int;not null;default:0;comment:供应商ID" json:"enterpriseID" ` //供应商ID
|
ProductId string `gorm:"type:varchar(191);comment:产品id" json:"productId"`
|
ProductName string `gorm:"type:varchar(191);comment:产品名称" json:"productName"`
|
Specs string `gorm:"type:varchar(191);comment:物料规格" json:"specs"`
|
Type string `gorm:"type:varchar(191);comment:物料型号" json:"type"`
|
Amount int64 `gorm:"type:int(11);comment:订单数量" json:"amount"`
|
Unit string `gorm:"type:varchar(100);comment:单位" json:"unit"`
|
BomID string `gorm:"type:varchar(255);default '';comment:bomID" json:"bomID"`
|
SendAmount decimal.Decimal `gorm:"-" json:"sendAmount"`
|
}
|
|
OutsourcingOrderProductSearch struct {
|
OutsourcingOrderProduct
|
Order string
|
PageNum int
|
PageSize int
|
Orm *gorm.DB
|
IDs []uint
|
OutsourcingOrderIDs []uint
|
}
|
)
|
|
func (slf OutsourcingOrderProduct) TableName() string {
|
return "outsourcing_order_product"
|
}
|
|
func NewOutsourcingOrderProductSearch() *OutsourcingOrderProductSearch {
|
return &OutsourcingOrderProductSearch{Orm: mysqlx.GetDB()}
|
}
|
|
func (slf *OutsourcingOrderProductSearch) SetOrm(tx *gorm.DB) *OutsourcingOrderProductSearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *OutsourcingOrderProductSearch) SetPage(page, size int) *OutsourcingOrderProductSearch {
|
slf.PageNum, slf.PageSize = page, size
|
return slf
|
}
|
|
func (slf *OutsourcingOrderProductSearch) SetOrder(order string) *OutsourcingOrderProductSearch {
|
slf.Order = order
|
return slf
|
}
|
|
func (slf *OutsourcingOrderProductSearch) SetId(id uint) *OutsourcingOrderProductSearch {
|
slf.ID = id
|
return slf
|
}
|
|
func (slf *OutsourcingOrderProductSearch) SetOutsourcingOrderID(id uint) *OutsourcingOrderProductSearch {
|
slf.OutsourcingOrderID = id
|
return slf
|
}
|
|
func (slf *OutsourcingOrderProductSearch) SetOutsourcingOrderIDs(ids []uint) *OutsourcingOrderProductSearch {
|
slf.OutsourcingOrderIDs = ids
|
return slf
|
}
|
|
func (slf *OutsourcingOrderProductSearch) SetEnterpriseID(id uint) *OutsourcingOrderProductSearch {
|
slf.EnterpriseID = id
|
return slf
|
}
|
|
func (slf *OutsourcingOrderProductSearch) SetIds(ids []uint) *OutsourcingOrderProductSearch {
|
slf.IDs = ids
|
return slf
|
}
|
|
func (slf *OutsourcingOrderProductSearch) build() *gorm.DB {
|
var db = slf.Orm.Table(slf.TableName())
|
if slf.ID != 0 {
|
db = db.Where("id = ?", slf.ID)
|
}
|
if slf.Order != "" {
|
db = db.Order(slf.Order)
|
}
|
if len(slf.IDs) > 0 {
|
db = db.Where("id in ?", slf.IDs)
|
}
|
|
if slf.OutsourcingOrderID != 0 {
|
db = db.Where("outsourcing_order_id = ?", slf.OutsourcingOrderID)
|
}
|
|
if len(slf.OutsourcingOrderIDs) > 0 {
|
db = db.Where("outsourcing_order_id in (?)", slf.OutsourcingOrderIDs)
|
}
|
|
if slf.EnterpriseID != 0 {
|
db = db.Where("enterprise_id = ?", slf.EnterpriseID)
|
}
|
|
return db
|
}
|
|
// Create 单条插入
|
func (slf *OutsourcingOrderProductSearch) Create(record *OutsourcingOrderProduct) error {
|
var db = slf.build()
|
|
if err := db.Create(record).Error; err != nil {
|
return fmt.Errorf("create err: %v, record: %+v", err, record)
|
}
|
|
return nil
|
}
|
|
// CreateBatch 批量插入
|
func (slf *OutsourcingOrderProductSearch) CreateBatch(records []*OutsourcingOrderProduct) 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 *OutsourcingOrderProductSearch) Save(record *OutsourcingOrderProduct) error {
|
var db = slf.build()
|
|
if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
|
return fmt.Errorf("save err: %v, record: %+v", err, record)
|
}
|
|
return nil
|
}
|
|
func (slf *OutsourcingOrderProductSearch) UpdateByMap(upMap map[string]interface{}) error {
|
var (
|
db = slf.build()
|
)
|
|
if err := db.Updates(upMap).Error; err != nil {
|
return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
|
}
|
|
return nil
|
}
|
|
func (slf *OutsourcingOrderProductSearch) Delete() error {
|
var db = slf.build()
|
|
if err := db.Unscoped().Delete(&OutsourcingOrderProduct{}).Error; err != nil {
|
return err
|
}
|
|
return nil
|
}
|
|
func (slf *OutsourcingOrderProductSearch) First() (*OutsourcingOrderProduct, error) {
|
var (
|
record = new(OutsourcingOrderProduct)
|
db = slf.build()
|
)
|
|
if err := db.First(record).Error; err != nil {
|
return record, err
|
}
|
|
return record, nil
|
}
|
|
func (slf *OutsourcingOrderProductSearch) Find() ([]*OutsourcingOrderProduct, int64, error) {
|
var (
|
records = make([]*OutsourcingOrderProduct, 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 *OutsourcingOrderProductSearch) FindNotTotal() ([]*OutsourcingOrderProduct, error) {
|
var (
|
records = make([]*OutsourcingOrderProduct, 0)
|
db = slf.build()
|
)
|
|
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, fmt.Errorf("find records err: %v", err)
|
}
|
|
return records, nil
|
}
|
|
func (slf *OutsourcingOrderProductSearch) Count() (int64, error) {
|
var (
|
total int64
|
db = slf.build()
|
)
|
|
err := db.Count(&total).Error
|
return total, err
|
}
|