作者 yangfu

计算集可选值

@@ -2,14 +2,15 @@ package service @@ -2,14 +2,15 @@ package service
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 -  
6 "github.com/linmadan/egglib-go/core/application" 5 "github.com/linmadan/egglib-go/core/application"
  6 + "github.com/zeromicro/go-zero/core/collection"
7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory" 7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
8 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/command" 8 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/command"
9 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
10 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg" 10 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg"
11 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks" 11 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
12 "gorm.io/gorm" 12 "gorm.io/gorm"
  13 + "sort"
13 ) 14 )
14 15
15 func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *command.FieldOptionalValuesCommand) (interface{}, error) { 16 func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *command.FieldOptionalValuesCommand) (interface{}, error) {
@@ -84,6 +85,28 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd * @@ -84,6 +85,28 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
84 if !domain.SQLType(field.SQLType).IsString() { 85 if !domain.SQLType(field.SQLType).IsString() {
85 match = "" 86 match = ""
86 } 87 }
  88 +
  89 + if table.TableType == domain.CalculateSet.ToString() {
  90 + var querySet *domain.QuerySet
  91 + querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
  92 + querySet, err = querySetRepository.FindOne(map[string]interface{}{"BindTableId": cmd.ObjectId})
  93 + if err != nil {
  94 + return nil, factory.FastError(err)
  95 + }
  96 + svr, _ := factory.FastQuerySetServices(transactionContext)
  97 + dataTable, err = svr.LoadCalculateSetData(ctx, querySet, querySet.QueryComponents)
  98 + if err != nil {
  99 + return nil, factory.FastError(err)
  100 + }
  101 + values := removeDuplicate(dataTable.Values(field))
  102 + sort.SliceStable(values, func(i, j int) bool {
  103 + return values[i] < values[j]
  104 + })
  105 + return map[string]interface{}{
  106 + "values": values,
  107 + "total": len(values),
  108 + }, nil
  109 + }
87 options := &starrocks.QueryOptions{ 110 options := &starrocks.QueryOptions{
88 Table: table, 111 Table: table,
89 TableName: table.SQLName, 112 TableName: table.SQLName,
@@ -127,3 +150,15 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd * @@ -127,3 +150,15 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
127 "total": len(values), 150 "total": len(values),
128 }, nil 151 }, nil
129 } 152 }
  153 +
  154 +func removeDuplicate(inputs []string) []string {
  155 + s := collection.NewSet()
  156 + var result = make([]string, 0)
  157 + for _, input := range inputs {
  158 + if !s.Contains(input) {
  159 + s.Add(input)
  160 + result = append(result, input)
  161 + }
  162 + }
  163 + return result
  164 +}