package com.template.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.template.annotation.DESRespondSecret;
import com.template.annotation.PassToken;
import com.template.api.SmartCarAccessControllerAPI;
import com.template.api.SmartFaceDiscernControllerAPI;
import com.template.common.utils.*;
import com.template.config.ControlConfig;
import com.template.model.pojo.*;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.vo.AskForLeaveScreenVo;
import com.template.model.vo.FaceManagementVo;
import com.template.model.vo.LastSevenDaysTrackVo;
import com.template.model.vo.SmartAccessVo;
import com.template.services.*;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;


import static com.template.common.utils.AesTestOne.decrypt;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-01-18
 */
@RestController
//返回参数加密注解
@DESRespondSecret
public class SmartFaceDiscernController implements SmartFaceDiscernControllerAPI {
    private static Logger logger = LoggerFactory.getLogger(SmartFaceDiscernController.class);

    @Autowired
    SmartFaceDiscernService smartFaceDiscernService;

    @Autowired
    SmartQrcodeService smartQrcodeService;

    @Autowired
    SmartCarAccessService smartCarAccessService;

    @Autowired
    SmartAttendanceService smartAttendanceService;

    @Autowired
    SmartUserService smartUserService;

    @Autowired
    SmartClassService smartClassService;

    @Autowired
    SmartUploadService smartUploadService;

    @Autowired
    SmartNotificationService smartNotificationService;

    @Autowired
    SmartDeviceService smartDeviceService;

    @Autowired
    SmartAccessService smartAccessService;

    @Autowired
    SmartPushConfigService smartPushConfigService;

    @Override
    @DESRespondSecret(validated = true)
    public CommonResult track(Integer id, int dateTime) {
        JSONObject date = getDate(dateTime);

        String stateTime = date.getString("stateTime");
        String endTime = date.getString("endTime");

        List<SmartFaceDiscern> smartFaceDiscerns = smartFaceDiscernService.track(stateTime, endTime, id);
        ArrayList<LastSevenDaysTrackVo> vos = new ArrayList<>();
        for (SmartFaceDiscern faceDiscern : smartFaceDiscerns) {
            LastSevenDaysTrackVo lastSevenDaysTrackVo = new LastSevenDaysTrackVo();
            lastSevenDaysTrackVo.setName(faceDiscern.getName());
            lastSevenDaysTrackVo.setType(faceDiscern.getType());
            lastSevenDaysTrackVo.setLocation(faceDiscern.getLocation());
            lastSevenDaysTrackVo.setImage(faceDiscern.getImage());
            lastSevenDaysTrackVo.setDateTime(faceDiscern.getDateTime());
            vos.add(lastSevenDaysTrackVo);
        }

        List<SmartAttendance> attendances = smartAttendanceService.track(stateTime, endTime, id);
//        获取班级名称
        SmartUser smartUser = smartUserService.getSmartById(id);
        SmartClass smartClass = smartClassService.getSmartClassById(smartUser.getSchoolClass());
        String className = smartClass.getName();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (SmartAttendance attendance : attendances) {
            LastSevenDaysTrackVo lastSevenDaysTrackVo = new LastSevenDaysTrackVo();
            lastSevenDaysTrackVo.setName(attendance.getXwUserName());
            lastSevenDaysTrackVo.setType("打卡");
            lastSevenDaysTrackVo.setLocation(className);
            Date attendTime = attendance.getAttendTime();
            lastSevenDaysTrackVo.setDateTime(sdf.format(attendTime));
            vos.add(lastSevenDaysTrackVo);
        }

//        门禁信息
        List<SmartAccess> smartAccesses = smartAccessService.track(stateTime, endTime, id);
        for (SmartAccess faceDiscern : smartAccesses) {
            LastSevenDaysTrackVo lastSevenDaysTrackVo = new LastSevenDaysTrackVo();
            lastSevenDaysTrackVo.setName(faceDiscern.getName());
            lastSevenDaysTrackVo.setType(faceDiscern.getType());
            lastSevenDaysTrackVo.setLocation(faceDiscern.getLocation());
            lastSevenDaysTrackVo.setImage(faceDiscern.getImage());
            lastSevenDaysTrackVo.setDateTime(faceDiscern.getDateTime());
            vos.add(lastSevenDaysTrackVo);
        }

//        排序
        Collections.sort(vos, Comparator.comparing((h) -> {
            return h.getDateTime();
        }));


        return CommonResult.ok(vos);
    }

