9 changed files with 97 additions and 49 deletions
@ -1,22 +1,47 @@ |
|||
package com.project.exam.domain.service.impl; |
|||
|
|||
import cn.hutool.core.bean.BeanUtil; |
|||
import com.alibaba.excel.EasyExcel; |
|||
import com.project.base.domain.exception.BusinessErrorException; |
|||
import com.project.base.domain.result.PageResult; |
|||
import com.project.base.domain.result.Result; |
|||
import com.project.exam.domain.dto.ExamRecordDTO; |
|||
import com.project.exam.domain.dto.ExportExamRecordDTO; |
|||
import com.project.exam.domain.param.ExamRecordParam; |
|||
import com.project.exam.domain.service.AdminExportExamRecordDomainService; |
|||
import com.project.exam.domain.service.AdminSearchExamRecordDomainService; |
|||
import com.project.exam.domain.service.handler.ExamResultColorHandler; |
|||
import jakarta.servlet.http.HttpServletResponse; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Service |
|||
public class AdminExportExamRecordDomainServiceImpl implements AdminExportExamRecordDomainService { |
|||
@Autowired |
|||
private AdminSearchExamRecordDomainService adminSearchExamRecordDomainService; |
|||
@Override |
|||
public void export(ExamRecordParam appealParam, HttpServletResponse response) throws Exception { |
|||
Result<PageResult<ExamRecordDTO>> pageResultResult = adminSearchExamRecordDomainService.adminSearch(appealParam); |
|||
|
|||
private final static Long MAX_EXPORT_NUM = 5000L; |
|||
|
|||
@Override |
|||
public void export(ExamRecordParam examRecordParam , HttpServletResponse response) throws Exception { |
|||
long count = adminSearchExamRecordDomainService.count(examRecordParam); |
|||
if (count > MAX_EXPORT_NUM) { |
|||
throw new BusinessErrorException("超过最大导出条数5000条,请重新调整搜索条件"); |
|||
} |
|||
examRecordParam.setSize(MAX_EXPORT_NUM.intValue()); |
|||
Result<PageResult<ExamRecordDTO>> pageResultResult = adminSearchExamRecordDomainService.adminSearch(examRecordParam); |
|||
List<ExamRecordDTO> dataList = pageResultResult.getData().getContent(); |
|||
List<ExportExamRecordDTO> exportList = dataList.stream().map(dto -> { |
|||
ExportExamRecordDTO exportDTO = new ExportExamRecordDTO(); |
|||
BeanUtil.copyProperties(dto, exportDTO); |
|||
exportDTO.setScore(String.format("%.2f" , dto.getScore())); |
|||
return exportDTO; |
|||
}).toList(); |
|||
EasyExcel.write(response.getOutputStream(), ExportExamRecordDTO.class) |
|||
.registerWriteHandler(new ExamResultColorHandler()) // 调用优化后的拦截器
|
|||
.sheet("考试数据") |
|||
.doWrite(exportList); |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue