refactor: 重构 PDF 模板

11111
winter 10 months ago
parent b9950c6022
commit fa1c6a9b20

@ -31,6 +31,7 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jfree.chart.ChartUtils; import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart; import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.general.DefaultPieDataset;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
@ -40,12 +41,14 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.*; import java.util.*;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -55,8 +58,8 @@ import java.util.stream.Collectors;
@Service @Service
public class EvaluationServiceImpl implements EvaluationService { public class EvaluationServiceImpl implements EvaluationService {
/** /**
* *
*/ */
private final static String TARGET_STUDENT = "student"; private final static String TARGET_STUDENT = "student";
private final static String TARGET_CLASS = "class"; private final static String TARGET_CLASS = "class";
@Resource @Resource
@ -112,8 +115,8 @@ public class EvaluationServiceImpl implements EvaluationService {
/** /**
* appraise () * appraise ()
*/ */
private void refreshAppraiseTree(Appraise appraise) { private void refreshAppraiseTree(Appraise appraise) {
List<AppraiseTreeNode> nodes = appraise.getNodes(); List<AppraiseTreeNode> nodes = appraise.getNodes();
List<AchievementRule> rules = appraise.getAchievementRules(); List<AchievementRule> rules = appraise.getAchievementRules();
@ -332,7 +335,7 @@ public class EvaluationServiceImpl implements EvaluationService {
// 获取班级信息 // 获取班级信息
ClassInfo classInfo = classRepository.findClassByIdAndCode(classId, String.format(PK.CLASS, schoolId)); ClassInfo classInfo = classRepository.findClassByIdAndCode(classId, String.format(PK.CLASS, schoolId));
className = classInfo.getName(); className = classInfo.getName();
} else if (targetType.equals(TARGET_CLASS)){ } else if (targetType.equals(TARGET_CLASS)) {
ClassInfo classInfo = classRepository.findClassByIdAndCode(targetId, String.format(PK.CLASS, schoolId)); ClassInfo classInfo = classRepository.findClassByIdAndCode(targetId, String.format(PK.CLASS, schoolId));
if (classInfo == null) { if (classInfo == null) {
throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "班级不存在"); throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "班级不存在");
@ -365,7 +368,7 @@ public class EvaluationServiceImpl implements EvaluationService {
targetId, targetId,
classId, classId,
academicYearId, academicYearId,
String.format(PK.PK_APPRAISE_RECORD,schoolId) String.format(PK.PK_APPRAISE_RECORD, schoolId)
); );
// 初始化新的评价节点 // 初始化新的评价节点
@ -376,7 +379,7 @@ public class EvaluationServiceImpl implements EvaluationService {
item.setCreatorId(loginUser.getId()); item.setCreatorId(loginUser.getId());
item.setCreateTime(Instant.now().toEpochMilli()); item.setCreateTime(Instant.now().toEpochMilli());
// 处理学校与学生的差异 // 处理学校与学生的差异
if (targetType.equals(TARGET_CLASS)){ if (targetType.equals(TARGET_CLASS)) {
item.setSpread(spread); item.setSpread(spread);
} else { } else {
item.setPushParent(pushParent); item.setPushParent(pushParent);
@ -463,12 +466,12 @@ public class EvaluationServiceImpl implements EvaluationService {
.findFirst().orElseThrow(() -> new ServiceException("该记录节点不存在")); .findFirst().orElseThrow(() -> new ServiceException("该记录节点不存在"));
// 鉴权(不是创建老师不能撤回) // 鉴权(不是创建老师不能撤回)
if (!loginUser.getRoles().contains("admin") && !userId.equals(record.getCreatorId())) { if (!loginUser.getRoles().contains("admin") && !userId.equals(record.getCreatorId())) {
throw new ServiceException(ErrorCode.NO_AUTH_ERROR.getCode(), "您不是创建老师,不能撤回"); throw new ServiceException(ErrorCode.NO_AUTH_ERROR.getCode(), "您不是创建老师,不能撤回");
} }
// 删除评价项并且恢复评分 // 删除评价项并且恢复评分
appraiseRecord.getNodes().removeIf(item -> nodeId.equals(item.getId())); appraiseRecord.getNodes().removeIf(item -> nodeId.equals(item.getId()));
boolean praise = record.getAppraiseNode().isPraise(); boolean praise = record.getAppraiseNode().isPraise();
Integer newPraiseCount = appraiseRecord.getPraiseCount() + (praise ? -1 : 1); Integer newPraiseCount = appraiseRecord.getPraiseCount() + (praise ? -1 : 1);
appraiseRecord.setPraiseCount(newPraiseCount); appraiseRecord.setPraiseCount(newPraiseCount);
int score = record.getAppraiseNode().getScore() == null ? 0 : record.getAppraiseNode().getScore(); int score = record.getAppraiseNode().getScore() == null ? 0 : record.getAppraiseNode().getScore();
Integer newScore = appraiseRecord.getScore() - score; Integer newScore = appraiseRecord.getScore() - score;
@ -522,7 +525,7 @@ public class EvaluationServiceImpl implements EvaluationService {
// 计算当前学生排名在全班次位 // 计算当前学生排名在全班次位
int greaterCount = appraiseRecordRepository.findClassRecord(String.format(PK.PK_APPRAISE_RECORD, schoolId), academicYearId, classId, praiseCount); int greaterCount = appraiseRecordRepository.findClassRecord(String.format(PK.PK_APPRAISE_RECORD, schoolId), academicYearId, classId, praiseCount);
float beyondPercent; float beyondPercent;
long currentRank = stuInClassCount - greaterCount; long currentRank = stuInClassCount - greaterCount;
beyondPercent = (float) currentRank / stuInClassCount; beyondPercent = (float) currentRank / stuInClassCount;
// 根据全部数据计算结果 // 根据全部数据计算结果
@ -530,7 +533,7 @@ public class EvaluationServiceImpl implements EvaluationService {
AppraiseTreeNode appraiseNode = record.getAppraiseNode(); AppraiseTreeNode appraiseNode = record.getAppraiseNode();
String[] path = appraiseNode.getPath(); String[] path = appraiseNode.getPath();
// 数据异常则跳过 // 数据异常则跳过
if (path == null || path.length == 0){ if (path == null || path.length == 0) {
continue; continue;
} }
String root = path[0]; String root = path[0];
@ -620,7 +623,7 @@ public class EvaluationServiceImpl implements EvaluationService {
} }
@Override @Override
public void exportStuReportPdf(IdRequest idRequest, HttpServletResponse response) throws IOException, DocumentException{ public void exportStuReportPdf(IdRequest idRequest, HttpServletResponse response) throws IOException, DocumentException {
// 设置response参数 // 设置response参数
response.reset(); response.reset();
response.setContentType("application/pdf"); response.setContentType("application/pdf");
@ -636,34 +639,43 @@ public class EvaluationServiceImpl implements EvaluationService {
PdfReader pdfReader = new PdfReader(in); PdfReader pdfReader = new PdfReader(in);
PdfStamper stamper = new PdfStamper(pdfReader, os); PdfStamper stamper = new PdfStamper(pdfReader, os);
int criticalCount = reportVo.getCriticalDistribution().values().stream().mapToInt(Integer::intValue).sum();
Map<String, String> data = new HashMap<>(); Map<String, String> data = new HashMap<>();
data.put("name", reportVo.getName()); data.put("name", reportVo.getName());
data.put("studentId", reportVo.getStudentId()); data.put("studentId", reportVo.getStudentId());
data.put("className", reportVo.getClassName()); data.put("className", reportVo.getClassName());
data.put("totalCount", String.valueOf(reportVo.getPraiseCount() + criticalCount));
data.put("praiseCount", String.valueOf(reportVo.getPraiseCount())); data.put("praiseCount", String.valueOf(reportVo.getPraiseCount()));
data.put("criticalCount", String.valueOf(criticalCount));
data.put("beyondPercent", String.valueOf(reportVo.getBeyondPercent())); data.put("beyondPercent", String.valueOf(reportVo.getBeyondPercent()));
data.put("topPraiseTeacher", StringUtils.isBlank(reportVo.getTopPraiseTeacher().getName()) ? "暂无" : reportVo.getTopPraiseTeacher().getName()); data.put("topPraiseTeacher", StringUtils.isBlank(reportVo.getTopPraiseTeacher().getName()) ? "暂无" : reportVo.getTopPraiseTeacher().getName());
data.put("topCriticalTeacher", StringUtils.isBlank(reportVo.getTopCriticalTeacher().getName()) ? "暂无" : reportVo.getTopCriticalTeacher().getName()); data.put("topCriticalTeacher", StringUtils.isBlank(reportVo.getTopCriticalTeacher().getName()) ? "暂无" : reportVo.getTopCriticalTeacher().getName());
data.put("topPraiseNode", StringUtils.isBlank(reportVo.getTopPraiseNode()) ? "暂无" : reportVo.getTopPraiseNode()); data.put("topPraiseNode", StringUtils.isBlank(reportVo.getTopPraiseNode()) ? "暂无" : reportVo.getTopPraiseNode());
data.put("topCriticalNode", StringUtils.isBlank(reportVo.getTopCriticalNode()) ? "暂无" : reportVo.getTopCriticalNode()); data.put("topCriticalNode", StringUtils.isBlank(reportVo.getTopCriticalNode()) ? "暂无" : reportVo.getTopCriticalNode());
DefaultPieDataset praiseDistributionDataset = new DefaultPieDataset( ); DefaultPieDataset praiseDistributionDataset = new DefaultPieDataset();
praiseDistributionDataset.setValue(FiveEducations.VIRTUE.getName(), reportVo.getPraiseDistribution().get(FiveEducations.VIRTUE.getCode())); praiseDistributionDataset.setValue(FiveEducations.VIRTUE.getName(), reportVo.getPraiseDistribution().get(FiveEducations.VIRTUE.getCode()));
praiseDistributionDataset.setValue(FiveEducations.INTELLIGENCE.getName(), reportVo.getPraiseDistribution().get(FiveEducations.INTELLIGENCE.getCode())); praiseDistributionDataset.setValue(FiveEducations.INTELLIGENCE.getName(), reportVo.getPraiseDistribution().get(FiveEducations.INTELLIGENCE.getCode()));
praiseDistributionDataset.setValue(FiveEducations.SPORTS.getName(), reportVo.getPraiseDistribution().get(FiveEducations.SPORTS.getCode())); praiseDistributionDataset.setValue(FiveEducations.SPORTS.getName(), reportVo.getPraiseDistribution().get(FiveEducations.SPORTS.getCode()));
praiseDistributionDataset.setValue(FiveEducations.ART.getName(), reportVo.getPraiseDistribution().get(FiveEducations.ART.getCode())); praiseDistributionDataset.setValue(FiveEducations.ART.getName(), reportVo.getPraiseDistribution().get(FiveEducations.ART.getCode()));
praiseDistributionDataset.setValue(FiveEducations.LABOUR.getName(), reportVo.getPraiseDistribution().get(FiveEducations.LABOUR.getCode())); praiseDistributionDataset.setValue(FiveEducations.LABOUR.getName(), reportVo.getPraiseDistribution().get(FiveEducations.LABOUR.getCode()));
JFreeChart praisePieChart = ChartUtil.pieChart("五育表扬指标分布", praiseDistributionDataset); JFreeChart praisePieChart = ChartUtil.pieChart("五育表扬指标分布", praiseDistributionDataset);
ByteArrayOutputStream praiseBos = new ByteArrayOutputStream(); ByteArrayOutputStream praiseBos = new ByteArrayOutputStream();
ChartUtils.writeChartAsJPEG(praiseBos, praisePieChart, 340, 330); ChartUtils.writeChartAsJPEG(praiseBos, praisePieChart, 340, 330);
DefaultPieDataset criticalDistributionDataset = new DefaultPieDataset( ); // 如果分布为空,则填充默认值 1
DefaultPieDataset criticalDistributionDataset = new DefaultPieDataset();
criticalDistributionDataset.setValue(FiveEducations.VIRTUE.getName(), reportVo.getCriticalDistribution().get(FiveEducations.VIRTUE.getCode())); criticalDistributionDataset.setValue(FiveEducations.VIRTUE.getName(), reportVo.getCriticalDistribution().get(FiveEducations.VIRTUE.getCode()));
criticalDistributionDataset.setValue(FiveEducations.INTELLIGENCE.getName(), reportVo.getCriticalDistribution().get(FiveEducations.INTELLIGENCE.getCode())); criticalDistributionDataset.setValue(FiveEducations.INTELLIGENCE.getName(), reportVo.getCriticalDistribution().get(FiveEducations.INTELLIGENCE.getCode()));
criticalDistributionDataset.setValue(FiveEducations.SPORTS.getName(), reportVo.getCriticalDistribution().get(FiveEducations.SPORTS.getCode())); criticalDistributionDataset.setValue(FiveEducations.SPORTS.getName(), reportVo.getCriticalDistribution().get(FiveEducations.SPORTS.getCode()));
criticalDistributionDataset.setValue(FiveEducations.ART.getName(), reportVo.getCriticalDistribution().get(FiveEducations.ART.getCode())); criticalDistributionDataset.setValue(FiveEducations.ART.getName(), reportVo.getCriticalDistribution().get(FiveEducations.ART.getCode()));
criticalDistributionDataset.setValue(FiveEducations.LABOUR.getName(), reportVo.getCriticalDistribution().get(FiveEducations.LABOUR.getCode())); criticalDistributionDataset.setValue(FiveEducations.LABOUR.getName(), reportVo.getCriticalDistribution().get(FiveEducations.LABOUR.getCode()));
JFreeChart criticalPieChart = ChartUtil.pieChart("五育待改进指标分布", criticalDistributionDataset); JFreeChart criticalPieChart = ChartUtil.pieChart("五育待改进指标分布", criticalDistributionDataset);
PiePlot plot = (PiePlot) criticalPieChart.getPlot();
plot.setNoDataMessage("无数据");
plot.setNoDataMessageFont(new Font("宋体", Font.PLAIN, 18));
ByteArrayOutputStream criticalBos = new ByteArrayOutputStream(); ByteArrayOutputStream criticalBos = new ByteArrayOutputStream();
ChartUtils.writeChartAsJPEG(criticalBos, criticalPieChart, 340, 330); ChartUtils.writeChartAsJPEG(criticalBos, criticalPieChart, 340, 330);
@ -675,12 +687,11 @@ public class EvaluationServiceImpl implements EvaluationService {
// stamper.setFormFlattening(true); // stamper.setFormFlattening(true);
stamper.close(); stamper.close();
os.close(); os.close();
} }
/** /**
* id id () * id id ()
*/ */
private void collectNodesToDelete(String id, List<AppraiseTreeNode> nodes, List<AppraiseTreeNode> nodesToDelete) { private void collectNodesToDelete(String id, List<AppraiseTreeNode> nodes, List<AppraiseTreeNode> nodesToDelete) {
for (AppraiseTreeNode node : nodes) { for (AppraiseTreeNode node : nodes) {
if (id.equals(node.getPid())) { if (id.equals(node.getPid())) {
@ -712,8 +723,8 @@ public class EvaluationServiceImpl implements EvaluationService {
/** /**
* *
*/ */
private List<AppraiseTreeNode> getChildren(AppraiseTreeNode parent, List<AppraiseTreeNode> nodes) { private List<AppraiseTreeNode> getChildren(AppraiseTreeNode parent, List<AppraiseTreeNode> nodes) {
List<AppraiseTreeNode> children = new ArrayList<>(); List<AppraiseTreeNode> children = new ArrayList<>();
for (AppraiseTreeNode node : nodes) { for (AppraiseTreeNode node : nodes) {
@ -723,4 +734,24 @@ public class EvaluationServiceImpl implements EvaluationService {
} }
return children; return children;
} }
/**
* distribution 0, 1
*/
private void checkIfDistributionNull(Map<String, Integer> distribution) {
boolean flag = false;
for (Map.Entry<String, Integer> entry : distribution.entrySet()) {
if (entry.getValue() != 0) {
flag = true;
break;
}
}
if(flag) {
return;
}
// distribution 全部为 0,则初始化为 1
distribution.forEach((k, v) -> distribution.put(k, 1));
}
} }

Loading…
Cancel
Save