    @Override
    @DESRespondSecret(validated = true)
    public CommonResult access() {

        LocalDateTime now = LocalDateTime.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < 30; i++) {
            LocalDateTime stateTime = now.minusDays(i + 1);
            LocalDateTime endTime = now.minusDays(i);
            String dateTime = endTime.format(dateTimeFormatter);
            LambdaQueryWrapper<SmartAccess> smartAccessWrapper = new LambdaQueryWrapper<>();
            smartAccessWrapper.between(SmartAccess::getDateTime, stateTime, endTime);
            int faceDiscernCount = smartAccessService.count(smartAccessWrapper);

            LambdaQueryWrapper<SmartQrcode> qrcodeWrapper = new LambdaQueryWrapper<>();
            qrcodeWrapper.between(SmartQrcode::getCreateTime, stateTime, endTime);
            int qrcodeCount = smartQrcodeService.count(qrcodeWrapper);

            LambdaQueryWrapper<SmartCarAccess> smartCarAccessWrapper = new LambdaQueryWrapper<>();
            smartCarAccessWrapper.between(SmartCarAccess::getEnterTime, stateTime, endTime);
            int carCount = smartCarAccessService.count(smartCarAccessWrapper);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("date", dateTime);
            jsonObject.put("face", faceDiscernCount);
            jsonObject.put("code", qrcodeCount);
            jsonObject.put("car", carCount);
            jsonArray.add(jsonObject);

        }

        return CommonResult.ok(jsonArray);
    }

    @Override
    @DESRespondSecret(validated = true)
    public CommonResult lastSevenDaysTrack(int userId) {
        LocalDateTime end = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).plusDays(1);
        LocalDateTime satrt = end.minusDays(7);
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String endTime = end.format(dateTimeFormatter1);
        String startTime = satrt.format(dateTimeFormatter1);
        ArrayList<LastSevenDaysTrackVo> vos = new ArrayList<>();
//        人脸抓拍
        List<SmartFaceDiscern> faceDiscerns = smartFaceDiscernService.track(startTime, endTime, userId);
        for (SmartFaceDiscern faceDiscern : faceDiscerns) {
            LastSevenDaysTrackVo lastSevenDaysTrackVo = new LastSevenDaysTrackVo();
            lastSevenDaysTrackVo.setName(faceDiscern.getName());
            lastSevenDaysTrackVo.setType(faceDiscern.getType());
            lastSevenDaysTrackVo.setLocation(faceDiscern.getLocation());
            lastSevenDaysTrackVo.setImage(faceDiscern.getImage());
            lastSevenDaysTrackVo.setDateTime(faceDiscern.getDateTime());
            vos.add(lastSevenDaysTrackVo);
        }

//        打卡
        List<SmartAttendance> attendances = smartAttendanceService.track(startTime, endTime, userId);
//        获取班级名称
        SmartUser smartUser = smartUserService.getSmartById(userId);
        SmartClass smartClass = smartClassService.getSmartClassById(smartUser.getSchoolClass());
        String className = smartClass.getName();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (SmartAttendance attendance : attendances) {
            LastSevenDaysTrackVo lastSevenDaysTrackVo = new LastSevenDaysTrackVo();
            lastSevenDaysTrackVo.setName(attendance.getXwUserName());
            lastSevenDaysTrackVo.setType("打卡");
            lastSevenDaysTrackVo.setLocation(className);
            Date attendTime = attendance.getAttendTime();
            lastSevenDaysTrackVo.setDateTime(sdf.format(attendTime));
            vos.add(lastSevenDaysTrackVo);
        }

        //        门禁信息
        List<SmartAccess> smartAccesses = smartAccessService.track(startTime, endTime, userId);
        for (SmartAccess faceDiscern : smartAccesses) {
            LastSevenDaysTrackVo lastSevenDaysTrackVo = new LastSevenDaysTrackVo();
            lastSevenDaysTrackVo.setName(faceDiscern.getName());
            lastSevenDaysTrackVo.setType(faceDiscern.getType());
            lastSevenDaysTrackVo.setLocation(faceDiscern.getLocation());
            lastSevenDaysTrackVo.setImage(faceDiscern.getImage());
            lastSevenDaysTrackVo.setDateTime(faceDiscern.getDateTime());
            vos.add(lastSevenDaysTrackVo);
        }

