package com.qxueyou.scc.sys.action; import com.qxueyou.scc.admin.classes.model.ClsClass; import com.qxueyou.scc.admin.classes.service.IClassService; import com.qxueyou.scc.admin.classroom.model.ClassRoom; import com.qxueyou.scc.admin.classroom.service.IClassRoomService; import com.qxueyou.scc.base.dao.CommonDAO; import com.qxueyou.scc.base.util.*; import com.qxueyou.scc.exam.model.ExamBatchInfo; import com.qxueyou.scc.exam.model.ExamInfo; import com.qxueyou.scc.exam.model.ExamResultV; import com.qxueyou.scc.exam.service.IExamBatchService; import com.qxueyou.scc.exam.service.IExamService; import com.qxueyou.scc.sys.utils.DrawingUtil; import com.qxueyou.scc.teach.student.model.StuStudent; import com.qxueyou.scc.teach.student.service.IStudentService; import com.qxueyou.scc.user.service.IUserService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.*; /** * @author luodong * @version 1.0 * @date 2020/5/25 15:01 */ @Controller @RequestMapping(value = "/certImage") public class ImgController { @Autowired IStudentService studentService; @Autowired private CommonDAO commonDAO; @Autowired IUserService userService; @Autowired IClassService classService; @Autowired IExamService examService; @Autowired IExamBatchService examBatchService; @Autowired private IClassRoomService classRoomService; /** * 根据请求的id查询数据生成图片 * @param * @return */ @RequestMapping(value = "/createImg",method = RequestMethod.GET) @ResponseBody public ResponseEntity draw(String studentNo,String examBatchId, HttpServletRequest req, HttpServletResponse resp) throws Exception { //获取准考证信息 Map stu = getStudent(studentNo,examBatchId); ResponseEntity entity = null; ByteArrayOutputStream bo=null; //字节流 String fileName=UUID.randomUUID().toString() +".jpg"; HttpStatus statusCode = HttpStatus.CREATED; String header = req.getHeader("User-Agent").toUpperCase(); try { if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) { fileName = URLEncoder.encode(fileName, "gbk"); fileName = fileName.replace("+", "%20"); // IE下载文件名空格变+号问题 statusCode = HttpStatus.OK; } else { fileName = new String(fileName.getBytes("gbk"), "ISO8859-1"); } bo=new ByteArrayOutputStream(); String simg="static/images/ca.jpg"; if (stu.get("classRoomName").contains("八卦岭")) { simg="static/images/cb.jpg"; } //生成图片流 DrawingUtil.drawImage("static/images/cc.jpg", simg, stu.get("name"), stu.get("studentNo"), stu.get("examName"), // "深圳市保安员资格考试", // "SZBAZGKS" + stu.get("studentNo").substring(stu.get("studentNo").length()>=6?stu.get("studentNo").length()-6:0), stu.get("number"), stu.get("classRoomName"), stu.get("examRoomName"), stu.get("address"), stu.get("address2"), stu.get("startTime"), stu.get("endTime"), stu.get("sex"),bo); //设置头信息 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//static/images/ca1.jpg //UUID作为文件名 headers.setContentDispositionFormData("attachment",fileName); //获取字节数据 byte[] bytes = bo.toByteArray(); //交给spring entity = new ResponseEntity(bytes, headers, statusCode); // } catch (IOException e) { // // } catch (Exception e) { // e.printStackTrace(); } finally { try { assert bo != null; bo.close(); } catch (IOException e) { e.printStackTrace(); } } return entity; } private Map getStudent(String studentNo,String examBatchId){ //获取用户信息 StuStudent stu=null; if (studentNo!=null&&!"".equals(studentNo)&&!"null".equals(studentNo)){ stu = studentService.getStudentByNo(studentNo); }else { stu= studentService.getStudentByUserId(ClientUtils.getUserId()); } //获取批次信息 ExamBatchInfo examBatchInfo = examBatchService.queryExamBatchDetail(examBatchId); ExamInfo examInfo = examBatchInfo.getExamInfo(); //获取考点名称 String classRoomId=examBatchInfo.getClassRoomID(); ClassRoom detail = classRoomService.detail(classRoomId); String classRoomName = detail.getName(); //获取考点地址 String address = detail.getAddress(); //获取考场名称 String examRoomName=examBatchInfo.getExamRoomName(); //获取考试开始结束时间 String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(examBatchInfo.getStartTime()); String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(examBatchInfo.getEndTime()); //根据批次Id查询所在组Id与组的所有考生集合 String classId = examBatchService.queryExamBatchByClassId(examBatchId).getClassId(); // RedisTemplate redisTemplate = new RedisTemplate<>(); // //取出 // List students = (List) redisTemplate.boundHashOps("Hashlist").get("students"); // if(students==null){ // students= studentService.getStudentByclassId(classId); // //存储 // redisTemplate.boundHashOps("Hashlist").put("students", students); // } // int number=1; String number = ClientUtils.getUserInfo().getInfo("age"); // for (int i=1;i<=students.size();i++) { // if (students.get(i-1).getStudentNo().equals(stu.getStudentNo()) ) { // //座位号 // number=i; // } // } //添加信息 Map student=new HashMap<>(); student.put("name",stu.getName()); student.put("studentNo",stu.getStudentNo()); student.put("classRoomName",classRoomName); student.put("examName",examInfo.getExamName()); if(address.contains("/")){ String[] split = address.split("/"); if(split.length>=2){ student.put("address",split[0]); student.put("address2",split[1]); }else { student.put("address",address); student.put("address2",address); } }else { student.put("address",address); student.put("address2",address); } student.put("examRoomName",examRoomName); student.put("startTime",startTime); student.put("endTime",endTime); student.put("number",number+""); student.put("sex",stu.getSex()?"男":"女"); //TODO 下载状态 studentService.updateImgStudent(stu.getStudentId()); ClientUtils.getUserInfo().setInfo("age",String.valueOf(Integer.parseInt(number)+1)); return student; } }