作者 yangfu
提交者 yangfu

fix: duplicate table name

@@ -26,6 +26,7 @@ require ( @@ -26,6 +26,7 @@ require (
26 github.com/sergi/go-diff v1.2.0 // indirect 26 github.com/sergi/go-diff v1.2.0 // indirect
27 github.com/shopspring/decimal v1.3.1 27 github.com/shopspring/decimal v1.3.1
28 github.com/smartystreets/goconvey v1.7.2 // indirect 28 github.com/smartystreets/goconvey v1.7.2 // indirect
  29 + github.com/sony/sonyflake v1.1.0 // indirect
29 github.com/stretchr/testify v1.7.1 30 github.com/stretchr/testify v1.7.1
30 github.com/valyala/fasthttp v1.38.0 // indirect 31 github.com/valyala/fasthttp v1.38.0 // indirect
31 github.com/xeipuuv/gojsonschema v1.2.0 // indirect 32 github.com/xeipuuv/gojsonschema v1.2.0 // indirect
@@ -71,7 +71,10 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd * @@ -71,7 +71,10 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
71 if table.TableType == domain.SubTable.ToString() && field.Flag == domain.ManualField { 71 if table.TableType == domain.SubTable.ToString() && field.Flag == domain.ManualField {
72 return empty, nil 72 return empty, nil
73 } 73 }
74 - 74 + match := cmd.Match
  75 + if !domain.SQLType(field.SQLType).IsString() {
  76 + match = ""
  77 + }
75 options := &starrocks.QueryOptions{ 78 options := &starrocks.QueryOptions{
76 Table: table, 79 Table: table,
77 TableName: table.SQLName, 80 TableName: table.SQLName,
@@ -80,7 +83,7 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd * @@ -80,7 +83,7 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
80 { 83 {
81 Condition: domain.Condition{ 84 Condition: domain.Condition{
82 Field: field, 85 Field: field,
83 - Like: cmd.Match, 86 + Like: match,
84 Order: "ASC", 87 Order: "ASC",
85 }, 88 },
86 Distinct: true, 89 Distinct: true,
@@ -107,8 +110,9 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd * @@ -107,8 +110,9 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
107 if err := transactionContext.CommitTransaction(); err != nil { 110 if err := transactionContext.CommitTransaction(); err != nil {
108 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 111 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
109 } 112 }
  113 + values := dataTable.OptionalValue(cmd.Match)
110 return map[string]interface{}{ 114 return map[string]interface{}{
111 - "values": dataTable.OptionalValue(),  
112 - "total": dataTable.Total, 115 + "values": values,
  116 + "total": len(values),
113 }, nil 117 }, nil
114 } 118 }
@@ -68,6 +68,10 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command @@ -68,6 +68,10 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command
68 if err != nil { 68 if err != nil {
69 return nil, factory.FastError(err) 69 return nil, factory.FastError(err)
70 } 70 }
  71 + // 计算项只返回第一个值
  72 + // if table.TableType==domain.CalculateItem.ToString() && dataTable!=nil && len(dataTable.Data)>0{
  73 + // dataTable.Data = dataTable.Data[:1]
  74 + // }
71 response.Load(table, dataTable, domain.ObjectMetaTable) 75 response.Load(table, dataTable, domain.ObjectMetaTable)
72 } 76 }
73 } 77 }
1 package domain 1 package domain
2 2
  3 +import "strings"
  4 +
