event_handler_conflict_status.go
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package service
import (
"errors"
"fmt"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/event/command"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
"reflect"
"sort"
)
func (tableEventService *TableEventService) HandlerTableAffectedMarkToConflictStatus(ctx *domain.Context, cmd *command.TableEventCommand) (interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
data := cmd.EventTable
tableId := 0
switch data.Type {
case domain.TableStructEditEvent, domain.TableDeleteEvent:
tableId = data.Table.TableId
case domain.QuerySetUpdateEvent:
// 结构变更才报冲突
if !checkStructChange(cmd) {
return nil, err
}
tableId = data.QuerySet.QuerySetInfo.BindTableId
case domain.QuerySetUpdateRenameEvent:
tableId = data.QuerySet.QuerySetInfo.BindTableId
default:
return nil, err
}
if tableId == 0 {
return nil, nil
}
// tableId 相关联的
tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
_, tables, err := tableRepository.Find(map[string]interface{}{"context": ctx, "dependencyTable": tableId, "tableTypesNotIn": []string{domain.TemporaryTable.ToString()}})
if errors.Is(err, domain.ErrorNotFound) {
return nil, nil
}
tableIds := make([]int, 0)
for _, table := range tables {
tableIds = append(tableIds, table.TableId)
}
if len(tableIds) == 0 {
return nil, nil
}
querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
_, querySets, _ := querySetRepository.Find(map[string]interface{}{"context": ctx, "bindTableIds": tableIds})
for _, querySet := range querySets {
log.Logger.Debug(fmt.Sprintf("【集合状态更新】 id:%v name:%v ReadyStatus:1", querySet.QuerySetId, querySet.Name))
querySet.QuerySetInfo.WithConflictStatus()
_, err = querySetRepository.Save(querySet)
if err != nil {
return nil, err
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
func checkStructChange(cmd *command.TableEventCommand) bool {
var (
newSet = cmd.EventTable.QuerySet
oldSet = cmd.EventTable.OldQuerySet
newTable = cmd.EventTable.Table
oldTable = cmd.EventTable.OldTable
)
if newSet == nil || oldSet == nil {
return false
}
var (
t string = newSet.Type
)
switch t {
case domain.SchemaTable.ToString(), domain.SubProcessTable.ToString():
newSetDepTables := newSet.GetDependencyTables(newSet.QueryComponents)
oldSetDepTables := newSet.GetDependencyTables(oldSet.QueryComponents)
sort.SliceStable(newSetDepTables, func(i, j int) bool {
return newSetDepTables[i] < newSetDepTables[j]
})
sort.SliceStable(oldSetDepTables, func(i, j int) bool {
return oldSetDepTables[i] < oldSetDepTables[j]
})
if !reflect.DeepEqual(newSetDepTables, oldSetDepTables) {
log.Logger.Debug(fmt.Sprintf("方案/过程:%v 依赖变更 %v -> %v", t, oldSetDepTables, newSetDepTables))
return true
}
case domain.CalculateTable.ToString():
if newTable == nil || oldTable == nil {
return false
}
newTableFields := tableFields(newTable)
oldTableFields := tableFields(oldTable)
sort.Strings(newTableFields)
sort.Strings(oldTableFields)
if !reflect.DeepEqual(newTableFields, oldTableFields) {
log.Logger.Debug(fmt.Sprintf("计算表:%v 结构变更 %v -> %v", t, oldTableFields, newTableFields))
return true
}
case domain.CalculateItem.ToString(), domain.CalculateSet.ToString():
return false
}
return false
}
func tableFields(t *domain.Table) []string {
var result = make([]string, 0)
for _, f := range t.Fields(false) {
result = append(result, f.SQLName)
}
return result
}