package model import ( "aps_crm/pkg/mysqlx" "fmt" "gorm.io/gorm" ) type ( SystemSet struct { Id int `json:"id" gorm:"column:id;primaryKey;autoIncrement;not null"` Name string `json:"name" gorm:"type:varchar(255);comment:名称"` Value string `json:"value" gorm:"type:varchar(255);comment:值"` SystemType string `json:"systemType" gorm:"type:varchar(255);comment:系统类型"` } SystemSetSearch struct { SystemSet Types []string Orm *gorm.DB } ) func (slf SystemSet) TableName() string { return "system_set" } func NewSystemSetSearch() *SystemSetSearch { return &SystemSetSearch{Orm: mysqlx.GetDB()} } func (slf *SystemSetSearch) SetOrm(tx *gorm.DB) *SystemSetSearch { slf.Orm = tx return slf } func (slf *SystemSetSearch) SetTypes(types []string) *SystemSetSearch { slf.Types = types return slf } func (slf *SystemSetSearch) SetName(name string) *SystemSetSearch { slf.Name = name return slf } func (slf *SystemSetSearch) build() *gorm.DB { var db = slf.Orm.Table(slf.TableName()) if len(slf.Types) > 0 { db = db.Where("system_type in (?)", slf.Types) } if slf.Name != "" { db = db.Where("name = ?", slf.Name) } return db } // CreateBatch 批量插入 func (slf *SystemSetSearch) CreateBatch(records []*SystemSet) error { var db = slf.build() if err := db.Create(&records).Error; err != nil { return fmt.Errorf("create batch err: %v, records: %+v", err, records) } return nil } func (slf *SystemSetSearch) DeleteAll() (err error) { var db = slf.build() err = db.Delete(&SystemSet{}).Error return err } func (slf *SystemSetSearch) First() (*SystemSet, error) { var ( record = new(SystemSet) db = slf.build() ) if err := db.First(record).Error; err != nil { return record, err } return record, nil } func (slf *SystemSetSearch) FindAll() ([]*SystemSet, error) { var ( records = make([]*SystemSet, 0) db = slf.build() ) if err := db.Find(&records).Error; err != nil { return records, fmt.Errorf("find records err: %v", err) } return records, nil }