company.go
8.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package service
import (
"encoding/json"
"errors"
"fmt"
"time"
"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 SyncCompanyService struct{}
var _ SyncAction = (*SyncCompanyService)(nil)
//企业平台的公司基础数据
type CompanyBase struct {
Id int64 `json:"id"` //id
Name string `json:"name"` //公司名称名称
AdminCompanyId int `json:"admin_company_id"` //总后台的公司id
Logo string `json:"logo"` //公司图标
Remarks string `json:"remarks"` //备注
Status int8 `json:"status"`
}
// CompanytData 企业平台发送过来的公司数据数据
type CompanytData struct {
Company CompanyBase `json:"company"`
User EmployeeData `json:"user"`
}
//CompanyCharge 企业平台发送过来的变更公司主管数据
type CompanyCharge struct {
Id int64 `json:"id"`
Charge []int64 `json:"charge"`
}
func (service SyncCompanyService) DoAction(action string, byteData []byte) error {
switch action {
case "add":
//添加公司
var (
data CompanytData
err error
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return service.addCompany(data)
case "edit":
//编辑
var (
data CompanytData
err error
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return service.updateCompany(data)
case "setCompanyCharge":
//设置公司主管
var (
data CompanyCharge
err error
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return service.updateCompanyCharge(data)
default:
return errors.New("action not found")
}
}
func (service SyncCompanyService) addCompany(data CompanytData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
companyRespository domain.CompanyRepository
userRespository domain.UsersRepository
)
if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if userRespository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
comany := domain.Company{
Id: data.Company.Id,
Name: data.Company.Name,
Phone: "",
Logo: data.Company.Logo,
Remarks: data.Company.Remarks,
AdminCompanyId: data.Company.AdminCompanyId,
Status: data.Company.Status,
Enable: domain.CompanyEnableNo, //默认初始化值
}
err = companyRespository.Add(&comany)
if err != nil {
return fmt.Errorf("添加公司数据失败,%s", err)
}
user := domain.Users{
Id: data.User.Id,
CompanyId: data.User.CompanyId,
OpenId: data.User.OpenId,
Name: data.User.Name,
Sex: data.User.Sex,
JobNum: data.User.JobNum,
Phone: data.User.Phone,
PrivatePhone: data.User.PrivatePhone,
Email: data.User.Email,
ExtensionNum: data.User.ExtensionNum,
Workspace: data.User.WorkSpace,
Status: data.User.Status,
Avatar: data.User.Avatar,
Remarks: data.User.Remarks,
ChargeStatus: data.User.ChargeStatus,
Permission: []domain.AdminPermissionBase{}, //初始化权限
AccessPartners: []domain.Partner{}, //默认初始化
}
err = userRespository.Add(&user)
if err != nil {
return fmt.Errorf("添加用户数据失败,%s", err)
}
err = transactionContext.CommitTransaction()
return nil
}
func (service SyncCompanyService) updateCompany(data CompanytData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
companyRespository domain.CompanyRepository
userRespository domain.UsersRepository
oldCompany domain.Company
oldUser domain.Users
)
if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if userRespository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
oldCompany, err = companyRespository.FindOne(domain.CompanyFindOneOptions{
Id: data.Company.Id,
})
oldUser, err = userRespository.FindOne(domain.UsersFindOneQuery{
Id: data.User.Id,
})
oldCompany.Update(map[string]interface{}{
"Name": data.Company.Name,
"Logo": data.Company.Logo,
"Remarks": data.Company.Remarks,
"AdminCompanyId": data.Company.AdminCompanyId,
"Status": data.User.Status,
})
entryTime, _ := time.Parse("2006-01-02", data.User.EntryTime)
oldUser.Update(map[string]interface{}{
"CompanyId": data.User.CompanyId,
"OpenId": data.User.OpenId,
"Name": data.User.Name,
"Sex": data.User.Sex,
"JobNum": data.User.JobNum,
"Phone": data.User.Phone,
"PrivatePhone": data.User.PrivatePhone,
"Email": data.User.Email,
"ExtensionNum": data.User.ExtensionNum,
"Workspace": data.User.WorkSpace,
"Status": data.User.Status,
"Avatar": data.User.Avatar,
"Remarks": data.User.Remarks,
"ChargeStatus": data.User.ChargeStatus,
"EntryTime": entryTime,
})
err = companyRespository.Edit(&oldCompany)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = userRespository.Edit(&oldUser)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
return nil
}
func (service SyncCompanyService) updateCompanyCharge(data CompanyCharge) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
userRespository domain.UsersRepository
)
if userRespository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var oldUsers []domain.Users
_, oldUsers, err = userRespository.Find(domain.UsersFindQuery{
CompanyId: data.Id,
ChargeStatus: domain.UserIsCompanyCharge,
Limit: -1,
})
for i := range oldUsers {
_ = oldUsers[i].Update(map[string]interface{}{
"charge_status": domain.UserIsNotCompanyCharge,
})
err = userRespository.Edit(&oldUsers[i])
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "更新用户的charge_status失败:"+err.Error())
}
}
for _, v := range data.Charge {
var userToUpdate domain.Users
userToUpdate, err := userRespository.FindOne(domain.UsersFindOneQuery{
Id: v,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取用户(id=%d)的数据失败:%s", v, err.Error()))
}
_ = userToUpdate.Update(map[string]interface{}{
"charge_status": domain.UserIsCompanyCharge,
})
err = userRespository.Edit(&userToUpdate)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("更新用户(id=%d)的数据失败:%s", v, err.Error()))
}
}
err = transactionContext.CommitTransaction()
return nil
}