派生自 projectDept/qhighschool

yn147
2023-05-10 96286178ee1c257c130cb2ad964a781f36c4eee5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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<byte[]> draw(String studentNo,String examBatchId, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取准考证信息
        Map<String,String> stu = getStudent(studentNo,examBatchId);
 
        ResponseEntity<byte[]> 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<byte[]>(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<String,String> 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<StuStudent> students = (List<StuStudent>) 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<String,String> 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;
    }
}