|
@@ -0,0 +1,176 @@
|
|
|
|
+package cn.iselab.mooctest.site.service.common.impl;
|
|
|
|
+
|
|
|
|
+import cn.iselab.mooctest.site.data.AssignedCase;
|
|
|
|
+import cn.iselab.mooctest.site.data.CasePDF;
|
|
|
|
+import cn.iselab.mooctest.site.data.ReportPDF;
|
|
|
|
+import cn.iselab.mooctest.site.models.*;
|
|
|
|
+import cn.iselab.mooctest.site.service.*;
|
|
|
|
+import cn.iselab.mooctest.site.service.common.MongoAPIService;
|
|
|
|
+import cn.iselab.mooctest.site.service.common.PdfService;
|
|
|
|
+import cn.iselab.mooctest.site.web.data.forMongo.ReportForMongoDTO;
|
|
|
|
+import cn.iselab.mooctest.site.web.exception.HttpBadRequestException;
|
|
|
|
+import com.itextpdf.text.Document;
|
|
|
|
+import com.itextpdf.text.Image;
|
|
|
|
+import com.itextpdf.text.PageSize;
|
|
|
|
+import com.itextpdf.text.pdf.*;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Iterator;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @Author ROKG
|
|
|
|
+ * @Description
|
|
|
|
+ * @Date: Created in 下午1:24 2017/12/19
|
|
|
|
+ * @Modified By:
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class PdfServiceImpl implements PdfService{
|
|
|
|
+
|
|
|
|
+ private String templatePdfPath="/Users/ROKG/学生测试报告文档3.pdf";
|
|
|
|
+ private String templateImgPath="/Users/ROKG/柱状图.jpg";
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ ExamService examService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ UserService userService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ GroupService groupService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ AssignedTaskService assignedTaskService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ CalculateScoreService calculateScoreService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ MongoAPIService mongoAPIService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ CaseService caseService;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ReportPDF getReportPDF(Long examId,Long workerId) throws Exception{
|
|
|
|
+ ReportPDF reportPDF=new ReportPDF();
|
|
|
|
+ Task task=examService.getExamByIdAndParticipantIdIfPermited(examId,workerId);
|
|
|
|
+ if (task == null) {
|
|
|
|
+ throw new HttpBadRequestException("task not exits");
|
|
|
|
+ }
|
|
|
|
+ reportPDF.setExamName(task.getName());
|
|
|
|
+ SimpleDateFormat format=new SimpleDateFormat("HH:mm:ss");
|
|
|
|
+ reportPDF.setDateTime(task.getBeginTime()+"-"+format.format(task.getEndTime()));
|
|
|
|
+ User user=userService.findByUserId(workerId);
|
|
|
|
+ if (user == null) {
|
|
|
|
+ throw new HttpBadRequestException("user not exits");
|
|
|
|
+ }
|
|
|
|
+ reportPDF.setWorkerName(user.getName());
|
|
|
|
+ reportPDF.setSchool(user.getSchool());
|
|
|
|
+ List<Group> groups=groupService.getByExamId(task.getId());
|
|
|
|
+ if(!groups.isEmpty()){
|
|
|
|
+ throw new HttpBadRequestException("group not exits");
|
|
|
|
+ }
|
|
|
|
+ reportPDF.setGroupName(groups.get(0).getName());
|
|
|
|
+ User teacher=userService.findByUserId(task.getOwnerId());
|
|
|
|
+ if (teacher == null) {
|
|
|
|
+ throw new HttpBadRequestException("teacher not exits");
|
|
|
|
+ }
|
|
|
|
+ reportPDF.setTeacherName(teacher.getName());
|
|
|
|
+ AssignedTask assignedTask=assignedTaskService.getAssignedTask(examId,workerId);
|
|
|
|
+ if(assignedTask == null){
|
|
|
|
+ throw new HttpBadRequestException("score not exits");
|
|
|
|
+ }
|
|
|
|
+ reportPDF.setScore(assignedTask.getScore().toString());
|
|
|
|
+ List<AssignedTask> assignedTasks=assignedTaskService.getAssignedTasks(examId);
|
|
|
|
+ List<Double> scores=calculateScoreService.calculateScores(assignedTasks.stream().map(assignedTask1 -> assignedTask1.getScore()).collect(Collectors.toList()));
|
|
|
|
+ reportPDF.setMax(scores.get(0).toString());
|
|
|
|
+ reportPDF.setMin(scores.get(1).toString());
|
|
|
|
+ reportPDF.setAvg(scores.get(2).toString());
|
|
|
|
+ reportPDF.setVariance(scores.get(3).toString());
|
|
|
|
+
|
|
|
|
+ List<CasePDF> pdfs=new ArrayList<>();
|
|
|
|
+ List<AssignedCase> cases=assignedTaskService.wrapAssignedCases(assignedTask);
|
|
|
|
+ List<Long> caseIds=caseService.getCasesByTaskId(examId);
|
|
|
|
+ for (Long caseId:caseIds){
|
|
|
|
+ CasePDF pdf=new CasePDF();
|
|
|
|
+ Case caseEntity=caseService.getCaseById(caseId);
|
|
|
|
+ pdf.setCaseName(caseEntity.getName());
|
|
|
|
+ pdf.setDescription(caseEntity.getDescription());
|
|
|
|
+ pdf.setCaseScore(String.valueOf(cases.stream().findAny().filter(casse-> casse.getCaseId()==caseId).get().getMaxScore()));
|
|
|
|
+ pdf.setReport(this.getReport(examId,workerId,caseId));
|
|
|
|
+ pdfs.add(pdf);
|
|
|
|
+ }
|
|
|
|
+ return reportPDF;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void generatePDF(ReportPDF pdf, File file) throws Exception{
|
|
|
|
+ CasePDF casePDF=pdf.getCases().get(0);
|
|
|
|
+ PdfReader reader=new PdfReader(templatePdfPath);
|
|
|
|
+ ByteArrayOutputStream bos=new ByteArrayOutputStream();
|
|
|
|
+ PdfStamper ps=new PdfStamper(reader,bos);
|
|
|
|
+
|
|
|
|
+ BaseFont bf=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
|
|
|
|
+ ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
|
|
|
|
+ fontList.add(bf);
|
|
|
|
+ AcroFields fields=ps.getAcroFields();
|
|
|
|
+ fields.setSubstitutionFonts(fontList);
|
|
|
|
+
|
|
|
|
+ fields.setField("examName",pdf.getExamName());
|
|
|
|
+ fields.setField("workerName",pdf.getWorkerName());
|
|
|
|
+ fields.setField("school",pdf.getSchool());
|
|
|
|
+ fields.setField("teacherName",pdf.getTeacherName());
|
|
|
|
+ fields.setField("groupName",pdf.getGroupName());
|
|
|
|
+ fields.setField("dateTime",pdf.getDateTime());
|
|
|
|
+ fields.setField("score",pdf.getScore());
|
|
|
|
+ fields.setField("max",pdf.getMax());
|
|
|
|
+ fields.setField("min",pdf.getMin());
|
|
|
|
+ fields.setField("avg",pdf.getAvg());
|
|
|
|
+ fields.setField("variance",pdf.getVariance());
|
|
|
|
+
|
|
|
|
+ Image img = Image.getInstance(templateImgPath); //使用png格式
|
|
|
|
+ img.setAlignment(Image.MIDDLE | Image.TEXTWRAP);
|
|
|
|
+ img.setBorderWidth(10);
|
|
|
|
+ img.setAbsolutePosition(100, 250);
|
|
|
|
+ img.scaleToFit(2000, 150);// 大小
|
|
|
|
+ PdfContentByte over = ps.getUnderContent(1); // overCount 与underCount
|
|
|
|
+ over.addImage(img);
|
|
|
|
+
|
|
|
|
+ fields.setField("caseName",casePDF.getCaseName());
|
|
|
|
+ fields.setField("caseScore",casePDF.getCaseScore());
|
|
|
|
+ fields.setField("description",casePDF.getDescription());
|
|
|
|
+ fields.setField("content",casePDF.getContent());
|
|
|
|
+
|
|
|
|
+ ps.setFormFlattening(true);
|
|
|
|
+ ps.close();
|
|
|
|
+
|
|
|
|
+ FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
+ //方法一,生成的pdf文件较大
|
|
|
|
+// fos.write(bos.toByteArray());
|
|
|
|
+// fos.close();
|
|
|
|
+ //方法二,生成的pdf文件较小
|
|
|
|
+ Document doc = new Document(PageSize.A4);
|
|
|
|
+ PdfCopy copy = new PdfCopy(doc, fos);
|
|
|
|
+ doc.open();
|
|
|
|
+ for(int i=1;i<=2;i++) {
|
|
|
|
+ doc.newPage();
|
|
|
|
+ PdfImportedPage importPage = copy.getImportedPage(
|
|
|
|
+ new PdfReader(bos.toByteArray()), i);
|
|
|
|
+ copy.addPage(importPage);
|
|
|
|
+ }
|
|
|
|
+ doc.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private ReportForMongoDTO getReport(long examId, long participantId, long caseId){
|
|
|
|
+ ReportForMongoDTO report=new ReportForMongoDTO();
|
|
|
|
+ return report;
|
|
|
|
+ }
|
|
|
|
+}
|