package model
|
|
import (
|
"apsClient/constvar"
|
"apsClient/pkg/sqlitex"
|
"errors"
|
"fmt"
|
"github.com/jinzhu/gorm"
|
"sync"
|
)
|
|
type (
|
// SystemStatus 系统状态
|
SystemStatus struct {
|
gorm.Model
|
Key constvar.SystemStatusKey `json:"key" gorm:"column:key;type:varchar(255);not null"`
|
Value constvar.SystemStatusValue `json:"value" gorm:"column:value;type:varchar(255);not null"`
|
}
|
|
// SystemStatusSearch 系统状态搜索条件
|
SystemStatusSearch struct {
|
SystemStatus
|
Orm *gorm.DB
|
Keyword string
|
PageNum int
|
PageSize int
|
}
|
)
|
|
func (SystemStatus) TableName() string {
|
return "system_status"
|
}
|
|
func NewSystemStatusSearch() *SystemStatusSearch {
|
return &SystemStatusSearch{
|
Orm: sqlitex.GetDB(),
|
}
|
}
|
|
func (slf *SystemStatusSearch) build() *gorm.DB {
|
var db = slf.Orm.Model(&SystemStatus{})
|
if slf.ID != 0 {
|
db = db.Where("id = ?", slf.ID)
|
}
|
if slf.Key != "" {
|
db = db.Where("key = ?", slf.Key)
|
}
|
|
return db
|
}
|
|
func (slf *SystemStatusSearch) Create(record *SystemStatus) error {
|
var db = slf.build()
|
return db.Create(record).Error
|
}
|
|
func (slf *SystemStatusSearch) CreateBatch(records []*SystemStatus) error {
|
var db = slf.build()
|
for _, record := range records {
|
db.Create(record)
|
}
|
return nil
|
}
|
|
func (slf *SystemStatusSearch) Delete() error {
|
var db = slf.build()
|
return db.Delete(&SystemStatus{}).Error
|
}
|
|
func (slf *SystemStatusSearch) Update(record *SystemStatus) error {
|
var db = slf.build()
|
return db.Updates(record).Error
|
}
|
|
func (slf *SystemStatusSearch) FindAll() ([]*SystemStatus, error) {
|
var db = slf.build()
|
var record = make([]*SystemStatus, 0)
|
err := db.Find(&record).Error
|
return record, err
|
}
|
|
func (slf *SystemStatusSearch) SetId(id uint) *SystemStatusSearch {
|
slf.ID = id
|
return slf
|
}
|
|
func (slf *SystemStatusSearch) SetKey(key constvar.SystemStatusKey) *SystemStatusSearch {
|
slf.Key = key
|
return slf
|
}
|
|
func (slf *SystemStatusSearch) SetPage(page, size int) *SystemStatusSearch {
|
slf.PageNum, slf.PageSize = page, size
|
return slf
|
}
|
|
func (slf *SystemStatusSearch) SetOrm(tx *gorm.DB) *SystemStatusSearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *SystemStatusSearch) First() (*SystemStatus, error) {
|
var db = slf.build()
|
var record = new(SystemStatus)
|
err := db.First(record).Error
|
return record, err
|
}
|
|
func (slf *SystemStatusSearch) Updates(values interface{}) error {
|
var db = slf.build()
|
return db.Updates(values).Error
|
}
|
|
func (slf *SystemStatusSearch) Save(record *SystemStatus) error {
|
if record.ID == 0 {
|
return errors.New("id为空")
|
}
|
var db = slf.build()
|
|
if err := db.Save(record).Error; err != nil {
|
return fmt.Errorf("save err: %v, record: %+v", err, record)
|
}
|
|
return nil
|
}
|
|
func (slf *SystemStatusSearch) Find() ([]*SystemStatus, int64, error) {
|
var db = slf.build()
|
var records = make([]*SystemStatus, 0)
|
var total int64
|
if err := db.Count(&total).Error; err != nil {
|
return records, total, err
|
}
|
if slf.PageNum > 0 && slf.PageSize > 0 {
|
db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
|
}
|
|
err := db.Find(&records).Error
|
return records, total, err
|
}
|
|
// InitDefaultData 初始化数据
|
func (slf *SystemStatusSearch) 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 := []*SystemStatus{
|
{
|
Key: constvar.SystemStatusKeyNsq,
|
Value: constvar.SystemStatusValueNormal,
|
},
|
}
|
err := slf.CreateBatch(records)
|
if err != nil {
|
errCh <- err
|
return
|
}
|
}
|