SchduleTask.java 6.57 KB
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());
    }


}