审查视图

pkg/application/staff_assess/service/export_data_1.go 6.3 KB
Your Name authored
1 2 3 4 5 6 7 8 9
package service

import (
	"fmt"
	"strings"

	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
)
tangxvhui authored
10
// 员工绩效-综合管理-导出绩效-个人
Your Name authored
11 12

// excel表头部字段
13
type HeaderLevel struct {
Your Name authored
14 15 16
	Name   string         `json:"name"`
	Filter map[string]int `json:"-"`
	Child  []HeaderLevel  `json:"child"`
Your Name authored
17 18 19
}

// 添加下一层级的字段
20
func (h *HeaderLevel) addChild(name string) (child *HeaderLevel) {
Your Name authored
21 22
	cIndex, ok := h.Filter[name]
	if !ok {
23
		h.Child = append(h.Child, HeaderLevel{
Your Name authored
24 25
			Name:   name,
			Filter: map[string]int{},
26
			Child:  []HeaderLevel{},
Your Name authored
27 28 29 30 31 32 33 34
		})
		h.Filter[name] = len(h.Child) - 1
		cIndex = h.Filter[name]
	}
	return &h.Child[cIndex]
}

type exportData struct {
tangxvhui authored
35 36
	userName    []string                    //员工的名称列表 ,对应excel文件的多个sheet
	usrIdMap    map[string]string           //员工id对应的名字
Your Name authored
37
	userDayMap  map[string][]string         //每个员工对应的日期列表  key=员工名称 value= 日期列表
38
	tableHeader map[string]*HeaderLevel     //每个员工数据表格对应表头 key=员工名称
Your Name authored
39 40 41 42 43
	data        map[string]*strings.Builder //每个员工表头对应的评估填写的数据 key=员工名称+日期+表头
	data2       map[string]string           //每个员工评估项的标准描述
	data3       map[string]string           //每个员工评估项的标准权重
}
Your Name authored
44 45 46 47 48
func newExportData() *exportData {
	return &exportData{
		userName:    nil,
		usrIdMap:    map[string]string{},
		userDayMap:  map[string][]string{},
49
		tableHeader: map[string]*HeaderLevel{},
Your Name authored
50 51 52 53 54 55
		data:        map[string]*strings.Builder{},
		data2:       map[string]string{},
		data3:       map[string]string{},
	}
}
Your Name authored
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
// 设置表格头部字段。和对应的员工列表
func (e *exportData) setCategoryNameList(param []dao.ContentCategoryName) {
	for _, v := range param {
		if _, ok := e.usrIdMap[v.TargetUserId]; !ok {
			if _, ok := e.tableHeader[v.TargetUserName]; ok {
				//出现重名,id不同但名称相同
				uname := fmt.Sprintf("%s%d", v.TargetUserName, len(e.usrIdMap))
				e.usrIdMap[v.TargetUserId] = uname
			} else {
				e.usrIdMap[v.TargetUserId] = v.TargetUserName
			}
			e.userName = append(e.userName, e.usrIdMap[v.TargetUserId])
		}
		userName := e.usrIdMap[v.TargetUserId]
		if _, ok := e.tableHeader[userName]; !ok {
71
			e.tableHeader[userName] = &HeaderLevel{
Your Name authored
72 73
				Name:   "个人绩效评估等级统计表",
				Filter: map[string]int{},
74
				Child:  []HeaderLevel{},
Your Name authored
75 76 77
			}
		}
		child := e.tableHeader[userName].addChild(v.Category) //第一级,"分类"
Your Name authored
78 79 80
		weight := e.weightDesc(v.Weight)
		child = child.addChild(weight) //第二级 '得分项' '加分项'
		child.addChild(v.Name)         //第三级 评估项名称
Your Name authored
81 82 83 84 85 86 87 88 89 90 91 92 93
	}
}

