import_controller.go
5.4 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 controllers
import (
"strconv"
"strings"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"github.com/linmadan/egglib-go/web/beego"
"github.com/xuri/excelize/v2"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
)
type ImportController struct {
beego.BaseController
}
func (controller *ImportController) Import() {
_, header, err := controller.GetFile("file")
if err != nil {
controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()))
return
}
file, err := header.Open()
if err != nil {
controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()))
return
}
reader, err := excelize.OpenReader(file)
if err != nil {
controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()))
return
}
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
}
formType := controller.GetString("type")
switch formType {
case "PerformanceDimension":
dimensions, err := domain.LoadPerformanceDimensions(rows)
if err != nil {
controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
}
list := controller.parseTemplateNodeContent(dimensions)
controller.Response(tool_funs.SimpleWrapGridMap(int64(len(list)), list), nil)
default:
controller.Response(nil, application.ThrowError(application.ARG_ERROR, "请确认您导入的表单类型"))
}
}
func (controller *ImportController) parseTemplateNodeContent(data []*domain.PerformanceDimension) []*domain.NodeContent {
nodeContents := make([]*domain.NodeContent, 0)
transactionContext, err := factory.StartTransaction()
if err != nil {
return nodeContents
}
defer func() {
transactionContext.RollbackTransaction()
}()
// 获取当前公司下的默认规则
ua := middlewares.GetUser(controller.Ctx)
// 系统默认规则确认
ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext})
_, rules, err := ruleRepository.Find(map[string]interface{}{"companyId": ua.CompanyId, "sysType": domain.EvaluationSysTypeSystem, "limit": 1})
if err != nil {
return nodeContents
}
var ruleId = int64(0)
if len(rules) == 0 {
newRule := domain.GenerateSysRule(ua.CompanyId) // 生成一个系统默认规则
if rule, err := ruleRepository.Insert(newRule); err != nil {
return nodeContents
} else {
ruleId = rule.Id
}
if err := transactionContext.CommitTransaction(); err != nil {
return nodeContents
}
} else {
ruleId = rules[0].Id
}
// 评估内容主导人名称
evaluatorNames := make([]string, 0)
evaluatorMap := map[string]string{}
for i := range data {
dimension := data[i]
for i2 := range dimension.PerformanceModule {
nc := &domain.NodeContent{}
nc.Category = dimension.Name // 类别
module := dimension.PerformanceModule[i2]
nc.Name = module.ModuleName // 名称
nc.RuleId = ruleId // 规则ID
sIndex := strings.Index(module.Weight, "%") // 权重
if sIndex != -1 {
iWeight, _ := strconv.ParseFloat(module.Weight[:sIndex], 64)
nc.Weight = iWeight
} else {
nc.Weight = 0
}
nc.PromptTitle = "" // 提示项标题
nc.PromptText = module.Standard // 提示项内容
nc.EntryItems = make([]*domain.EntryItem, 0) // 输入项
for i3 := range module.Target {
target := module.Target[i3]
nc.EntryItems = append(nc.EntryItems, &domain.EntryItem{
Title: target.Task, // 输入型标题
HintText: "", // 输入项提示文本
Definition: target.Definition, //定义
})
}
// 没有任何输入项时,默认1个
if len(nc.EntryItems) == 0 {
nc.EntryItems = append(nc.EntryItems, &domain.EntryItem{
Title: "填写反馈",
HintText: "",
})
}
// 必填项
nc.Required = module.Required
// 项目评估人
if module.Evaluator == "HRBP" {
nc.EvaluatorId = -1
} else {
if len(module.Evaluator) > 0 {
evaluatorNames = append(evaluatorNames, module.Evaluator) // 项目评估人名称数组
evaluatorMap[nc.Category+nc.Name] = module.Evaluator // k,v = (类别+名称, 项目评估人名称)
}
}
nodeContents = append(nodeContents, nc)
}
}
if len(evaluatorNames) > 0 {
userRepository := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
_, users, err := userRepository.Find(map[string]interface{}{"companyId": ua.CompanyId, "names": evaluatorNames})
if err != nil {
return nodeContents
}
nameIdMap := map[string]int64{}
for i := range users {
nameIdMap[users[i].Name] = users[i].Id
}
// 名称 -> ID
var nc *domain.NodeContent
for i := range nodeContents {
nc = nodeContents[i]
if nc.EvaluatorId == -1 { // HRBP
continue
}
if evaluatorName, ok := evaluatorMap[nc.Category+nc.Name]; ok {
if userId, ok := nameIdMap[evaluatorName]; ok {
nc.EvaluatorId = userId
}
}
}
}
return nodeContents
}