package com.securitycontrol.screen.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.securitycontrol.common.core.utils.ExcelStyleUtil;
import com.securitycontrol.common.core.utils.aes.StringHelper;
import com.securitycontrol.common.core.web.controller.BaseController;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.common.core.web.page.TableDataInfo;
import com.securitycontrol.common.log.annotation.Log;
import com.securitycontrol.common.log.enums.OperationType;
import com.securitycontrol.entity.background.vo.TowerRecordVo;
import com.securitycontrol.entity.screen.dto.AccessMgeDto;
import com.securitycontrol.entity.screen.vo.AccessMgeVo;
import com.securitycontrol.entity.screen.vo.AccessMgesVo;
import com.securitycontrol.screen.service.AccessMgeService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lsun
 * 出入管理
 */
@RestController
@RequestMapping("/largeScreen/accessMge/")
@Slf4j
public class AccessMgeController extends BaseController {

    @Resource(name = "AccessMgeService")
    private AccessMgeService service;

    /**
     * 今日、当月车辆出入场统计
     * @param dto
     * @return
     */
    @ApiOperation(value = "今日、当月车辆出入场统计")
    @GetMapping("getVehicleStatistics")
    @Log(title = "出入管理", menu = "出入管理->今日、当月车辆出入场统计", grade = OperationType.QUERY_BUSINESS, details = "查询今日、当月车辆出入场统计列表", type = "业务日志")
    public AjaxResult getVehicleStatistics(AccessMgeDto dto) {
        return service.getVehicleStatistics(dto);
    }


    /**
     * 今日、当月车辆出入场统计图片
     * @param dto
     * @return
     */
    @ApiOperation(value = "今日、当月车辆出入场统计图片")
    @GetMapping("getVehicleStatisticsPhoto")
    @Log(title = "出入管理", menu = "出入管理->今日、当月车辆出入场统计图片", grade = OperationType.QUERY_BUSINESS, details = "查询今日、当月车辆出入场统计列表图片", type = "业务日志")
    public AjaxResult getVehicleStatisticsPhoto(AccessMgeDto dto) {
        return service.getVehicleStatisticsPhoto(dto);
    }

    @ApiOperation(value = "今日、当月车辆出入场统计列表")
    @GetMapping("getVehicleStatisticsList")
    @Log(title = "出入管理", menu = "出入管理->今日、当月车辆出入场统计列表", grade = OperationType.QUERY_BUSINESS, details = "查询今日、当月车辆出入场统计列表", type = "业务日志")
    public TableDataInfo getVehicleStatisticsList(AccessMgeDto dto) {
        try{
            startLayPage();
            if(StringHelper.isNotEmpty(dto.getDate())){
                String[] date = dto.getDate().split("~");
                dto.setStartDate(date[0]);
                dto.setEndDate(date[1]);
            }
            List<AccessMgeVo> list = service.getVehicleStatisticsList(dto);
            return getDataTableLayui(list);
        }catch (Exception e){
            log.error(e.toString(),e);
            return getDataTableBadLayui(new ArrayList<>(),"请求出错了");
        }
    }

    @GetMapping("exportVehicleData")
    @Log(title = "今日、当月车辆出入场统计列表", menu = "出入管理->今日、当月车辆出入场统计列表", grade = OperationType.EXPORT_BUSINESS, details = "导出列表", type = "业务日志")
    public void exportVehicleData(HttpServletRequest request, HttpServletResponse response, AccessMgeDto dto) {
        try {
            List<AccessMgeVo> proExportVoList = new ArrayList<>();
            if(StringHelper.isNotEmpty(dto.getDate())){
                String[] date = dto.getDate().split("~");
                dto.setStartDate(date[0]);
                dto.setEndDate(date[1]);
            }
            List<AccessMgeVo> proLists = service.getVehicleStatisticsList(dto);
            for (int i = 0; i < proLists.size(); i++) {
                proLists.get(i).setProId((i + 1) + "");
                AccessMgeVo exportVo = new AccessMgeVo();
                BeanUtils.copyProperties(proLists.get(i), exportVo);
                proExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("今日、当月车辆出入场统计", "今日、当月车辆出入场统计", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, AccessMgeVo.class, proExportVoList);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("今日、当月车辆出入场统计" + ".xlsx", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            log.error("导出今日、当月车辆出入场统计列表", e);
        }
    }


    /**
     * 今日、当月人员出入场统计
     * @param dto
     * @return
     */
    @ApiOperation(value = "今日、当月人员出入场统计")
    @GetMapping("getPerStatistics")
    @Log(title = "出入管理", menu = "出入管理->今日、当月人员出入场统计", grade = OperationType.QUERY_BUSINESS, details = "查询今日、当月人员出入场统计列表", type = "业务日志")
    public AjaxResult getPerStatistics(AccessMgeDto dto) {
        return service.getPerStatistics(dto);
    }

    /**
     * 今日、当月人员出入场统计图片
     * @param dto
     * @return
     */
    @ApiOperation(value = "今日、当月人员出入场统计图片")
    @GetMapping("getPerStatisticsPhoto")
    @Log(title = "出入管理", menu = "出入管理->今日、当月人员出入场统计图片", grade = OperationType.QUERY_BUSINESS, details = "查询今日、当月人员出入场统计列表图片", type = "业务日志")
    public AjaxResult getPerStatisticsPhoto(AccessMgeDto dto) {
        return service.getPerStatisticsPhoto(dto);
    }

    @ApiOperation(value = "今日、当月人员出入场统计列表")
    @GetMapping("getPerStatisticsList")
    @Log(title = "出入管理", menu = "出入管理->今日、当月人员出入场统计列表", grade = OperationType.QUERY_BUSINESS, details = "查询今日、当月人员出入场统计列表", type = "业务日志")
    public TableDataInfo getPerStatisticsList(AccessMgeDto dto) {
        try{
            startLayPage();
            if(StringHelper.isNotEmpty(dto.getDate())){
                String[] date = dto.getDate().split("~");
                dto.setStartDate(date[0]);
                dto.setEndDate(date[1]);
            }
            List<AccessMgeVo> list = service.getPerStatisticsList(dto);
            return getDataTableLayui(list);
        }catch (Exception e){
            log.error(e.toString(),e);
            return getDataTableBadLayui(new ArrayList<>(),"请求出错了");
        }
    }

    @GetMapping("exportPerData")
    @Log(title = "今日、当月人员出入场统计列表", menu = "出入管理->今日、当月人员出入场统计列表", grade = OperationType.EXPORT_BUSINESS, details = "导出列表", type = "业务日志")
    public void exportData(HttpServletRequest request, HttpServletResponse response, AccessMgeDto dto) {
        try {
            List<AccessMgesVo> proExportVoList = new ArrayList<>();
            if(StringHelper.isNotEmpty(dto.getDate())){
                String[] date = dto.getDate().split("~");
                dto.setStartDate(date[0]);
                dto.setEndDate(date[1]);
            }
            List<AccessMgeVo> proLists = service.getPerStatisticsList(dto);
            for (int i = 0; i < proLists.size(); i++) {
                proLists.get(i).setProId((i + 1) + "");
                AccessMgesVo exportVo = new AccessMgesVo();
                BeanUtils.copyProperties(proLists.get(i), exportVo);
                proExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("今日、当月人员出入场统计", "今日、当月人员出入场统计", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, AccessMgesVo.class, proExportVoList);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("今日、当月人员出入场统计" + ".xlsx", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            log.error("导出今日、当月人员出入场统计列表", e);
        }
    }

}