3 type DataTable struct { 5 type DataTable struct {
4 Fields []*Field `json:"fields"` 6 Fields []*Field `json:"fields"`
5 Data [][]string `json:"data"` 7 Data [][]string `json:"data"`
@@ -51,19 +53,20 @@ type Condition struct { @@ -51,19 +53,20 @@ type Condition struct {
51 Order string `json:"order"` 53 Order string `json:"order"`
52 } 54 }
53 55
54 -func (t *DataTable) OptionalValue() []string {  
55 - //set := make(map[string]string) 56 +func (t *DataTable) OptionalValue(args ...string) []string {
56 var values = make([]string, 0) 57 var values = make([]string, 0)
  58 + match:=""
  59 + if len(args)>0{
  60 + match = args[0]
  61 + }
57 if len(t.Data) > 0 && len(t.Data[0]) == 1 { 62 if len(t.Data) > 0 && len(t.Data[0]) == 1 {
58 for i := range t.Data { 63 for i := range t.Data {
59 if len(t.Data[i]) == 0 { 64 if len(t.Data[i]) == 0 {
60 continue 65 continue
61 } 66 }
62 - //if _, ok := set[t.Data[i][0]]; ok {  
63 - // continue  
64 - //} else {  
65 - // set[t.Data[i][0]] = ""  
66 - //} 67 + if len(match)>0 && !strings.Contains(t.Data[i][0],match){
  68 + continue
  69 + }
67 values = append(values, t.Data[i][0]) 70 values = append(values, t.Data[i][0])
68 } 71 }
69 } 72 }
@@ -67,12 +67,25 @@ func (table *Table) TableIdString() string { @@ -67,12 +67,25 @@ func (table *Table) TableIdString() string {
67 67
68 func (table *Table) WithContext(ctx *Context) *Table { 68 func (table *Table) WithContext(ctx *Context) *Table {
69 rand.Seed(time.Now().Unix()) 69 rand.Seed(time.Now().Unix())
70 - table.SQLName = fmt.Sprintf("%v_t%v_c%v", table.SQLName, rand.Intn(1000000), ctx.CompanyId) 70 + table.SQLName = fmt.Sprintf("%v_t%v_c%v", limitStringLen(table.SQLName,40), rand.Intn(1000000), limitStringLen(fmt.Sprintf("%d",ctx.CompanyId),8))
71 table.Context = ctx 71 table.Context = ctx
72 return table 72 return table
73 } 73 }
74 74
  75 +func limitStringLen(s string,l int)string{
  76 + result:=s
  77 + subLength :=l /2
  78 + if len(result)>l{
  79 + result = result[:subLength] + result[len(result)-subLength:]
  80 + }
  81 + return result
  82 +}
  83 +
75 func (table *Table) WithPrefix(prefix string) *Table { 84 func (table *Table) WithPrefix(prefix string) *Table {
  85 + if strings.HasPrefix(table.SQLName,"_"){
  86 + table.SQLName = fmt.Sprintf("%v%v", strings.ToLower(prefix), table.SQLName)
  87 + return table
  88 + }
76 table.SQLName = fmt.Sprintf("%v_%v", strings.ToLower(prefix), table.SQLName) 89 table.SQLName = fmt.Sprintf("%v_%v", strings.ToLower(prefix), table.SQLName)
77 return table 90 return table
78 } 91 }
@@ -132,3 +145,11 @@ func (t *Table) AssertTableType(types ...TableType) bool { @@ -132,3 +145,11 @@ func (t *Table) AssertTableType(types ...TableType) bool {
132 } 145 }
133 return false 146 return false
134 } 147 }
  148 +
  149 +func TableTypesToStringList(list ...TableType)[]string{
  150 + var result = make([]string,0)
  151 + for _,item:=range list{
  152 + result = append(result, item.ToString())
  153 + }
  154 + return result
  155 +}
  1 +package domain
  2 +
  3 +import (
  4 + "testing"
  5 +
  6 + "github.com/stretchr/testify/assert"
  7 +)
  8 +
  9 +func TestLimitStringLen(t *testing.T){
  10 + inputs:=[]struct{
  11 + input string
  12 + length int
  13 + want string
  14 + }{
  15 + {
  16 + input: "123456789",
  17 + length: 6,
  18 + want: "123789",
  19 + },
  20 + {
  21 + input: "123456789",
  22 + length: 7,
  23 + want: "123789",
  24 + },
  25 + {
  26 + input: "123456789",
  27 + length: 10,
  28 + want: "123456789",
  29 + },
  30 + {
  31 + input: "pai_xu_ce_shi_zhu_biao_fu_zhi",
  32 + length: 20,
  33 + want: "pai_xu_ce_iao_fu_zhi",
  34 + },
  35 + }
  36 + for _,input:=range inputs{
  37 + got:=limitStringLen(input.input,input.length)
  38 + assert.Equal(t,input.want,got)
  39 + }
  40 +}
@@ -159,7 +159,7 @@ func NewCopyTable(tableType domain.TableType, fileName string, dataFields []*dom @@ -159,7 +159,7 @@ func NewCopyTable(tableType domain.TableType, fileName string, dataFields []*dom
159 table.Name = fileName 159 table.Name = fileName
160 table.SQLName = pin(fileName) //SQLTableName() 160 table.SQLName = pin(fileName) //SQLTableName()
161 table.PK = PK() 161 table.PK = PK()
162 - if table.TableType == domain.CalculateTable.ToString() || table.TableType == domain.CalculateItem.ToString() { 162 + if table.TableType == domain.CalculateItem.ToString() { //table.TableType == domain.CalculateTable.ToString() ||
163 table.PK = nil 163 table.PK = nil
164 } 164 }
165 table.DataFieldIndex = len(dataFields) 165 table.DataFieldIndex = len(dataFields)
@@ -30,7 +30,9 @@ func (ptr *GenerateMainTableService) GenerateTable(ctx *domain.Context, fileId i @@ -30,7 +30,9 @@ func (ptr *GenerateMainTableService) GenerateTable(ctx *domain.Context, fileId i
30 return nil, fmt.Errorf("表名称重复") 30 return nil, fmt.Errorf("表名称重复")
31 } 31 }
32 32
33 - mainTable := NewTable(domain.MainTable, tableName, table.DataFields, table.RowCount).WithContext(ctx) 33 + mainTable := NewTable(domain.MainTable, tableName, table.DataFields, table.RowCount).
  34 + WithContext(ctx).
  35 + WithPrefix(domain.MainTable.ToString())
34 _, err = tableRepository.Save(mainTable) 36 _, err = tableRepository.Save(mainTable)
35 if err != nil { 37 if err != nil {
36 return nil, err 38 return nil, err
@@ -199,6 +199,8 @@ func FastTable(table *domain.Table) (*domain.DataTable, error) { @@ -199,6 +199,8 @@ func FastTable(table *domain.Table) (*domain.DataTable, error) {
199 TableName: table.SQLName, 199 TableName: table.SQLName,
200 Select: table.Fields(false), 200 Select: table.Fields(false),
201 } 201 }
  202 + options.SetDefaultOrder()
  203 + options.SetOffsetLimit(1,10000)
202 var dataTable *domain.DataTable 204 var dataTable *domain.DataTable
203 dataTable, err = FastDataTable(options) 205 dataTable, err = FastDataTable(options)
204 if err != nil { 206 if err != nil {
@@ -40,7 +40,7 @@ func (ptr *AddTableStructService) AddTableStruct(ctx *domain.Context, parentTabl @@ -40,7 +40,7 @@ func (ptr *AddTableStructService) AddTableStruct(ctx *domain.Context, parentTabl
40 fields = MappingFields(mainTable, fields) 40 fields = MappingFields(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) 43 + table := NewTable(domain.SubTable, name, fields, mainTable.RowCount).WithContext(ctx).WithPrefix(string(domain.SubTable))
44 table.DataFieldIndex = mainTable.DataFieldIndex 44 table.DataFieldIndex = mainTable.DataFieldIndex
45 table.DataFields = dataFields 45 table.DataFields = dataFields
46 table.ManualFields = manualFields 46 table.ManualFields = manualFields
@@ -2,6 +2,7 @@ package domainService @@ -2,6 +2,7 @@ package domainService
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 +
5 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
6 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain" 7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository" 8 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
@@ -45,7 +46,10 @@ func (ptr *CopyDataTableService) CopyTable(ctx *domain.Context, tableId int, tab @@ -45,7 +46,10 @@ func (ptr *CopyDataTableService) CopyTable(ctx *domain.Context, tableId int, tab
45 } 46 }
46 } 47 }
47 // 验证表名是否重复 48 // 验证表名是否重复
48 - duplicateTable, err := tableRepository.FindOne(map[string]interface{}{"context": ctx, "tableName": tableName}) 49 + duplicateTable, err := tableRepository.FindOne(map[string]interface{}{
  50 + "context": ctx,
  51 + "tableName": tableName,
  52 + "tableTypes":domain.TableTypesToStringList(domain.MainTable,domain.SubTable,domain.SideTable)})
49 if err == nil && duplicateTable != nil { 53 if err == nil && duplicateTable != nil {
50 return nil, fmt.Errorf("表名称重复") 54 return nil, fmt.Errorf("表名称重复")
51 } 55 }
@@ -53,7 +57,8 @@ func (ptr *CopyDataTableService) CopyTable(ctx *domain.Context, tableId int, tab @@ -53,7 +57,8 @@ func (ptr *CopyDataTableService) CopyTable(ctx *domain.Context, tableId int, tab
53 sideTable := NewCopyTable(domain.SideTable, tableName, dataFields, table.RowCount). 57 sideTable := NewCopyTable(domain.SideTable, tableName, dataFields, table.RowCount).
54 WithContext(ctx). 58 WithContext(ctx).
55 WithParentId(table.TableId). 59 WithParentId(table.TableId).
56 - WithDataFieldIndex(table.DataFieldIndex) 60 + WithDataFieldIndex(table.DataFieldIndex).
  61 + WithPrefix(domain.SideTable.ToString())
57 if sideTable, err = tableRepository.Save(sideTable); err != nil { 62 if sideTable, err = tableRepository.Save(sideTable); err != nil {
58 return nil, err 63 return nil, err
59 } 64 }
@@ -62,7 +62,9 @@ func (ptr *UpdateTableStructService) UpdateTableStruct(ctx *domain.Context, tabl @@ -62,7 +62,9 @@ func (ptr *UpdateTableStructService) UpdateTableStruct(ctx *domain.Context, tabl
62 return nil, err 62 return nil, err
63 } 63 }
64 } 64 }
65 - 65 + defer func() {
  66 + AsyncEvent(domain.NewEventTable(ctx, domain.TableDataEditEvent).WithTable(table))
  67 + }()
66 // Log 68 // Log
67 // 日志 69 // 日志
68 if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &SubTableEditLog{ 70 if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &SubTableEditLog{
@@ -3,8 +3,8 @@ package starrocks @@ -3,8 +3,8 @@ package starrocks
3 import ( 3 import (
4 "bytes" 4 "bytes"
5 "fmt" 5 "fmt"
6 - "github.com/google/uuid"  
7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain" 6 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
8 "gorm.io/gorm" 8 "gorm.io/gorm"
9 "html" 9 "html"
10 "html/template" 10 "html/template"
@@ -15,8 +15,12 @@ func Insert(db *gorm.DB, tableName string, fields []*domain.FieldValue) error { @@ -15,8 +15,12 @@ func Insert(db *gorm.DB, tableName string, fields []*domain.FieldValue) error {
15 for _, f := range fields { 15 for _, f := range fields {
16 if f.Field.Flag == domain.PKField && f.Value == "" { 16 if f.Field.Flag == domain.PKField && f.Value == "" {
17 //continue 17 //continue
18 - id, _ := uuid.NewUUID()  
19 - f.Value = id.String() 18 + //id, _ := uuid.NewUUID()
  19 + // idString := id.String()
  20 + // 需要调用分布式id生成
  21 + id, _ := utils.NewSnowflakeId()
  22 + idString := fmt.Sprintf("%d", id)
  23 + f.Value = idString
20 } 24 }
21 value[f.Field.SQLName] = f.TypeValue() 25 value[f.Field.SQLName] = f.TypeValue()
22 } 26 }
@@ -117,3 +117,10 @@ func TestRound(t *testing.T) { @@ -117,3 +117,10 @@ func TestRound(t *testing.T) {
117 t.Logf("%.1f", Truncate(99.99, 1)) 117 t.Logf("%.1f", Truncate(99.99, 1))
118 t.Logf("%v", Truncate(99, 0)) 118 t.Logf("%v", Truncate(99, 0))
119 } 119 }
  120 +
  121 +func TestNewSnowflakeId(t *testing.T) {
  122 + id, _ := NewSnowflakeId()
  123 + for i := 0; i < 100; i++ {
  124 + t.Log(id / 2)
  125 + }
  126 +}