func (e *exportData) setData(param []*dao.ExportData2) {
	userName := ""
	key := ""
	userDay := map[string]struct{}{}
	for _, v := range param {
		//员工填写的评估内容
		if _, ok := e.usrIdMap[v.TargetUserId]; !ok {
			continue
		}
		userName = e.usrIdMap[v.TargetUserId]
Your Name authored
94 95
		weight := e.weightDesc(v.Weight)
		key = e.dataKey(userName, v.BeginDay, v.Category, weight, v.ContentName)
Your Name authored
96 97 98
		e.data[key] = &strings.Builder{}
		e.data[key].WriteString(v.Value + "\n") //填写的等级
		for _, vv := range v.Remark {
tangxvhui authored
99
			e.data[key].WriteString(vv.Title + "\n")
Your Name authored
100 101 102 103 104 105
			e.data[key].WriteString(vv.RemarkText + "\n")
		}
		if _, ok := userDay[userName+v.BeginDay]; !ok {
			userDay[userName+v.BeginDay] = struct{}{}
			e.userDayMap[userName] = append(e.userDayMap[userName], v.BeginDay)
		}
Your Name authored
106 107 108 109 110
		key23 := e.data23Key(userName, v.Category, weight, v.ContentName)
		e.data2[key23] = v.PromptText
		if v.Weight == 0 {
			e.data3[key23] = ""
		} else {
tangxvhui authored
111
			e.data3[key23] = fmt.Sprintf("%.2f%%", v.Weight)
Your Name authored
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		}
	}
}

func (e *exportData) dataKey(userName string, beginDay string, category string, weight string, contentName string) string {
	key := fmt.Sprintf("%s-%s-%s-%s-%s", userName, weight, beginDay, category, contentName)
	return key
}

func (e *exportData) data23Key(userName string, category string, weight string, contentName string) string {
	key := fmt.Sprintf("%s-%s-%s-%s", userName, weight, category, contentName)
	return key
}

func (e *exportData) weightDesc(weight float64) string {
	if weight == 0 {
		return "加分项"
	} else {
		return "得分项"
Your Name authored
131 132
	}
}
Your Name authored
133
tangxvhui authored
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
type exportData2 struct {
	tableHeader HeaderLevel                 //数据表格对应表头
	userIdMap   map[string]string           //员工id 对应的名称
	rowSort     HeaderLevel                 //
	data        map[string]*strings.Builder //需要导出的数据 key=员工Id+日期+表头

}

func newExportData2() *exportData2 {
	return &exportData2{
		tableHeader: HeaderLevel{
			Name:   "每日绩效汇总",
			Filter: map[string]int{},
			Child:  []HeaderLevel{},
		},
		userIdMap: map[string]string{},
		rowSort: HeaderLevel{
			Name:   "",
			Filter: map[string]int{},
			Child:  []HeaderLevel{},
		},
		data: map[string]*strings.Builder{},
	}
}

func (e *exportData2) setData(param []dao.ExportData1) {
	for _, v := range param {
		e.userIdMap[v.TargetUserId] = v.TargetUserName
		//提取表头数据
		child := e.tableHeader.addChild(v.Category) //第一级 ,分类
		weigh := e.weightDesc(v.Weight)
		child = child.addChild(weigh)         //第二级,加分项、得分项
		child = child.addChild(v.ContentName) //第三级,评估项名称
		child.addChild(v.PromptText)          //第四级, 标准

		//提取 纵向列表 索引
		child = e.rowSort.addChild(v.BeginDay) //日期
		child.addChild(v.TargetUserId)         //员工id
		key := e.dataKey(v.TargetUserId, v.BeginDay, v.Category, v.ContentName)
tangxvhui authored
173
		//整理 员工填写的数据
tangxvhui authored
174 175 176
		e.data[key] = &strings.Builder{}
		e.data[key].WriteString(v.Value + "\n")
		for _, v2 := range v.Remark {
tangxvhui authored
177 178 179 180
			v2.Title = strings.TrimSpace(v2.Title)
			if v2.Title != "填写自评反馈" {
				e.data[key].WriteString(v2.Title + "\n")
			}
tangxvhui authored
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
			e.data[key].WriteString(v2.RemarkText + "\n")
		}
	}
}

func (e *exportData2) weightDesc(weight float64) string {
	if weight == 0 {
		return "加分项"
	} else {
		return "得分项"
	}
}

func (e *exportData2) dataKey(userId string, beginDay string, category string, contentName string) string {
	key := fmt.Sprintf("%s-%s-%s-%s", userId, beginDay, category, contentName)
	return key
}