package com.securitycontrol.background.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

import com.securitycontrol.background.service.EarlyWarningsRecordService;
import com.securitycontrol.entity.background.vo.EarlyWarningsRecordVo;
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 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("/constructionQuality/earlyWarningRecord/")
@Slf4j
public class EarlyWarningsRecordController extends BaseController {

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

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

    @ApiOperation(value = "预警处置")
    @PostMapping("updEarly")
    @Log(title = "施工质量", menu = "施工质量->预警处置", grade = OperationType.UPDATE_BUSINESS, details = "预警处置", type = "业务日志")
    public AjaxResult addEarly(@RequestBody EarlyWarningsRecordVo vo) {
        return service.updEarly(vo);
    }

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