performance_application_form.go 7.8 KB
package domain

import (
	"errors"
	"strings"
)

type PerformanceApplicationForm struct {
	DimensionName string // 维度名称
	ModuleName    string // 模块名称
	Weight        string // 权重
	Standard      string // 标准
	Task          string // 任务、指标
	Definition    string // 定义
	Required      string // 是否必填 是 否
	Evaluator     string // 项目评估人
	IndicatorType string // 指标类型(任务 )
}

type PerformanceDimension struct {
	Name              string               `json:"name"`              // 维度名称
	PerformanceModule []*PerformanceModule `json:"performanceModule"` // 模块
}

type PerformanceModule struct {
	ModuleName    string               `json:"moduleName"`        // 模块名称
	Weight        string               `json:"weight"`            // 权重
	Standard      string               `json:"standard"`          // 标准(结构化的成果描述)
	Target        []*PerformanceTarget `json:"performanceTarget"` // 任务\指标
	Required      int                  `json:"required"`          // 是否必填
	Evaluator     string               `json:"evaluator"`         // 项目评估人
	IndicatorType int                  `json:"indicatorType"`     // 指标类型(任务 )
}

type PerformanceTarget struct {
	Task       string `json:"task"`
	Definition string `json:"definition"`
}

func LoadPerformanceDimensions(rows [][]string) ([]*PerformanceDimension, error) {
	formRows := make([]*PerformanceApplicationForm, 0)
	var dimensionName, moduleName, taskName, weightName, standardName, evaluator string
	required := "是"
	for key, item := range rows {
		if key < 3 {
			continue
		}
		if len(item) >= 1 {
			if item[0] == "部门长审核" || strings.Contains(item[0], "审核") {
				break
			}
		}
		form := &PerformanceApplicationForm{}
		//维度名称
		if len(item) > 1 && item[1] != "" {
			dimensionName = item[1]
		}
		form.DimensionName = dimensionName
		//模块名称
		if len(item) > 3 && item[3] != "" {
			moduleName = item[3]
		}
		form.ModuleName = moduleName
		//指标任务
		if len(item) > 4 && item[4] != "" {
			taskName = item[4]
		} else {
			taskName = "填写自评反馈"
		}
		form.Task = taskName
		//定义
		if len(item) > 5 {
			form.Definition = strings.TrimSpace(item[5])
		}
		//权重
		if len(item) > 6 && item[6] != "" {
			weightName = item[6]
		}
		form.Weight = weightName
		//标准
		if key < len(rows)-1 {
			if len(rows[key+1]) > 1 && rows[key+1][1] != "" {
				standardName = ""
			}
		}
		if len(item) > 7 && item[7] != "" {
			standardName = item[7]
		}
		form.Standard = standardName
		//是否必填
		if len(item) > 9 && item[9] != "" {
			required = strings.TrimSpace(item[9])
		}
		form.Required = required

		// 项目评估人
		if len(item) > 10 && item[10] != "" {
			evaluator = strings.TrimSpace(item[10])
		}
		form.Evaluator = evaluator

		formRows = append(formRows, form)
	}
	dimensions := make([]*PerformanceDimension, 0)
	//按维度分
	mapData := make(map[string][]*PerformanceApplicationForm)
	//维度顺序
	sortBy := make([]string, 0)
	for _, item := range formRows {
		if _, ok := mapData[item.DimensionName]; !ok {
			mapData[item.DimensionName] = make([]*PerformanceApplicationForm, 0)
			sortBy = append(sortBy, item.DimensionName)
		}
		mapData[item.DimensionName] = append(mapData[item.DimensionName], item)
	}
	for dimensionName, item := range mapData {
		dimension := &PerformanceDimension{
			Name: dimensionName,
		}
		modules, err := loadPerformanceModule(item)
		if err != nil {
			return dimensions, err
		}
		dimension.PerformanceModule = modules
		dimensions = append(dimensions, dimension)
	}
	result := make([]*PerformanceDimension, 0)
	for _, name := range sortBy {
		for _, item := range dimensions {
			if name == item.Name {
				result = append(result, item)
			}
		}
	}
	return result, nil
}

