作者 郑周

1. 导入时,校验总权重值 == 100%

2. 提交评估内容时,校验总权重优化
@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "fmt" 4 "fmt"
5 "github.com/beego/beego/v2/core/validation" 5 "github.com/beego/beego/v2/core/validation"
6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" 6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
7 ) 8 )
8 9
9 type UpdateTemplateCommand struct { 10 type UpdateTemplateCommand struct {
@@ -34,7 +35,8 @@ func (in *UpdateTemplateCommand) Valid(validation *validation.Validation) { @@ -34,7 +35,8 @@ func (in *UpdateTemplateCommand) Valid(validation *validation.Validation) {
34 weightTotal += linkNode.NodeContents[i2].Weight 35 weightTotal += linkNode.NodeContents[i2].Weight
35 } 36 }
36 if weightTotal != 100 { 37 if weightTotal != 100 {
37 - validation.SetError("linkNodes", fmt.Sprintf("权重错误,当前%s的总权重值为:%f(必须等于100)", linkNode.Name, weightTotal)) 38 + formatWeightTotal := utils.FormatFloatDecimal(weightTotal, 2)
  39 + validation.SetError("linkNodes", fmt.Sprintf("权重错误,当前%s的总权重值为:%s%%(必须等于100%%)", linkNode.Name, formatWeightTotal))
38 return 40 return
39 } 41 }
40 } 42 }
1 package controllers 1 package controllers
2 2
3 import ( 3 import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
4 "strconv" 6 "strconv"
5 "strings" 7 "strings"
6 8
@@ -46,19 +48,22 @@ func (controller *ImportController) Import() { @@ -46,19 +48,22 @@ func (controller *ImportController) Import() {
46 if err != nil { 48 if err != nil {
47 controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error())) 49 controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
48 } 50 }
49 - list := controller.parseTemplateNodeContent(dimensions) 51 + if err, list := controller.parseTemplateNodeContent(dimensions); err != nil {
  52 + controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
  53 + } else {
50 controller.Response(tool_funs.SimpleWrapGridMap(int64(len(list)), list), nil) 54 controller.Response(tool_funs.SimpleWrapGridMap(int64(len(list)), list), nil)
  55 + }
51 default: 56 default:
52 controller.Response(nil, application.ThrowError(application.ARG_ERROR, "请确认您导入的表单类型")) 57 controller.Response(nil, application.ThrowError(application.ARG_ERROR, "请确认您导入的表单类型"))
53 } 58 }
54 } 59 }
55 60
56 -func (controller *ImportController) parseTemplateNodeContent(data []*domain.PerformanceDimension) []*domain.NodeContent { 61 +func (controller *ImportController) parseTemplateNodeContent(data []*domain.PerformanceDimension) (error, []*domain.NodeContent) {
57 nodeContents := make([]*domain.NodeContent, 0) 62 nodeContents := make([]*domain.NodeContent, 0)
58 63
59 transactionContext, err := factory.StartTransaction() 64 transactionContext, err := factory.StartTransaction()
60 if err != nil { 65 if err != nil {
61 - return nodeContents 66 + return err, nodeContents
62 } 67 }
63 defer func() { 68 defer func() {
64 transactionContext.RollbackTransaction() 69 transactionContext.RollbackTransaction()
@@ -70,18 +75,18 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf @@ -70,18 +75,18 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf
70 ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext}) 75 ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext})
71 _, rules, err := ruleRepository.Find(map[string]interface{}{"companyId": ua.CompanyId, "sysType": domain.EvaluationSysTypeSystem, "limit": 1}) 76 _, rules, err := ruleRepository.Find(map[string]interface{}{"companyId": ua.CompanyId, "sysType": domain.EvaluationSysTypeSystem, "limit": 1})
72 if err != nil { 77 if err != nil {
73 - return nodeContents 78 + return err, nodeContents
74 } 79 }
75 var ruleId = int64(0) 80 var ruleId = int64(0)
76 if len(rules) == 0 { 81 if len(rules) == 0 {
77 newRule := domain.GenerateSysRule(ua.CompanyId) // 生成一个系统默认规则 82 newRule := domain.GenerateSysRule(ua.CompanyId) // 生成一个系统默认规则
78 if rule, err := ruleRepository.Insert(newRule); err != nil { 83 if rule, err := ruleRepository.Insert(newRule); err != nil {
79 - return nodeContents 84 + return err, nodeContents
80 } else { 85 } else {
81 ruleId = rule.Id 86 ruleId = rule.Id
82 } 87 }
83 if err := transactionContext.CommitTransaction(); err != nil { 88 if err := transactionContext.CommitTransaction(); err != nil {
84 - return nodeContents 89 + return err, nodeContents
85 } 90 }
86 } else { 91 } else {
87 ruleId = rules[0].Id 92 ruleId = rules[0].Id
@@ -91,6 +96,7 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf @@ -91,6 +96,7 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf
91 evaluatorNames := make([]string, 0) 96 evaluatorNames := make([]string, 0)
92 evaluatorMap := map[string]string{} 97 evaluatorMap := map[string]string{}
93 98
  99 + weightTotal := 0.0
94 for i := range data { 100 for i := range data {
95 dimension := data[i] 101 dimension := data[i]
96 for i2 := range dimension.PerformanceModule { 102 for i2 := range dimension.PerformanceModule {
@@ -107,6 +113,7 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf @@ -107,6 +113,7 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf
107 } else { 113 } else {
108 nc.Weight = 0 114 nc.Weight = 0
109 } 115 }
  116 + weightTotal += nc.Weight // 总权重
110 nc.PromptTitle = "" // 提示项标题 117 nc.PromptTitle = "" // 提示项标题
111 nc.PromptText = module.Standard // 提示项内容 118 nc.PromptText = module.Standard // 提示项内容
112 nc.EntryItems = make([]*domain.EntryItem, 0) // 输入项 119 nc.EntryItems = make([]*domain.EntryItem, 0) // 输入项
@@ -132,7 +139,6 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf @@ -132,7 +139,6 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf
132 if module.Evaluator == "HRBP" { 139 if module.Evaluator == "HRBP" {
133 nc.EvaluatorId = -1 140 nc.EvaluatorId = -1
134 } else { 141 } else {
135 -  
136 if len(module.Evaluator) > 0 { 142 if len(module.Evaluator) > 0 {
137 evaluatorNames = append(evaluatorNames, module.Evaluator) // 项目评估人名称数组 143 evaluatorNames = append(evaluatorNames, module.Evaluator) // 项目评估人名称数组
138 evaluatorMap[nc.Category+nc.Name] = module.Evaluator // k,v = (类别+名称, 项目评估人名称) 144 evaluatorMap[nc.Category+nc.Name] = module.Evaluator // k,v = (类别+名称, 项目评估人名称)
@@ -143,11 +149,17 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf @@ -143,11 +149,17 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf
143 } 149 }
144 } 150 }
145 151
  152 + // 权重总数不等于100%,提示报错
  153 + if weightTotal != 100 {
  154 + sprintf := fmt.Sprintf("当前导入的总权重值为:%s%%(必须等于100%%)", utils.FormatFloatDecimal(weightTotal, 2))
  155 + return application.ThrowError(application.INTERNAL_SERVER_ERROR, sprintf), nodeContents
  156 + }
  157 +
146 if len(evaluatorNames) > 0 { 158 if len(evaluatorNames) > 0 {
147 userRepository := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext}) 159 userRepository := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
148 _, users, err := userRepository.Find(map[string]interface{}{"companyId": ua.CompanyId, "names": evaluatorNames}) 160 _, users, err := userRepository.Find(map[string]interface{}{"companyId": ua.CompanyId, "names": evaluatorNames})
149 if err != nil { 161 if err != nil {
150 - return nodeContents 162 + return err, nodeContents
151 } 163 }
152 nameIdMap := map[string]int64{} 164 nameIdMap := map[string]int64{}
153 for i := range users { 165 for i := range users {
@@ -169,5 +181,5 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf @@ -169,5 +181,5 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf
169 } 181 }
170 } 182 }
171 183
172 - return nodeContents 184 + return nil, nodeContents
173 } 185 }
@@ -7,6 +7,7 @@ import ( @@ -7,6 +7,7 @@ import (
7 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" 7 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
8 "math" 8 "math"
9 "reflect" 9 "reflect"
  10 + "strconv"
10 "strings" 11 "strings"
11 "time" 12 "time"
12 ) 13 )
@@ -36,6 +37,16 @@ func ValidateCommand(commandType interface{}) error { @@ -36,6 +37,16 @@ func ValidateCommand(commandType interface{}) error {
36 return nil 37 return nil
37 } 38 }
38 39
  40 +// FormatFloatDecimal 格式化小数点位数
  41 +func FormatFloatDecimal(num float64, decimal int) string {
  42 + d := float64(1)
  43 + if decimal > 0 {
  44 + d = math.Pow10(decimal) // 10的N次方
  45 + }
  46 + // math.trunc作用就是返回浮点数的整数部分
  47 + return strconv.FormatFloat(math.Trunc(num*d)/d, 'f', -1, 64)
  48 +}
  49 +
39 // NextTime 0点时刻为标准计算 50 // NextTime 0点时刻为标准计算
40 func NextTime(now0 time.Time, start time.Time, kpiCycle int) time.Time { 51 func NextTime(now0 time.Time, start time.Time, kpiCycle int) time.Time {
41 year, month, day := start.Date() 52 year, month, day := start.Date()