|
@@ -1,8 +1,5 @@
|
|
|
package cn.com.qmth.stmms.api.controller;
|
|
|
|
|
|
-import java.awt.Color;
|
|
|
-import java.awt.Font;
|
|
|
-import java.awt.Graphics2D;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
@@ -19,20 +16,29 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.core.task.AsyncTaskExecutor;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import cn.com.qmth.stmms.admin.utils.UpyunConfig;
|
|
|
+import cn.com.qmth.stmms.api.utils.SheetBuildUtil;
|
|
|
+import cn.com.qmth.stmms.api.utils.SheetDownloadThread;
|
|
|
import cn.com.qmth.stmms.biz.campus.model.Campus;
|
|
|
import cn.com.qmth.stmms.biz.campus.service.CampusService;
|
|
|
+import cn.com.qmth.stmms.biz.exam.model.Exam;
|
|
|
import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
|
|
|
+import cn.com.qmth.stmms.biz.exam.service.ExamService;
|
|
|
import cn.com.qmth.stmms.biz.exam.service.ExamStudentService;
|
|
|
+import cn.com.qmth.stmms.biz.lock.LockService;
|
|
|
import cn.com.qmth.stmms.biz.utils.PictureTag;
|
|
|
+import cn.com.qmth.stmms.common.enums.LockType;
|
|
|
import cn.com.qmth.stmms.common.upyun.UpYun;
|
|
|
import cn.com.qmth.stmms.common.utils.PictureUrlBuilder;
|
|
|
+import net.sf.json.JSONObject;
|
|
|
|
|
|
@Controller("pictureController")
|
|
|
@RequestMapping("/api")
|
|
@@ -40,6 +46,9 @@ public class PictureController {
|
|
|
|
|
|
protected static final Logger log = LoggerFactory.getLogger(PictureController.class);
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ExamService examService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private CampusService campusService;
|
|
|
|
|
@@ -49,6 +58,12 @@ public class PictureController {
|
|
|
@Autowired
|
|
|
private UpyunConfig config;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private LockService lockService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AsyncTaskExecutor taskExecutor;
|
|
|
+
|
|
|
@Value("${sheet.image.server}")
|
|
|
private String sheetServer;
|
|
|
|
|
@@ -65,23 +80,11 @@ public class PictureController {
|
|
|
return;
|
|
|
}
|
|
|
try {
|
|
|
- BufferedImage image = getSheetImage(student, index);
|
|
|
- if (withTag != null && withTag.booleanValue()) {
|
|
|
- List<PictureTag> tags = studentService.buildSheetTags(student).get(index);
|
|
|
- if (!tags.isEmpty()) {
|
|
|
- BufferedImage newImg = new BufferedImage(image.getWidth(), image.getHeight(),
|
|
|
- BufferedImage.TYPE_INT_RGB);
|
|
|
- Graphics2D g = newImg.createGraphics();
|
|
|
- g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
|
|
|
- g.setColor(Color.RED);
|
|
|
- g.setFont(new Font("Arial", Font.PLAIN, 50));
|
|
|
- for (PictureTag tag : tags) {
|
|
|
- g.drawString(tag.getContent(), tag.getLeft(), tag.getTop());
|
|
|
- }
|
|
|
- g.dispose();
|
|
|
- image = newImg;
|
|
|
- }
|
|
|
+ List<PictureTag> tags = null;
|
|
|
+ if (withTag != null && withTag) {
|
|
|
+ tags = studentService.buildSheetTags(student).get(index);
|
|
|
}
|
|
|
+ BufferedImage image = SheetBuildUtil.buildSheetImage(getSheetImage(student, index), tags);
|
|
|
response.setContentType("image/jpeg");
|
|
|
ImageIO.write(image, "jpg", response.getOutputStream());
|
|
|
} catch (Exception e) {
|
|
@@ -92,7 +95,30 @@ public class PictureController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private BufferedImage getSheetImage(ExamStudent student, int index) throws FileNotFoundException, IOException {
|
|
|
+ @RequestMapping("/sheets/{examId}")
|
|
|
+ @ResponseBody
|
|
|
+ public Object getAllSheet(HttpServletResponse response, @PathVariable Integer examId,
|
|
|
+ @RequestParam(required = false, defaultValue = "true") Boolean withTag) throws IOException {
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ Exam exam = examService.findById(examId);
|
|
|
+ if (exam == null) {
|
|
|
+ result.accumulate("success", false);
|
|
|
+ result.accumulate("message", "exam not exists");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(baseDir)) {
|
|
|
+ result.accumulate("success", false);
|
|
|
+ result.accumulate("message", "file.root not exists");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ if (lockService.trylock(LockType.EXAM, examId)) {
|
|
|
+ taskExecutor.submit(new SheetDownloadThread(examId, baseDir, this, studentService, lockService));
|
|
|
+ }
|
|
|
+ result.accumulate("running", true);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public BufferedImage getSheetImage(ExamStudent student, int index) throws FileNotFoundException, IOException {
|
|
|
Campus campus = campusService.findBySchoolAndName(student.getSchoolId(), student.getCampusName());
|
|
|
String url = PictureUrlBuilder.getSheetUrl(student.getExamId(), campus.getId(), student.getSubjectCode(),
|
|
|
student.getExamNumber(), index);
|