package model import ( "aps_crm/pkg/mysqlx" "gorm.io/gorm" ) type ( CollectionProjection struct { Id int `json:"id" gorm:"column:id;primaryKey;autoIncrement;not null"` SaleChanceId int `json:"sale_chance_id" gorm:"column:sale_chance_id;type:int(11);comment:销售机会id"` Creator int `json:"creator" gorm:"column:creator;type:int(11);comment:创建人"` Modifier int `json:"modifier" gorm:"column:modifier;type:int(11);comment:修改人"` EstimatedCollectionDate *string `json:"estimated_collection_date" gorm:"column:estimated_collection_date;type:datetime;comment:预计收款日期"` EstimatedCollectionAmount float64 `json:"estimated_collection_amount" gorm:"column:estimated_collection_amount;type:decimal(10,2);comment:预计收款金额"` gormModel } CollectionProjectionSearch struct { CollectionProjection Orm *gorm.DB SearchMap map[string]interface{} OrderBy string PageNum int PageSize int } ) func (CollectionProjection) TableName() string { return "collection_projection" } func NewCollectionProjectionSearch() *CollectionProjectionSearch { return &CollectionProjectionSearch{ Orm: mysqlx.GetDB(), } } func (slf *CollectionProjectionSearch) build() *gorm.DB { var db = slf.Orm.Model(&CollectionProjection{}) if slf.Id != 0 { db = db.Where("id = ?", slf.Id) } if len(slf.SearchMap) > 0 { for key, value := range slf.SearchMap { switch v := value.(type) { case string: if key == "estimated_collection_date" { db = db.Where(key+" = ?", v) } case int: if key == "client_id" || key == "sale_chance_id" { db = db.Where(key+" = ?", v) } } } } return db } func (slf *CollectionProjectionSearch) Create(record *CollectionProjection) (err error) { var db = slf.build() err = db.Create(record).Error return } func (slf *CollectionProjectionSearch) Update(record *CollectionProjection) (err error) { var db = slf.build() err = db.Updates(record).Error return } func (slf *CollectionProjectionSearch) Delete() (err error) { var db = slf.build() err = db.Delete(&CollectionProjection{}).Error return } func (slf *CollectionProjectionSearch) Find() (int64, error, []*CollectionProjection) { var db = slf.build() var records = make([]*CollectionProjection, 0) var total int64 if err := db.Count(&total).Error; err != nil { return total, err, records } if slf.PageNum > 0 && slf.PageSize > 0 { db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize) } err := db.Find(&records).Error return total, err, records } func (slf *CollectionProjectionSearch) SetID(id int) *CollectionProjectionSearch { slf.Id = id return slf } func (slf *CollectionProjectionSearch) SetSaleChanceId(saleChanceId int) *CollectionProjectionSearch { slf.SaleChanceId = saleChanceId return slf } func (slf *CollectionProjectionSearch) SetPage(page, size int) *CollectionProjectionSearch { slf.PageNum, slf.PageSize = page, size return slf } func (slf *CollectionProjectionSearch) SetSearchMap(data map[string]interface{}) *CollectionProjectionSearch { slf.SearchMap = data return slf }