作者 Your Name

输出excel

... ... @@ -66,6 +66,18 @@ type exportData struct {
data3 map[string]string //每个员工评估项的标准权重
}
func newExportData() *exportData {
return &exportData{
userName: nil,
usrIdMap: map[string]string{},
userDayMap: map[string][]string{},
tableHeader: map[string]*headerLevel{},
data: map[string]*strings.Builder{},
data2: map[string]string{},
data3: map[string]string{},
}
}
// 设置表格头部字段。和对应的员工列表
func (e *exportData) setCategoryNameList(param []dao.ContentCategoryName) {
for _, v := range param {
... ... @@ -88,12 +100,9 @@ func (e *exportData) setCategoryNameList(param []dao.ContentCategoryName) {
}
}
child := e.tableHeader[userName].addChild(v.Category) //第一级,"分类"
if v.Weight == 0 {
child = child.addChild("加分项") //第二级 '得分项' '加分项'
} else {
child = child.addChild("得分项") //第二级 '得分项' '加分项'
}
child.addChild(v.Name) //第三级 评估项名称
weight := e.weightDesc(v.Weight)
child = child.addChild(weight) //第二级 '得分项' '加分项'
child.addChild(v.Name) //第三级 评估项名称
}
}
... ... @@ -107,23 +116,46 @@ func (e *exportData) setData(param []*dao.ExportData2) {
continue
}
userName = e.usrIdMap[v.TargetUserId]
if v.Weight == 0 {
key = fmt.Sprintf("%s-%s-加分项-%s-%s", userName, v.BeginDay, v.Category, v.ContentName)
e.data3[key] = ""
} else {
key = fmt.Sprintf("%s-%s-得分项-%s-%s", userName, v.BeginDay, v.Category, v.ContentName)
e.data3[key] = fmt.Sprintf("%.2f %%", v.Weight)
}
weight := e.weightDesc(v.Weight)
key = e.dataKey(userName, v.BeginDay, v.Category, weight, v.ContentName)
e.data[key] = &strings.Builder{}
e.data[key].WriteString(v.Value + "\n") //填写的等级
for _, vv := range v.Remark {
e.data[key].WriteString(vv.Definition + "\n")
e.data[key].WriteString(vv.RemarkText + "\n")
}
e.data2[key] = v.PromptText
if _, ok := userDay[userName+v.BeginDay]; !ok {
userDay[userName+v.BeginDay] = struct{}{}
e.userDayMap[userName] = append(e.userDayMap[userName], v.BeginDay)
}
key23 := e.data23Key(userName, v.Category, weight, v.ContentName)
e.data2[key23] = v.PromptText
if v.Weight == 0 {
e.data3[key23] = ""
} else {
e.data3[key23] = fmt.Sprintf("%.2f %%", v.Weight)
}
}
}
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 "得分项"
}
}
func (e *exportData) userDayKey(userName string) string {
return userName
}
... ...
... ... @@ -227,38 +227,7 @@ func (srv StaffAssessServeice) ExportUserAssess(param *query.ListAssessContentCy
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
//roleRepo := factory.CreateRoleRepository(map[string]interface{}{
// "transactionContext": transactionContext,
//})
//roleUserRepo := factory.CreateRoleUserRepository(map[string]interface{}{
// "transactionContext": transactionContext,
//})
//_, roleList, err := roleRepo.Find(map[string]interface{}{
// "type": domain.RoleTypeSystem,
// "companyId": param.CompanyId,
//})
//if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取角色信息列表"+err.Error())
//}
//_, userRoleList, err := roleUserRepo.Find(map[string]interface{}{
// "companyId": param.CompanyId,
// "userId": param.OperaterId,
//})
//if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户的角色信息列表"+err.Error())
//}
//hrbp := -1
//for _, v := range userRoleList {
// for _, v2 := range roleList {
// if v.RoleId == v2.Id {
// hrbp = 1
// break
// }
// }
// if hrbp == 1 {
// break
// }
//}
assessDao := dao.NewStaffAssessDao(map[string]interface{}{
"transactionContext": transactionContext,
})
... ... @@ -595,56 +564,99 @@ func (srv StaffAssessServeice) ExportUserAssess2(param *query.SummaryCommand) (*
defer func() {
_ = transactionContext.RollbackTransaction()
}()
roleRepo := factory.CreateRoleRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
roleUserRepo := factory.CreateRoleUserRepository(map[string]interface{}{
hrbp, err := srv.getHRBP(transactionContext, param.CompanyId, param.OperatorId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
assessDao := dao.NewStaffAssessDao(map[string]interface{}{
"transactionContext": transactionContext,
})
_, roleList, err := roleRepo.Find(map[string]interface{}{
"type": domain.RoleTypeSystem,
"companyId": param.CompanyId,
})
// 获取所有的评估项
categoryNameList, err := assessDao.SearchContentCategoryName(param.CompanyId, param.CycleId, param.OperatorId, hrbp)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户的角色信息列表"+err.Error())
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
_, userRoleList, err := roleUserRepo.Find(map[string]interface{}{
"companyId": param.CompanyId,
"userId": param.OperatorId,
})
//获取员工填写的评估
userAssessList, err := assessDao.ExportDataUserAssess2(
param.CompanyId,
param.CycleId,
param.OperatorId,
hrbp,
)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户的角色信息列表"+err.Error())
}
hrbp := -1
for _, v := range userRoleList {
for _, v2 := range roleList {
if v.RoleId == v2.Id {
hrbp = 1
break
}
}
if hrbp == 1 {
break
}
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// assessDao := dao.NewStaffAssessDao(map[string]interface{}{
// "transactionContext": transactionContext,
// })
// 获取所有的评估项
// categoryNameList, err := assessDao.SearchContentCategoryName(param.CompanyId, param.CycleId, param.OperatorId, hrbp)
// if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// }
// categoryNameTree := map[string]headerLevel{}
// userNameMap := map[string]struct{}{} //处理员工重名
// userIdMap := map[string]string{} //映射员工id和员工名称
// userNameList := []string{} //员工名称列表
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// tableHeader := excelTableHeader{}
return nil, nil
eData := newExportData()
eData.setCategoryNameList(categoryNameList)
eData.setData(userAssessList)
xlsxFile := excelize.NewFile()
//设置默认的第一个sheet
sheetIndex := xlsxFile.GetActiveSheetIndex()
firstSheetName := xlsxFile.GetSheetName(sheetIndex)
cycleName := ""
if len(userAssessList) > 0 {
cycleName = userAssessList[0].CycleName
}
//填入excel 文件
for _, v := range eData.userName {
// 根据员工名称 添加一个sheet
xlsxFile.NewSheet(v)
//填写前4行数据
tableHeader, ok := eData.tableHeader[v]
if !ok {
continue
}
xlsxFile.SetCellStr(v, "B2", v)
//填充第一列数据
xlsxFile.SetCellStr(v, "A2", cycleName)
xlsxFile.SetCellStr(v, "A1", tableHeader.Name)
xlsxFile.SetCellStr(v, "A5", "权重")
xlsxFile.SetCellStr(v, "A6", "评估标准")
//日期
dayList := eData.userDayMap[v]
for ii, vv := range dayList {
if ii == 0 {
axis := fmt.Sprintf("A%d", ii+7)
xlsxFile.SetCellStr(v, axis, vv)
} else {
axis := fmt.Sprintf("A%d", ii+7+2)
xlsxFile.SetCellStr(v, axis, vv)
}
}
for _, v2 := range tableHeader.Child {
//第二行
for _, v3 := range v2.Child {
//第三行
for i4, v4 := range v3.Child {
//按列填充数据
colName, _ := excelize.ColumnNumberToName(i4 + 3) //第3列开始
xlsxFile.SetCellStr(v, colName+"2", v2.Name) //分类
xlsxFile.SetCellStr(v, colName+"3", v3.Name) //加分项 得分项
xlsxFile.SetCellStr(v, colName+"4", v4.Name) // 评估项名称
//权重 填写第5行数据
k23 := eData.data23Key(v, v2.Name, v3.Name, v4.Name)
xlsxFile.SetCellStr(v, colName+"5", eData.data3[k23])
//评估标准 填写第6行数据
xlsxFile.SetCellStr(v, colName+"6", eData.data2[k23])
//按日期填充评估的填写的值
for i5, v5 := range dayList {
k1 := eData.dataKey(v, v5, v2.Name, v3.Name, v4.Name)
if i5 > 0 {
axis := fmt.Sprintf("%s%d", colName, i5+7+2) //单元格高度按三个单元格合并
xlsxFile.SetCellStr(v, axis, eData.data[k1].String())
} else {
axis := fmt.Sprintf("%s%d", colName, i5+7)
xlsxFile.SetCellStr(v, axis, eData.data[k1].String())
}
}
}
}
}
}
//删除默认的第一个sheet
xlsxFile.DeleteSheet(firstSheetName)
return xlsxFile, nil
}
... ...