package model
|
|
import (
|
"aps_crm/constvar"
|
"aps_crm/pkg/mysqlx"
|
"fmt"
|
"gorm.io/gorm"
|
)
|
|
type (
|
// SalesDetailsProduct 销售明细和产品关联
|
SalesDetailsProduct struct {
|
SalesDetailsId int `json:"id" gorm:"column:sales_details_id;type:int;primary_key;not null;default:0"`
|
ProductId uint `json:"name" gorm:"primary_key;column:product_id;type:int;not null;default:0;comment:产品id"`
|
}
|
|
// SalesDetailsProductSearch 销售明细和产品关联搜索条件
|
SalesDetailsProductSearch struct {
|
SalesDetailsProduct
|
Orm *gorm.DB
|
QueryClass constvar.SalesDetailsProductQueryClass
|
KeywordType constvar.SalesDetailsProductKeywordType
|
Keyword string
|
PageNum int
|
PageSize int
|
ProductIds []uint
|
}
|
)
|
|
func (SalesDetailsProduct) TableName() string {
|
return "sales_details_product"
|
}
|
|
func NewSalesDetailsProductSearch() *SalesDetailsProductSearch {
|
return &SalesDetailsProductSearch{
|
Orm: mysqlx.GetDB(),
|
}
|
}
|
|
func (slf *SalesDetailsProductSearch) build() *gorm.DB {
|
var db = slf.Orm.Model(&SalesDetailsProduct{})
|
if len(slf.ProductIds) > 0 {
|
db = db.Where("product_id in (?)", slf.ProductIds)
|
}
|
|
return db
|
}
|
|
func (slf *SalesDetailsProductSearch) SetProductIds(ids []uint) *SalesDetailsProductSearch {
|
slf.ProductIds = ids
|
return slf
|
}
|
|
func (slf *SalesDetailsProductSearch) Create(record *SalesDetailsProduct) error {
|
var db = slf.build()
|
return db.Create(record).Error
|
}
|
|
func (slf *SalesDetailsProductSearch) CreateBatch(records []*SalesDetailsProduct) error {
|
var db = slf.build()
|
return db.Create(records).Error
|
}
|
|
func (slf *SalesDetailsProductSearch) Delete() error {
|
var db = slf.build()
|
return db.Delete(&SalesDetailsProduct{}).Error
|
}
|
|
func (slf *SalesDetailsProductSearch) Update(record *SalesDetailsProduct) error {
|
var db = slf.build()
|
return db.Updates(record).Error
|
}
|
|
func (slf *SalesDetailsProductSearch) FindAll() ([]*SalesDetailsProduct, error) {
|
var db = slf.build()
|
var record = make([]*SalesDetailsProduct, 0)
|
err := db.Find(&record).Error
|
return record, err
|
}
|
|
func (slf *SalesDetailsProductSearch) SetPage(page, size int) *SalesDetailsProductSearch {
|
slf.PageNum, slf.PageSize = page, size
|
return slf
|
}
|
|
func (slf *SalesDetailsProductSearch) SetOrm(tx *gorm.DB) *SalesDetailsProductSearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *SalesDetailsProductSearch) First() (*SalesDetailsProduct, error) {
|
var db = slf.build()
|
var record = new(SalesDetailsProduct)
|
err := db.First(record).Error
|
return record, err
|
}
|
|
func (slf *SalesDetailsProductSearch) Updates(values interface{}) error {
|
var db = slf.build()
|
return db.Updates(values).Error
|
}
|
|
func (slf *SalesDetailsProductSearch) Save(record *SalesDetailsProduct) error {
|
var db = slf.build()
|
|
if err := db.Save(record).Error; err != nil {
|
return fmt.Errorf("save err: %v, record: %+v", err, record)
|
}
|
|
return nil
|
}
|
|
func (slf *SalesDetailsProductSearch) Find() ([]*SalesDetailsProduct, int64, error) {
|
var db = slf.build()
|
var records = make([]*SalesDetailsProduct, 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 *SalesDetailsProductSearch) 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 := []*SalesDetailsProduct{}
|
return slf.CreateBatch(records)
|
}
|