SchduleTask.java 7.94 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.apache.http.util.TextUtils;
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;

    @Autowired
    DeviceDao deviceDao;

    private boolean isSending = false;

    /**
     * 定时任务,取更新的学生信息
     */
    @Scheduled(fixedRate = 5000)
    public synchronized void getUpdateStudentInfo() {
        if (!isSending) {
            isSending = true;

            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.size());
                if (sendUserAsync != null && studentBeans != null && studentBeans.size() > 0) {
                    sendUserAsync.sendStu(studentBeans, 1);
                    for (int i = studentBeans.size() - 1; i >= 0; i--) {
                        Thread.sleep(200);
                        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);
                            FileOutputStream fileOutputStream = null;
                            fileOutputStream = new FileOutputStream(savePth);
                            System.out.println("getId:" + studentBeans.get(0).getID());
                            fileOutputStream.write((studentBean.getID() + "").getBytes());
                            fileInputStream.close();
                            fileOutputStream.close();
                        }
                    }
                }

                /*if (studentBeans.size() > 0 && CMSServer.deviceAndLoginIdMap.keySet().size() >= 0) {
                    FileOutputStream fileOutputStream = null;
                    fileOutputStream = new FileOutputStream(savePth);
                    System.out.println("getId:"+studentBeans.get(0).getID());
                    fileOutputStream.write((studentBeans.get(0).getID() + "").getBytes());
                    fileInputStream.close();
                    fileOutputStream.close();
                }*/
            } catch (Exception e) {
                e.printStackTrace();
            }
            isSending = false;
        }
    }

    private void sendCImg(StudentBean studentBean) {

        int SchoolId = studentBean.getSchoolId();

        List<AttendanceBean> attendanceBeans = deviceDao.getAttendancesWithSchoolId(SchoolId);

        int type = -1;//18为海康,22为大华

        String clientIds = "";
        for (int i = 0; i < attendanceBeans.size(); i++) {
            if (i < attendanceBeans.size() - 1) {
                if (attendanceBeans.get(i).getClint_type().equals("22")) {
                    type = 22;
                    clientIds += attendanceBeans.get(i).getClint_id() + ",";
                } else if (attendanceBeans.get(i).getClint_type().equals("18")) {
                    type = 18;
                    clientIds += attendanceBeans.get(i).getClint_id();
                }
            }
        }

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        String card = TextUtils.isEmpty(studentBean.getCard()) ? studentBean.getOldCard() : studentBean.getCard();
        String imgFile = "E://wwwhtdocs//SmartCampus//face17e5//School" + SchoolId + "//Student//" + studentBean.getStudentCode() + ".png";

        if (type == 18 && !TextUtils.isEmpty(card)) {
            String url = "http://114.55.30.100:8089/facereco/sendFaceToDevices?card=" + card + "&deviceIds=" + clientIds
                    + "&file=" + imgFile + "&name=" + studentBean.getName() + "&userType=2";
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
            System.out.println("下发人脸:" + responseEntity.getBody());
        } else if (type == 22 && !TextUtils.isEmpty(card)) {
            String url = "http://121.40.109.21:8991/user/uploadImgAndUserInfo?clint_type=22&file=" + imgFile + "&schoolId=" + SchoolId + "&studentCode=" + studentBean.getStudentCode();
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
            System.out.println("下发人脸:" + responseEntity.getBody());

        }


//            String result = restTemplate.getForObject(url, String.class);
//        System.out.println("result:" + result.getBody() + " studdentCode:" + studentBean.getStudentCode());
    }

    private void sendCard(StudentBean studentBean) {
        int SchoolId = studentBean.getSchoolId();
        String card = TextUtils.isEmpty(studentBean.getCard()) ? studentBean.getOldCard() : studentBean.getCard();
        if (!TextUtils.isEmpty(card)) {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            String url = "http://campus.myjxt.com/api/OneCard/SendSingle?SchoolId=" + SchoolId + "&Card=" + card + "&ClassId=" + studentBean.getClassId();
            System.out.println("下发卡号url:" + url);
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
            System.out.println("下发卡号:" + responseEntity.getBody());
        }


    }


}