作者 yangfu

chore: add DataFieldsBuilder

... ... @@ -17,7 +17,7 @@ type QuerySetDetailDto struct {
TableId int `json:"tableId"`
}
func (d *QuerySetDetailDto) Load(m *domain.QuerySet) *QuerySetDetailDto {
func (d *QuerySetDetailDto) Load(m *domain.QuerySet, mapTables map[int]*domain.Table) *QuerySetDetailDto {
d.QuerySetId = m.QuerySetId
d.Type = m.Type
d.Flag = m.Flag
... ... @@ -26,7 +26,12 @@ func (d *QuerySetDetailDto) Load(m *domain.QuerySet) *QuerySetDetailDto {
if m.QuerySetInfo != nil {
d.TableId = m.QuerySetInfo.BindTableId
}
for i := range d.QueryComponents {
for i, q := range d.QueryComponents {
if q.MasterTable != nil && q.MasterTable.TableId != 0 {
if t, ok := mapTables[q.MasterTable.TableId]; ok {
d.QueryComponents[i].MasterTable = domain.NewQueryComponentTable(t)
}
}
if d.QueryComponents[i].Aggregation != nil {
d.QueryComponents[i].Aggregation.Aggregation.AllFields = d.QueryComponents[i].Aggregation.AggregationFields()
}
... ...
... ... @@ -159,7 +159,17 @@ func (querySetService *QuerySetService) GetQuerySet(ctx *domain.Context, getQuer
if err != nil {
return nil, factory.FastError(err)
}
return (&dto.QuerySetDetailDto{}).Load(querySet), nil
tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
dependencyTables := querySet.GetDependencyTables(querySet.QueryComponents)
var tables domain.Tables
if len(dependencyTables) > 0 {
_, tables, err = tableRepository.Find(map[string]interface{}{"context": ctx, "tableIds": dependencyTables})
if err != nil {
return nil, factory.FastError(err)
}
}
return (&dto.QuerySetDetailDto{}).Load(querySet, tables.ToMap()), nil
}
// 返回查询集合服务列表
... ...
... ... @@ -163,3 +163,13 @@ func TableTypesToStringList(list ...TableType) []string {
}
return result
}
type Tables []*Table
func (tables Tables) ToMap() map[int]*Table {
var result = make(map[int]*Table)
for i := range tables {
result[tables[i].TableId] = tables[i]
}
return result
}
... ...
... ... @@ -139,9 +139,11 @@ func NewTable(tableType domain.TableType, fileName string, dataFields []*domain.
if table.TableType == domain.CalculateTable.ToString() || table.TableType == domain.CalculateItem.ToString() {
table.PK = nil
}
builder := NewDataFieldsBuilder()
table.DataFieldIndex = len(dataFields)
for i, field := range dataFields {
table.DataFields = append(table.DataFields, DataField(field.Name, field.SQLType, domain.MainTableField, i+1))
for _, field := range dataFields {
//table.DataFields = append(table.DataFields, DataField(field.Name, field.SQLType, domain.MainTableField, i+1))
table.DataFields = append(table.DataFields, builder.NewDataField(field.Name, field.SQLType, domain.MainTableField))
}
table.ManualFields = make([]*domain.Field, 0)
table.CreatedAt = time.Now()
... ... @@ -197,6 +199,44 @@ func DataField(name string, sqlType string, flag int, index int) *domain.Field {
}
}
type DataFieldsBuilder struct {
mapPinFields map[string]int
}
func NewDataFieldsBuilder() *DataFieldsBuilder {
builder := &DataFieldsBuilder{
mapPinFields: make(map[string]int),
}
return builder
}
func (builder *DataFieldsBuilder) NewDataField(name string, sqlType string, flag int) *domain.Field {
var index = 0
pinName := pin(name)
index = builder.AddPinItem(pinName)
return &domain.Field{
Index: index,
Name: name,
SQLName: fmt.Sprintf("%v_%d", pinName, index),
SQLType: sqlType,
Description: "",
Flag: flag,
}
}
func (builder *DataFieldsBuilder) AddPinItem(pin string) int {
var index = 1
for {
key := fmt.Sprintf("%v_%d", pin, index)
if _, ok := builder.mapPinFields[key]; !ok {
builder.mapPinFields[key] = index
break
}
index++
}
return index
}
func pin(name string) string {
pinyin := tool_funs.ToPinYin(name, "_")
newPinyin := bytes.NewBuffer(nil)
... ...
... ... @@ -295,8 +295,10 @@ func (ptr *QuerySetService) PreviewPrepare(ctx *domain.Context, querySetId int,
dependencyTables := querySet.GetDependencyTables(queryComponents)
if querySet.Type == domain.CalculateTable.ToString() {
aggregationFields := append(queryComponents[0].Aggregation.RowFields, queryComponents[0].Aggregation.ValueFields...)
for index, f := range aggregationFields {
fields = append(fields, DataField(f.DisplayName, f.Field.SQLType, domain.MainTableField, index))
builder := NewDataFieldsBuilder()
for _, f := range aggregationFields {
//fields = append(fields, DataField(f.DisplayName, f.Field.SQLType, domain.MainTableField, index))
fields = append(fields, builder.NewDataField(f.DisplayName, f.Field.SQLType, domain.MainTableField))
}
} else {
masterTable := queryComponents[0].MasterTable
... ... @@ -723,7 +725,9 @@ func (ptr *QuerySetService) CreateOrUpdateCalculateItemTable(ctx *domain.Context
//if queryComponent.Formula.MixTableModel() {
// queryComponent.Formula.Complete()
//}
field := DataField(querySet.Name, domain.String.ToString(), domain.MainTableField, 1)
builder := NewDataFieldsBuilder()
//field := DataField(querySet.Name, domain.String.ToString(), domain.MainTableField, 1)
field := builder.NewDataField(querySet.Name, domain.String.ToString(), domain.MainTableField)
if len(queryComponent.Formula.TableFields) > 0 {
field.SQLType = queryComponent.Formula.TableFields[0].FieldSQLType
}
... ... @@ -768,13 +772,15 @@ func (ptr *QuerySetService) CreateOrUpdateCalculateTable(ctx *domain.Context, qu
return nil, fmt.Errorf("行、值不能同时为空")
}
selectedFields := make([]string, 0)
for index, f := range aggregationFields {
builder := NewDataFieldsBuilder()
for _, f := range aggregationFields {
// 数值类型转浮点类型, 兼容类似表达式 1 * 1.1
sqlType := f.Field.SQLType
if f.Field.SQLType == domain.Int.ToString() || f.Field.SQLType == domain.BigInt.ToString() {
sqlType = domain.Float.ToString()
}
fields = append(fields, DataField(f.DisplayName, sqlType, domain.MainTableField, index))
//fields = append(fields, DataField(f.DisplayName, sqlType, domain.MainTableField, index))
fields = append(fields, builder.NewDataField(f.DisplayName, sqlType, domain.MainTableField))
selectedFields = append(selectedFields, f.Field.Name)
}
queryComponent.Aggregation.SelectFields = selectedFields
... ...
... ... @@ -37,7 +37,7 @@ func (ptr *AddTableStructService) AddTableStruct(ctx *domain.Context, parentTabl
return nil, fmt.Errorf("表名称重复")
}
fields = MappingFields(mainTable, fields)
fields = MappingFieldsV2(mainTable, fields)
dataFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.MainTableField})
manualFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.ManualField})
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
return nil, fmt.Errorf("主表不存在")
}
fields = MappingFields(mainTable, fields)
fields = MappingFieldsV2(mainTable, fields)
reserves, deletes, adds := domain.FieldsChange(table.Fields(false), fields)
dataFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.MainTableField})
manualFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.ManualField})
... ... @@ -81,9 +81,33 @@ func (ptr *UpdateTableStructService) UpdateTableStruct(ctx *domain.Context, tabl
return struct{}{}, nil
}
func MappingFields(mainTable *domain.Table, fields []*domain.Field) []*domain.Field {
//func MappingFields(mainTable *domain.Table, fields []*domain.Field) []*domain.Field {
// tableFields := mainTable.Fields(false)
// tableFieldsMap := (domain.Fields)(tableFields).ToMap()
// for i := range fields {
// f := fields[i]
// if v, ok := tableFieldsMap[f.Name]; ok {
// fields[i].Name = v.Name
// fields[i].SQLName = v.SQLName
// fields[i].Index = v.Index
// fields[i].SQLType = v.SQLType
// fields[i].Description = f.Description
// fields[i].Flag = v.Flag
// } else {
// if f.Flag == domain.ManualField && f.Index == 0 {
// mainTable.DataFieldIndex += 1
// fields[i] = DataField(f.Name, f.SQLType, domain.ManualField, mainTable.DataFieldIndex)
// fields[i].Description = f.Description
// }
// }
// }
// return fields
//}
func MappingFieldsV2(mainTable *domain.Table, fields []*domain.Field) []*domain.Field {
tableFields := mainTable.Fields(false)
tableFieldsMap := (domain.Fields)(tableFields).ToMap()
builder := NewDataFieldsBuilder()
for i := range fields {
f := fields[i]
if v, ok := tableFieldsMap[f.Name]; ok {
... ... @@ -93,10 +117,11 @@ func MappingFields(mainTable *domain.Table, fields []*domain.Field) []*domain.Fi
fields[i].SQLType = v.SQLType
fields[i].Description = f.Description
fields[i].Flag = v.Flag
builder.NewDataField(v.Name, v.SQLType, v.Flag)
} else {
if f.Flag == domain.ManualField && f.Index == 0 {
mainTable.DataFieldIndex += 1
fields[i] = DataField(f.Name, f.SQLType, domain.ManualField, mainTable.DataFieldIndex)
//mainTable.DataFieldIndex += 1
fields[i] = builder.NewDataField(f.Name, f.SQLType, domain.ManualField)
fields[i].Description = f.Description
}
}
... ...