table_object_search.go 5.4 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/dto"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/query"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
	"sort"
)

func (tableService *TableService) TableObjectSearch(searchQuery *query.SearchTableQuery) (interface{}, error) {
	if err := searchQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}

	tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
	_, tables, err := tableRepository.Find(utils.ObjectToMap(searchQuery))
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	var result = make([]*dto.TableObjectDto, 0)
	for _, table := range tables {
		var item = &dto.TableObjectDto{}
		item.Load(table)
		if searchQuery.ReturnDetailStructInfo {
			item.SetDetailStructInfo(table)
		}
		item.Flag = domain.FlagSet
		if item.TableType == domain.MainTable.ToString() ||
			item.TableType == domain.SubTable.ToString() ||
			item.TableType == domain.SideTable.ToString() {
			item.ParentId = 0
			item.Status = domain.StatusOn
		}
		result = append(result, item)
	}
	if searchQuery.TableId > 0 {
		return map[string]interface{}{
			"count":        len(result),
			"tableObjects": result,
		}, nil
	}

	querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
	_, querySets, _ := querySetRepository.Find(map[string]interface{}{"context": searchQuery.Context})

	var (
		querySetMapById      = make(map[int]*domain.QuerySet)
		querySetMapByTableId = make(map[int]*domain.QuerySet)
	)
	if searchQuery.ReturnGroupItem != nil && *searchQuery.ReturnGroupItem {
		for _, qs := range querySets {
			querySetMapById[qs.QuerySetId] = qs
		}
	}

	for _, qs := range querySets {
		if qs.QuerySetInfo.BindTableId == 0 {
			continue
		}
		querySetMapByTableId[qs.QuerySetInfo.BindTableId] = qs
	}

	var response = make([]*dto.TableObjectDto, 0)
	for index, t := range result {
		v, ok := querySetMapByTableId[t.TableId]
		if !ok {
			continue
		}
		result[index].Update(v)
	}
	// 分组
	querySetMapGroup := make(map[int]bool)
	querySetGroups := make([]*domain.QuerySet, 0)
	for _, t := range result {
		if filterTableByFilterRule(t, searchQuery) {
			continue
		}
		// 无模块权限 并且 关闭状态的都不返回(拆解、计算)
		if !moduleHasAuth(searchQuery.CurrentModule, t.TableType) && t.Status == domain.StatusOff {
			continue
		}
		if !domain.TableType(t.TableType).TableIsSplitByGroup() {
			response = append(response, t)
			continue
		}
		v, ok := querySetMapByTableId[t.TableId]
		if !ok {
			response = append(response, t)
			continue
		}
		t.Update(v)
		parentGroupId := v.ParentId

		response = append(response, t)
		for {
			if parentGroupId == 0 {
				break
			}
			if _, ok := querySetMapGroup[parentGroupId]; ok {
				break
			}
			querySetMapGroup[parentGroupId] = true
			if v, ok := querySetMapById[parentGroupId]; ok {
				querySetGroups = append(querySetGroups, v)
				parentGroupId = v.ParentId
			}
		}
	}

	for _, querySetGroup := range querySetGroups {
		var groupItem = &dto.TableObjectDto{}
		groupItem.LoadGroup(querySetGroup)
		response = append(response, groupItem)
	}

	sort.Slice(response, func(i, j int) bool {
		item1 := response[i]
		item2 := response[j]
		// 拆解模块按应用时间倒叙排列
		if domain.AssertTableType(item1.TableType, domain.MainTable, domain.SubTable, domain.SideTable) &&
			domain.AssertTableType(item2.TableType, domain.MainTable, domain.SubTable, domain.SideTable) {
			return item1.ApplyAt > item2.ApplyAt
		}
		return item1.Id < item2.Id
	})
	return map[string]interface{}{
		"count":        len(response),
		"tableObjects": response,
	}, nil
}

// true:代表需要过滤  false:不需要过滤
func filterTableByFilterRule(item *dto.TableObjectDto, searchQuery *query.SearchTableQuery) bool {
	filterRules := searchQuery.FilterRules
	if len(searchQuery.ExcludeTables) > 0 {
		for _, t := range searchQuery.ExcludeTables {
			if t == item.TableId {
				return true
			}
		}
	}
	for _, rule := range filterRules {
		if rule.TableType == item.TableType && rule.Status > 0 && rule.Status != item.Status {
			return true
		}
		if rule.TableType == item.TableType && rule.Status == 0 {
			return true
		}
	}
	for _, rule := range filterRules {
		if rule.TableType == "*" && rule.Status > 0 && rule.Status != item.Status {
			return true
		}
	}
	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
}