//        排序
        Collections.sort(vos, Comparator.comparing((h) -> {
            return h.getDateTime();
        }));


        return CommonResult.ok(vos);
    }

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public String callBack(HttpServletRequest request, HttpServletResponse response) {
        try {
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null) {
                responseStrBuilder.append(inputStr);
            }
            JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString());

            DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            //                比对时间
            String time = jsonObject.getString("time");
            logger.info("time = " + time);
//            LocalDateTime localDateTime = LocalDateTime.now().withHour(0).withSecond(0).withMinute(0);
//            LocalDateTime localDateTime2 = LocalDateTime.parse(time, dateTimeFormatter1);
////            只获取当天的
//            if (localDateTime.isBefore(localDateTime2)) {
//            比对结果
            Integer resultStatus = jsonObject.getInteger("resultStatus");
//                if (1 == resultStatus) {

            //          设备sn
            String sn = jsonObject.getString("sn");
            SmartDevice smartDevice = smartDeviceService.getNum(sn);
            String address = "";
            String type = "";
            if (ObjectUtils.isNotEmpty(smartDevice)) {
                //            地点
                address = smartDevice.getAddress();
//            类型
                type = smartDevice.getName();
            }

//            名字,学生会加班级[]
            String name = jsonObject.getString("name");
            System.out.println("name = " + name);
            String[] split = name.split("\\[");
            String s = split[0];
            logger.info("name = " + s);
//            百胜门禁编号
            String idNum = jsonObject.getString("idNum");
            logger.info("idNum = " + idNum);

            Integer userId = 0;
            SmartUser smartUser = null;
            if (ObjectUtils.isNotEmpty(idNum)) {
                smartUser = smartUserService.getBsStudentNo(idNum);
                if (ObjectUtils.isNotEmpty(smartUser)) {
                    userId = smartUser.getId();
                } else {
                    userId = 0;
                }
            }

//                    出入标识: 1-进, 0-出
            Integer inout = jsonObject.getInteger("inout");
