export.go
5.8 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
package service
import (
"fmt"
"strconv"
"github.com/linmadan/egglib-go/core/application"
"github.com/xuri/excelize/v2"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
roleService "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/summary_evaluation/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
// 导出数据
// 综合管理-周期评估
func (srv *SummaryEvaluationService) ExportAllEvaluationSuper(param *command.QueryEvaluationList) (*excelize.File, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
//判断是否是hrbp
flagHrbp, err := roleService.GetHRBP(transactionContext, param.CompanyId, param.UserId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if flagHrbp != 1 {
return nil, application.ThrowError(application.BUSINESS_ERROR, "暂无数据")
}
//判断是否是上级
userRepo := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
_, parentUser, _ := userRepo.Find(map[string]interface{}{
"parentId": param.UserId,
"limit": 1,
})
if len(parentUser) == 0 && flagHrbp != 1 {
return nil, application.ThrowError(application.BUSINESS_ERROR, "暂无数据")
}
evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
positionRepo := factory.CreatePositionRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
limit := param.PageSize
offset := (param.PageNumber - 1) * param.PageSize
//获取评估列表信息
condition1 := map[string]interface{}{
"cycleId": param.CycleId,
"types": int(domain.EvaluationSuper),
"limit": limit,
}
if offset > 0 {
condition1["offset"] = offset
}
if len(param.TargetUserName) > 0 {
condition1["targetUserName"] = "%" + param.TargetUserName + "%"
}
if len(param.SummaryEvaluationId) > 0 {
summaryEvaluationIds := []int{}
for _, v := range param.SummaryEvaluationId {
id, _ := strconv.Atoi(v)
summaryEvaluationIds = append(summaryEvaluationIds, id)
}
if len(summaryEvaluationIds) > 0 {
condition1["id"] = summaryEvaluationIds
}
}
//获取评估列表信息
_, evaluationList, err := evaluationRepo.Find(condition1)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
targetUserIds := []int{}
//获取员工信息
userMap := map[int64]*domain.User{}
for _, v := range evaluationList {
if _, ok := userMap[int64(v.TargetUser.UserId)]; ok {
continue
}
userMap[int64(v.TargetUser.UserId)] = nil
targetUserIds = append(targetUserIds, v.TargetUser.UserId)
}
if len(targetUserIds) > 0 {
_, userList, err := userRepo.Find(map[string]interface{}{
"ids": targetUserIds,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
userMap = map[int64]*domain.User{}
for _, v := range userList {
userMap[v.Id] = v
}
}
var positionIds []int
//获取职位列表
positionMap := map[int64]*domain.Position{}
for _, v := range userMap {
for _, v2 := range v.DepartmentId {
if _, ok := positionMap[int64(v2)]; ok {
continue
}
positionMap[int64(v2)] = nil
positionIds = append(positionIds, v2)
}
}
if len(positionIds) > 0 {
_, positionList, err := positionRepo.Find(map[string]interface{}{"ids": positionIds})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取部门信息"+err.Error())
}
positionMap = map[int64]*domain.Position{}
for _, v := range positionList {
positionMap[v.Id] = v
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
ratingHeader := []string{} //动态列,评级内容
ratingMap := map[string]struct{}{} //过滤重复code
evaluationRatingMap := map[int]map[string]int{}
for _, v := range evaluationList {
evaluationRatingMap[v.Id] = map[string]int{}
for _, v2 := range v.TotalRating {
if _, ok := ratingMap[v2.Code]; !ok {
ratingMap[v2.Code] = struct{}{}
ratingHeader = append(ratingHeader, v2.Code)
}
evaluationRatingMap[v.Id][v2.Code] = v2.Number
}
}
xlsxFile := excelize.NewFile()
//设置默认的第一个sheet
sheetIndex := xlsxFile.GetActiveSheetIndex()
firstSheetName := xlsxFile.GetSheetName(sheetIndex)
tableHead := []string{"姓名", "部门", "职位", "最终绩效得分"}
tableHead = append(tableHead, ratingHeader...)
xlsxFile.SetSheetRow(firstSheetName, "A1", &tableHead)
for i, v := range evaluationList {
departmentName := ""
for _, dep := range v.TargetDepartment {
departmentName += dep.DepartmentName + " "
}
//填充员工信息
positinName := ""
if targetUser, ok := userMap[int64(v.TargetUser.UserId)]; ok {
//填充职位信息
for _, positionId := range targetUser.PositionId {
if position, ok := positionMap[int64(positionId)]; ok {
positinName += position.Name + " "
}
}
}
dataRaw := []string{
v.TargetUser.UserName,
departmentName,
positinName,
v.TotalScore,
}
for _, v2 := range ratingHeader {
if num, ok := evaluationRatingMap[v.Id][v2]; ok {
dataRaw = append(dataRaw, fmt.Sprintf("%d", num))
} else {
dataRaw = append(dataRaw, "0")
}
}
xlsxFile.SetSheetRow(firstSheetName, fmt.Sprintf("A%d", i+2), &dataRaw)
}
return xlsxFile, nil
}