register.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package definition
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  4. "git.sxidc.com/go-framework/baize/framework/core/tag/rule"
  5. "github.com/pkg/errors"
  6. "sync"
  7. )
  8. var registeredDomainMap = &sync.Map{}
  9. type RegisterByEntityItem struct {
  10. Entity entity.Entity
  11. FieldMap map[string]string
  12. }
  13. func RegisterByDomainEntity(items ...RegisterByEntityItem) error {
  14. for _, item := range items {
  15. e := item.Entity
  16. fields, err := rule.DefaultUsage(e)
  17. if err != nil {
  18. return err
  19. }
  20. definitions := make([]Entity, 0)
  21. for _, field := range fields {
  22. definitions = append(definitions, Entity{
  23. FieldType: field.Type,
  24. FieldName: field.FieldName,
  25. ColumnName: field.ColumnName,
  26. FieldCNName: item.FieldMap[field.FieldName],
  27. Operators: operatorOfTypeMap[field.Type],
  28. })
  29. }
  30. registeredDomainMap.Store(e.DomainCamelName(), definitions)
  31. }
  32. return nil
  33. }
  34. func GetQueryRuleDefinitions(domainName string) []Entity {
  35. result, ok := registeredDomainMap.Load(domainName)
  36. if !ok {
  37. return make([]Entity, 0)
  38. }
  39. return result.([]Entity)
  40. }
  41. func GetColumnName(domainName string, fieldName string) (string, error) {
  42. definitions := GetQueryRuleDefinitions(domainName)
  43. if definitions == nil || len(definitions) == 0 {
  44. return "", errors.Errorf("领域没有注册: %v", domainName)
  45. }
  46. for _, definition := range definitions {
  47. if definition.FieldName == fieldName {
  48. return definition.ColumnName, nil
  49. }
  50. }
  51. return "", errors.Errorf("领域没有该字段: %v", fieldName)
  52. }