github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/server/service/section.go (about) 1 package serverService 2 3 import ( 4 "github.com/easysoft/zendata/internal/pkg/model" 5 "github.com/easysoft/zendata/internal/pkg/service" 6 serverRepo "github.com/easysoft/zendata/internal/server/repo" 7 ) 8 9 type SectionService struct { 10 FieldRepo *serverRepo.FieldRepo `inject:""` 11 ConfigRepo *serverRepo.ConfigRepo `inject:""` 12 RangesRepo *serverRepo.RangesRepo `inject:""` 13 InstancesRepo *serverRepo.InstancesRepo `inject:""` 14 15 SectionRepo *serverRepo.SectionRepo `inject:""` 16 DefService *DefService `inject:""` 17 ConfigService *ConfigService `inject:""` 18 InstancesService *InstancesService `inject:""` 19 RangesService *RangesService `inject:""` 20 21 RangesService2 *service.RangeService `inject:""` 22 } 23 24 func (s *SectionService) List(ownerId uint, ownerType string) (sections []*model.ZdSection, err error) { 25 sections, err = s.SectionRepo.List(ownerId, ownerType) 26 return 27 } 28 29 func (s *SectionService) Create(ownerId, sectionsId uint, ownerType string) (err error) { 30 preSection, err := s.SectionRepo.Get(sectionsId, ownerType) 31 32 section := model.ZdSection{ 33 Value: "0-9", 34 OwnerID: ownerId, 35 OwnerType: ownerType, 36 Ord: preSection.Ord + 1, 37 Start: "0", 38 End: "9", 39 } 40 41 err = s.SectionRepo.Create(§ion) 42 43 if ownerType == "def" { 44 s.FieldRepo.SetIsRange(section.OwnerID, true) 45 } else if ownerType == "instances" { 46 s.InstancesRepo.SetIsRange(section.OwnerID, true) 47 } 48 49 return 50 } 51 52 func (s *SectionService) Update(section *model.ZdSection) (err error) { 53 err = s.SectionRepo.Update(section) 54 55 ownerId := section.OwnerID 56 ownerType := section.OwnerType 57 58 s.updateFieldRangeProp(ownerId, ownerType) 59 if ownerType == "def" { 60 s.FieldRepo.SetIsRange(section.OwnerID, true) 61 s.DefService.updateYamlByField(section.OwnerID) 62 63 } else if ownerType == "config" { 64 s.ConfigService.updateYaml(section.OwnerID) 65 } else if ownerType == "ranges" { 66 s.RangesService.updateYamlByItem(section.OwnerID) 67 68 } else if ownerType == "instances" { 69 s.InstancesRepo.SetIsRange(section.OwnerID, true) 70 s.InstancesService.updateYamlByItem(section.OwnerID) 71 } 72 73 return 74 } 75 76 func (s *SectionService) Remove(sectionId int, ownerType string) (ownerId uint, err error) { 77 section, err := s.SectionRepo.Get(uint(sectionId), ownerType) 78 ownerId = section.OwnerID 79 80 err = s.SectionRepo.Remove(uint(sectionId), ownerType) 81 82 s.updateFieldRangeProp(ownerId, ownerType) 83 if ownerType == "def" { 84 s.FieldRepo.SetIsRange(section.OwnerID, true) 85 s.DefService.updateYamlByField(section.OwnerID) 86 } else if ownerType == "config" { 87 s.ConfigService.updateYaml(section.OwnerID) 88 } else if ownerType == "ranges" { 89 s.RangesService.updateYamlByItem(section.OwnerID) 90 91 } else if ownerType == "instances" { 92 s.InstancesRepo.SetIsRange(section.OwnerID, true) 93 s.InstancesService.updateYamlByItem(section.OwnerID) 94 } 95 96 return 97 } 98 99 func (s *SectionService) updateFieldRangeProp(ownerId uint, ownerType string) (err error) { 100 rangeStr := "" 101 102 sections, _ := s.SectionRepo.List(ownerId, ownerType) 103 for index, sect := range sections { 104 if index > 0 { 105 rangeStr += "," 106 } 107 rangeStr += sect.Value 108 } 109 110 if ownerType == "def" { 111 s.FieldRepo.UpdateRange(rangeStr, ownerId) 112 } else if ownerType == "config" { 113 s.ConfigRepo.UpdateConfigRange(rangeStr, ownerId) 114 } else if ownerType == "ranges" { 115 s.RangesRepo.UpdateItemRange(rangeStr, ownerId) 116 } else if ownerType == "instances" { 117 s.InstancesRepo.UpdateItemRange(rangeStr, ownerId) 118 } 119 120 return 121 } 122 123 func (s *SectionService) SaveFieldSectionToDB(rangeSection string, ord int, fieldID uint, ownerType string) { 124 descStr, stepStr, count, countTag := s.RangesService2.ParseRangeSection(rangeSection) 125 typ, desc := s.RangesService2.ParseRangeSectionDesc(descStr) 126 127 s.SectionRepo.SaveFieldSectionToDB(typ, desc, stepStr, count, countTag, 128 ord, fieldID, ownerType) 129 }