派生自 projectDept/qhighschool

胡仁荣
2022-10-31 fe7381d6e8ec1f427408de0297ac7f41533202f6
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;
    }
}