WeigengApplicationTests.java
23.7 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package com.sincere.weigeng;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifDirectory;
import com.sincere.weigeng.dao.UserDao;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class WeigengApplicationTests {
@Autowired
TestDao testDao;
@Test
public void contextLoads() {
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\taohandong\\Desktop\\json.txt"));
String content = null;
StringBuilder stringBuilder = new StringBuilder();
while ((content = bufferedReader.readLine()) != null) {
stringBuilder.append(content);
}
JSONObject jsonObject = new JSONObject(stringBuilder.toString());
JSONObject data = jsonObject.optJSONObject("data");
JSONArray questions = data.optJSONArray("questions");
for (int i = 0; i < questions.length(); i++) {
JSONObject question = questions.optJSONObject(i);
String question_text = question.optString("question_text");
String answer = question.optString("answer");//答案
String explanation = question.optString("explanation");//解析
// <p><img src="/Web/Assets/ueditor/asp/upload/image/20190916/15686369926495741.png" title="image.png" alt="image.png"/></p>
explanation = "<p><img src=\"" + explanation + "\" title=\"image.png\" alt=\"image.png\"/></p>";
answer = getAnswer(answer);
System.out.println("question_text:" + question_text + "\r\nanswer:" + answer + "\r\nexplanation:" + explanation);
List<QuestionBean> questionBeans = testDao.getQuestions(question_text);//获取题目
for (int j = 0; j < questionBeans.size(); j++) {
QuestionBean questionBean = questionBeans.get(j);
testDao.updateQuestion(questionBean.getID(), answer, explanation);
}
// System.out.println(questionBeans.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=24.26683b650b73b63e0b08afa7ac36e880.2592000.1571310480.282335-15990462";
private String getAnswer(String imgUrl) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
// multiValueMap.add("image", ImageUtils.ImageToBase64ByLocal("C:\\Users\\taohandong\\Desktop\\识别.png"));
multiValueMap.add("url", imgUrl);
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(multiValueMap,
headers);
ResponseEntity<String> result = restTemplate.postForEntity(url, requestEntity, String.class);
JSONObject jsonObject = new JSONObject(result.getBody());
// System.out.println("result:"+result.getBody());
JSONArray words_result = jsonObject.optJSONArray("words_result");
// System.out.println("words_result:"+words_result);
StringBuilder stringBuilder = new StringBuilder();
if (words_result != null) {
for (int i = 0; i < words_result.length(); i++) {
String words = words_result.optJSONObject(i).optString("words");
stringBuilder.append(words + " ");
}
System.out.println("result:" + stringBuilder.toString());
}
return stringBuilder.toString().equals("") ? "B" : stringBuilder.toString();
}
@Test
public void get() {
// String url = "https://zujuan.21cnjy.com/api/question/list?xd=1&chid=3&categories=3877&knowledges=&question_channel_type=1&difficult_index=&exam_type=&kid_num=&grade_id=&sort_field=time&filterquestion=0&page=&_grade_id=&tree_type=category&version_id=&_=1568766250186";
// RestTemplate restTemplate = new RestTemplate();
// System.out.println(restTemplate.getForObject(url, String.class));
/*String createrUserId = "zy273789", time = "2019-10-08";
List<TestPaper> testPaperList = testDao.getTestPapers(createrUserId, time);
for (int i = 0; i < testPaperList.size(); i++) {
TestPaper testPaper = testPaperList.get(i);
int schoolId = 11;
testDao.addTestPaper("zy284782", testPaper.getQuestionIds(), testPaper.getStatus(), testPaper.getPublishTime(),
testPaper.getName(), testPaper.getIsRecommend(), testPaper.getState(), testPaper.getIntime(), schoolId + "");
}*/
}
/**
* 创建试卷
*/
@Test
public void createTestPaper() {
String createUserId = "zy309728";
List<TestPaper> testPaperList = testDao.getTestPapers(createUserId, "2019-09-18 16:00:00");
System.out.println("testPaperList:" + testPaperList.toString());
String addCreateUserId = "zy411337", SchoolId = "885";
/* for (int i = 1; i < testPaperList.size(); i++) {
TestPaper testPaper = testPaperList.get(i);
int ID = testPaper.getID();
//添加试卷
testDao.addTestPaper(addCreateUserId, testPaper.getQuestionIds(), testPaper.getStatus(), testPaper.getPublishTime()
, testPaper.getName(), testPaper.getIsRecommend(), testPaper.getState(), testPaper.getIntime(), SchoolId);
//发布作业
testDao.addHomework(addCreateUserId, testPaper.getID(), testPaper.getName(), "50", "2019-09-20 18:00", "2", "2019-09-19 19:10", 1, 0, 1, "2019-09-19 19:10", 0, testPaper.getQuestionIds(), 0, 0);
}*/
/*String[] answers = new String[]{"A", "B", "C", "D"};
String QuestionType = "1";
String CorrectAnswer = "";
List<Homework> homeworkList = testDao.getHomeWork(addCreateUserId);
for (int i = 0; i < homeworkList.size(); i++) {
Homework homework = homeworkList.get(i);
String[] questionIds = homework.getQuestionIds().split(",");
for (int j = 0; j < questionIds.length; j++) {
String questionId = questionIds[j];
QuestionBean questionBean = testDao.getQues(questionId);
CorrectAnswer = questionBean.getCorrectAnswer();
String answer = answers[new Random().nextInt(4)];
String IsCorresct = answer.trim().equals(CorrectAnswer.trim()) ? "1" : "3";
testDao.StudentAnswer(questionId, answer, CorrectAnswer, QuestionType, IsCorresct, homework.getID());
}
}*/
// if (ID <= 4191 && ID >= 4187) {
String classId = "36398";
List<Homework> homeworkList = testDao.getHomeWork(addCreateUserId);
for (int i = 0; i < homeworkList.size(); i++) {
String workId = homeworkList.get(i).getID();
// List<String> stuAnsIds = testDao.getStudentAnsIds(workId);
// String stuAnIds = stuAnsIds.toString().replace("[", "");
// stuAnIds = stuAnIds.replace("]", "");
List<String> strings = testDao.getStudentIdsWithClassId(classId);
String stuIds = strings.toString().replace("[", "");
stuIds = stuIds.replace("]", "");
String[] stuStrings = stuIds.split(",");//学生id
for (int j = 0; j < stuStrings.length; j++) {
String studentId = stuStrings[j];
testDao.HomeworkReceive(workId, classId, studentId, "", "1", j + "", "2019-09-23 21:28:00", "2019-09-22 17:50:00", "1", j + "");
}
}
}
@Autowired
UserDao userDao;
@Test
public void doHomeWork() {
StudentBean studentBean = new StudentBean();
studentBean.setCard("9A1229A9");
studentBean.setSchool_id(110);
studentBean.setStudentCode("140881200710270056");
studentBean.setCustomerId("583912");
// userDao.addStudentUpdate(studentBean.getCard(),studentBean.getSchool_id(),studentBean.getStudentCode(),studentBean.getCustomerId(),"1","2"
// ,"55640","2014级(08)班","0","1","2019-09-22 16:26");
/* String QuestionId = "21656, 21657, 21658, 21659, 21753, 21706, 21724, 21796, 21797, 21798";
String Answer = "";
String CorrectAnswer = "";
String QuestionType = "1";
String IsCorresct = "";
String WorkID = "4198";
String[] questionIds = QuestionId.split(",");
for (int i = 0; i < questionIds.length; i++) {
String questionId = questionIds[i];
QuestionBean questionBean = testDao.getQues(questionId);
CorrectAnswer = questionBean.getCorrectAnswer();
Answer = "B";
IsCorresct = Answer.trim().equals(CorrectAnswer.trim()) ? "1" : "3";
testDao.StudentAnswer(questionId, Answer, CorrectAnswer, QuestionType, IsCorresct, WorkID);
}*/
}
@Test
public void HomeworkRec() {
String WorkID = "4198";
String ClassId = "";
//查询学生id
List<String> list = testDao.getStudentIds("4172");
String StudentIds = list.toString().replace("[", "");
StudentIds = StudentIds.replace("]", "");
System.out.println("StudentIds:" + StudentIds);
String StudnetAnswerIds = testDao.getStudentAnsIds(WorkID).toString().replace("[", "");
StudnetAnswerIds = StudnetAnswerIds.replace("]", "");
String[] studentIds = StudentIds.split(",");
for (int i = 0; i < studentIds.length; i++) {
String studentId = studentIds[i];
String finishTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
testDao.HomeworkReceive(WorkID, ClassId, studentId, StudnetAnswerIds, "1", i + "", "2019-09-19 16:50", finishTime
, "1", "" + i);
}
}
@Test
public void getQIds() {
List<Knowledge> knowledges = userDao.getKnowledge();
System.out.println("knowledges:" + knowledges.toString());
List<JiaoCai> jiaoCais = userDao.getJiaoCai();
System.out.println("jiaoCais:" + jiaoCais.toString());
for (int i = 0; i < 1; i++) {
JiaoCai jiaoCai = jiaoCais.get(i);
String SubjectId = jiaoCai.getSubjectId();
for (int j = 0; j < knowledges.size(); j++) {
Knowledge knowledge = knowledges.get(j);
if (SubjectId.equals(knowledge.getSubjectId())) {
userDao.addKnowAndJicaoCai(jiaoCai.getID(), knowledge.getId(), 0, 1);
}
}
}
}
@Test
public void tongji() {
String[] answers = new String[]{"A", "B", "C", "D", "A", "A"};
String createUserId = "zy411337";
List<Homework> homeworkList = testDao.getHomeWork(createUserId);//获取布置的作业
System.out.println("homeworkList:" + homeworkList);
for (int m = 0; m < homeworkList.size(); m++) {
String WorkId = homeworkList.get(m).getID();
Map<String, List<String>> mapRecIDs = new HashMap<>();
System.out.println("workId:" + WorkId);
// List<String> queIds = testDao.getStuAnsIds(WorkId);
// String questionIds = queIds.toString().replace("[", "");
// questionIds = questionIds.replace("]", "");
//获取正确答案
// List<String> correct = testDao.correctAns(WorkId);
// String corrStr = correct.toString().replace("[", "");
// corrStr = corrStr.replace("]", "");
// String[] correctAnser = corrStr.split(",");
String questionIds = testDao.getQuestionIds(WorkId);
//获取答题学生数量
List<String> homeworkReceiveIds = testDao.get_HomeworkReceive(WorkId, "12012");
System.out.println("homeworkReceiveIds:" + homeworkReceiveIds.size());
for (int j = 0; j < homeworkReceiveIds.size(); j++) {//接收学生数量
String[] quesStirngs = questionIds.split(",");
System.out.println("quesStirngs:" + quesStirngs.length);
for (int i = 0; i < quesStirngs.length; i++) {
String questionId = quesStirngs[i];
String CorrectAnswer = testDao.getCorrectAnswer(questionId);
int randow = new Random().nextInt(6);
String answer = null;
String isCorrsct = "";
if (randow == 0 || randow == 1 || randow == 2 || randow == 3 || randow == 4) {
answer = CorrectAnswer.trim();
isCorrsct = "1";
} else {
answer = answers[randow];
isCorrsct = "3";
}
//插入一条新的记录
testDao.addStudentAnswer(quesStirngs[i], answer.trim(), "", CorrectAnswer.trim(), "1", "", isCorrsct, "", "", WorkId);
}
//获取最新的十条记录
List<String> idLists = testDao.getTopIDs(WorkId, questionIds.split(",").length);
System.out.println("idLists:" + idLists.toString());
mapRecIDs.put(j + "", idLists);
}
// System.out.println("mapRecIDs:" + mapRecIDs.toString() + " 0:" + mapRecIDs.get("0").toString().replace("[", ""));
for (int i = 0; i < homeworkReceiveIds.size(); i++) {
String homeRecId = homeworkReceiveIds.get(i);
System.out.println("i:" + i);
String ansIds = mapRecIDs.get(i + "").toString().replace("[", "");
ansIds = ansIds.replace("]", "");
testDao.updateHomeWork(ansIds, homeRecId);
}
List<HomeworkReceive> homRecIDs = testDao.get_HomeworkRec(WorkId);
Map<String, String> stuMap = new HashMap<>();
for (int i = 0; i < homRecIDs.size(); i++) {
HomeworkReceive homeworkReceive = homRecIDs.get(i);
String studentId = homeworkReceive.getStudentId();
String studentName = testDao.getStudentName(studentId);
String studnetAnswerIds = homeworkReceive.getStudnetAnswerIds();
String[] stuIds = studnetAnswerIds.split(",");
String isC = "";
for (int j = 0; j < stuIds.length; j++) {
String id = stuIds[j];
int idCorrect = testDao.getIsCorrect(id);
if (idCorrect == 1) {
isC += "1 ";
} else if (idCorrect == 3) {
isC += "0 ";
}
}
stuMap.put(studentName, isC);
System.out.println("" + studentName + " " + isC);
}
}
}
private void writeToExcel(HSSFWorkbook workbook, String HomeworkTitle, Map<String, String[]> correctAns, String[] names, String title) {
//第二部,在workbook中创建一个sheet对应excel中的sheet
HSSFSheet sheet = workbook.createSheet(HomeworkTitle);
//第三部,在sheet表中添加表头第0行,老版本的poi对sheet的行列有限制
HSSFRow row = sheet.createRow(0);
//第四步,创建单元格,设置表头
HSSFCell cell = null;
for (int i = 0; i < correctAns.get(names[0]).length; i++) {
cell = row.createCell(i);
cell.setCellValue("第" + (i + 1) + "");
}
//第五步,写入数据
for (int i = 0; i < names.length; i++) {
// String oneData =correctAns[i];//姓名对应的正确率
HSSFRow row1 = sheet.createRow(i + 1);
String[] corrects = correctAns.get(names[i]);
System.out.println("names:" + names[i]);
for (int j = 0; j < corrects.length; j++) {
//创建单元格设值
row1.createCell(j).setCellValue(corrects[j]);
}
row1.createCell(corrects.length).setCellValue(names[i]);
}
//将文件保存到指定的位置
try {
File file = new File("C:\\Users\\taohandong\\Desktop\\文澜\\"+title+".xls");
if (!file.exists())file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
workbook.write(fos);
System.out.println("写入成功");
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void getStu() {
//第一步,创建一个workbook对应一个excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
String createUserId = "zy595910";
String title="作业";
//ZY_Homework表取出要统计的作业
String WorkId = "4408";
String workName = testDao.getWorkName(WorkId);
//ZY_HomeworkReceive 表获取学生作业答题情况;
List<HomeworkReceive> homRecIDs = testDao.get_HomeworkRec(WorkId);
Map<String, String[]> stuMap = new HashMap<>();
String[] names = new String[homRecIDs.size()];
String[] isCorrect = null;
for (int i = 0; i < homRecIDs.size(); i++) {
HomeworkReceive homeworkReceive = homRecIDs.get(i);
String studentId = homeworkReceive.getStudentId();
String studentName = testDao.getStudentName(studentId);
String studnetAnswerIds = homeworkReceive.getStudnetAnswerIds();
// System.out.println("studnetAnswerIds:"+studnetAnswerIds+" WorkId:"+WorkId + " StudentId:"+studentId);
if (null != studnetAnswerIds) {
names[i] = studentName;
String[] stuIds = studnetAnswerIds.split(",");
isCorrect = new String[stuIds.length];
String isC = "";
for (int j = 0; j < stuIds.length; j++) {
String id = stuIds[j];
System.out.println("IsCorrectId:" + id);
//ZY_StudentAnswer表获取题目是否正确
int idCorrect = testDao.getIsCorrect(id);
if (idCorrect == 1) {
isC += "1 ";
isCorrect[j] = "1";
} else if (idCorrect == 3) {
isC += "0 ";
isCorrect[j] = "0";
}
}
stuMap.put(studentName, isCorrect);
System.out.println("" + studentName + " " + isC);
}
}
if (names!=null&&stuMap.size()>0)
writeToExcel(workbook, workName, stuMap, names,title);
// System.out.println("stuMap:" + stuMap.toString());
}
/**
* 旋转图片
*/
@Test
public void trnImgs() {
File file = new File("C:\\TaoHandong\\copy\\school479\\StudentCompressed");
File[] files = file.listFiles();
List<StudentInfo> studentInfos = testDao.getStudents(479);
List<StudentInfo> studentInfoList = new ArrayList<>();
for (int i = 0; i < studentInfos.size(); i++) {
StudentInfo studentInfo = studentInfos.get(i);
boolean isHas = false;
for (int j = 0; j < files.length; j++) {
File img = files[j];
String imgName = img.getName().split("\\.")[0];
if (imgName.equals(studentInfo.getStudentcode())) {
isHas = true;
break;
}
}
if (!isHas) {
studentInfoList.add(studentInfo);
}
}
System.out.println("studentInfoList:" + studentInfoList);
//第一步,创建一个workbook对应一个excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
//第二部,在workbook中创建一个sheet对应excel中的sheet
HSSFSheet sheet = workbook.createSheet("缺少的学生人脸");
//第三部,在sheet表中添加表头第0行,老版本的poi对sheet的行列有限制
HSSFRow row = sheet.createRow(0);
String[] titles = new String[]{"姓名", "班级", "学籍号", "号码"};
//第四步,创建单元格,设置表头
HSSFCell cell = null;
for (int i = 0; i < titles.length; i++) {
cell = row.createCell(i);
cell.setCellValue(titles[i]);
}
//第五步,写入数据
for (int i = 0; i < studentInfoList.size(); i++) {
HSSFRow row1 = sheet.createRow(i + 1);
for (int j = 0; j < titles.length; j++) {
String value = studentInfoList.get(i).getClass_name();
switch (j) {
case 0:
value = studentInfoList.get(i).getName();
break;
case 1:
value = studentInfoList.get(i).getClass_name();
break;
case 2:
value = studentInfoList.get(i).getStudentcode();
break;
case 3:
value = studentInfoList.get(i).getParentMobile();
break;
}
//创建单元格设值
row1.createCell(j).setCellValue(value);
}
}
//将文件保存到指定的位置
try {
FileOutputStream fos = new FileOutputStream("C:\\Users\\taohandong\\Desktop\\result.xls");
workbook.write(fos);
System.out.println("写入成功");
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
/* File file = new File("C:\\TaoHandong\\copy\\School1030\\Student");
File outFile = new File("C:\\TaoHandong\\copy\\School1030\\Student1");
if (!outFile.exists()) outFile.mkdirs();
File[] files = file.listFiles();
System.out.println("files:" + files.length);
BufferedImage bufferedImage;
for (int i = 0; i < files.length; i++) {
File img = files[i];
int ang = ImageUtils.getImgRotateAngle(img.getAbsolutePath());
System.out.println(img.getName() + " ang:" + ang);
// try {
// bufferedImage = ImageIO.read(img);
// moveImg(bufferedImage,img,outFile);
// } catch (IOException e) {
// e.printStackTrace();
// }
}*/
}
private List<String> name = new ArrayList<>();
}