作者 tangxvhui

excel导出

package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/xuri/excelize/v2"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/summary_evaluation/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
// 导出数据
// 综合管理-周期评估
func (srv *SummaryEvaluationService) ExportAllEvaluationSuper(param *command.QueryEvaluationList) (*excelize.File, error) {
return nil, nil
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
userRepo := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
positionRepo := factory.CreatePositionRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
limit := param.PageSize
offset := (param.PageNumber - 1) * param.PageSize
//获取评估列表信息
condition1 := map[string]interface{}{
"cycleId": param.CycleId,
"types": int(domain.EvaluationSuper),
"limit": limit,
}
if offset > 0 {
condition1["offset"] = offset
}
if len(param.TargetUserName) == 0 {
condition1["targetUserName"] = "%" + param.TargetUserName + "%"
}
//获取评估列表信息
_, evaluationList, err := evaluationRepo.Find(condition1)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
targetUserIds := []int{}
//获取员工信息
userMap := map[int64]*domain.User{}
for _, v := range evaluationList {
if _, ok := userMap[int64(v.TargetUser.UserId)]; ok {
continue
}
userMap[int64(v.TargetUser.UserId)] = nil
targetUserIds = append(targetUserIds, v.TargetUser.UserId)
}
if len(targetUserIds) > 0 {
_, userList, err := userRepo.Find(map[string]interface{}{
"ids": targetUserIds,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
for _, v := range userList {
userMap[v.Id] = v
}
}
var positionIds []int
//获取职位列表
positionMap := map[int64]*domain.Position{}
for _, v := range userMap {
for _, v2 := range v.DepartmentId {
if _, ok := positionMap[int64(v2)]; ok {
continue
}
positionMap[int64(v2)] = nil
positionIds = append(positionIds, v2)
}
}
if len(positionIds) > 0 {
_, positionList, err := positionRepo.Find(map[string]interface{}{"ids": positionIds})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取部门信息"+err.Error())
}
for _, v := range positionList {
positionMap[v.Id] = v
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
ratingHeader := []string{} //动态列,评级内容
ratingMap := map[string]struct{}{} //过滤重复code
evaluationRatingMap := map[int]map[string]int{}
for _, v := range evaluationList {
evaluationRatingMap[v.Id] = map[string]int{}
for _, v2 := range v.TotalRating {
if _, ok := ratingMap[v2.Code]; !ok {
ratingMap[v2.Code] = struct{}{}
ratingHeader = append(ratingHeader, v2.Code)
}
evaluationRatingMap[v.Id][v2.Code] = v2.Number
}
}
xlsxFile := excelize.NewFile()
//设置默认的第一个sheet
sheetIndex := xlsxFile.GetActiveSheetIndex()
firstSheetName := xlsxFile.GetSheetName(sheetIndex)
tableHead := []string{"姓名", "部门", "职位", "最终绩效得分"}
tableHead = append(tableHead, ratingHeader...)
xlsxFile.SetSheetRow(firstSheetName, "A1", &tableHead)
for i, v := range evaluationList {
departmentName := ""
for _, dep := range v.TargetDepartment {
departmentName += dep.DepartmentName + " "
}
//填充员工信息
positinName := ""
if targetUser, ok := userMap[int64(v.TargetUser.UserId)]; ok {
//填充职位信息
for _, positionId := range targetUser.PositionId {
if position, ok := positionMap[int64(positionId)]; ok {
positinName += position.Name + " "
}
}
}
dataRaw := []string{
v.TargetUser.UserName,
departmentName,
positinName,
v.TotalScore,
}
for _, v2 := range ratingHeader {
if num, ok := evaluationRatingMap[v.Id][v2]; ok {
dataRaw = append(dataRaw, fmt.Sprintf("%d", num))
} else {
dataRaw = append(dataRaw, "0")
}
}
xlsxFile.SetSheetRow(firstSheetName, fmt.Sprintf("A%d", i+2), &dataRaw)
}
return xlsxFile, nil
}
... ...
... ... @@ -188,6 +188,7 @@ func (c *StaffAssessController) ExportAssessContentCycleDay() {
data, err := srv.ExportUserAssess(paramReq)
if err != nil {
c.Response(nil, err)
return
}
c.Ctx.Output.Header("Content-Disposition", "attachment;filename=每日绩效汇总")
c.Ctx.Output.Header("Content-Description", "FileTransfer")
... ...
... ... @@ -323,3 +323,28 @@ func (c *SummaryEvaluationController) ListAllEvaluationSuper() {
data, err := srv.ListAllEvaluationSuper(param)
c.Response(data, err)
}
// 按周期 导出上级评估列表
func (c *SummaryEvaluationController) ExportAllEvaluationSuper() {
srv := service.NewSummaryEvaluationService()
param := &command.QueryEvaluationList{}
err := c.BindJSON(param)
if err != nil {
e := application.ThrowError(application.ARG_ERROR, "json 解析错误"+err.Error())
c.Response(nil, e)
return
}
userReq := middlewares.GetUser(c.Ctx)
param.CompanyId = int(userReq.CompanyId)
data, err := srv.ExportAllEvaluationSuper(param)
if err != nil {
c.Response(nil, err)
return
}
c.Ctx.Output.Header("Content-Disposition", "attachment;filename=每日绩效汇总")
c.Ctx.Output.Header("Content-Description", "FileTransfer")
c.Ctx.Output.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
c.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
c.Ctx.Output.Header("Expires", "0")
data.Write(c.Ctx.ResponseWriter)
}
... ...
... ... @@ -29,6 +29,8 @@ func init() {
web.NSCtrlPost("/evaluation-super/confirm", (*controllers.SummaryEvaluationController).ConfirmScoreSuperEvaluation),
web.NSCtrlPost("/target_user/evaluation-super", (*controllers.SummaryEvaluationController).GetTargetUserEvaluationSuper),
web.NSCtrlPost("/evaluation-super/all", (*controllers.SummaryEvaluationController).ListAllEvaluationSuper),
web.NSCtrlPost("/evaluation-super/all/export", (*controllers.SummaryEvaluationController).ExportAllEvaluationSuper),
//
)
web.AddNamespace(summaryNS)
... ...