package model
|
|
import (
|
"aps_crm/pkg/mysqlx"
|
"gorm.io/gorm"
|
)
|
|
type (
|
// InvoiceProduct 合同产品
|
InvoiceProduct struct {
|
InvoiceId int `gorm:"invoice_id" json:"invoiceId"`
|
ProductId int `gorm:"product_id" json:"productId"`
|
}
|
|
// InvoiceProductSearch 合同产品搜索条件
|
InvoiceProductSearch struct {
|
InvoiceProduct
|
Orm *gorm.DB
|
Keyword string
|
PageNum int
|
PageSize int
|
ProductIds []uint
|
}
|
)
|
|
func (InvoiceProduct) TableName() string {
|
return "invoice_product"
|
}
|
|
func NewInvoiceProductSearch() *InvoiceProductSearch {
|
return &InvoiceProductSearch{
|
Orm: mysqlx.GetDB(),
|
}
|
}
|
|
func (slf *InvoiceProductSearch) build() *gorm.DB {
|
var db = slf.Orm.Model(&InvoiceProduct{})
|
if len(slf.ProductIds) != 0 {
|
db = db.Where("product_id in ?", slf.ProductIds)
|
}
|
if slf.InvoiceId != 0 {
|
db = db.Where("invoice_id = ?", slf.InvoiceId)
|
}
|
|
return db
|
}
|
|
func (slf *InvoiceProductSearch) Create(record *InvoiceProduct) error {
|
var db = slf.build()
|
return db.Create(record).Error
|
}
|
|
func (slf *InvoiceProductSearch) CreateBatch(records []*InvoiceProduct) error {
|
var db = slf.build()
|
return db.Create(records).Error
|
}
|
|
func (slf *InvoiceProductSearch) Delete() error {
|
var db = slf.build()
|
return db.Delete(&InvoiceProduct{}).Error
|
}
|
|
func (slf *InvoiceProductSearch) Update(record *InvoiceProduct) error {
|
var db = slf.build()
|
return db.Updates(record).Error
|
}
|
|
func (slf *InvoiceProductSearch) FindAll() ([]*InvoiceProduct, error) {
|
var db = slf.build()
|
var record = make([]*InvoiceProduct, 0)
|
err := db.Find(&record).Error
|
return record, err
|
}
|
|
func (slf *InvoiceProductSearch) SetProductIds(ids []uint) *InvoiceProductSearch {
|
slf.ProductIds = ids
|
return slf
|
}
|
|
func (slf *InvoiceProductSearch) SetInvoiceId(id int) *InvoiceProductSearch {
|
slf.InvoiceId = id
|
return slf
|
}
|
|
func (slf *InvoiceProductSearch) SetOrm(tx *gorm.DB) *InvoiceProductSearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *InvoiceProductSearch) First() (*InvoiceProduct, error) {
|
var db = slf.build()
|
var record = new(InvoiceProduct)
|
err := db.First(record).Error
|
return record, err
|
}
|
|
func (slf *InvoiceProductSearch) Updates(values interface{}) error {
|
var db = slf.build()
|
return db.Updates(values).Error
|
}
|
|
func (slf *InvoiceProductSearch) Find() ([]*InvoiceProduct, int64, error) {
|
var db = slf.build()
|
var records = make([]*InvoiceProduct, 0)
|
var total int64
|
if err := db.Count(&total).Error; err != nil {
|
return records, total, err
|
}
|
if slf.PageNum > 0 && slf.PageSize > 0 {
|
db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
|
}
|
|
err := db.Find(&records).Error
|
return records, total, err
|
}
|
|
// InitDefaultData 初始化数据
|
func (slf *InvoiceProductSearch) InitDefaultData() error {
|
var (
|
db = slf.Orm.Table(slf.TableName())
|
total int64 = 0
|
)
|
if err := db.Count(&total).Error; err != nil {
|
return err
|
}
|
if total != 0 {
|
return nil
|
}
|
records := []*InvoiceProduct{}
|
return slf.CreateBatch(records)
|
}
|