-
Notifications
You must be signed in to change notification settings - Fork 48
/
instances.go
310 lines (254 loc) · 8.67 KB
/
instances.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package serverService
import (
"os"
"path/filepath"
"strconv"
"strings"
"github.com/easysoft/zendata/internal/pkg/domain"
"github.com/easysoft/zendata/internal/pkg/service"
consts "github.com/easysoft/zendata/internal/pkg/const"
"github.com/easysoft/zendata/internal/pkg/helper"
"github.com/easysoft/zendata/internal/pkg/model"
serverRepo "github.com/easysoft/zendata/internal/server/repo"
serverUtils "github.com/easysoft/zendata/internal/server/utils"
fileUtils "github.com/easysoft/zendata/pkg/utils/file"
"github.com/easysoft/zendata/pkg/utils/vari"
"gopkg.in/yaml.v3"
"gorm.io/gorm"
)
type InstancesService struct {
InstancesRepo *serverRepo.InstancesRepo `inject:""`
ReferRepo *serverRepo.ReferRepo `inject:""`
ResService *ResService `inject:""`
SectionService *SectionService `inject:""`
SectionRepo *serverRepo.SectionRepo `inject:""`
RangeService *service.RangeService `inject:""`
}
func (s *InstancesService) List(keywords string, page int) (list []*model.ZdInstances, total int) {
list, total, _ = s.InstancesRepo.List(strings.TrimSpace(keywords), page)
return
}
func (s *InstancesService) Get(id int) (instances model.ZdInstances, dirs []domain.Dir) {
instances, _ = s.InstancesRepo.Get(uint(id))
serverUtils.GetDirs(consts.ResDirYaml, &dirs)
return
}
func (s *InstancesService) Save(instances *model.ZdInstances) (err error) {
instances.Folder = serverUtils.DealWithPathSepRight(instances.Folder)
instances.Path = vari.WorkDir + instances.Folder + serverUtils.AddExt(instances.FileName, ".yaml")
instances.ReferName = helper.PathToName(instances.Path, consts.ResDirYaml, consts.ResTypeInstances)
if instances.ID == 0 {
err = s.Create(instances)
} else {
err = s.Update(instances)
}
s.updateYaml(instances.ID)
return
}
func (s *InstancesService) Create(instances *model.ZdInstances) (err error) {
err = s.InstancesRepo.Create(instances)
return
}
func (s *InstancesService) Update(instances *model.ZdInstances) (err error) {
var old model.ZdInstances
old, err = s.InstancesRepo.Get(instances.ID)
if err == gorm.ErrRecordNotFound {
return
}
if instances.Path != old.Path {
fileUtils.RemoveExist(old.Path)
}
err = s.InstancesRepo.Update(instances)
return
}
func (s *InstancesService) Remove(id int) (err error) {
var old model.ZdInstances
old, err = s.InstancesRepo.Get(uint(id))
if err == gorm.ErrRecordNotFound {
return
}
fileUtils.RemoveExist(old.Path)
err = s.InstancesRepo.Remove(uint(id))
return
}
func (s *InstancesService) updateYamlByItem(itemId uint) (err error) {
item, _ := s.InstancesRepo.GetItem(itemId)
return s.updateYaml(item.InstancesID)
}
func (s *InstancesService) updateYaml(id uint) (err error) {
var po model.ZdInstances
po, _ = s.InstancesRepo.Get(id)
s.genYaml(&po)
err = s.InstancesRepo.UpdateYaml(po)
fileUtils.WriteFile(po.Path, po.Yaml)
return
}
func (s *InstancesService) genYaml(instances *model.ZdInstances) (str string) {
//records, err := s.InstancesRepo.GetItems(instances.ID)
//if err != nil {
// return
//}
//
//yamlObj := model.ResInstances{}
//s.InstancesRepo.GenInst(*instances, &yamlObj)
//
//for _, item := range records {
// inst := instancesItemToResInstForExport(*item)
//
// yamlObj.Instances = append(yamlObj.Instances, inst)
//}
root := s.GetItemTree(instances.ID)
for _, item := range root.Fields {
instances.Instances = append(instances.Instances, *item)
}
bytes, _ := yaml.Marshal(instances)
instances.Yaml = helper.ConvertYamlStringToMapFormat(bytes)
return
}
func (s *InstancesService) Sync(files []domain.ResFile) {
list := s.InstancesRepo.ListAll()
mp := map[string]*model.ZdInstances{}
for _, item := range list {
mp[item.Path] = item
}
for _, fi := range files {
_, found := mp[fi.Path]
if !found { // no record
s.SyncToDB(fi)
} else if fi.UpdatedAt.Unix() > mp[fi.Path].UpdatedAt.Unix() { // db is old
s.InstancesRepo.Remove(mp[fi.Path].ID)
s.SyncToDB(fi)
}
}
return
}
func (s *InstancesService) SyncToDB(fi domain.ResFile) (err error) {
content, _ := os.ReadFile(fi.Path)
yamlContent := helper.ReplaceSpecialChars(content)
po := model.ZdInstances{}
err = yaml.Unmarshal(yamlContent, &po)
po.Title = fi.Title
po.Desc = fi.Desc
po.Path = fi.Path
po.Folder = serverUtils.GetRelativePath(po.Path)
if strings.Index(po.Path, consts.ResDirYaml) > -1 {
po.ReferName = helper.PathToName(po.Path, consts.ResDirYaml, consts.ResTypeInstances)
} else {
po.ReferName = helper.PathToName(po.Path, consts.ResDirUsers, consts.ResTypeInstances)
}
po.FileName = fileUtils.GetFileName(po.Path)
po.Yaml = string(content)
s.InstancesRepo.Create(&po)
for i, item := range po.Instances {
item.Ord = i + 1
s.saveItemToDB(&item, po, fi.Path, 0, po.ID)
}
return
}
func (s *InstancesService) saveItemToDB(item *model.ZdInstancesItem, instances model.ZdInstances, currPath string, parentID, instancesID uint) {
if item.Froms != nil && len(item.Froms) > 0 {
for idx, from := range item.Froms {
if from.Field == "" {
from.Field = "from" + strconv.Itoa(idx+1)
}
s.saveItemToDB(from, instances, currPath, parentID, instancesID)
}
return
}
// update field
if item.Instance != "" { // instance node
item.Field = item.Instance
}
item.InstancesID = instancesID
item.ParentID = parentID
if item.From == "" && instances.From != "" {
item.From = instances.From
}
if item.Type == "" {
item.Type = consts.FieldTypeList
}
if item.Mode == "" {
item.Mode = consts.ModeParallel
}
item.Range = strings.TrimSpace(item.Range)
// save item
s.InstancesRepo.SaveItem(item)
// create refer
refer := model.ZdRefer{OwnerType: "instances", OwnerID: item.ID}
needToCreateSections := false
if item.Select != "" { // refer to excel
refer.Type = consts.ResTypeExcel
refer.ColName = item.Select
refer.Condition = item.Where
refer.Rand = item.Rand
_, sheet := fileUtils.ConvertResExcelPath(item.From, currPath)
refer.File = item.From
refer.Sheet = sheet
} else if item.Use != "" { // refer to ranges or instances, need to read yaml to get the type
rangeSections := s.RangeService.ParseRangeProperty(item.Use)
if len(rangeSections) > 0 { // only get the first one
rangeSection := rangeSections[0]
desc, _, count, countTag := s.RangeService.ParseRangeSection(rangeSection) // medium{2}
refer.ColName = desc
refer.Count = count
refer.CountTag = countTag
}
path := fileUtils.ConvertReferRangeToPath(item.From, currPath)
_, _, refer.Type = helper.ReadYamlInfo(path)
refer.File = item.From
} else if item.Config != "" { // refer to config
refer.Type = consts.ResTypeConfig
rangeSections := s.RangeService.ParseRangeProperty(item.Config) // dir/config.yaml
if len(rangeSections) > 0 { // only get the first one
rangeSection := rangeSections[0]
desc, _, count, countTag := s.RangeService.ParseRangeSection(rangeSection)
refer.Count = count
refer.CountTag = countTag
path := fileUtils.ConvertReferRangeToPath(desc, currPath)
refer.File = GetRelatedPathWithResDir(path)
}
} else if item.Range != "" { // deal with yaml and text refer using range prop
rangeSections := s.RangeService.ParseRangeProperty(item.Range)
if len(rangeSections) > 0 { // only get the first one
rangeSection := rangeSections[0]
desc, step, count, countTag := s.RangeService.ParseRangeSection(rangeSection) // dir/users.txt:R{3}
if filepath.Ext(desc) == ".txt" || filepath.Ext(desc) == ".yaml" {
if filepath.Ext(desc) == ".txt" { // dir/users.txt:2
refer.Type = consts.ResTypeText
if strings.ToLower(step) == "r" {
refer.Rand = true
} else {
refer.Step, _ = strconv.Atoi(step)
}
} else if filepath.Ext(desc) == ".yaml" { // dir/content.yaml{3}
refer.Type = consts.ResTypeYaml
refer.Count = count
refer.CountTag = countTag
}
path := fileUtils.ConvertReferRangeToPath(desc, currPath)
refer.File = GetRelatedPathWithResDir(path)
} else { // like 1-9,a-z
needToCreateSections = true
}
}
}
// save refer
refer.OwnerID = item.ID
s.ReferRepo.Save(&refer)
// gen sections if needed
if needToCreateSections {
rangeSections := s.RangeService.ParseRangeProperty(item.Range)
for i, rangeSection := range rangeSections {
s.SectionService.SaveFieldSectionToDB(rangeSection, i, item.ID, "instances")
}
}
// deal with field's children
for i, child := range item.Fields {
child.Ord = i + 1
s.saveItemToDB(child, instances, currPath, item.ID, instancesID)
}
}
func NewInstancesService(instancesRepo *serverRepo.InstancesRepo, referRepo *serverRepo.ReferRepo,
sectionRepo *serverRepo.SectionRepo) *InstancesService {
return &InstancesService{InstancesRepo: instancesRepo, ReferRepo: referRepo, SectionRepo: sectionRepo}
}