-
Notifications
You must be signed in to change notification settings - Fork 48
/
def.go
361 lines (289 loc) · 9.45 KB
/
def.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package serverService
import (
"fmt"
"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 DefService struct {
DefRepo *serverRepo.DefRepo `inject:""`
FieldRepo *serverRepo.FieldRepo `inject:""`
ReferRepo *serverRepo.ReferRepo `inject:""`
SectionRepo *serverRepo.SectionRepo `inject:""`
MockRepo *serverRepo.MockRepo `inject:""`
ResService *ResService `inject:""`
SectionService *SectionService `inject:""`
RangeService *service.RangeService `inject:""`
}
func (s *DefService) List(keywords string, page int) (list []*model.ZdDef, total int) {
list, total, _ = s.DefRepo.List(strings.TrimSpace(keywords), page)
return
}
func (s *DefService) Get(id int) (def model.ZdDef, dirs []domain.Dir) {
if id > 0 {
def, _ = s.DefRepo.Get(uint(id))
} else {
def.Folder = "users" + consts.PthSep
def.Type = "text"
}
serverUtils.GetDirs(consts.ResDirUsers, &dirs)
return
}
func (s *DefService) Save(def *model.ZdDef) (err error) {
def.Folder = serverUtils.DealWithPathSepRight(def.Folder)
def.Path = vari.WorkDir + def.Folder +
serverUtils.AddExt(strings.TrimSuffix(def.FileName, ".yaml"), ".yaml")
if def.ID == 0 {
err = s.Create(def)
} else {
err = s.Update(def)
}
s.updateYaml(def.ID)
return
}
func (s *DefService) Create(def *model.ZdDef) (err error) {
def.ReferName = helper.PathToName(def.Path, consts.ResDirUsers, def.Type)
err = s.DefRepo.Create(def)
// add root field node
rootField, err := s.FieldRepo.CreateTreeNode(def.ID, 0, "字段", "root")
s.ReferRepo.CreateDefault(rootField.ID, consts.ResTypeDef)
err = s.DefRepo.Update(def)
return
}
func (s *DefService) Update(def *model.ZdDef) (err error) {
var old model.ZdDef
old, err = s.DefRepo.Get(def.ID)
if err == gorm.ErrRecordNotFound {
return
}
if def.Path != old.Path {
fileUtils.RemoveExist(old.Path)
}
def.ReferName = helper.PathToName(def.Path, consts.ResDirUsers, def.Type)
err = s.DefRepo.Update(def)
return
}
func (s *DefService) Remove(id int) (err error) {
var old model.ZdDef
old, err = s.DefRepo.Get(uint(id))
if err == gorm.ErrRecordNotFound {
return
}
fileUtils.RemoveExist(old.Path)
err = s.DefRepo.Remove(uint(id))
return
}
func (s *DefService) updateYamlByField(fieldId uint) (err error) {
field, _ := s.FieldRepo.Get(fieldId)
return s.updateYaml(field.DefID)
}
func (s *DefService) updateYaml(id uint) (err error) {
var po model.ZdDef
po, _ = s.DefRepo.Get(id)
s.genYaml(&po)
err = s.DefRepo.UpdateYaml(po)
//update mock content
mockData := s.MockRepo.GetByDefID(id)
if mockData.ID > 0 {
mockData.DataContent = po.Yaml
s.MockRepo.Save(&mockData)
}
fileUtils.WriteFile(po.Path, po.Yaml)
return
}
func (s *DefService) genYaml(def *model.ZdDef) (str string) {
root, err := s.FieldRepo.GetDefFieldTree(def.ID)
if err != nil {
return
}
yamlObj := domain.DefData{}
s.DefRepo.GenDef(*def, &yamlObj)
for _, child := range root.Fields { // ignore the root
defField := domain.DefField{}
refer, _ := s.ReferRepo.GetByOwnerIdAndType(child.ID, consts.ResTypeDef)
s.zdFieldToFieldForExport(*child, refer, &defField)
yamlObj.Fields = append(yamlObj.Fields, defField)
}
bytes, err := yaml.Marshal(yamlObj)
def.Yaml = helper.ConvertYamlStringToMapFormat(bytes)
return
}
func (s *DefService) zdFieldToFieldForExport(treeNode model.ZdField, refer model.ZdRefer, field *domain.DefField) {
genFieldFromZdField(treeNode, refer, field)
for _, child := range treeNode.Fields {
childField := domain.DefField{}
childRefer, _ := s.ReferRepo.GetByOwnerId(child.ID)
s.zdFieldToFieldForExport(*child, childRefer, &childField)
field.Fields = append(field.Fields, childField)
}
//for _, from := range treeNode.Froms { // only one level
// childField := model.DefField{}
// genFieldFromZdField(*from, &childField)
//
// field.Froms = append(field.Froms, childField)
//}
if len(field.Fields) == 0 {
field.Fields = nil
}
if len(field.Froms) == 0 {
field.Froms = nil
}
return
}
func (s *DefService) Sync(files []domain.ResFile) (err error) {
list := s.DefRepo.ListAll()
mp := map[string]*model.ZdDef{}
for _, item := range list {
mp[item.Path] = item
}
for _, fi := range files {
// for yaml "res", "data" type should be default value text
if fi.ResType == "" || fi.ResType == consts.ResTypeYaml {
fi.ResType = consts.ResTypeText
}
_, found := mp[fi.Path]
if !found { // no record
s.SyncToDB(fi, false)
} else if fi.UpdatedAt.Unix() > mp[fi.Path].UpdatedAt.Unix() { // db is old
s.DefRepo.Remove(mp[fi.Path].ID)
s.SyncToDB(fi, false)
}
}
return
}
func (s *DefService) SyncToDB(fi domain.ResFile, isMock bool) (err error, id uint) {
content, _ := os.ReadFile(fi.Path)
yamlContent := helper.ReplaceSpecialChars(content)
po := model.ZdDef{}
err = yaml.Unmarshal(yamlContent, &po)
po.Title = fi.Title
po.Type = fi.ResType
po.Desc = fi.Desc
po.Path = fi.Path
po.IsMock = isMock
po.Folder = serverUtils.GetRelativePath(po.Path)
po.ReferName = helper.PathToName(po.Path, consts.ResDirUsers, po.Type)
po.FileName = fileUtils.GetFileName(po.Path)
po.Yaml = string(content)
s.DefRepo.Create(&po)
id = po.ID
rootField, _ := s.FieldRepo.CreateTreeNode(po.ID, 0, "字段", "root")
s.ReferRepo.CreateDefault(rootField.ID, consts.ResTypeDef)
fmt.Println(rootField.ID, po.Type, po, rootField)
for i, field := range po.Fields {
field.Ord = i + 1
s.saveFieldToDB(&field, po, fi.Path, rootField.ID, po.ID)
}
return
}
func (s *DefService) saveFieldToDB(field *model.ZdField, def model.ZdDef, currPath string, parentID, defID uint) {
if field.Froms != nil && len(field.Froms) > 0 {
for idx, from := range field.Froms {
if from.Field == "" {
from.Field = "from" + strconv.Itoa(idx+1)
}
s.saveFieldToDB(from, def, currPath, parentID, defID)
}
return
}
// update field
field.DefID = defID
field.ParentID = parentID
if field.From == "" && def.From != "" {
field.From = def.From
}
if field.Type == "" {
field.Type = consts.FieldTypeList
}
if field.Mode == "" {
field.Mode = consts.ModeParallel
}
field.Range = strings.TrimSpace(field.Range)
// save field
s.FieldRepo.Save(field)
// create refer
refer := model.ZdRefer{OwnerType: "def", OwnerID: field.ID}
needToCreateSections := false
if field.Select != "" { // refer to excel
refer.Type = consts.ResTypeExcel
refer.ColName = field.Select
refer.Condition = field.Where
refer.Rand = field.Rand
_, sheet := fileUtils.ConvertResExcelPath(field.From, currPath)
refer.File = field.From
refer.Sheet = sheet
} else if field.Use != "" { // refer to ranges or instances, need to read yaml to get the type
rangeSections := s.RangeService.ParseRangeProperty(field.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(field.From, currPath)
_, _, refer.Type = helper.ReadYamlInfo(path)
refer.File = field.From
} else if field.Config != "" { // refer to config
refer.Type = consts.ResTypeConfig
rangeSections := s.RangeService.ParseRangeProperty(field.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 field.Range != "" {
rangeSections := s.RangeService.ParseRangeProperty(field.Range)
if len(rangeSections) > 0 {
rangeSection := rangeSections[0] // deal with yaml and text refer using range prop
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 = field.ID
s.ReferRepo.Save(&refer)
// gen sections if needed
if needToCreateSections {
rangeSections := s.RangeService.ParseRangeProperty(field.Range)
for i, rangeSection := range rangeSections {
s.SectionService.SaveFieldSectionToDB(rangeSection, i, field.ID, "def")
}
}
// deal with field's children
for _, child := range field.Fields {
s.saveFieldToDB(child, def, currPath, field.ID, defID)
}
}