package model import ( "aps_crm/pkg/mysqlx" "gorm.io/gorm" ) type ( ContactInformation struct { Id int `json:"id" gorm:"column:id;primaryKey;autoIncrement;not null"` Name string `json:"name" gorm:"column:name;type:varchar(255);comment:名称"` } ContactInformationSearch struct { ContactInformation Orm *gorm.DB } ) func (ContactInformation) TableName() string { return "contact_information" } func NewContactInformationSearch() *ContactInformationSearch { return &ContactInformationSearch{ Orm: mysqlx.GetDB(), } } func (slf *ContactInformationSearch) build() *gorm.DB { var db = slf.Orm.Model(&ContactInformation{}) if slf.Id != 0 { db = db.Where("id = ?", slf.Id) } if slf.Name != "" { db = db.Where("name = ?", slf.Name) } return db } // Create 创建 func (slf *ContactInformationSearch) Create(record []*ContactInformation) error { var db = slf.build() return db.Create(record).Error } func (slf *ContactInformationSearch) Delete() error { var db = slf.build() return db.Delete(&ContactInformation{}).Error } func (slf *ContactInformationSearch) Update(record *ContactInformation) error { var db = slf.build() return db.Updates(record).Error } func (slf *ContactInformationSearch) Find() ([]*ContactInformation, error) { var db = slf.build() var result []*ContactInformation err := db.Find(&result).Error return result, err } func (slf *ContactInformationSearch) FindOne() (*ContactInformation, error) { var db = slf.build() var result ContactInformation err := db.First(&result).Error return &result, err } func (slf *ContactInformationSearch) SetId(id int) *ContactInformationSearch { slf.Id = id return slf } func (slf *ContactInformationSearch) SetName(name string) *ContactInformationSearch { slf.Name = name return slf }