作者 yangfu

冲突修改 17P0

package service
import (
"errors"
"fmt"
"github.com/linmadan/egglib-go/core/application"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
... ... @@ -77,62 +76,6 @@ func (tableEventService *TableEventService) Handler(ctx *domain.Context, cmd *co
return nil, nil
}
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, 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 NewTableEventService(options map[string]interface{}) *TableEventService {
svr := &TableEventService{}
delayNotifyTimingWheel, _ := collection.NewTimingWheel(time.Second, 10, svr.TimingWheelFunc)
... ...
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
}
... ...
... ... @@ -380,17 +380,25 @@ func (querySetService *QuerySetService) UpdateQuerySet(ctx *domain.Context, upda
}()
var (
qs *domain.QuerySet
qsTable *domain.Table
oldQs *domain.QuerySet
oldQsTable *domain.Table
)
defer func() {
if qs != nil {
domainService.AsyncEvent(domain.NewEventTable(ctx, domain.QuerySetUpdateEvent).WithQuerySet(qs))
domainService.AsyncEvent(domain.NewEventTable(ctx, domain.QuerySetUpdateEvent).
WithQuerySet(qs).WithOldQuerySet(oldQs).WithTable(qsTable).WithOldTable(oldQsTable))
}
}()
_, oldQs, _ = factory.FastPgQuerySet(transactionContext, updateQuerySetCommand.QuerySetId)
if oldQs != nil {
_, oldQsTable, _ = factory.FastPgTable(transactionContext, oldQs.QuerySetInfo.BindTableId)
}
svr, _ := factory.FastQuerySetServices(transactionContext)
if qs, err = svr.Update(ctx, updateQuerySetCommand.QuerySetId, updateQuerySetCommand.QueryComponents); err != nil {
return nil, factory.FastError(err)
}
_, qsTable, _ = factory.FastPgTable(transactionContext, qs.QuerySetInfo.BindTableId)
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
... ...
... ... @@ -17,7 +17,9 @@ type SearchTableQuery struct {
TableTypes []string `cname:"表类型 MainTable:主表 SideTable:副表 SubTable:分表 ExcelTable:Excel表" json:"tableTypes" valid:"Required"`
// 父级ID
ParentId int `cname:"父级ID" json:"parentId"`
// 模块 应用于模块 1:数控中心 2:拆解模块 4:计算模块
// 当前所处的模块
CurrentModule int `json:"currentModule"`
// 需要查询的模块 应用于模块 1:数控中心 2:拆解模块 4:计算模块
Module int `json:"module"`
// 父级ID
ParentTableId int `cname:"父级ID" json:"parentTableId"`
... ...
... ... @@ -98,6 +98,11 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
if err != nil {
return nil, factory.FastError(err)
}
if cmd.Where != nil {
data, length := dataTable.FilterByWhere(*cmd.Where)
dataTable.Data = data
dataTable.Total = length
}
values := removeDuplicate(dataTable.Values(field))
sort.SliceStable(values, func(i, j int) bool {
return values[i] < values[j]
... ...
... ... @@ -83,8 +83,8 @@ func (tableService *TableService) TableObjectSearch(searchQuery *query.SearchTab
if filterTableByFilterRule(t, searchQuery) {
continue
}
// 默认关闭的都不返回(拆解、计算)
if t.Status == domain.StatusOff {
// 无模块权限 并且 关闭状态的都不返回(拆解、计算)
if !moduleHasAuth(searchQuery.CurrentModule, t.TableType) && t.Status == domain.StatusOff {
continue
}
if !domain.TableType(t.TableType).TableIsSplitByGroup() {
... ... @@ -160,3 +160,21 @@ func filterTableByFilterRule(item *dto.TableObjectDto, searchQuery *query.Search
}
return false
}
// 验证当前模块的权限。属于当前模块可以访问所有
func moduleHasAuth(currentModule int, t string) bool {
if currentModule == 0 {
return false
}
if currentModule == domain.ModuleQuerySetCenter {
if t == domain.SchemaTable.ToString() || t == domain.SubProcessTable.ToString() || t == domain.CalculateTable.ToString() {
return true
}
}
if currentModule == domain.ModuleCalculateCenter {
if t == domain.CalculateItem.ToString() || t == domain.CalculateSet.ToString() {
return true
}
}
return false
}
... ...
... ... @@ -27,6 +27,8 @@ type EventTable struct {
Type EventType
Table *Table
QuerySet *QuerySet
OldQuerySet *QuerySet
OldTable *Table
Metadata map[string]interface{}
}
... ... @@ -52,11 +54,21 @@ func (et *EventTable) WithTable(t *Table) *EventTable {
return et
}
func (et *EventTable) WithOldTable(t *Table) *EventTable {
et.OldTable = t
return et
}
func (et *EventTable) WithQuerySet(t *QuerySet) *EventTable {
et.QuerySet = t
return et
}
func (et *EventTable) WithOldQuerySet(t *QuerySet) *EventTable {
et.OldQuerySet = t
return et
}
func (et *EventTable) WithMetadata(key string, values interface{}) *EventTable {
et.Metadata[key] = values
return et
... ... @@ -67,6 +79,8 @@ func (et *EventTable) ResolveEvent(e event.Event) {
et.Type = e.Get("Type").(EventType)
et.Table = e.Get("Table").(*Table)
et.QuerySet = e.Get("QuerySet").(*QuerySet)
et.OldQuerySet = e.Get("OldQuerySet").(*QuerySet)
et.OldTable = e.Get("OldTable").(*Table)
et.Metadata = e.Get("Metadata").(map[string]interface{})
}
... ... @@ -75,7 +89,9 @@ func (et *EventTable) FireEvent() event.Event {
e["Context"] = et.Context
e["Type"] = et.Type
e["Table"] = et.Table
e["OldTable"] = et.OldTable
e["QuerySet"] = et.QuerySet
e["OldQuerySet"] = et.OldQuerySet
e["Metadata"] = et.Metadata
return event.MustFire(et.Type.ToString(), e)
}
... ...