FileControl.java 7.61 KB
package com.example.dahua.control;

import com.example.dahua.MyTask;
import com.example.dahua.async.SendUserInfoTask;
import com.example.dahua.attendance.Attendance;
import com.example.dahua.bean.AttendanceBean;
import com.example.dahua.bean.TeacherBean;
import com.example.dahua.bean.UploadImg;
import com.example.dahua.dao.UserDao;
import com.example.dahua.lib.ToolKits;
import com.example.dahua.module.GateModule;
import com.example.dahua.service.UserService;
import com.sun.jna.Memory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

@RestController
@Api("文件管理器")
@RequestMapping(value = "file/*", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class FileControl {

    @Autowired
    UserService userService;

    @Autowired
    SendUserInfoTask sendUserInfoTask;

    @Autowired
    UserDao userDao;

    @RequestMapping(method = RequestMethod.POST, value = "uploadImg")
    public String uploadImg(@RequestParam("file") MultipartFile file, @RequestParam("schoolId") String schoolId, @RequestParam("studentCode") String studentCode,
                            @RequestParam("clint_type") String clint_type, @RequestParam("userType") int userType) {
        System.out.println("schoolId:" + schoolId + " studentCode:" + studentCode);
        String fileName = file.getOriginalFilename();//文件名

        File outFile = new File("C://imgCom");
        if (!outFile.exists()) outFile.mkdirs();
        try {
            File dest = new File(outFile, fileName);
            FileOutputStream fileOutputStream = new FileOutputStream(dest);

            fileOutputStream.write(file.getBytes());
            fileOutputStream.close();
            userService.uploadImgAndUserInfo(dest.getAbsolutePath(), schoolId, studentCode, clint_type, userType, "");
            return "1";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "0";
    }


    @RequestMapping(method = RequestMethod.POST, value = "uploadImgToDev")
    public String uploadImgToDev(@RequestParam("file") MultipartFile file, @RequestParam("schoolId") String schoolId, @RequestParam("studentCode") String studentCode,
                                 @RequestParam("clint_type") String clint_type, @RequestParam("userType") int userType, @RequestParam("devid") String devid) {
        String fileName = file.getOriginalFilename();//文件名

        File outFile = new File("C://imgCom");
        if (!outFile.exists()) outFile.mkdirs();
        try {
            File dest = new File(outFile, fileName);
            FileOutputStream fileOutputStream = new FileOutputStream(dest);

            fileOutputStream.write(file.getBytes());
            fileOutputStream.close();
            userService.uploadImgAndUserInfo(dest.getAbsolutePath(), schoolId, studentCode, clint_type, userType, devid);
            return "1";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "0";
    }

    @RequestMapping(value = "deleteFace", method = RequestMethod.GET)
    @ApiOperation("删除人脸")
    public void deleteFace(@RequestParam("cardNum") String cardNum, @RequestParam("deviceId") String deviceId) {

        sendUserInfoTask.deleteFace(cardNum, deviceId);

    }


    @RequestMapping(value = "imgsSend", method = RequestMethod.GET)
    @ApiOperation(value = "照片下放")
    public boolean imgsSend(@RequestParam("schoolId") String schoolId, @RequestParam("type") int type) {
        userService.sendUserInfos(schoolId, "22", type);
        return true;
    }


    @RequestMapping(value = "findCard", method = RequestMethod.GET)
    @ApiOperation(value = "查询设备卡信息")
    public boolean findCard(@RequestParam("cardNum") String cardNum, @RequestParam("deviceId") String deviceId) {
        boolean fincard = GateModule.findCard(cardNum, MyTask.lLongMap.get(deviceId));
        System.out.println("fincard:" + fincard + MyTask.lLongMap.get(deviceId));

        return true;
    }


    @RequestMapping(value = "addCard", method = RequestMethod.GET)
    @ApiOperation(value = "下发卡信息")
    public int addCard(@RequestParam("schoolId") String schoolId, @RequestParam("targPath") String targPath) {

        File file = new File(targPath);

        File[] files = file.listFiles();

        for (int i = 0; i < files.length; i++) {
            File imgFile = files[i];
            String num = imgFile.getName().split("\\.")[0];
            TeacherBean teacherBean = userDao.getTeacher(schoolId, num);

            List<AttendanceBean> attendanceBeans = userDao.selectDeviceWithschool_id("22", schoolId);
            for (int j = 0; j < attendanceBeans.size(); j++) {
                AttendanceBean attendanceBean = attendanceBeans.get(j);
                int addCard = GateModule.insertCard(cardNo(teacherBean.getTeacher_num()), teacherBean.getUser_id(), teacherBean.getName(),
                        "123456", 1, 1, 1, 1, 1,
                        "2020-01-01 00:00:00", "2040-01-01 12:00:00", MyTask.lLongMap.get(attendanceBean.getClint_id()));
                Memory memory = ToolKits.readPictureFile(imgFile.getAbsolutePath());
                GateModule.addFaceInfo(teacherBean.getUser_id(), memory, MyTask.lLongMap.get(attendanceBean.getClint_id()));
                userDao.saveRecordNo(teacherBean.getUser_id(), addCard, teacherBean.getName(), attendanceBean.getClint_id());
                System.out.println("teacherBean:" + teacherBean.toString() + "  clientid:" + attendanceBean.getClint_id());
            }


        }


        return 1;
    }


    @RequestMapping(value = "deleteCard", method = RequestMethod.GET)
    @ApiOperation(value = "删除卡信息")
    public boolean deleteCard(@RequestParam("recordNum") int recordNum, @RequestParam("deviceId") String deviceId) {
        boolean deleteCard = GateModule.deleteCard(recordNum, MyTask.lLongMap.get(deviceId));
        System.out.println("addCard:" + deleteCard + "----" + MyTask.lLongMap.get(deviceId));
        return deleteCard;
    }


    @RequestMapping(value = "clearCard", method = RequestMethod.GET)
    @ApiOperation(value = "清空卡信息")
    public boolean clearCard(@RequestParam("deviceId") String deviceId) {
        boolean clearCard = GateModule.clearCard(MyTask.lLongMap.get(deviceId));
        System.out.println("addCard:" + clearCard + "----" + MyTask.lLongMap.get(deviceId));
        return clearCard;
    }

    @RequestMapping(value = "clearFaceInfo", method = RequestMethod.GET)
    @ApiOperation(value = "清空人脸信息")
    public boolean clearFaceInfo(@RequestParam("deviceId") String deviceId) {
        boolean clearCard = GateModule.clearFaceInfo(MyTask.lLongMap.get(deviceId));
        System.out.println("addCard:" + clearCard + "----" + MyTask.lLongMap.get(deviceId));
        return clearCard;
    }


    public 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 cardR;

    }
}