SendUserInfoTask.java 11.9 KB
package com.example.dahua.async;

import com.example.dahua.MyTask;
import com.example.dahua.bean.AttendanceBean;
import com.example.dahua.bean.TeacherBean;
import com.example.dahua.bean.UserInfoBean;
import com.example.dahua.common.Res;
import com.example.dahua.dao.UserDao;
import com.example.dahua.lib.CompressPic;
import com.example.dahua.lib.FilePath;
import com.example.dahua.lib.NetSDKLib;
import com.example.dahua.lib.ToolKits;
import com.example.dahua.module.GateModule;
import com.example.dahua.service.UserService;
import com.sun.jna.Memory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import sun.rmi.runtime.Log;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 下发卡号的异步操作
 */
@Component
public class SendUserInfoTask {

    @Autowired
    UserDao userDao;

    @Async("taskExecutor")
    public void doTaskOne(String file, List<AttendanceBean> attendanceBeans, UserInfoBean userInfoBean) throws Exception {
        File studentFile = new File(file);
        String studentCode = studentFile.getName().split("\\.")[0];
        //判断是否是副卡
        String[] studentCodes = studentCode.split("_");

        if (studentCodes.length > 1) {//副卡

            String cardType = studentCodes[1];
            String cardNum = userDao.getCardNum(userInfoBean.getStudent_id(), cardType);
            userInfoBean.setStudent_num(cardNum);

        }

        sendUserInfoToDev(file, attendanceBeans, userInfoBean);
    }

