import_controller.go
4.2 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
package controllers
import (
"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"
"strconv"
"strings"
)
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
}
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.Atoi(module.Weight[:sIndex])
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: "",
})
}
nodeContents = append(nodeContents, nc)
}
}
return nodeContents
}