package definition import ( "git.sxidc.com/go-framework/baize/framework/core/domain/entity" "git.sxidc.com/go-framework/baize/framework/core/tag/rule" "github.com/pkg/errors" "sync" ) var registeredDomainMap = &sync.Map{} type RegisterByEntityItem struct { Entity entity.Entity FieldMap map[string]string } func RegisterByDomainEntity(items ...RegisterByEntityItem) error { for _, item := range items { e := item.Entity fields, err := rule.DefaultUsage(e) if err != nil { return err } definitions := make([]Entity, 0) for _, field := range fields { definitions = append(definitions, Entity{ FieldType: field.Type, FieldName: field.FieldName, ColumnName: field.ColumnName, FieldCNName: item.FieldMap[field.FieldName], Operators: operatorOfTypeMap[field.Type], }) } registeredDomainMap.Store(e.DomainCamelName(), definitions) } return nil } func GetQueryRuleDefinitions(domainName string) []Entity { result, ok := registeredDomainMap.Load(domainName) if !ok { return make([]Entity, 0) } return result.([]Entity) } func GetColumnName(domainName string, fieldName string) (string, error) { definitions := GetQueryRuleDefinitions(domainName) if definitions == nil || len(definitions) == 0 { return "", errors.Errorf("领域没有注册: %v", domainName) } for _, definition := range definitions { if definition.FieldName == fieldName { return definition.ColumnName, nil } } return "", errors.Errorf("领域没有该字段: %v", fieldName) }