作者 郑周

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

2. 提交评估内容时,校验总权重优化
... ... @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
)
type UpdateTemplateCommand struct {
... ... @@ -34,7 +35,8 @@ func (in *UpdateTemplateCommand) Valid(validation *validation.Validation) {
weightTotal += linkNode.NodeContents[i2].Weight
}
if weightTotal != 100 {
validation.SetError("linkNodes", fmt.Sprintf("权重错误,当前%s的总权重值为:%f(必须等于100)", linkNode.Name, weightTotal))
formatWeightTotal := utils.FormatFloatDecimal(weightTotal, 2)
validation.SetError("linkNodes", fmt.Sprintf("权重错误,当前%s的总权重值为:%s%%(必须等于100%%)", linkNode.Name, formatWeightTotal))
return
}
}
... ...
package controllers
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
"strconv"
"strings"
... ... @@ -46,19 +48,22 @@ func (controller *ImportController) Import() {
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)
if err, list := controller.parseTemplateNodeContent(dimensions); err != nil {
controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
} else {
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 {
func (controller *ImportController) parseTemplateNodeContent(data []*domain.PerformanceDimension) (error, []*domain.NodeContent) {
nodeContents := make([]*domain.NodeContent, 0)
transactionContext, err := factory.StartTransaction()
if err != nil {
return nodeContents
return err, nodeContents
}
defer func() {
transactionContext.RollbackTransaction()
... ... @@ -70,18 +75,18 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf
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
return err, nodeContents
}
var ruleId = int64(0)
if len(rules) == 0 {
newRule := domain.GenerateSysRule(ua.CompanyId) // 生成一个系统默认规则
if rule, err := ruleRepository.Insert(newRule); err != nil {
return nodeContents
return err, nodeContents
} else {
ruleId = rule.Id
}
if err := transactionContext.CommitTransaction(); err != nil {
return nodeContents
return err, nodeContents
}
} else {
ruleId = rules[0].Id
... ... @@ -91,6 +96,7 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf
evaluatorNames := make([]string, 0)
evaluatorMap := map[string]string{}
weightTotal := 0.0
for i := range data {
dimension := data[i]
for i2 := range dimension.PerformanceModule {
... ... @@ -107,6 +113,7 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf
} else {
nc.Weight = 0
}
weightTotal += nc.Weight // 总权重
nc.PromptTitle = "" // 提示项标题
nc.PromptText = module.Standard // 提示项内容
nc.EntryItems = make([]*domain.EntryItem, 0) // 输入项
... ... @@ -132,7 +139,6 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf
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 = (类别+名称, 项目评估人名称)
... ... @@ -143,11 +149,17 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf
}
}
// 权重总数不等于100%,提示报错
if weightTotal != 100 {
sprintf := fmt.Sprintf("当前导入的总权重值为:%s%%(必须等于100%%)", utils.FormatFloatDecimal(weightTotal, 2))
return application.ThrowError(application.INTERNAL_SERVER_ERROR, sprintf), nodeContents
}
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
return err, nodeContents
}
nameIdMap := map[string]int64{}
for i := range users {
... ... @@ -169,5 +181,5 @@ func (controller *ImportController) parseTemplateNodeContent(data []*domain.Perf
}
}
return nodeContents
return nil, nodeContents
}
... ...
... ... @@ -7,6 +7,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"math"
"reflect"
"strconv"
"strings"
"time"
)
... ... @@ -36,6 +37,16 @@ func ValidateCommand(commandType interface{}) error {
return nil
}
// FormatFloatDecimal 格式化小数点位数
func FormatFloatDecimal(num float64, decimal int) string {
d := float64(1)
if decimal > 0 {
d = math.Pow10(decimal) // 10的N次方
}
// math.trunc作用就是返回浮点数的整数部分
return strconv.FormatFloat(math.Trunc(num*d)/d, 'f', -1, 64)
}
// NextTime 0点时刻为标准计算
func NextTime(now0 time.Time, start time.Time, kpiCycle int) time.Time {
year, month, day := start.Date()
... ...