performance_application_form.go 5.4 KB
package domain

import (
	"errors"
	"strings"
)

type PerformanceApplicationForm struct {
	DimensionName string //维度名称
	ModuleName    string //模块名称
	Weight        string //权重
	Standard      string //标准
	Task          string //任务、指标
	Definition    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"` //任务\指标
}

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 string
	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 len(item) > 7 && item[7] != "" {
			standardName = item[7]
		}
		if key < len(rows)-1 {
			if len(rows[key+1]) > 1 && rows[key+1][1] != "" {
				standardName = ""
			}
		}
		form.Standard = standardName
		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
		}
		module := &PerformanceModule{
			ModuleName: moduleName,
			Weight:     weightName,
			Standard:   standardName,
			Target:     tasks,
		}
		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 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
}