service.go
7.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
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/excel"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/domainService"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
"strconv"
"time"
)
type ExcelDataService struct {
}
func NewExcelDataService(param map[string]interface{}) *ExcelDataService {
return &ExcelDataService{}
}
// ExportCompanyUser 导出公司用户信息列表
func (srv ExcelDataService) ExportCompanyUser(companyUserListQuery *query.CompanyUserListQuery) (ExportCompanyUserData, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(companyUserListQuery.Operator)
result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
Limit: 10000,
CompanyId: companyUserListQuery.Operator.CompanyId,
OrganizationId: 0,
DepartmentId: 0,
UserName: companyUserListQuery.UserName,
DepName: companyUserListQuery.DepartmentName,
Phone: "",
UserType: domain.UserTypeEmployee,
InOrgIds: companyUserListQuery.Operator.OrgIds,
})
if err != nil {
return ExportCompanyUserData{}, fmt.Errorf("获取企业用户数据失败:%w", err)
}
return ExportCompanyUserData(result.Users), nil
}
// ExportCooperationUser 导出共创用户信息列表
func (srv ExcelDataService) ExportCooperationUser(companyUserListQuery *query.CompanyUserListQuery) (ExportCooperationUserData, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(companyUserListQuery.Operator)
result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
Limit: 10000,
CompanyId: companyUserListQuery.Operator.CompanyId,
OrganizationId: 0,
DepartmentId: 0,
UserName: companyUserListQuery.UserName,
DepName: companyUserListQuery.DepartmentName,
Phone: "",
UserType: domain.UserTypeCooperation,
InOrgIds: companyUserListQuery.Operator.OrgIds,
})
if err != nil {
return ExportCooperationUserData{}, fmt.Errorf("获取企业用户数据失败:%w", err)
}
return ExportCooperationUserData(result.Users), nil
}
// ImportCompanyUser 导入公司用户信息
func (srv ExcelDataService) ImportCompanyUser(importDataCommand *command.ImportDataCommand) (interface{}, error) {
initPassword, _, err := domainService.GetInitPassword(importDataCommand.Operator)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
excelImport := excel.NewExcelImport()
excelImport.RowBegin = 2 //第二行开始读取
excelImport.DataFields = []excel.DataField{
{EnName: "UserCode", CnName: "*用户编号"},
{EnName: "UserName", CnName: "*用户姓名"},
{EnName: "OrganizationID", CnName: "*组织机构"},
{EnName: "DepartmentID", CnName: "*所属部门"},
{EnName: "Phone", CnName: "*手机号"},
{EnName: "Email", CnName: "邮箱"},
{EnName: "Status", CnName: "状态"},
}
excelData, err := excelImport.OpenExcelFromIoReader(importDataCommand.Reader)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
users := make([]allied_creation_user.BatchAddUserItem, 0)
for _, v := range excelData {
item := allied_creation_user.BatchAddUserItem{
CompanyID: importDataCommand.Operator.CompanyId,
UserType: domain.UserTypeEmployee,
UserCode: v["UserCode"],
OrganizationID: 0,
DepartmentID: 0,
UserInfo: allied_creation_user.BatchAddUserItemUserInfo{
UserName: v["UserName"],
Phone: v["Phone"],
Email: v["Email"],
},
CooperationInfo: allied_creation_user.BatchAddUserItemCooperationInfo{},
}
item.OrganizationID, err = strconv.Atoi(v["OrganizationID"])
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, fmt.Sprintf("用户:%v 组织值:%v 类型无效(数值类型)", item.UserInfo.UserName, item.OrganizationID))
}
item.DepartmentID, err = strconv.Atoi(v["DepartmentID"])
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, fmt.Sprintf("用户:%v 部门值:%v 类型无效(数值类型)", item.UserInfo.UserName, item.DepartmentID))
}
users = append(users, item)
}
userGateway := allied_creation_user.NewHttplibAlliedCreationUser(importDataCommand.Operator)
result, err := userGateway.UserBatchAdd(allied_creation_user.ReqBatchAddUser{
Users: users,
Password: initPassword,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return result, nil
}
// ImportCooperationUser 导入共创用户信息
func (srv ExcelDataService) ImportCooperationUser(importDataCommand *command.ImportDataCommand) (interface{}, error) {
initPassword, _, err := domainService.GetInitPassword(importDataCommand.Operator)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
excelImport := excel.NewExcelImport()
excelImport.RowBegin = 2 //第二行开始读取
excelImport.DataFields = []excel.DataField{
{EnName: "UserCode", CnName: "*用户编号"},
{EnName: "UserName", CnName: "*用户姓名"},
{EnName: "CooperationCompany", CnName: "*合伙公司"},
{EnName: "CooperationDeadline", CnName: "*合伙到期"},
{EnName: "Phone", CnName: "*手机号"},
{EnName: "Email", CnName: "邮箱"},
}
excelData, err := excelImport.OpenExcelFromIoReader(importDataCommand.Reader)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
users := make([]allied_creation_user.BatchAddUserItem, 0)
var fail int
for _, v := range excelData {
item := allied_creation_user.BatchAddUserItem{
CompanyID: importDataCommand.Operator.CompanyId,
UserType: domain.UserTypeCooperation,
UserCode: v["UserCode"],
OrganizationID: int(importDataCommand.Operator.OrgId),
UserInfo: allied_creation_user.BatchAddUserItemUserInfo{
UserName: v["UserName"],
Phone: v["Phone"],
Email: v["Email"],
},
CooperationInfo: allied_creation_user.BatchAddUserItemCooperationInfo{
CooperationCompany: v["CooperationCompany"],
},
}
item.CooperationInfo.CooperationDeadline, err = time.Parse("2006-01-02", v["CooperationDeadline"])
if err != nil {
fail++
continue
}
users = append(users, item)
}
userGateway := allied_creation_user.NewHttplibAlliedCreationUser(importDataCommand.Operator)
result, err := userGateway.UserBatchAdd(allied_creation_user.ReqBatchAddUser{
Users: users,
Password: initPassword,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return result, nil
}