zhangqian
2023-11-09 3817b0e527eadc47d7fe32cd5d73278b3d867b58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
    }
}