| | |
| | | "errors" |
| | | "fmt" |
| | | "gorm.io/gorm" |
| | | "sync" |
| | | ) |
| | | |
| | | type ( |
| | | // Severity 严重程度 |
| | | Severity struct { |
| | | Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` |
| | | Name string `json:"name" gorm:"column:name"` |
| | | Name string `json:"name" gorm:"column:name"` |
| | | } |
| | | |
| | | // SeveritySearch 严重程度搜索条件 |
| | | SeveritySearch struct { |
| | | Severity |
| | | Orm *gorm.DB |
| | | QueryClass constvar.SeverityQueryClass |
| | | KeywordType constvar.SeverityKeywordType |
| | | Keyword string |
| | | PageNum int |
| | | PageSize int |
| | | Orm *gorm.DB |
| | | QueryClass constvar.SeverityQueryClass |
| | | KeywordType constvar.SeverityKeywordType |
| | | Keyword string |
| | | PageNum int |
| | | PageSize int |
| | | } |
| | | ) |
| | | |
| | | func (Severity) TableName() string { |
| | | return "severity" |
| | | return "severity_order" |
| | | } |
| | | |
| | | func NewSeveritySearch() *SeveritySearch { |
| | |
| | | func (slf *SeveritySearch) Create(record *Severity) error { |
| | | var db = slf.build() |
| | | return db.Create(record).Error |
| | | } |
| | | |
| | | func (slf *SeveritySearch) CreateBatch(records []*Severity) error { |
| | | var db = slf.build() |
| | | return db.Create(records).Error |
| | | } |
| | | |
| | | func (slf *SeveritySearch) Delete() error { |
| | |
| | | |
| | | err := db.Find(&records).Error |
| | | return records, total, err |
| | | } |
| | | } |
| | | |
| | | // InitDefaultData 初始化数据 |
| | | func (slf *SeveritySearch) 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 := []*Severity{ |
| | | {1, "轻"}, |
| | | {2, "严重"}, |
| | | {3, "较严重"}, |
| | | {4, "特严重"}, |
| | | } |
| | | err := slf.CreateBatch(records) |
| | | if err != nil { |
| | | errCh <- err |
| | | return |
| | | } |
| | | } |