作者 yangfu

冲突修改 17P0

1 package service 1 package service
2 2
3 import ( 3 import (
4 - "errors"  
5 "fmt" 4 "fmt"
6 "github.com/linmadan/egglib-go/core/application" 5 "github.com/linmadan/egglib-go/core/application"
7 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 6 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
@@ -77,62 +76,6 @@ func (tableEventService *TableEventService) Handler(ctx *domain.Context, cmd *co @@ -77,62 +76,6 @@ func (tableEventService *TableEventService) Handler(ctx *domain.Context, cmd *co
77 return nil, nil 76 return nil, nil
78 } 77 }
79 78
80 -func (tableEventService *TableEventService) HandlerTableAffectedMarkToConflictStatus(ctx *domain.Context, cmd *command.TableEventCommand) (interface{}, error) {  
81 - transactionContext, err := factory.CreateTransactionContext(nil)  
82 - if err != nil {  
83 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
84 - }  
85 - if err := transactionContext.StartTransaction(); err != nil {  
86 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
87 - }  
88 - defer func() {  
89 - transactionContext.RollbackTransaction()  
90 - }()  
91 -  
92 - data := cmd.EventTable  
93 - tableId := 0  
94 - switch data.Type {  
95 - case domain.TableStructEditEvent, domain.TableDeleteEvent:  
96 - tableId = data.Table.TableId  
97 - case domain.QuerySetUpdateEvent, domain.QuerySetUpdateRenameEvent:  
98 - tableId = data.QuerySet.QuerySetInfo.BindTableId  
99 - default:  
100 - return nil, err  
101 - }  
102 - if tableId == 0 {  
103 - return nil, nil  
104 - }  
105 - // tableId 相关联的  
106 - tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)  
107 -  
108 - _, tables, err := tableRepository.Find(map[string]interface{}{"context": ctx, "dependencyTable": tableId, "tableTypesNotIn": []string{domain.TemporaryTable.ToString()}})  
109 - if errors.Is(err, domain.ErrorNotFound) {  
110 - return nil, nil  
111 - }  
112 - tableIds := make([]int, 0)  
113 - for _, table := range tables {  
114 - tableIds = append(tableIds, table.TableId)  
115 - }  
116 - if len(tableIds) == 0 {  
117 - return nil, nil  
118 - }  
119 - querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)  
120 - _, querySets, _ := querySetRepository.Find(map[string]interface{}{"context": ctx, "bindTableIds": tableIds})  
121 - for _, querySet := range querySets {  
122 - log.Logger.Debug(fmt.Sprintf("【集合状态更新】 id:%v name:%v ReadyStatus:1", querySet.QuerySetId, querySet.Name))  
123 - querySet.QuerySetInfo.WithConflictStatus()  
124 - _, err = querySetRepository.Save(querySet)  
125 - if err != nil {  
126 - return nil, err  
127 - }  
128 - }  
129 -  
130 - if err := transactionContext.CommitTransaction(); err != nil {  
131 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())  
132 - }  
133 - return nil, nil  
134 -}  
135 -  
136 func NewTableEventService(options map[string]interface{}) *TableEventService { 79 func NewTableEventService(options map[string]interface{}) *TableEventService {
137 svr := &TableEventService{} 80 svr := &TableEventService{}
138 delayNotifyTimingWheel, _ := collection.NewTimingWheel(time.Second, 10, svr.TimingWheelFunc) 81 delayNotifyTimingWheel, _ := collection.NewTimingWheel(time.Second, 10, svr.TimingWheelFunc)
  1 +package service
  2 +
  3 +import (
  4 + "errors"
  5 + "fmt"
  6 + "github.com/linmadan/egglib-go/core/application"
  7 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/event/command"
  8 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
  9 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
  11 + "reflect"
  12 + "sort"
  13 +)
  14 +
  15 +func (tableEventService *TableEventService) HandlerTableAffectedMarkToConflictStatus(ctx *domain.Context, cmd *command.TableEventCommand) (interface{}, error) {
  16 + transactionContext, err := factory.CreateTransactionContext(nil)
  17 + if err != nil {
  18 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  19 + }
  20 + if err := transactionContext.StartTransaction(); err != nil {
  21 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  22 + }
  23 + defer func() {
  24 + transactionContext.RollbackTransaction()
  25 + }()
  26 +
  27 + data := cmd.EventTable
  28 + tableId := 0
  29 + switch data.Type {
  30 + case domain.TableStructEditEvent, domain.TableDeleteEvent:
  31 + tableId = data.Table.TableId
  32 + case domain.QuerySetUpdateEvent:
  33 + // 结构变更才报冲突
  34 + if !checkStructChange(cmd) {
  35 + return nil, err
  36 + }
  37 + tableId = data.QuerySet.QuerySetInfo.BindTableId
  38 + case domain.QuerySetUpdateRenameEvent:
  39 + tableId = data.QuerySet.QuerySetInfo.BindTableId
  40 + default:
  41 + return nil, err
  42 + }
  43 + if tableId == 0 {
  44 + return nil, nil
  45 + }
  46 + // tableId 相关联的
  47 + tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
  48 +
  49 + _, tables, err := tableRepository.Find(map[string]interface{}{"context": ctx, "dependencyTable": tableId, "tableTypesNotIn": []string{domain.TemporaryTable.ToString()}})
  50 + if errors.Is(err, domain.ErrorNotFound) {
  51 + return nil, nil
  52 + }
  53 + tableIds := make([]int, 0)
  54 + for _, table := range tables {
  55 + tableIds = append(tableIds, table.TableId)
  56 + }
  57 + if len(tableIds) == 0 {
  58 + return nil, nil
  59 + }
  60 + querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
  61 + _, querySets, _ := querySetRepository.Find(map[string]interface{}{"context": ctx, "bindTableIds": tableIds})
  62 + for _, querySet := range querySets {
  63 + log.Logger.Debug(fmt.Sprintf("【集合状态更新】 id:%v name:%v ReadyStatus:1", querySet.QuerySetId, querySet.Name))
  64 + querySet.QuerySetInfo.WithConflictStatus()
  65 + _, err = querySetRepository.Save(querySet)
  66 + if err != nil {
  67 + return nil, err
  68 + }
  69 + }
  70 +
  71 + if err := transactionContext.CommitTransaction(); err != nil {
  72 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  73 + }
  74 + return nil, nil
  75 +}
  76 +
  77 +func checkStructChange(cmd *command.TableEventCommand) bool {
  78 + var (
  79 + newSet = cmd.EventTable.QuerySet
  80 + oldSet = cmd.EventTable.OldQuerySet
  81 + newTable = cmd.EventTable.Table
  82 + oldTable = cmd.EventTable.OldTable
  83 + )
  84 + if newSet == nil || oldSet == nil {
  85 + return false
  86 + }
  87 + var (
  88 + t string = newSet.Type
  89 + )
  90 +
  91 + switch t {
  92 + case domain.SchemaTable.ToString(), domain.SubProcessTable.ToString():
  93 + newSetDepTables := newSet.GetDependencyTables(newSet.QueryComponents)
  94 + oldSetDepTables := newSet.GetDependencyTables(oldSet.QueryComponents)
  95 + sort.SliceStable(newSetDepTables, func(i, j int) bool {
  96 + return newSetDepTables[i] < newSetDepTables[j]
  97 + })
  98 + sort.SliceStable(oldSetDepTables, func(i, j int) bool {
  99 + return oldSetDepTables[i] < oldSetDepTables[j]
  100 + })
  101 + if !reflect.DeepEqual(newSetDepTables, oldSetDepTables) {
  102 + log.Logger.Debug(fmt.Sprintf("方案/过程:%v 依赖变更 %v -> %v", t, oldSetDepTables, newSetDepTables))
  103 + return true
  104 + }
  105 + case domain.CalculateTable.ToString():
  106 + if newTable == nil || oldTable == nil {
  107 + return false
  108 + }
  109 + newTableFields := tableFields(newTable)
  110 + oldTableFields := tableFields(oldTable)
  111 + sort.Strings(newTableFields)
  112 + sort.Strings(oldTableFields)
  113 + if !reflect.DeepEqual(newTableFields, oldTableFields) {
  114 + log.Logger.Debug(fmt.Sprintf("计算表:%v 结构变更 %v -> %v", t, oldTableFields, newTableFields))
  115 + return true
  116 + }
  117 + case domain.CalculateItem.ToString(), domain.CalculateSet.ToString():
  118 + return false
  119 + }
  120 + return false
  121 +}
  122 +
  123 +func tableFields(t *domain.Table) []string {
  124 + var result = make([]string, 0)
  125 + for _, f := range t.Fields(false) {
  126 + result = append(result, f.SQLName)
  127 + }
  128 + return result
  129 +}
@@ -380,17 +380,25 @@ func (querySetService *QuerySetService) UpdateQuerySet(ctx *domain.Context, upda @@ -380,17 +380,25 @@ func (querySetService *QuerySetService) UpdateQuerySet(ctx *domain.Context, upda
380 }() 380 }()
381 var ( 381 var (
382 qs *domain.QuerySet 382 qs *domain.QuerySet
  383 + qsTable *domain.Table
  384 + oldQs *domain.QuerySet
  385 + oldQsTable *domain.Table
383 ) 386 )
384 defer func() { 387 defer func() {
385 if qs != nil { 388 if qs != nil {
386 - domainService.AsyncEvent(domain.NewEventTable(ctx, domain.QuerySetUpdateEvent).WithQuerySet(qs)) 389 + domainService.AsyncEvent(domain.NewEventTable(ctx, domain.QuerySetUpdateEvent).
  390 + WithQuerySet(qs).WithOldQuerySet(oldQs).WithTable(qsTable).WithOldTable(oldQsTable))
387 } 391 }
388 }() 392 }()
  393 + _, oldQs, _ = factory.FastPgQuerySet(transactionContext, updateQuerySetCommand.QuerySetId)
  394 + if oldQs != nil {
  395 + _, oldQsTable, _ = factory.FastPgTable(transactionContext, oldQs.QuerySetInfo.BindTableId)
  396 + }
389 svr, _ := factory.FastQuerySetServices(transactionContext) 397 svr, _ := factory.FastQuerySetServices(transactionContext)
390 if qs, err = svr.Update(ctx, updateQuerySetCommand.QuerySetId, updateQuerySetCommand.QueryComponents); err != nil { 398 if qs, err = svr.Update(ctx, updateQuerySetCommand.QuerySetId, updateQuerySetCommand.QueryComponents); err != nil {
391 return nil, factory.FastError(err) 399 return nil, factory.FastError(err)
392 } 400 }
393 - 401 + _, qsTable, _ = factory.FastPgTable(transactionContext, qs.QuerySetInfo.BindTableId)
394 if err := transactionContext.CommitTransaction(); err != nil { 402 if err := transactionContext.CommitTransaction(); err != nil {
395 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 403 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
396 } 404 }
@@ -17,7 +17,9 @@ type SearchTableQuery struct { @@ -17,7 +17,9 @@ type SearchTableQuery struct {
17 TableTypes []string `cname:"表类型 MainTable:主表 SideTable:副表 SubTable:分表 ExcelTable:Excel表" json:"tableTypes" valid:"Required"` 17 TableTypes []string `cname:"表类型 MainTable:主表 SideTable:副表 SubTable:分表 ExcelTable:Excel表" json:"tableTypes" valid:"Required"`
18 // 父级ID 18 // 父级ID
19 ParentId int `cname:"父级ID" json:"parentId"` 19 ParentId int `cname:"父级ID" json:"parentId"`
20 - // 模块 应用于模块 1:数控中心 2:拆解模块 4:计算模块 20 + // 当前所处的模块
  21 + CurrentModule int `json:"currentModule"`
  22 + // 需要查询的模块 应用于模块 1:数控中心 2:拆解模块 4:计算模块
21 Module int `json:"module"` 23 Module int `json:"module"`
22 // 父级ID 24 // 父级ID
23 ParentTableId int `cname:"父级ID" json:"parentTableId"` 25 ParentTableId int `cname:"父级ID" json:"parentTableId"`
@@ -98,6 +98,11 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd * @@ -98,6 +98,11 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
98 if err != nil { 98 if err != nil {
99 return nil, factory.FastError(err) 99 return nil, factory.FastError(err)
100 } 100 }
  101 + if cmd.Where != nil {
  102 + data, length := dataTable.FilterByWhere(*cmd.Where)
  103 + dataTable.Data = data
  104 + dataTable.Total = length
  105 + }
101 values := removeDuplicate(dataTable.Values(field)) 106 values := removeDuplicate(dataTable.Values(field))
102 sort.SliceStable(values, func(i, j int) bool { 107 sort.SliceStable(values, func(i, j int) bool {
103 return values[i] < values[j] 108 return values[i] < values[j]
@@ -83,8 +83,8 @@ func (tableService *TableService) TableObjectSearch(searchQuery *query.SearchTab @@ -83,8 +83,8 @@ func (tableService *TableService) TableObjectSearch(searchQuery *query.SearchTab
83 if filterTableByFilterRule(t, searchQuery) { 83 if filterTableByFilterRule(t, searchQuery) {
84 continue 84 continue
85 } 85 }
86 - // 默认关闭的都不返回(拆解、计算)  
87 - if t.Status == domain.StatusOff { 86 + // 无模块权限 并且 关闭状态的都不返回(拆解、计算)
  87 + if !moduleHasAuth(searchQuery.CurrentModule, t.TableType) && t.Status == domain.StatusOff {
88 continue 88 continue
89 } 89 }
90 if !domain.TableType(t.TableType).TableIsSplitByGroup() { 90 if !domain.TableType(t.TableType).TableIsSplitByGroup() {
@@ -160,3 +160,21 @@ func filterTableByFilterRule(item *dto.TableObjectDto, searchQuery *query.Search @@ -160,3 +160,21 @@ func filterTableByFilterRule(item *dto.TableObjectDto, searchQuery *query.Search
160 } 160 }
161 return false 161 return false
162 } 162 }
  163 +
  164 +// 验证当前模块的权限。属于当前模块可以访问所有
  165 +func moduleHasAuth(currentModule int, t string) bool {
  166 + if currentModule == 0 {
  167 + return false
  168 + }
  169 + if currentModule == domain.ModuleQuerySetCenter {
  170 + if t == domain.SchemaTable.ToString() || t == domain.SubProcessTable.ToString() || t == domain.CalculateTable.ToString() {
  171 + return true
  172 + }
  173 + }
  174 + if currentModule == domain.ModuleCalculateCenter {
  175 + if t == domain.CalculateItem.ToString() || t == domain.CalculateSet.ToString() {
  176 + return true
  177 + }
  178 + }
  179 + return false
  180 +}
@@ -27,6 +27,8 @@ type EventTable struct { @@ -27,6 +27,8 @@ type EventTable struct {
27 Type EventType 27 Type EventType
28 Table *Table 28 Table *Table
29 QuerySet *QuerySet 29 QuerySet *QuerySet
  30 + OldQuerySet *QuerySet
  31 + OldTable *Table
30 Metadata map[string]interface{} 32 Metadata map[string]interface{}
31 } 33 }
32 34
@@ -52,11 +54,21 @@ func (et *EventTable) WithTable(t *Table) *EventTable { @@ -52,11 +54,21 @@ func (et *EventTable) WithTable(t *Table) *EventTable {
52 return et 54 return et
53 } 55 }
54 56
  57 +func (et *EventTable) WithOldTable(t *Table) *EventTable {
  58 + et.OldTable = t
  59 + return et
  60 +}
  61 +
55 func (et *EventTable) WithQuerySet(t *QuerySet) *EventTable { 62 func (et *EventTable) WithQuerySet(t *QuerySet) *EventTable {
56 et.QuerySet = t 63 et.QuerySet = t
57 return et 64 return et
58 } 65 }
59 66
  67 +func (et *EventTable) WithOldQuerySet(t *QuerySet) *EventTable {
  68 + et.OldQuerySet = t
  69 + return et
  70 +}
  71 +
60 func (et *EventTable) WithMetadata(key string, values interface{}) *EventTable { 72 func (et *EventTable) WithMetadata(key string, values interface{}) *EventTable {
61 et.Metadata[key] = values 73 et.Metadata[key] = values
62 return et 74 return et
@@ -67,6 +79,8 @@ func (et *EventTable) ResolveEvent(e event.Event) { @@ -67,6 +79,8 @@ func (et *EventTable) ResolveEvent(e event.Event) {
67 et.Type = e.Get("Type").(EventType) 79 et.Type = e.Get("Type").(EventType)
68 et.Table = e.Get("Table").(*Table) 80 et.Table = e.Get("Table").(*Table)
69 et.QuerySet = e.Get("QuerySet").(*QuerySet) 81 et.QuerySet = e.Get("QuerySet").(*QuerySet)
  82 + et.OldQuerySet = e.Get("OldQuerySet").(*QuerySet)
  83 + et.OldTable = e.Get("OldTable").(*Table)
70 et.Metadata = e.Get("Metadata").(map[string]interface{}) 84 et.Metadata = e.Get("Metadata").(map[string]interface{})
71 } 85 }
72 86
@@ -75,7 +89,9 @@ func (et *EventTable) FireEvent() event.Event { @@ -75,7 +89,9 @@ func (et *EventTable) FireEvent() event.Event {
75 e["Context"] = et.Context 89 e["Context"] = et.Context
76 e["Type"] = et.Type 90 e["Type"] = et.Type
77 e["Table"] = et.Table 91 e["Table"] = et.Table
  92 + e["OldTable"] = et.OldTable
78 e["QuerySet"] = et.QuerySet 93 e["QuerySet"] = et.QuerySet
  94 + e["OldQuerySet"] = et.OldQuerySet
79 e["Metadata"] = et.Metadata 95 e["Metadata"] = et.Metadata
80 return event.MustFire(et.Type.ToString(), e) 96 return event.MustFire(et.Type.ToString(), e)
81 } 97 }