user_controller.go
4.5 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
package controllers
import (
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/web/beego"
"github.com/xuri/excelize/v2"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/adapter"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/query"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
"strings"
)
type UserController struct {
beego.BaseController
}
// ListUsers 搜索用户
func (controller *UserController) ListUsers() {
listUserQuery := &query.ListUserQuery{}
_ = controller.Unmarshal(listUserQuery)
userAuth := controller.Ctx.Input.GetData(domain.UserAuth{}).(*domain.UserAuth)
listUserQuery.CompanyId = userAuth.CompanyId
resp, err := (&user.UserService{}).ListUsers(listUserQuery)
controller.Response(resp, err)
}
func (controller *UserController) ListByDepartment() {
in := &query.ListByDepartmentQuery{}
if err := controller.Unmarshal(in); err != nil {
controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
} else {
ua := middlewares.GetUser(controller.Ctx)
in.CompanyId = ua.CompanyId
controller.Response((&user.UserService{}).ListByDepartment(in))
}
}
func (controller *UserController) EditParentUser() {
in := &command.EditParentCommand{}
if err := controller.Unmarshal(in); err != nil {
controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
} else {
ua := middlewares.GetUser(controller.Ctx)
in.CompanyId = int(ua.CompanyId)
in.OperatorId = int(ua.UserId)
controller.Response(nil, (&user.UserService{}).EditParentUser(in))
}
}
// ImportParentUser 导入用户上级
func (controller *UserController) ImportParentUser() {
in := &command.ImportParentUserCommand{}
if err := controller.Unmarshal(in); err != nil {
controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
} else {
if itcArray, err := controller.readExcelFormUserParent(); err != nil {
controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
} else {
ua := middlewares.GetUser(controller.Ctx)
in.CompanyId = int(ua.CompanyId)
in.OperatorId = int(ua.UserId)
in.Data = itcArray
if data, err := (&user.UserService{}).ImportParentUser(in); nil != err {
controller.Response(nil, err)
} else {
controller.Response(data, nil)
}
}
}
}
func (controller *UserController) readExcelFormUserParent() ([]adapter.ImportParentUser, error) {
// 读取文件
_, header, err := controller.GetFile("file")
if err != nil {
controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()))
return nil, err
}
file, err := header.Open()
if err != nil {
controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()))
return nil, err
}
defer func() {
if err := file.Close(); err != nil {
return
}
}()
reader, err := excelize.OpenReader(file)
if err != nil {
controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()))
return nil, err
}
index := reader.GetActiveSheetIndex()
rows, err := reader.GetRows(reader.GetSheetName(index))
if err != nil {
controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "读取excel错误:"+err.Error()))
return nil, err
}
if len(rows) <= 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "文件内数据不能为空:"+err.Error())
}
adapters := make([]adapter.ImportParentUser, 0)
for rowIndex, row := range rows {
if rowIndex < 2 { // 头2行不读取
continue
}
ipu := adapter.ImportParentUser{}
for colIndex, colCell := range row {
switch colIndex {
case 0:
ipu.Name = strings.TrimSpace(colCell) // 员工名称
case 1:
ipu.Phone = strings.TrimSpace(colCell) // 员工手机号码
case 2:
ipu.ParentName = strings.TrimSpace(colCell) // 直接上级名称
case 3:
ipu.ParentPhone = strings.TrimSpace(colCell) // 直接上级手机号码
}
}
adapters = append(adapters, ipu)
}
if len(adapters) <= 0 {
return adapters, application.ThrowError(application.INTERNAL_SERVER_ERROR, "文件内数据不能为空:"+err.Error())
}
return adapters, err
}