作者 陈志颖

feat:添加公司和部门适配器

@@ -8,3 +8,15 @@ import ( @@ -8,3 +8,15 @@ import (
8 func CreateUserService(options map[string]interface{}) (service.UserService, error) { 8 func CreateUserService(options map[string]interface{}) (service.UserService, error) {
9 return domain_service.NewUserService() 9 return domain_service.NewUserService()
10 } 10 }
  11 +
  12 +func CreateCompanyService(options map[string]interface{}) (service.CompanyService, error) {
  13 + return domain_service.NewCompanyService()
  14 +}
  15 +
  16 +func CreateDepartmentService(options map[string]interface{}) (service.DepartmentService, error) {
  17 + return domain_service.NewDepartmentService()
  18 +}
  19 +
  20 +func CreateOrganization(options map[string]interface{}) (service.OrgService, error) {
  21 + return domain_service.NewOrganizationService()
  22 +}
@@ -8,6 +8,4 @@ type UserService interface { @@ -8,6 +8,4 @@ type UserService interface {
8 RelevantFrom(companyId int64, orgId int64, userId int64) (*domain.Relevant, error) 8 RelevantFrom(companyId int64, orgId int64, userId int64) (*domain.Relevant, error)
9 SalesmanFrom(companyId int64, orgId int64, userId int64) (*domain.Salesman, error) 9 SalesmanFrom(companyId int64, orgId int64, userId int64) (*domain.Salesman, error)
10 OperatorFrom(companyId int64, orgId int64, userId int64) (*domain.User, error) 10 OperatorFrom(companyId int64, orgId int64, userId int64) (*domain.User, error)
11 - UserInMenu(companyId int64, orgId int64, userId int64, code string) (bool, error)  
12 - UserInOrganization(companyId int64, orgId int64, userId int64) (bool, error)  
13 } 11 }
@@ -18,10 +18,12 @@ func (service *UserService) ReferrerFrom(companyId int64, orgId int64, userId in @@ -18,10 +18,12 @@ func (service *UserService) ReferrerFrom(companyId int64, orgId int64, userId in
18 if referrer, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Referrer"); err != nil { 18 if referrer, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Referrer"); err != nil {
19 return nil, err 19 return nil, err
20 } else { 20 } else {
  21 + if referrer != nil {
21 err := json.Unmarshal(referrer.([]byte), returnData) 22 err := json.Unmarshal(referrer.([]byte), returnData)
22 if err != nil { 23 if err != nil {
23 return nil, err 24 return nil, err
24 } 25 }
  26 + }
25 return returnData, nil 27 return returnData, nil
26 } 28 }
27 } 29 }
@@ -33,13 +35,15 @@ func (service *UserService) UndertakerFrom(companyId int64, orgId int64, userId @@ -33,13 +35,15 @@ func (service *UserService) UndertakerFrom(companyId int64, orgId int64, userId
33 if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil { 35 if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil {
34 return nil, err 36 return nil, err
35 } else { 37 } else {
36 - if referrer, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Undertaker"); err != nil { 38 + if undertaker, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Undertaker"); err != nil {
37 return nil, err 39 return nil, err
38 } else { 40 } else {
39 - err := json.Unmarshal(referrer.([]byte), returnData) 41 + if undertaker != nil {
  42 + err := json.Unmarshal(undertaker.([]byte), returnData)
40 if err != nil { 43 if err != nil {
41 return nil, err 44 return nil, err
42 } 45 }
  46 + }
43 return returnData, nil 47 return returnData, nil
44 } 48 }
45 } 49 }
@@ -51,13 +55,15 @@ func (service *UserService) RelevantFrom(companyId int64, orgId int64, userId in @@ -51,13 +55,15 @@ func (service *UserService) RelevantFrom(companyId int64, orgId int64, userId in
51 if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil { 55 if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil {
52 return nil, err 56 return nil, err
53 } else { 57 } else {
54 - if referrer, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Relevant"); err != nil { 58 + if relevant, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Relevant"); err != nil {
55 return nil, err 59 return nil, err
56 } else { 60 } else {
57 - err := json.Unmarshal(referrer.([]byte), returnData) 61 + if relevant != nil {
  62 + err := json.Unmarshal(relevant.([]byte), returnData)
58 if err != nil { 63 if err != nil {
59 return nil, err 64 return nil, err
60 } 65 }
  66 + }
61 return returnData, nil 67 return returnData, nil
62 } 68 }
63 } 69 }
@@ -69,13 +75,15 @@ func (service *UserService) SalesmanFrom(companyId int64, orgId int64, userId in @@ -69,13 +75,15 @@ func (service *UserService) SalesmanFrom(companyId int64, orgId int64, userId in
69 if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil { 75 if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil {
70 return nil, err 76 return nil, err
71 } else { 77 } else {
72 - if referrer, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Salesman"); err != nil { 78 + if salesman, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Salesman"); err != nil {
73 return nil, err 79 return nil, err
74 } else { 80 } else {
75 - err := json.Unmarshal(referrer.([]byte), returnData) 81 + if salesman != nil {
  82 + err := json.Unmarshal(salesman.([]byte), returnData)
76 if err != nil { 83 if err != nil {
77 return nil, err 84 return nil, err
78 } 85 }
  86 + }
79 return returnData, nil 87 return returnData, nil
80 } 88 }
81 } 89 }
@@ -87,26 +95,20 @@ func (service *UserService) OperatorFrom(companyId int64, orgId int64, userId in @@ -87,26 +95,20 @@ func (service *UserService) OperatorFrom(companyId int64, orgId int64, userId in
87 if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil { 95 if userAdaptor, err := adaptor.NewUserAdaptor(); err != nil {
88 return nil, err 96 return nil, err
89 } else { 97 } else {
90 - if referrer, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Operator"); err != nil { 98 + if operator, err := userAdaptor.ToParticipator(companyId, orgId, userId, "Operator"); err != nil {
91 return nil, err 99 return nil, err
92 } else { 100 } else {
93 - err := json.Unmarshal(referrer.([]byte), returnData) 101 + if operator != nil {
  102 + err := json.Unmarshal(operator.([]byte), returnData)
94 if err != nil { 103 if err != nil {
95 return nil, err 104 return nil, err
96 } 105 }
  106 + }
97 return returnData, nil 107 return returnData, nil
98 } 108 }
99 } 109 }
100 } 110 }
101 111
102 -func (service *UserService) UserInMenu(companyId int64, orgId int64, userId int64, code string) (bool, error) {  
103 - return false, nil  
104 -}  
105 -  
106 -func (service *UserService) UserInOrganization(companyId int64, orgId int64, userId int64) (bool, error) {  
107 - return false, nil  
108 -}  
109 -  
110 func NewUserService() (*UserService, error) { 112 func NewUserService() (*UserService, error) {
111 return &UserService{}, nil 113 return &UserService{}, nil
112 } 114 }
1 package adaptor 1 package adaptor
2 2
3 -import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/service_gateway"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/service_gateway/translator"
  7 +)
