package model import ( "aps_crm/pkg/mysqlx" "gorm.io/gorm" "sync" ) type ( // SalesSources 商机来源 SalesSources struct { Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` Name string `json:"name" gorm:"column:name;type:varchar(255);comment:商机来源名称"` } // SalesSourcesSearch 商机来源搜索条件 SalesSourcesSearch struct { SalesSources Orm *gorm.DB } ) func (SalesSources) TableName() string { return "sales_sources" } func NewSalesSourcesSearch() *SalesSourcesSearch { return &SalesSourcesSearch{ Orm: mysqlx.GetDB(), } } func (slf *SalesSourcesSearch) build() *gorm.DB { var db = slf.Orm.Model(&SalesSources{}) if slf.Id != 0 { db = db.Where("id = ?", slf.Id) } if slf.Name != "" { db = db.Where("name = ?", slf.Name) } return db } func (slf *SalesSourcesSearch) Create(record *SalesSources) error { var db = slf.build() return db.Create(record).Error } func (slf *SalesSourcesSearch) Delete() error { var db = slf.build() return db.Delete(&SalesSources{}).Error } func (slf *SalesSourcesSearch) Update(record *SalesSources) error { var db = slf.build() return db.Updates(record).Error } func (slf *SalesSourcesSearch) Find() (*SalesSources, error) { var db = slf.build() var record = &SalesSources{} err := db.First(record).Error return record, err } func (slf *SalesSourcesSearch) FindAll() ([]*SalesSources, error) { var db = slf.build() var record = make([]*SalesSources, 0) err := db.Find(&record).Error return record, err } func (slf *SalesSourcesSearch) SetId(id int) *SalesSourcesSearch { slf.Id = id return slf } func (slf *SalesSourcesSearch) SetName(name string) *SalesSourcesSearch { slf.Name = name return slf } func (slf *SalesSourcesSearch) Updates(data map[string]interface{}) error { var db = slf.build() return db.Updates(data).Error } func (slf *SalesSourcesSearch) CreateBatch(records []*SalesSources) error { var db = slf.build() return db.Create(records).Error } // InitDefaultData 初始化数据 func (slf *SalesSourcesSearch) InitDefaultData(errCh chan<- error, wg *sync.WaitGroup) { var ( db = slf.Orm.Table(slf.TableName()) total int64 = 0 ) defer wg.Done() if err := db.Count(&total).Error; err != nil { errCh <- err return } if total != 0 { return } records := []*SalesSources{ {1, "电话来访"}, {2, "公司分配"}, {3, "客户介绍"}, {4, "独立开发"}, } err := slf.CreateBatch(records) if err != nil { errCh <- err return } }