    @Async("taskExecutor")
    public void doTaskSendUserInfos(String schoolId, String clint_type, int type) {
        try {
            List<AttendanceBean> attendanceBeans = userDao.getAttendanceBeans(schoolId, clint_type);
            String filePathStudent = "E:\\wwwhtdocs\\SmartCampus\\face17e50\\School" + schoolId + "\\Student";
            String filePathParent = "E:\\wwwhtdocs\\SmartCampus\\face17e50\\School" + schoolId + "\\Parent";
            String filePathTeacher = "E:\\wwwhtdocs\\SmartCampus\\face17e50\\School" + schoolId + "\\Teacher";
//            String filePathStudent = "F:\\wwwroot\\SmartCampus\\face17e50\\School" + schoolId + "\\Student";
//            String filePathParent = "F:\\wwwroot\\SmartCampus\\face17e50\\School" + schoolId + "\\Parent";
//            String filePathParent = "C:\\Users\\taohandong\\Desktop\\小视频";
            List<File> fileList = new ArrayList<>();
            File filePaths = null;
            if (type == 0) {//主卡下发

                filePaths = new File(filePathStudent);

            } else if (type == 1) {//副卡下发

                filePaths = new File(filePathParent);

            } else if (type == 2) {//教师卡下发
                filePaths = new File(filePathTeacher);
            }else {
                filePaths = new File("");
            }

            if (!filePaths.exists()){
                System.out.println("图片目录不存在");
                return;
            }

            File[] filesStudent = filePaths.listFiles();
            if (filesStudent == null) {
                System.out.println("没有找到人脸图片");
                return;
            }
            /**
             * 添加学生人脸
             */
            for (int i = 0; i < filesStudent.length; i++) {
                fileList.add(filesStudent[i]);
            }

//            System.out.println("files:" + fileList.size());

            while (fileList.size() > 0) {
                File studentFile = fileList.get(0);
                String studentCode = studentFile.getName().split("\\.")[0];
                UserInfoBean userInfoBean = null;
                //判断是否是副卡
                String[] studentCodes = studentCode.split("_");
                System.out.println("学籍号:" + studentCode);
                if (studentCodes.length > 1) {//副卡

                    String studentcode = studentCodes[0];
                    String cardType = studentCodes[1];
                    userInfoBean = userDao.getUserInfo(schoolId, studentcode);
                    if (!userIsExit(fileList, studentcode, userInfoBean)) continue;
                    String cardNum = userDao.getCardNum(userInfoBean.getStudent_id(), cardType);
                    userInfoBean.setStudent_num(cardNum);

                } else {

                    if (type == 0 || type == 1) {//学生信息或家长信息
                        userInfoBean = userDao.getUserInfo(schoolId, studentCode);
                    } else if (type == 2) {//教师信息
                        TeacherBean teacher = userDao.getTeacher(schoolId, studentCode);
                        if (null != teacher) {
                            userInfoBean = new UserInfoBean();
                            userInfoBean.setStudent_num(teacher.getTeacher_num());
                            userInfoBean.setName(teacher.getName());
                            userInfoBean.setUser_id(teacher.getUser_id());
                            userInfoBean.setStudentcode(teacher.getNum());
                        }
                    }

                    //判断用户是否存在
                    if (!userIsExit(fileList, studentCode, userInfoBean)) continue;

                }

                boolean sendResult = sendUserInfoToDev(studentFile.getAbsolutePath(), attendanceBeans, userInfoBean);
                fileList.remove(0);
            }
//            System.out.println("全部下发完成:");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 判断用户是否存在
     *
     * @param fileList
     * @param studentcode
     * @param userInfoBean
     */
    private boolean userIsExit(List<File> fileList, String studentcode, UserInfoBean userInfoBean) {
        if (userInfoBean == null) {
            System.out.println("用户信息不存在:" + studentcode);
            fileList.remove(0);
            return false;
        }
        return true;
    }

    /**
     * 发送人员信息给设备
     *
     * @param file
     * @param attendanceBeans
     * @param userInfoBean
     * @return
     */

    private boolean sendUserInfoToDev(String file, List<AttendanceBean> attendanceBeans, UserInfoBean userInfoBean) {
        try {
            if (userInfoBean == null) {//"用户不存在"
                System.out.println("用户不存在");
                return false;
            }
            if (attendanceBeans.size() == 0) {//"该学校下没有人脸设备"
                System.out.println("该学校下没有人脸设备:");
                return false;
            }

            String cardNum = userInfoBean.getStudent_num();
            System.out.println("cardNum:" + cardNum);
            if (StringUtils.isEmpty(cardNum) || cardNum.equals("null")) {
                System.out.println("卡号为空");
                return false;
            }

            /**
             * 保存图片到本地
             */
            sendTodev(file, userInfoBean, attendanceBeans);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * @param picSrc          图片地址
     * @param userInfoBean    用户信息
     * @param attendanceBeans 设备列表
     */
    private void sendTodev(String picSrc, UserInfoBean userInfoBean, List<AttendanceBean> attendanceBeans) {
        Memory memory = null;//图片缓存
        try {
            String targPath = FilePath.picPathComp + userInfoBean.getStudentcode() + ".jpg";
            CompressPic.CompressPic(picSrc, targPath);//压缩后的图片
            memory = ToolKits.readPictureFile(targPath);
            System.out.println("下发学生信息:" + "targPath:" + new File(targPath).exists() + targPath + " userInfoBean:" + userInfoBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (AttendanceBean attendanceBean :
                attendanceBeans) {

            pushCardAndFace(attendanceBean, userInfoBean.getUser_id(), userInfoBean.getStudent_num(), userInfoBean.getName(), memory);
        }
    }

    private synchronized void pushCardAndFace(AttendanceBean attendanceBean, String user_id, String student_num, String name, Memory memory) {
        String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, 4);
        String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
        int bCardFlags = -1;//记录集编号
        bCardFlags = userDao.getRecordNo(user_id) == null ? -1 : Integer.parseInt(userDao.getRecordNo(user_id));

        NetSDKLib.LLong loginHandleLong = MyTask.lLongMap.get(attendanceBean.getClint_id());

        boolean bFaceFalgs = false;

        String cardNum = cardNo(student_num);
        boolean ifExits = GateModule.findCard(cardNum, loginHandleLong);
        System.out.println("卡号是否存在:" + ifExits);
        if (bCardFlags != -1) {//修改卡信息
            GateModule.deleteCard(bCardFlags, loginHandleLong);
            userDao.deleteRecordNo(user_id, bCardFlags);

//            boolean update = GateModule.modifyCard(bCardFlags, cardNum, userInfoBean.getUser_id(), userInfoBean.getName(), "123456"
//                    , Res.string().getCardStatusInt(1), Res.string().getCardTypeInt(1), 0, 0
//                    , 1, startTime, endTime, loginHandleLong);
//            System.out.println("update:" + update);

            bCardFlags = GateModule.insertCard(cardNum, user_id, name, "123456"
                    , Res.string().getCardStatusInt(1), Res.string().getCardTypeInt(1), 0, 0
                    , 1, startTime, endTime, loginHandleLong);

            if (bCardFlags != -1) {
                int index = userDao.saveRecordNo(user_id, bCardFlags, name);//存储记录集编号
            }
            bFaceFalgs = GateModule.modifyFaceInfo(user_id, memory, loginHandleLong);
            //卡号添加成功,但是人脸不成功的话,就需要新增人脸
            if (!bFaceFalgs)
                bFaceFalgs = GateModule.addFaceInfo(user_id, memory, loginHandleLong);

        } else {//新增卡信息
            bCardFlags = GateModule.insertCard(cardNum, user_id, name, "123456"
                    , Res.string().getCardStatusInt(1), Res.string().getCardTypeInt(1), 0, 0
                    , 1, startTime, endTime, loginHandleLong);

            if (bCardFlags != -1) {
                int index = userDao.saveRecordNo(user_id, bCardFlags, name);//存储记录集编号
            }

            //添加人脸
            bFaceFalgs = GateModule.addFaceInfo(user_id, memory, loginHandleLong);

        }

        if (bCardFlags != -1 && bFaceFalgs) {
            System.out.println("人脸下发成功");
        }

        // 添加卡信息和人脸失败
        if (bCardFlags == -1 && !bFaceFalgs) {
            System.out.println("添加卡信息和人脸失败");
        }

        // 添加卡信息成功,添加人脸失败
        if (bCardFlags != -1 && !bFaceFalgs) {
//                System.out.println("添加卡信息成功,添加人脸失败");
        }

        // 卡信息已存在,添加人脸成功
        if (bCardFlags == -1 && bFaceFalgs) {
            System.out.println("卡信息已存在,添加人脸成功");
        }
    }


    private String cardNo(String cardDex) {

        String cardR = "";

        int length = cardDex.length();
        if (length != 8) {
            System.out.println("卡号格式不正确:" + cardDex);
            return cardDex;
        }
        while (length > 0) {
            length -= 2;
            cardR += cardDex.substring(length, length + 2);
        }

//        return Long.parseLong(cardR, 16) + "";
        return cardR;

    }

}