4 8
5 type CompanyAdaptor struct { 9 type CompanyAdaptor struct {
6 } 10 }
7 11
8 func (adaptor *CompanyAdaptor) ToCompany(companyId int64) (*domain.Company, error) { 12 func (adaptor *CompanyAdaptor) ToCompany(companyId int64) (*domain.Company, error) {
9 - return nil, nil 13 + companyServiceGateway := service_gateway.NewHttplibUserServiceGateway()
  14 + response, err := companyServiceGateway.GetCompany(companyId)
  15 + if err != nil {
  16 + return nil, err
  17 + }
  18 + if companyTranslator, err := translator.NewCompanyTranslator(); err != nil {
  19 + return nil, err
  20 + } else {
  21 + company, err := companyTranslator.ToCompanyFromRepresentation(response)
  22 + if err != nil {
  23 + return nil, err
  24 + }
  25 + return company, nil
  26 + }
  27 +}
  28 +
  29 +func NewCompanyAdaptor() (*CompanyAdaptor, error) {
  30 + return &CompanyAdaptor{}, nil
10 } 31 }
1 package adaptor 1 package adaptor
2 2
3 -import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/service_gateway"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/service_gateway/translator"
  7 +)
4 8
5 type DepartmentAdaptor struct { 9 type DepartmentAdaptor struct {
6 } 10 }
7 11
8 func (adaptor *DepartmentAdaptor) ToDepartment(companyId int64, departmentId int64) (*domain.Department, error) { 12 func (adaptor *DepartmentAdaptor) ToDepartment(companyId int64, departmentId int64) (*domain.Department, error) {
9 - return nil, nil 13 + departmentServiceGateway := service_gateway.NewHttplibUserServiceGateway()
  14 + response, err := departmentServiceGateway.GetDepartment(companyId, departmentId)
  15 + if err != nil {
  16 + return nil, err
  17 + }
  18 + if departmentTranslator, err := translator.NewDepartmentTranslator(); err != nil {
  19 + return nil, err
  20 + } else {
  21 + department, err := departmentTranslator.ToDepartmentFromRepresentation(response)
  22 + if err != nil {
  23 + return nil, err
  24 + }
  25 + return department, nil
  26 + }
  27 +}
  28 +
  29 +func NewDepartmentAdaptor() (*DepartmentAdaptor, error) {
  30 + return &DepartmentAdaptor{}, nil
10 } 31 }
1 package adaptor 1 package adaptor
2 2
3 -import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain" 3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/service_gateway"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/service_gateway/translator"
  7 +)
