package com.securitycontrol.background.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.securitycontrol.background.service.EarlyWarningRecordService;


import com.securitycontrol.entity.background.vo.EarlyWarningRecordVo;
import com.securitycontrol.common.core.web.controller.BaseController;
import com.securitycontrol.common.core.web.page.TableDataInfo;
import com.securitycontrol.common.log.annotation.Log;
import com.securitycontrol.common.log.enums.OperationType;
import cn.afterturn.easypoi.excel.entity.ExportParams;

import com.securitycontrol.common.core.utils.ExcelStyleUtil;
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.*;

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("/towerDetection/earlyWarningRecord/")
@Slf4j
public class EarlyWarningRecordController extends BaseController {

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

    @ApiOperation(value = "获取信息列表")
    @GetMapping("getEarlyWarningRecordLists")
    @Log(title = "组塔检测", menu = "组塔检测->预警记录", grade = OperationType.QUERY_BUSINESS, details = "查询列表", type = "业务日志")
    public TableDataInfo getEarlyWarningRecordLists(EarlyWarningRecordVo dto) {
        startPage();
        List<EarlyWarningRecordVo> list = service.getEarlyWarningRecordLists(dto);
        return getDataTable(list);
    }

    @GetMapping("exportProData")
    @Log(title = "组塔检测", menu = "组塔检测->预警记录", grade = OperationType.EXPORT_BUSINESS, details = "导出列表", type = "业务日志")
    public void exportData(HttpServletRequest request, HttpServletResponse response, EarlyWarningRecordVo dto) {
        try {
            List<EarlyWarningRecordVo> proExportVoList = new ArrayList<>();
            List<EarlyWarningRecordVo> proLists = service.getEarlyWarningRecordLists(dto);
            for (int i = 0; i < proLists.size(); i++) {
                proLists.get(i).setProId((i + 1) + "");
                EarlyWarningRecordVo exportVo = new EarlyWarningRecordVo();
                BeanUtils.copyProperties(proLists.get(i), exportVo);
                proExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("预警记录", "预警记录", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, EarlyWarningRecordVo.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);
        }
    }
}
