FileControl.java 5.55 KB
package com.sincere.haikangface.control;

import com.alibaba.fastjson.JSON;
import com.sincere.haikangface.CMSServer;
import com.sincere.haikangface.async.SendUserAsync;
import com.sincere.haikangface.bean.StudentBean;
import com.sincere.haikangface.bean.face.PermissionBean;
import com.sincere.haikangface.dao.UserDao;
import com.sincere.haikangface.service.impl.BaseService;
import com.sincere.haikangface.utils.CompressPic;
import com.sincere.haikangface.utils.FileUtils;
import com.sincere.haikangface.xiananDao.SendRecordDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;

@RestController
@Api("文件管理器")
@RequestMapping("file/*")
@Slf4j
public class FileControl {

    @Autowired
    SendUserAsync sendUserAsync;

    @Autowired
    SendRecordDao sendRecordDao;

    @Autowired
    UserDao userDao;

    @Autowired
    CMSServer cmsServer;

    @Autowired
    BaseService baseService;

    @RequestMapping(value = "sendPermiss", method = RequestMethod.POST)
    @ApiOperation("下发权限给设备")
    public boolean sendPermiss(@RequestBody PermissionBean permissionBean) {
        log.error("permissionBean:{}", JSON.toJSONString(permissionBean));
        if (!StringUtils.isEmpty(permissionBean)) {
            return sendUserAsync.sendPermiss(permissionBean, 1);
        }
        return false;
    }

    @RequestMapping(method = RequestMethod.POST, value = "uploadImg")
    public String uploadImg(@RequestParam("file") MultipartFile file, @RequestParam("card") String card
            , @RequestParam("name") String name, @RequestParam("deviceId") String deviceId, @RequestParam("startTime") String startTime,
                            @RequestParam("endTime") String endTime, @RequestParam("validTimeEnabled") int validTimeEnabled, @RequestParam("userType") String userType) {
        try {
            if (!cmsServer.getIsDeviceOnline(deviceId)) {
                log.warn("设备ID: {},设备不在线",deviceId);
                return "0";
            }
            if(StringUtils.isEmpty(card)){
                log.warn("卡号为空");
                return "0";
            }
            String fileName = file.getOriginalFilename();//文件名
            File outFile = new File(".//imgCom");
            if (!outFile.exists()) outFile.mkdirs();
            File dest = new File(outFile, fileName);
            FileOutputStream fileOutputStream = new FileOutputStream(dest);
            fileOutputStream.write(file.getBytes());
            fileOutputStream.close();
            String filePath = dest.getAbsolutePath();
            long time = System.currentTimeMillis();
            StudentBean studentBean = userDao.getStudentWithCard(card);
            Integer schoolId = studentBean ==null ?null:studentBean.getSchool_id();
            String cardNum = Long.parseLong(baseService.getCard(card),16) + "";
            if (new File(filePath.trim()).exists()) {
                String targetPath = FileUtils.picPathComp + new File(filePath).getName();
                try {
                    CompressPic.CompressPic(filePath, targetPath);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    log.info("下发人脸,人脸路径:{},用户名:{},下发设备:{}",filePath,name,deviceId);
                    sendUserAsync.sendStuToHaiKang(filePath, targetPath,cardNum,startTime, endTime, validTimeEnabled, name, deviceId, userType,schoolId,card);
                    System.out.println("time:" + (System.currentTimeMillis() - time) / 1000);
                    return "1";
                } catch (Exception e) {
                    e.printStackTrace();
                    return "0";
                }
            } else {
                baseService.sendFail(cardNum, filePath, deviceId,"文件不存在", userType);
                log.error("文件不存在:" + filePath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "0";
    }

    @RequestMapping(value = "IsDeviceOnline", method = RequestMethod.GET)
    @ApiOperation("判断设备是否在线")
    public String IsDeviceOnline(@RequestParam("deviceId") String deviceId) {
        return cmsServer.getIsDeviceOnline(deviceId) ? "1" : "0";
    }


    @RequestMapping(value = "DeleteCard", method = RequestMethod.GET)
    @ApiOperation("删除人脸")
    public String deleteCard(@RequestParam("deviceId") String deviceId, @RequestParam("card") String card) {
        StudentBean studentBean = userDao.getStudentWithCard(card);
        Integer schoolId = studentBean ==null ?null:studentBean.getSchool_id();
        cmsServer.deleteCard(deviceId, Long.parseLong(baseService.getCard(card), 16) + "",schoolId);
        return "1";
    }

    @RequestMapping(value = "getCard", method = RequestMethod.GET)
    @ApiOperation("获取设备人脸是否存在")
    public String getCard(@RequestParam("deviceId") String deviceId, @RequestParam("card") String card) {
        StudentBean studentBean = userDao.getStudentWithCard(card);
        Integer schoolId = studentBean ==null ?null:studentBean.getSchool_id();
        if (cmsServer.getFace(deviceId, Long.parseLong(baseService.getCard(card), 16) + "",schoolId)){
            return "1";
        }else{
            return "0";
        }
    }
}