//                    开门方式：0-白名单比对，1-人证比对，2-IC卡比对
            Integer openType = jsonObject.getInteger("openType");

            SmartAccess smartFaceDiscern = new SmartAccess();

            smartFaceDiscern.setName(s);
            smartFaceDiscern.setLocation(address);
            smartFaceDiscern.setType(type);
            smartFaceDiscern.setUserId(userId);
            smartFaceDiscern.setDateTime(time);
            smartFaceDiscern.setIdNum(idNum);
            smartFaceDiscern.setSn(sn);
            smartFaceDiscern.setInOut(inout);
            smartFaceDiscern.setOpenType(openType);
            smartFaceDiscern.setResultStatus(resultStatus);
            if (userId != 0) {
                //                    判断是否已经添加
                LambdaQueryWrapper<SmartAccess> wrapperFD = new LambdaQueryWrapper<>();
                wrapperFD.eq(SmartAccess::getDateTime, smartFaceDiscern.getDateTime())
                        .eq(SmartAccess::getUserId, userId);
                List<SmartAccess> list = smartAccessService.list(wrapperFD);
                if (ObjectUtils.isEmpty(list) && list.size() == 0) {
                    //          抓拍的照片 base64字符串
                    String scenePhoto = jsonObject.getString("scenePhoto");
                    scenePhoto = "data:image/jpeg;base64," + scenePhoto;
                    logger.info("scenePhoto =" + scenePhoto.substring(0, 50));
//            base64转文件
                    MultipartFile multipartFile = MultipartFileUtils.base64ToMultipartFile(scenePhoto);
                    String image = "";
                    try {
                        //            上传到cos桶,并生成图片地址
                        image = smartUploadService.upload(new MultipartFile[]{multipartFile});
                    } catch (Exception e) {
                        e.printStackTrace();
                        logger.error("上传cos桶失败");
                    }

                    smartFaceDiscern.setImage(image);
                    smartAccessService.save(smartFaceDiscern);

                    Integer identityId = smartUser.getIdentityId();
//                    判断是否是学生
                    if (2==identityId) {
                        //                    获取推送配置
                        List<SmartPushConfig> pushConfigs = smartPushConfigService.list(new LambdaQueryWrapper<>());
                        SmartPushConfig smartPushConfig = pushConfigs.get(0);
                        Integer parentsPush = smartPushConfig.getParentsPush();
                        Integer chargeTeacherPush = smartPushConfig.getChargeTeacherPush();

                        if (parentsPush == 1) {
//                        通过学生id找到关联的家长affiliate,并找到公众号,如果openid为空则不传
                            List<SmartUser> userList = smartUserService.getAffiliateList(userId);
                            if (ObjectUtils.isNotEmpty(userList) && userList.size() > 0) {

                                for (SmartUser user : userList) {
                                    String gzhOpenId = user.getGzhOpenId();
                                    if (ObjectUtils.isNotEmpty(gzhOpenId)) {
                                        String pushType = "";
                                        if (type.contains("进校")) {
                                            pushType = "进入大门";
                                        } else if (type.contains("出校")) {
                                            pushType = "离开大门";
                                        }else if (type.contains("进地下室")){
                                            pushType = "进入地下室";
                                        }else if (type.contains("出地下室")){
                                            pushType = "离开地下室";
                                        }
                                        LocalDateTime date = LocalDateTime.now();
                                        String format = date.format(dateTimeFormatter1);
                                        //                    公众号信息推送
                                        Message2.send2(gzhOpenId,s,format,address);

                                        SmartNotification smartNotification = new SmartNotification();
                                        smartNotification.setUserId(user.getId());
                                        smartNotification.setUserName(smartUser.getName());
                                        smartNotification.setTypeName(pushType);
                                        smartNotification.setLocation(address);
                                        smartNotification.setImage(image);
                                        smartNotification.setDateTime(time);
                                        smartNotification.setType(2);

                                        smartNotificationService.save(smartNotification);

                                    }

                                }
                            }
                        }

                        if (1==chargeTeacherPush) {
                            Integer schoolClass = smartUser.getSchoolClass();
//                        通过班级找到对应的班主任
                            List<SmartUser> userLists = smartUserService.getChargeTeacher(schoolClass);
                            if (ObjectUtils.isNotEmpty(userLists) && userLists.size() > 0) {

                                for (SmartUser user : userLists) {
                                    String gzhOpenId = user.getGzhOpenId();
                                    if (ObjectUtils.isNotEmpty(gzhOpenId)) {
                                        String pushType = "";
                                        if (type.contains("进校")) {
                                            pushType = "进入大门";
                                        } else if (type.contains("出校")) {
                                            pushType = "离开大门";
                                        }else if (type.contains("进地下室")){
                                            pushType = "进入地下室";
                                        }else if (type.contains("出地下室")){
                                            pushType = "离开地下室";
                                        }
                                        LocalDateTime date = LocalDateTime.now();
                                        String format = date.format(dateTimeFormatter1);
                                        //                    公众号信息推送
                                        Message2.send2(gzhOpenId,s,format,address);

                                        SmartNotification smartNotification = new SmartNotification();
                                        smartNotification.setUserId(user.getId());
                                        smartNotification.setUserName(smartUser.getName());
                                        smartNotification.setTypeName(pushType);
                                        smartNotification.setLocation(address);
                                        smartNotification.setImage(image);
                                        smartNotification.setDateTime(time);
                                        smartNotification.setType(2);

                                        smartNotificationService.save(smartNotification);

                                    }

                                }
                            }


                        }

                    }

                }
            } else {

                smartFaceDiscern.setUserId(0);
//                    判断是否已经添加
                LambdaQueryWrapper<SmartAccess> wrapperFD = new LambdaQueryWrapper<>();
                wrapperFD.eq(SmartAccess::getDateTime, time)
                        .eq(SmartAccess::getName, s)
                        .eq(SmartAccess::getType, type);
                List<SmartAccess> list = smartAccessService.list(wrapperFD);

                if (ObjectUtils.isEmpty(list) && list.size() == 0) {
                    //          抓拍的照片 base64字符串
                    String scenePhoto = jsonObject.getString("scenePhoto");
                    scenePhoto = "data:image/jpeg;base64," + scenePhoto;
                    logger.info("scenePhoto =" + scenePhoto.substring(0, 50));
//            base64转文件
                    MultipartFile multipartFile = MultipartFileUtils.base64ToMultipartFile(scenePhoto);
                    String image = "";
                    try {
                        //            上传到cos桶,并生成图片地址
                        image = smartUploadService.upload(new MultipartFile[]{multipartFile});
                    } catch (Exception e) {
                        e.printStackTrace();
                        logger.error("上传cos桶失败");
                    }
                    smartFaceDiscern.setImage(image);
                    smartAccessService.save(smartFaceDiscern);
                }

            }
