package com.template.controller;


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.api.SmartAccessControllerAPI;
import com.template.common.utils.ExcelUtils;
import com.template.model.pojo.SmartAccess;
import com.template.model.pojo.SmartCarAccess;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.vo.SmartAccessVo;
import com.template.model.vo.SmartAttendanceVo;
import com.template.model.vo.TotalCountVo;
import com.template.services.SmartAccessService;
import com.template.services.SmartCarAccessService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-05-31
 */
@RestController
@DESRespondSecret
public class SmartAccessController implements SmartAccessControllerAPI {

    @Autowired
    SmartAccessService smartAccessService;

    @Autowired
    SmartCarAccessService smartCarAccessService;

    @Override
    @DESRespondSecret(validated = true)
    public CommonResult getPage(Integer currentPage, Integer pageCount, String keyWord,Integer gradeId, Integer classId, Integer departmentId,String openType,String resultStatus,String startTime,String endTime) {
        PageUtils<SmartAccessVo> pageUtils=smartAccessService.getPage(currentPage,pageCount,keyWord,gradeId,classId,departmentId,openType,resultStatus,startTime,endTime);
        return CommonResult.ok(pageUtils);
    }

    @Override
    public void getPageExport(HttpServletResponse response, String keyWord, Integer gradeId, Integer classId, Integer departmentId, String openType,String resultStatus, 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("人员编号");
        headerRow.createCell(7).setCellValue("图像");
        headerRow.createCell(8).setCellValue("识别分组");
        headerRow.createCell(9).setCellValue("记录时间");
        headerRow.createCell(10).setCellValue("出口类型");
        headerRow.createCell(11).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<SmartAccessVo> vos=smartAccessService.getPageExport(keyWord,gradeId,classId,departmentId,openType,resultStatus,startTime,endTime);

        for (int i = 0; i < vos.size(); i++) {
            SmartAccessVo vo = vos.get(i);
            Row dataRow = sheet.createRow(i + 1);
            dataRow.createCell(0).setCellValue(i + 1);
            dataRow.createCell(1).setCellValue(vo.getSn());
            dataRow.createCell(2).setCellValue(vo.getType());
            dataRow.createCell(3).setCellValue(vo.getDepartmentName());
            dataRow.createCell(4).setCellValue(vo.getClassName());
            dataRow.createCell(5).setCellValue(vo.getName());
            dataRow.createCell(6).setCellValue(vo.getCardNo());
            dataRow.createCell(7).setCellValue(vo.getImage());

            Integer openType1 = vo.getOpenType();
            String type="";
            if (0==openType1) {
                type="白名单比对";
            }else if (1==openType1){
                type="人证比对";
            }else if (2==openType1){
                type="IC卡比对";
            }else if (5==openType1){
                type="职工二维码比对";
            }
            dataRow.createCell(8).setCellValue(type);
            dataRow.createCell(9).setCellValue(vo.getDateTime());
            Integer access = vo.getAccess();
            String accessSting="";
            if (1==access) {
                accessSting="入口";
            }else if (0==openType1){
                accessSting="出口";
            }
            dataRow.createCell(10).setCellValue(accessSting);

            Integer resultStatus1 = vo.getResultStatus();
            String result="";
            if (1==resultStatus1) {
                result="正常通行";
            }else if (0==resultStatus1){
                result="禁止通行";
            }

            dataRow.createCell(11).setCellValue(result);

        }

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


    }

    @Override
    @DESRespondSecret(validated = true)
    public CommonResult getAccessPage(Integer currentPage, Integer pageCount, String keyWord, Integer gradeId, Integer classId, String resultStatus, String inOut, String startTime, String endTime) {
        PageUtils<SmartAccessVo> pageUtils=smartAccessService.getAccessPage(currentPage,pageCount,keyWord,gradeId,classId,resultStatus,inOut,startTime,endTime);
        return CommonResult.ok(pageUtils);
    }

    @Override
    @DESRespondSecret(validated = true)
    public CommonResult totalCount() {
        Wrapper<SmartAccess> wrapperAccess=new LambdaQueryWrapper<>();
        int countAccess = smartAccessService.count(wrapperAccess);
        Wrapper<SmartCarAccess> wrapperCar=new LambdaQueryWrapper<>();
        int countCar = smartCarAccessService.count(wrapperCar);
        TotalCountVo totalCountVo = new TotalCountVo();
        totalCountVo.setAccessCount(countAccess);
        totalCountVo.setCarCount(countCar);
        return CommonResult.ok(totalCountVo);
    }
}

