service.go
4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package service
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
type CompanyService struct{}
func NewCompanyService(options map[string]interface{}) *CompanyService {
newCompanyService := &CompanyService{}
return newCompanyService
}
//AllowCompany 公司启用合伙人功能
func (service CompanyService) AllowCompany(companyId int64) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
companyRespository domain.CompanyRepository
oldCompanyData domain.Company
)
if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
oldCompanyData, err = companyRespository.FindOne(map[string]interface{}{
"Id": companyId,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取公司(id=%d)的数据失败:%s", companyId, err.Error()))
}
err = oldCompanyData.Update(map[string]interface{}{
"Enable": domain.CompanyEnableYes,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
//更新公司数据
err = companyRespository.Edit(&oldCompanyData)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("更新公司(id=%d)的数据失败:%s", companyId, err.Error()))
}
err = transactionContext.CommitTransaction()
return nil
}
//ForbidCompany 公司禁用合伙人功能
func (service CompanyService) ForbidCompany(companyId int64) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
companyRespository domain.CompanyRepository
oldCompanyData domain.Company
)
if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
oldCompanyData, err = companyRespository.FindOne(map[string]interface{}{
"Id": companyId,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取公司(id=%d)的数据失败:%s", companyId, err.Error()))
}
err = oldCompanyData.Update(map[string]interface{}{
"Enable": domain.CompanyEnableNo,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
//更新公司数据
err = companyRespository.Edit(&oldCompanyData)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("更新公司(id=%d)的数据失败:%s", companyId, err.Error()))
}
err = transactionContext.CommitTransaction()
return nil
}
//GetCompanyData 获取公司数据
func (service CompanyService) GetCompanyData(companyId int64) (*domain.Company, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
companyRespository domain.CompanyRepository
companyData domain.Company
)
if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
companyData, err = companyRespository.FindOne(map[string]interface{}{
"Id": companyId,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取公司(id=%d)的数据失败:%s", companyId, err.Error()))
}
err = transactionContext.CommitTransaction()
return &companyData, nil
}