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