SchduleTask.java
6.57 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
package com.sincere.haikang;
import com.sincere.haikang.async.SendUserAsync;
import com.sincere.haikang.bean.AttendanceBean;
import com.sincere.haikang.bean.StudentBean;
import com.sincere.haikang.dao.DeviceDao;
import com.sincere.haikang.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@Component
@EnableScheduling
public class SchduleTask {
File saveFile = new File("C:\\save");
File savePth = new File("C:\\save\\save.txt");
@Autowired
UserDao userDao;
@Value("${clint_type}")
private String clint_type;
@Autowired
SendUserAsync sendUserAsync;
/**
* 定时任务,取更新的学生信息
*/
@Scheduled(fixedRate = 5000)
public synchronized void getUpdateStudentInfo() {
try {
if (!saveFile.exists()) {
try {
saveFile.mkdirs();
File saveFile_txt = new File(saveFile, "save.txt");
if (!saveFile_txt.exists()) saveFile_txt.createNewFile();
} catch (IOException e) {
System.out.println("e:" + e.toString());
e.printStackTrace();
}
}
FileInputStream fileInputStream = new FileInputStream(savePth);
byte[] readByte = new byte[1024];
fileInputStream.read(readByte);
//存储的数据库下标
String index = new String(readByte, 0, readByte.length).trim();
// System.out.println("定时任务取得下标:" + index);
List<StudentBean> studentBeans = null;
if (StringUtils.isEmpty(index)) {//没记录,取前十条
studentBeans = userDao.getStudents();
// System.out.println("getStudents:"+studentBeans.toString());
} else {
studentBeans = userDao.getAllStudents(Long.parseLong(index),2);
// System.out.println("getAllStudents:"+studentBeans.toString());
}
// System.out.println("定时任务:" + studentBeans.toString());
// System.out.println("studentBeans:"+studentBeans);
if (sendUserAsync != null && studentBeans != null && studentBeans.size() > 0){
sendUserAsync.sendStu(studentBeans, 1);
for (int i = 0; i < studentBeans.size(); i++) {
Thread.sleep(100);
StudentBean studentBean = studentBeans.get(i);
// System.out.println("studentBeans:"+studentBean.toString());
if (null!=studentBean.getStudentCode()&&0!=studentBean.getSchoolId()&&null!=studentBean.getCard()){
sendCImg(studentBean);
sendCard(studentBean);
}
}
}
if (studentBeans.size() > 0 && CMSServer.deviceAndLoginIdMap.keySet().size() > 0) {
FileOutputStream fileOutputStream = null;
fileOutputStream = new FileOutputStream(savePth);
fileOutputStream.write((studentBeans.get(0).getID() + "").getBytes());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendCImg(StudentBean studentBean){
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("StudentCode", studentBean.getStudentCode());
multiValueMap.add("ParentId", "0");
multiValueMap.add("SchoolId", studentBean.getSchoolId()+"");
multiValueMap.add("OldCard", "");
multiValueMap.add("Card", studentBean.getCard());
multiValueMap.add("TerminalType", "1");//1:海康
multiValueMap.add("Count", "1");
multiValueMap.add("UpdateType", "1");
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(multiValueMap,
headers);
String url = "http://campus.myjxt.com/api/OneCard/UpdateDataBK";
// String url = "http://114.55.30.100:9020/facereco/SendStudentSingleImage?schoolId=110&deviceId=212476514&studentCode=" + studdentCode;
ResponseEntity<String> result = restTemplate.postForEntity(url, requestEntity, String.class);
// String result = restTemplate.getForObject(url, String.class);
// System.out.println("result:" + result.getBody() + " studdentCode:" + studentBean.getStudentCode());
}
private void sendCard(StudentBean studentBean){
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("StudentCode", studentBean.getStudentCode());
multiValueMap.add("ParentId", "0");
multiValueMap.add("SchoolId", studentBean.getSchoolId()+"");
multiValueMap.add("OldCard", "");
multiValueMap.add("Card", studentBean.getCard());
multiValueMap.add("TerminalType", "2");//2:微耕
multiValueMap.add("Count", "1");
multiValueMap.add("UpdateType", "1");
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(multiValueMap,
headers);
String url = "http://campus.myjxt.com/api/OneCard/UpdateDataBK";
// String url = "http://114.55.30.100:9020/facereco/SendStudentSingleImage?schoolId=110&deviceId=212476514&studentCode=" + studdentCode;
ResponseEntity<String> result = restTemplate.postForEntity(url, requestEntity, String.class);
// String result = restTemplate.getForObject(url, String.class);
// System.out.println("result:" + result.getBody() + " studdentCode:" + studentBean.getStudentCode());
}
}