package model import ( "aps_crm/pkg/mysqlx" "gorm.io/gorm" ) type ( // ReportSource 商机阶段 ReportSource struct { Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` Name string `json:"name" gorm:"column:name;type:varchar(255);comment:商机阶段名称"` } ReportSourceSearch struct { ReportSource Orm *gorm.DB } ) func (ReportSource) TableName() string { return "report_source" } func NewReportSourceSearch() *ReportSourceSearch { return &ReportSourceSearch{ Orm: mysqlx.GetDB(), } } func (slf *ReportSourceSearch) build() *gorm.DB { var db = slf.Orm.Model(&ReportSource{}) if slf.Id != 0 { db = db.Where("id = ?", slf.Id) } if slf.Name != "" { db = db.Where("name = ?", slf.Name) } return db } func (slf *ReportSourceSearch) Create(record *ReportSource) error { var db = slf.build() return db.Create(record).Error } func (slf *ReportSourceSearch) Delete() error { var db = slf.build() return db.Delete(&ReportSource{}).Error } func (slf *ReportSourceSearch) Update(record *ReportSource) error { var db = slf.build() return db.Updates(record).Error } func (slf *ReportSourceSearch) Find() (*ReportSource, error) { var db = slf.build() var record = new(ReportSource) err := db.First(record).Error return record, err } func (slf *ReportSourceSearch) FindAll() ([]*ReportSource, error) { var db = slf.build() var records = make([]*ReportSource, 0) err := db.Find(&records).Error return records, err } func (slf *ReportSourceSearch) SetId(id int) *ReportSourceSearch { slf.Id = id return slf } func (slf *ReportSourceSearch) SetName(name string) *ReportSourceSearch { slf.Name = name return slf } func (slf *ReportSourceSearch) Updates(data map[string]interface{}) error { var db = slf.build() return db.Updates(data).Error }