4 8
5 type OrganizationAdaptor struct { 9 type OrganizationAdaptor struct {
6 } 10 }
7 11
8 func (adaptor *OrganizationAdaptor) ToOrganization(companyId int64, orgId int64) (*domain.Org, error) { 12 func (adaptor *OrganizationAdaptor) ToOrganization(companyId int64, orgId int64) (*domain.Org, error) {
9 - return nil, nil 13 + organizationServiceGateway := service_gateway.NewHttplibUserServiceGateway()
  14 + response, err := organizationServiceGateway.GetOrganization(companyId, orgId)
  15 + if err != nil {
  16 + return nil, err
  17 + }
  18 + if organizationTranslator, err := translator.NewOrganizationTranslator(); err != nil {
  19 + return nil, err
  20 + } else {
  21 + organization, err := organizationTranslator.ToOrganizationFromRepresentation(response)
  22 + if err != nil {
  23 + return nil, err
  24 + }
  25 + return organization, nil
  26 + }
  27 +}
  28 +
  29 +func NewOrganizationAdaptor() (*OrganizationAdaptor, error) {
  30 + return &OrganizationAdaptor{}, nil
10 } 31 }
@@ -28,39 +28,31 @@ func (adaptor *UserAdaptor) ToParticipator(companyId int64, orgId int64, userId @@ -28,39 +28,31 @@ func (adaptor *UserAdaptor) ToParticipator(companyId int64, orgId int64, userId
28 case "Referrer": 28 case "Referrer":
29 referrer, err := userTranslator.ToReferrerFromRepresentation(response) 29 referrer, err := userTranslator.ToReferrerFromRepresentation(response)
30 if err != nil { 30 if err != nil {
31 - return struct{}{}, err 31 + return map[string]interface{}{}, err
32 } 32 }
33 return referrer, nil 33 return referrer, nil
34 case "Salesman": 34 case "Salesman":
35 salesman, err := userTranslator.ToSalesmanFromRepresentation(response) 35 salesman, err := userTranslator.ToSalesmanFromRepresentation(response)
36 if err != nil { 36 if err != nil {
37 - return struct{}{}, nil 37 + return map[string]interface{}{}, nil
38 } 38 }
39 return salesman, nil 39 return salesman, nil
40 case "Undertaker": 40 case "Undertaker":
41 undertaker, err := userTranslator.ToUndertakerFromRepresentation(response) 41 undertaker, err := userTranslator.ToUndertakerFromRepresentation(response)
42 if err != nil { 42 if err != nil {
43 - return struct{}{}, nil 43 + return map[string]interface{}{}, nil
44 } 44 }
45 return undertaker, nil 45 return undertaker, nil
46 case "Operator": 46 case "Operator":
47 operator, err := userTranslator.ToOperatorFromRepresentation(response) 47 operator, err := userTranslator.ToOperatorFromRepresentation(response)
48 if err != nil { 48 if err != nil {
49 - return struct{}{}, nil 49 + return map[string]interface{}{}, nil
50 } 50 }
51 return operator, nil 51 return operator, nil
52 } 52 }
53 } 53 }
54 } 54 }
55 - return struct{}{}, nil  
56 -}  
57 -  
58 -func (adaptor *UserAdaptor) UserInMenu() {  
59 -  
60 -}  
61 -  
62 -func (adaptor *UserAdaptor) UserInOrganization() {  
63 - 55 + return map[string]interface{}{}, nil
64 } 56 }
65 57
66 func NewUserAdaptor() (*UserAdaptor, error) { 58 func NewUserAdaptor() (*UserAdaptor, error) {
@@ -31,7 +31,7 @@ func (serviceGateway *HttplibUserServiceGateway) GetUser(companyId int64, orgId @@ -31,7 +31,7 @@ func (serviceGateway *HttplibUserServiceGateway) GetUser(companyId int64, orgId
31 return data, err 31 return data, err
32 } 32 }
33 33
34 -// GetCompany 获取公司 34 +// GetCompany 获取公司信息
35 func (serviceGateway *HttplibUserServiceGateway) GetCompany(companyId int64) (map[string]interface{}, error) { 35 func (serviceGateway *HttplibUserServiceGateway) GetCompany(companyId int64) (map[string]interface{}, error) {
36 companyIdStr := strconv.FormatInt(companyId, 10) 36 companyIdStr := strconv.FormatInt(companyId, 10)
37 url := strings.Join([]string{serviceGateway.baseURL, "companies/" + companyIdStr}, "/") 37 url := strings.Join([]string{serviceGateway.baseURL, "companies/" + companyIdStr}, "/")
@@ -51,6 +51,26 @@ func (serviceGateway *HttplibUserServiceGateway) GetCompany(companyId int64) (ma @@ -51,6 +51,26 @@ func (serviceGateway *HttplibUserServiceGateway) GetCompany(companyId int64) (ma
51 return data, err 51 return data, err
52 } 52 }
53 53
  54 +// GetDepartment 获取部门信息
  55 +func (serviceGateway *HttplibUserServiceGateway) GetDepartment(companyId int64, departmentId int64) (map[string]interface{}, error) {
  56 + companyIdStr := strconv.FormatInt(companyId, 10)
  57 + url := strings.Join([]string{serviceGateway.baseURL, "companies/" + companyIdStr}, "/")
  58 + request := serviceGateway.createRequest(url, "get")
  59 + options := make(map[string]interface{})
  60 + options["departmentId"] = departmentId
  61 + _, err1 := request.JSONBody(options)
  62 + if err1 != nil {
  63 + return nil, err1
  64 + }
  65 + response := make(map[string]interface{})
  66 + err2 := request.ToJSON(&response)
  67 + if err2 != nil {
  68 + return nil, err2
  69 + }
  70 + data, err := serviceGateway.responseHandle(response)
  71 + return data, err
  72 +}
  73 +
54 // GetOrganization 获取组织信息 74 // GetOrganization 获取组织信息
55 func (serviceGateway *HttplibUserServiceGateway) GetOrganization(companyId int64, organizationId int64) (map[string]interface{}, error) { 75 func (serviceGateway *HttplibUserServiceGateway) GetOrganization(companyId int64, organizationId int64) (map[string]interface{}, error) {
56 companyIdStr := strconv.FormatInt(companyId, 10) 76 companyIdStr := strconv.FormatInt(companyId, 10)
@@ -73,41 +93,47 @@ func (serviceGateway *HttplibUserServiceGateway) GetOrganization(companyId int64 @@ -73,41 +93,47 @@ func (serviceGateway *HttplibUserServiceGateway) GetOrganization(companyId int64
73 } 93 }
74 94
75 // UserInMenu 判断用户是否具有模块权限 95 // UserInMenu 判断用户是否具有模块权限
76 -func (serviceGateway *HttplibUserServiceGateway) UserInMenu(companyId int64, userId int64, menuCode string) (map[string]interface{}, error) { 96 +func (serviceGateway *HttplibUserServiceGateway) UserInMenu(companyId int64, userId int64, menuCode string) (bool, error) {
77 url := strings.Join([]string{serviceGateway.baseURL, "users/menu"}, "/") 97 url := strings.Join([]string{serviceGateway.baseURL, "users/menu"}, "/")
78 request := serviceGateway.createRequest(url, "get") 98 request := serviceGateway.createRequest(url, "get")
79 options := make(map[string]interface{}) 99 options := make(map[string]interface{})
80 options["code"] = menuCode 100 options["code"] = menuCode
81 _, err1 := request.JSONBody(options) 101 _, err1 := request.JSONBody(options)
82 if err1 != nil { 102 if err1 != nil {
83 - return nil, err1 103 + return false, err1
84 } 104 }
85 response := make(map[string]interface{}) 105 response := make(map[string]interface{})
86 err2 := request.ToJSON(&response) 106 err2 := request.ToJSON(&response)
87 if err2 != nil { 107 if err2 != nil {
88 - return nil, err2 108 + return false, err2
89 } 109 }
90 data, err := serviceGateway.responseHandle(response) 110 data, err := serviceGateway.responseHandle(response)
91 - return data, err 111 + if data["code"] == 200 {
  112 + return true, nil
  113 + }
  114 + return false, err
92 } 115 }
93 116
94 // UserInOrganization 判断用户是否存在组织内 117 // UserInOrganization 判断用户是否存在组织内
95 -func (serviceGateway *HttplibUserServiceGateway) UserInOrganization(companyId int64, orgId int64, userId int64) (map[string]interface{}, error) { 118 +func (serviceGateway *HttplibUserServiceGateway) UserInOrganization(companyId int64, orgId int64, userId int64) (bool, error) {
96 url := strings.Join([]string{serviceGateway.baseURL, "users/menu"}, "/") 119 url := strings.Join([]string{serviceGateway.baseURL, "users/menu"}, "/")
97 request := serviceGateway.createRequest(url, "get") 120 request := serviceGateway.createRequest(url, "get")
98 options := make(map[string]interface{}) 121 options := make(map[string]interface{})
99 options["orgId"] = orgId 122 options["orgId"] = orgId
100 _, err1 := request.JSONBody(options) 123 _, err1 := request.JSONBody(options)
101 if err1 != nil { 124 if err1 != nil {
102 - return nil, err1 125 + return false, err1
103 } 126 }
104 response := make(map[string]interface{}) 127 response := make(map[string]interface{})
105 err2 := request.ToJSON(&response) 128 err2 := request.ToJSON(&response)
106 if err2 != nil { 129 if err2 != nil {
107 - return nil, err2 130 + return false, err2
108 } 131 }
109 data, err := serviceGateway.responseHandle(response) 132 data, err := serviceGateway.responseHandle(response)
110 - return data, err 133 + if data["code"] == 200 {
  134 + return true, nil
  135 + }
  136 + return false, err
111 } 137 }
112 138
113 func NewHttplibUserServiceGateway() *HttplibUserServiceGateway { 139 func NewHttplibUserServiceGateway() *HttplibUserServiceGateway {
@@ -4,6 +4,6 @@ type UserServiceGateway interface { @@ -4,6 +4,6 @@ type UserServiceGateway interface {
4 GetUser(companyId int64, orgId int64, uid int64) (map[string]interface{}, error) 4 GetUser(companyId int64, orgId int64, uid int64) (map[string]interface{}, error)
5 GetCompany(companyId int64) (map[string]interface{}, error) 5 GetCompany(companyId int64) (map[string]interface{}, error)
6 GetOrganization(companyId int64, organizationId int64) (map[string]interface{}, error) 6 GetOrganization(companyId int64, organizationId int64) (map[string]interface{}, error)
7 - UserInMenu(companyId int64, userId int64, menuCode string) (map[string]interface{}, error)  
8 - UserInOrganization(companyId int64, orgId int64, userId int64) (map[string]interface{}, error) 7 + UserInMenu(companyId int64, userId int64, menuCode string) (bool, error)
  8 + UserInOrganization(companyId int64, orgId int64, userId int64) (bool, error)
9 } 9 }
@@ -11,3 +11,7 @@ func (translator *CompanyTranslator) ToCompanyFromRepresentation(data map[string @@ -11,3 +11,7 @@ func (translator *CompanyTranslator) ToCompanyFromRepresentation(data map[string
11 CompanyName: "", 11 CompanyName: "",
12 }, nil 12 }, nil
13 } 13 }
  14 +
  15 +func NewCompanyTranslator() (*CompanyTranslator, error) {
  16 + return &CompanyTranslator{}, nil
  17 +}
@@ -13,3 +13,7 @@ func (translator *DepartmentTranslator) ToDepartmentFromRepresentation(data map[ @@ -13,3 +13,7 @@ func (translator *DepartmentTranslator) ToDepartmentFromRepresentation(data map[
13 IsOrganization: false, 13 IsOrganization: false,
14 }, nil 14 }, nil
15 } 15 }
  16 +
  17 +func NewDepartmentTranslator() (*DepartmentTranslator, error) {
  18 + return &DepartmentTranslator{}, nil
  19 +}
@@ -12,3 +12,7 @@ func (translator *OrganizationTranslator) ToOrganizationFromRepresentation(data @@ -12,3 +12,7 @@ func (translator *OrganizationTranslator) ToOrganizationFromRepresentation(data
12 Company: nil, 12 Company: nil,
13 }, nil 13 }, nil
14 } 14 }
  15 +
  16 +func NewOrganizationTranslator() (*OrganizationTranslator, error) {
  17 + return &OrganizationTranslator{}, nil
  18 +}