作者 yangfu

chore: add DataFieldsBuilder

@@ -17,7 +17,7 @@ type QuerySetDetailDto struct { @@ -17,7 +17,7 @@ type QuerySetDetailDto struct {
17 TableId int `json:"tableId"` 17 TableId int `json:"tableId"`
18 } 18 }
19 19
20 -func (d *QuerySetDetailDto) Load(m *domain.QuerySet) *QuerySetDetailDto { 20 +func (d *QuerySetDetailDto) Load(m *domain.QuerySet, mapTables map[int]*domain.Table) *QuerySetDetailDto {
21 d.QuerySetId = m.QuerySetId 21 d.QuerySetId = m.QuerySetId
22 d.Type = m.Type 22 d.Type = m.Type
23 d.Flag = m.Flag 23 d.Flag = m.Flag
@@ -26,7 +26,12 @@ func (d *QuerySetDetailDto) Load(m *domain.QuerySet) *QuerySetDetailDto { @@ -26,7 +26,12 @@ func (d *QuerySetDetailDto) Load(m *domain.QuerySet) *QuerySetDetailDto {
26 if m.QuerySetInfo != nil { 26 if m.QuerySetInfo != nil {
27 d.TableId = m.QuerySetInfo.BindTableId 27 d.TableId = m.QuerySetInfo.BindTableId
28 } 28 }
29 - for i := range d.QueryComponents { 29 + for i, q := range d.QueryComponents {
  30 + if q.MasterTable != nil && q.MasterTable.TableId != 0 {
  31 + if t, ok := mapTables[q.MasterTable.TableId]; ok {
  32 + d.QueryComponents[i].MasterTable = domain.NewQueryComponentTable(t)
  33 + }
  34 + }
30 if d.QueryComponents[i].Aggregation != nil { 35 if d.QueryComponents[i].Aggregation != nil {
31 d.QueryComponents[i].Aggregation.Aggregation.AllFields = d.QueryComponents[i].Aggregation.AggregationFields() 36 d.QueryComponents[i].Aggregation.Aggregation.AllFields = d.QueryComponents[i].Aggregation.AggregationFields()
32 } 37 }
@@ -159,7 +159,17 @@ func (querySetService *QuerySetService) GetQuerySet(ctx *domain.Context, getQuer @@ -159,7 +159,17 @@ func (querySetService *QuerySetService) GetQuerySet(ctx *domain.Context, getQuer
159 if err != nil { 159 if err != nil {
160 return nil, factory.FastError(err) 160 return nil, factory.FastError(err)
161 } 161 }
162 - return (&dto.QuerySetDetailDto{}).Load(querySet), nil 162 +
  163 + tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
  164 + dependencyTables := querySet.GetDependencyTables(querySet.QueryComponents)
  165 + var tables domain.Tables
  166 + if len(dependencyTables) > 0 {
  167 + _, tables, err = tableRepository.Find(map[string]interface{}{"context": ctx, "tableIds": dependencyTables})
  168 + if err != nil {
  169 + return nil, factory.FastError(err)
  170 + }
  171 + }
  172 + return (&dto.QuerySetDetailDto{}).Load(querySet, tables.ToMap()), nil
163 } 173 }
164 174
165 // 返回查询集合服务列表 175 // 返回查询集合服务列表
@@ -163,3 +163,13 @@ func TableTypesToStringList(list ...TableType) []string { @@ -163,3 +163,13 @@ func TableTypesToStringList(list ...TableType) []string {
163 } 163 }
164 return result 164 return result
165 } 165 }
  166 +
  167 +type Tables []*Table
  168 +
  169 +func (tables Tables) ToMap() map[int]*Table {
  170 + var result = make(map[int]*Table)
  171 + for i := range tables {
  172 + result[tables[i].TableId] = tables[i]
  173 + }
  174 + return result
  175 +}
@@ -139,9 +139,11 @@ func NewTable(tableType domain.TableType, fileName string, dataFields []*domain. @@ -139,9 +139,11 @@ func NewTable(tableType domain.TableType, fileName string, dataFields []*domain.
139 if table.TableType == domain.CalculateTable.ToString() || table.TableType == domain.CalculateItem.ToString() { 139 if table.TableType == domain.CalculateTable.ToString() || table.TableType == domain.CalculateItem.ToString() {
140 table.PK = nil 140 table.PK = nil
141 } 141 }
  142 + builder := NewDataFieldsBuilder()
142 table.DataFieldIndex = len(dataFields) 143 table.DataFieldIndex = len(dataFields)
143 - for i, field := range dataFields {  
144 - table.DataFields = append(table.DataFields, DataField(field.Name, field.SQLType, domain.MainTableField, i+1)) 144 + for _, field := range dataFields {
  145 + //table.DataFields = append(table.DataFields, DataField(field.Name, field.SQLType, domain.MainTableField, i+1))
  146 + table.DataFields = append(table.DataFields, builder.NewDataField(field.Name, field.SQLType, domain.MainTableField))
145 } 147 }
146 table.ManualFields = make([]*domain.Field, 0) 148 table.ManualFields = make([]*domain.Field, 0)
147 table.CreatedAt = time.Now() 149 table.CreatedAt = time.Now()
@@ -197,6 +199,44 @@ func DataField(name string, sqlType string, flag int, index int) *domain.Field { @@ -197,6 +199,44 @@ func DataField(name string, sqlType string, flag int, index int) *domain.Field {
197 } 199 }
198 } 200 }
199 201
  202 +type DataFieldsBuilder struct {
  203 + mapPinFields map[string]int
  204 +}
  205 +
  206 +func NewDataFieldsBuilder() *DataFieldsBuilder {
  207 + builder := &DataFieldsBuilder{
  208 + mapPinFields: make(map[string]int),
  209 + }
  210 + return builder
  211 +}
  212 +
  213 +func (builder *DataFieldsBuilder) NewDataField(name string, sqlType string, flag int) *domain.Field {
  214 + var index = 0
  215 + pinName := pin(name)
  216 + index = builder.AddPinItem(pinName)
  217 + return &domain.Field{
  218 + Index: index,
  219 + Name: name,
  220 + SQLName: fmt.Sprintf("%v_%d", pinName, index),
  221 + SQLType: sqlType,
  222 + Description: "",
  223 + Flag: flag,
  224 + }
  225 +}
  226 +
  227 +func (builder *DataFieldsBuilder) AddPinItem(pin string) int {
  228 + var index = 1
  229 + for {
  230 + key := fmt.Sprintf("%v_%d", pin, index)
  231 + if _, ok := builder.mapPinFields[key]; !ok {
  232 + builder.mapPinFields[key] = index
  233 + break
  234 + }
  235 + index++
  236 + }
  237 + return index
  238 +}
  239 +
200 func pin(name string) string { 240 func pin(name string) string {
201 pinyin := tool_funs.ToPinYin(name, "_") 241 pinyin := tool_funs.ToPinYin(name, "_")
202 newPinyin := bytes.NewBuffer(nil) 242 newPinyin := bytes.NewBuffer(nil)
@@ -295,8 +295,10 @@ func (ptr *QuerySetService) PreviewPrepare(ctx *domain.Context, querySetId int, @@ -295,8 +295,10 @@ func (ptr *QuerySetService) PreviewPrepare(ctx *domain.Context, querySetId int,
295 dependencyTables := querySet.GetDependencyTables(queryComponents) 295 dependencyTables := querySet.GetDependencyTables(queryComponents)
296 if querySet.Type == domain.CalculateTable.ToString() { 296 if querySet.Type == domain.CalculateTable.ToString() {
297 aggregationFields := append(queryComponents[0].Aggregation.RowFields, queryComponents[0].Aggregation.ValueFields...) 297 aggregationFields := append(queryComponents[0].Aggregation.RowFields, queryComponents[0].Aggregation.ValueFields...)
298 - for index, f := range aggregationFields {  
299 - fields = append(fields, DataField(f.DisplayName, f.Field.SQLType, domain.MainTableField, index)) 298 + builder := NewDataFieldsBuilder()
  299 + for _, f := range aggregationFields {
  300 + //fields = append(fields, DataField(f.DisplayName, f.Field.SQLType, domain.MainTableField, index))
  301 + fields = append(fields, builder.NewDataField(f.DisplayName, f.Field.SQLType, domain.MainTableField))
300 } 302 }
301 } else { 303 } else {
302 masterTable := queryComponents[0].MasterTable 304 masterTable := queryComponents[0].MasterTable
@@ -723,7 +725,9 @@ func (ptr *QuerySetService) CreateOrUpdateCalculateItemTable(ctx *domain.Context @@ -723,7 +725,9 @@ func (ptr *QuerySetService) CreateOrUpdateCalculateItemTable(ctx *domain.Context
723 //if queryComponent.Formula.MixTableModel() { 725 //if queryComponent.Formula.MixTableModel() {
724 // queryComponent.Formula.Complete() 726 // queryComponent.Formula.Complete()
725 //} 727 //}
726 - field := DataField(querySet.Name, domain.String.ToString(), domain.MainTableField, 1) 728 + builder := NewDataFieldsBuilder()
  729 + //field := DataField(querySet.Name, domain.String.ToString(), domain.MainTableField, 1)
  730 + field := builder.NewDataField(querySet.Name, domain.String.ToString(), domain.MainTableField)
727 if len(queryComponent.Formula.TableFields) > 0 { 731 if len(queryComponent.Formula.TableFields) > 0 {
728 field.SQLType = queryComponent.Formula.TableFields[0].FieldSQLType 732 field.SQLType = queryComponent.Formula.TableFields[0].FieldSQLType
729 } 733 }
@@ -768,13 +772,15 @@ func (ptr *QuerySetService) CreateOrUpdateCalculateTable(ctx *domain.Context, qu @@ -768,13 +772,15 @@ func (ptr *QuerySetService) CreateOrUpdateCalculateTable(ctx *domain.Context, qu
768 return nil, fmt.Errorf("行、值不能同时为空") 772 return nil, fmt.Errorf("行、值不能同时为空")
769 } 773 }
770 selectedFields := make([]string, 0) 774 selectedFields := make([]string, 0)
771 - for index, f := range aggregationFields { 775 + builder := NewDataFieldsBuilder()
  776 + for _, f := range aggregationFields {
772 // 数值类型转浮点类型, 兼容类似表达式 1 * 1.1 777 // 数值类型转浮点类型, 兼容类似表达式 1 * 1.1
773 sqlType := f.Field.SQLType 778 sqlType := f.Field.SQLType
774 if f.Field.SQLType == domain.Int.ToString() || f.Field.SQLType == domain.BigInt.ToString() { 779 if f.Field.SQLType == domain.Int.ToString() || f.Field.SQLType == domain.BigInt.ToString() {
775 sqlType = domain.Float.ToString() 780 sqlType = domain.Float.ToString()
776 } 781 }
777 - fields = append(fields, DataField(f.DisplayName, sqlType, domain.MainTableField, index)) 782 + //fields = append(fields, DataField(f.DisplayName, sqlType, domain.MainTableField, index))
  783 + fields = append(fields, builder.NewDataField(f.DisplayName, sqlType, domain.MainTableField))
778 selectedFields = append(selectedFields, f.Field.Name) 784 selectedFields = append(selectedFields, f.Field.Name)
779 } 785 }
780 queryComponent.Aggregation.SelectFields = selectedFields 786 queryComponent.Aggregation.SelectFields = selectedFields
@@ -37,7 +37,7 @@ func (ptr *AddTableStructService) AddTableStruct(ctx *domain.Context, parentTabl @@ -37,7 +37,7 @@ func (ptr *AddTableStructService) AddTableStruct(ctx *domain.Context, parentTabl
37 return nil, fmt.Errorf("表名称重复") 37 return nil, fmt.Errorf("表名称重复")
38 } 38 }
39 39
40 - fields = MappingFields(mainTable, fields) 40 + fields = MappingFieldsV2(mainTable, fields)
41 dataFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.MainTableField}) 41 dataFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.MainTableField})
42 manualFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.ManualField}) 42 manualFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.ManualField})
43 table := NewTable(domain.SubTable, name, fields, mainTable.RowCount).WithContext(ctx).WithPrefix(string(domain.SubTable)) 43 table := NewTable(domain.SubTable, name, fields, mainTable.RowCount).WithContext(ctx).WithPrefix(string(domain.SubTable))
@@ -36,7 +36,7 @@ func (ptr *UpdateTableStructService) UpdateTableStruct(ctx *domain.Context, tabl @@ -36,7 +36,7 @@ func (ptr *UpdateTableStructService) UpdateTableStruct(ctx *domain.Context, tabl
36 return nil, fmt.Errorf("主表不存在") 36 return nil, fmt.Errorf("主表不存在")
37 } 37 }
38 38
39 - fields = MappingFields(mainTable, fields) 39 + fields = MappingFieldsV2(mainTable, fields)
40 reserves, deletes, adds := domain.FieldsChange(table.Fields(false), fields) 40 reserves, deletes, adds := domain.FieldsChange(table.Fields(false), fields)
41 dataFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.MainTableField}) 41 dataFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.MainTableField})
42 manualFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.ManualField}) 42 manualFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.ManualField})
@@ -81,9 +81,33 @@ func (ptr *UpdateTableStructService) UpdateTableStruct(ctx *domain.Context, tabl @@ -81,9 +81,33 @@ func (ptr *UpdateTableStructService) UpdateTableStruct(ctx *domain.Context, tabl
81 return struct{}{}, nil 81 return struct{}{}, nil
82 } 82 }
83 83
84 -func MappingFields(mainTable *domain.Table, fields []*domain.Field) []*domain.Field { 84 +//func MappingFields(mainTable *domain.Table, fields []*domain.Field) []*domain.Field {
  85 +// tableFields := mainTable.Fields(false)
  86 +// tableFieldsMap := (domain.Fields)(tableFields).ToMap()
  87 +// for i := range fields {
  88 +// f := fields[i]
  89 +// if v, ok := tableFieldsMap[f.Name]; ok {
  90 +// fields[i].Name = v.Name
  91 +// fields[i].SQLName = v.SQLName
  92 +// fields[i].Index = v.Index
  93 +// fields[i].SQLType = v.SQLType
  94 +// fields[i].Description = f.Description
  95 +// fields[i].Flag = v.Flag
  96 +// } else {
  97 +// if f.Flag == domain.ManualField && f.Index == 0 {
  98 +// mainTable.DataFieldIndex += 1
  99 +// fields[i] = DataField(f.Name, f.SQLType, domain.ManualField, mainTable.DataFieldIndex)
  100 +// fields[i].Description = f.Description
  101 +// }
  102 +// }
  103 +// }
  104 +// return fields
  105 +//}
  106 +
  107 +func MappingFieldsV2(mainTable *domain.Table, fields []*domain.Field) []*domain.Field {
85 tableFields := mainTable.Fields(false) 108 tableFields := mainTable.Fields(false)
86 tableFieldsMap := (domain.Fields)(tableFields).ToMap() 109 tableFieldsMap := (domain.Fields)(tableFields).ToMap()
  110 + builder := NewDataFieldsBuilder()
87 for i := range fields { 111 for i := range fields {
88 f := fields[i] 112 f := fields[i]
89 if v, ok := tableFieldsMap[f.Name]; ok { 113 if v, ok := tableFieldsMap[f.Name]; ok {
@@ -93,10 +117,11 @@ func MappingFields(mainTable *domain.Table, fields []*domain.Field) []*domain.Fi @@ -93,10 +117,11 @@ func MappingFields(mainTable *domain.Table, fields []*domain.Field) []*domain.Fi
93 fields[i].SQLType = v.SQLType 117 fields[i].SQLType = v.SQLType
94 fields[i].Description = f.Description 118 fields[i].Description = f.Description
95 fields[i].Flag = v.Flag 119 fields[i].Flag = v.Flag
  120 + builder.NewDataField(v.Name, v.SQLType, v.Flag)
96 } else { 121 } else {
97 if f.Flag == domain.ManualField && f.Index == 0 { 122 if f.Flag == domain.ManualField && f.Index == 0 {
98 - mainTable.DataFieldIndex += 1  
99 - fields[i] = DataField(f.Name, f.SQLType, domain.ManualField, mainTable.DataFieldIndex) 123 + //mainTable.DataFieldIndex += 1
  124 + fields[i] = builder.NewDataField(f.Name, f.SQLType, domain.ManualField)
100 fields[i].Description = f.Description 125 fields[i].Description = f.Description
101 } 126 }
102 } 127 }