package com.securitycontrol.background.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.securitycontrol.background.service.TowerRecordService;
import com.securitycontrol.entity.background.vo.TowerRecordVo;
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/towerRecord/")
@Slf4j
public class TowerRecordController extends BaseController {

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

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

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