// 筛选出模块
func loadPerformanceModule(forms []*PerformanceApplicationForm) ([]*PerformanceModule, error) {
	mapData := make(map[string][]*PerformanceApplicationForm)
	sortBy := make([]string, 0)
	for _, item := range forms {
		if _, ok := mapData[item.ModuleName]; !ok {
			mapData[item.ModuleName] = make([]*PerformanceApplicationForm, 0)
			sortBy = append(sortBy, item.ModuleName)
		}
		mapData[item.ModuleName] = append(mapData[item.ModuleName], item)
	}
	modules := make([]*PerformanceModule, 0)
	for moduleName, item := range mapData {
		if moduleName == "" {
			return modules, errors.New(item[0].DimensionName + "对应的模块不能为空")
		}
		weightName, err := getWeight(item)
		if err != nil {
			return modules, err
		}
		standardName, err := getStandard(item)
		if err != nil {
			return modules, err
		}
		tasks, err := getTasks(item)
		if err != nil {
			return modules, err
		}
		required, err := getRequired(item)
		if err != nil {
			return modules, err
		}
		evaluator, err := getEvaluator(item)
		if err != nil {
			return modules, err
		}
		indicatorType, err := getIndicatorType(item)
		if err != nil {
			return modules, err
		}

		module := &PerformanceModule{
			ModuleName:    moduleName,
			Weight:        weightName,
			Standard:      standardName,
			Target:        tasks,
			Required:      required,
			Evaluator:     evaluator,
			IndicatorType: indicatorType,
		}
		modules = append(modules, module)
	}
	result := make([]*PerformanceModule, 0)

	for _, name := range sortBy {
		for _, item := range modules {
			if name == item.ModuleName {
				result = append(result, item)
			}
		}
	}
	return result, nil
}

// 获取权重
func getWeight(items []*PerformanceApplicationForm) (string, error) {
	if len(items) <= 0 {
		return "", nil
	}
	var name string
	for _, item := range items {
		if name == "" {
			name = item.Weight
		}
		if name != item.Weight {
			return "", errors.New(item.ModuleName + " 对应的权重值不一致")
		}
	}
	if name == "/" {
		name = "0%"
	}
	return name, nil
}

// 获取标准
func getStandard(items []*PerformanceApplicationForm) (string, error) {
	if len(items) <= 0 {
		return "", nil
	}
	var name string
	for _, item := range items {
		if name == "" {
			name = item.Standard
		}
		if name != item.Standard {
			return "", errors.New(item.ModuleName + " 对应的标准值不一致")
		}
	}
	return name, nil
}

// 获取是否必填
func getRequired(items []*PerformanceApplicationForm) (int, error) {
	if len(items) <= 0 {
		return NodeRequiredYes, nil
	}
	var name string
	for _, item := range items {
		if name == "" {
			name = item.Required
		}
		if name != item.Required {
			return NodeRequiredYes, errors.New(item.ModuleName + " 对应的是否必填不一致")
		}
	}
	if name == "否" {
		return NodeRequiredNo, nil
	} else {
		return NodeRequiredYes, nil
	}
}

// 获取项目评估人
func getEvaluator(items []*PerformanceApplicationForm) (string, error) {
	if len(items) <= 0 {
		return "", nil
	}
	var prevName string
	for _, item := range items {
		if prevName == "" {
			prevName = item.Evaluator
		}
		if prevName != item.Evaluator {
			return "", errors.New(item.ModuleName + " 对应的项目评估人填写不一致")
		}
	}
	if prevName == "/" {
		prevName = ""
	}
	return prevName, nil
}

// 获取指标类型
func getIndicatorType(items []*PerformanceApplicationForm) (int, error) {
	if len(items) <= 0 {
		return IndicatorTypeDef, nil
	}
	var prevName string
	for _, item := range items {
		if prevName == "" {
			prevName = item.IndicatorType
		}
		if prevName != item.IndicatorType {
			return IndicatorTypeDef, errors.New(item.ModuleName + " 对应的指标类型填写不一致")
		}
	}
	if prevName == "任务" {
		return IndicatorTypeTask, nil
	} else {
		return IndicatorTypeDef, nil
	}
}

// 获取任务
func getTasks(items []*PerformanceApplicationForm) ([]*PerformanceTarget, error) {
	tasks := make([]*PerformanceTarget, 0)
	for _, item := range items {
		target := &PerformanceTarget{Task: item.Task, Definition: item.Definition}
		tasks = append(tasks, target)
	}
	return tasks, nil
}