//            }
//            }

        } catch (Exception e) {
            e.printStackTrace();
            JSONObject r = new JSONObject();
            r.put("message", "Success");
            r.put("result", 0);
            return r.toJSONString();
        }

        JSONObject r = new JSONObject();
        r.put("message", "Success");
        r.put("result", 0);
        return r.toJSONString();
    }

    @Override
    public CommonResult selectTrack(Integer userId, String startTime, String endTime) {
        ArrayList<LastSevenDaysTrackVo> vos = new ArrayList<>();
        //        人脸抓拍
        List<SmartFaceDiscern> faceDiscerns = smartFaceDiscernService.track(startTime, endTime, userId);
        for (SmartFaceDiscern faceDiscern : faceDiscerns) {
            LastSevenDaysTrackVo lastSevenDaysTrackVo = new LastSevenDaysTrackVo();
            lastSevenDaysTrackVo.setName(faceDiscern.getName());
            lastSevenDaysTrackVo.setType(faceDiscern.getType());
            lastSevenDaysTrackVo.setLocation(faceDiscern.getLocation());
            lastSevenDaysTrackVo.setImage(faceDiscern.getImage());
            lastSevenDaysTrackVo.setDateTime(faceDiscern.getDateTime());
            vos.add(lastSevenDaysTrackVo);
        }

//        打卡
        List<SmartAttendance> attendances = smartAttendanceService.track(startTime, endTime, userId);
//        获取班级名称
        SmartUser smartUser = smartUserService.getSmartById(userId);
        SmartClass smartClass = smartClassService.getSmartClassById(smartUser.getSchoolClass());
        String className = smartClass.getName();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (SmartAttendance attendance : attendances) {
            LastSevenDaysTrackVo lastSevenDaysTrackVo = new LastSevenDaysTrackVo();
            lastSevenDaysTrackVo.setName(attendance.getXwUserName());
            lastSevenDaysTrackVo.setType("打卡");
            lastSevenDaysTrackVo.setLocation(className);
            Date attendTime = attendance.getAttendTime();
            lastSevenDaysTrackVo.setDateTime(sdf.format(attendTime));
            vos.add(lastSevenDaysTrackVo);
        }

        //        门禁信息
        List<SmartAccess> smartAccesses = smartAccessService.track(startTime, endTime, userId);
        for (SmartAccess faceDiscern : smartAccesses) {
            LastSevenDaysTrackVo lastSevenDaysTrackVo = new LastSevenDaysTrackVo();
            lastSevenDaysTrackVo.setName(faceDiscern.getName());
            lastSevenDaysTrackVo.setType(faceDiscern.getType());
            lastSevenDaysTrackVo.setLocation(faceDiscern.getLocation());
            lastSevenDaysTrackVo.setImage(faceDiscern.getImage());
            lastSevenDaysTrackVo.setDateTime(faceDiscern.getDateTime());
            vos.add(lastSevenDaysTrackVo);
        }

//        排序
        Collections.sort(vos, Comparator.comparing((h) -> {
            return h.getDateTime();
        }));


        return CommonResult.ok(vos);
    }

    @Override
    @DESRespondSecret(validated = true)
    public CommonResult faceManagement(int currentPage, int pageCount, String keyWord,String gradeId,String classId,String startTime,String endTime) {
        PageUtils<FaceManagementVo> result= smartFaceDiscernService.faceManagement(currentPage,pageCount,keyWord,gradeId,classId,startTime,endTime);

        return CommonResult.ok(result);
    }

    @Override
    public void faceManagementExport(HttpServletResponse response, String keyWord, String gradeId, String classId, String startTime, String endTime) {
        //导出
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("人脸记录");
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("序号");
        headerRow.createCell(1).setCellValue("姓名");
        headerRow.createCell(2).setCellValue("班级");
        headerRow.createCell(3).setCellValue("年级");
        headerRow.createCell(4).setCellValue("地点");
        headerRow.createCell(5).setCellValue("图片");
        headerRow.createCell(6).setCellValue("创建时间");

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //        默认前一天的
        if (ObjectUtils.isEmpty(startTime)||ObjectUtils.isEmpty(endTime)) {
            LocalDateTime now = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
            LocalDateTime end = now.plusDays(1);
            startTime=now.format(dateTimeFormatter);
            endTime=end.format(dateTimeFormatter);
        }

        List<FaceManagementVo> vos= smartFaceDiscernService.faceManagementExport(keyWord,gradeId,classId,startTime,endTime);

        for (int i = 0; i < vos.size(); i++) {
            FaceManagementVo vo = vos.get(i);
            Row dataRow = sheet.createRow(i + 1);
            dataRow.createCell(0).setCellValue(i + 1);
            dataRow.createCell(1).setCellValue(vo.getName());
            dataRow.createCell(2).setCellValue(vo.getClassName());
            dataRow.createCell(3).setCellValue(vo.getGradeName());
            dataRow.createCell(4).setCellValue(vo.getLocation());
            dataRow.createCell(5).setCellValue(vo.getImage());
            dataRow.createCell(6).setCellValue(vo.getDateTime());

        }

        // 将工作簿写入文件
        ExcelUtils.excelDownload(workbook, "人脸记录.xlsx", response);
    }


    public static JSONObject getDate(Integer date) {
        JSONObject jsonObject = new JSONObject();

        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime stateTime = now.withHour(0).withMinute(0).withSecond(0).minusDays(date);

        LocalDateTime endTime = now.withHour(23).withMinute(59).withSecond(59).minusDays(date);

        jsonObject.put("stateTime", stateTime.format(dateTimeFormatter1));
        jsonObject.put("endTime", endTime.format(dateTimeFormatter1));

        return jsonObject;

    }

    public static void main(String[] args) {
        LocalDateTime dateTime1 = LocalDateTime.of(2022, 8, 29, 12, 0, 0);
        LocalDateTime dateTime2 = LocalDateTime.of(2022, 8, 30, 12, 0, 0);
        LocalDateTime dateTime3 = LocalDateTime.of(2022, 8, 28, 12, 0, 0);

        boolean isBefore = dateTime1.isBefore(dateTime2);
        boolean isBefore2 = dateTime1.isBefore(dateTime3);

        System.out.println("dateTime1 is before dateTime2: " + isBefore);
        System.out.println("dateTime1 is before dateTime2: " + isBefore2);

    }

}

