package com.njcn.harmonic.service.activepowerrange.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONArray;
import com.njcn.dataProcess.param.LineCountEvaluateParam;
import com.njcn.dataProcess.pojo.dto.*;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.device.pq.api.OverLimitClient;
import com.njcn.harmonic.constant.Param;
import com.njcn.harmonic.pojo.param.PowerStatisticsParam;
import com.njcn.harmonic.pojo.vo.*;
import com.njcn.harmonic.service.IRStatLimitRateDetailDService;
import com.njcn.harmonic.service.activepowerrange.PowerStatisticsService;
import com.njcn.harmonic.service.activepowerrange.RActivePowerRangeService;
import com.njcn.harmonic.service.influxdb.*;
import com.njcn.poi.util.PoiUtil;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 有功功率趋势统计 服务实现类
 *
 * @author guofeihu
 * @since 2024-08-20
 */
@Service
@RequiredArgsConstructor
public class PowerStatisticsServiceImpl implements PowerStatisticsService {

    private final RActivePowerRangeService rActivePowerRangeService;
    private final IRStatLimitRateDetailDService irStatLimitRateDetailDService;
    private final DecimalFormat dftwo = new DecimalFormat(Param.DECIMAL_FORMATTWOSTR);
    private final IDataV dataV;
    private final IDataI dataI;
    private final IDataPlt dataPlt;
    private final IDataInHarmV dataInHarmV;
    private final IDataHarmRateV dataHarmRateV;
    private final OverLimitClient overLimitClient;

    private List<String> times = Arrays.asList("0~10%", "10~20%", "20~30%", "30~40%", "40~50%", "50~60%", "60~70%", "70~80%", "80~90%", "90~100%");

    @Override
    public PowerStatisticsVO getDataByLineId(PowerStatisticsParam powerStatisticsParam) {
        //执行完就可以获取到区间数据了
        RActivePowerRangeVO rActivePowerRangePO = rActivePowerRangeService.getDataByLineId(powerStatisticsParam.getLineId(),
                powerStatisticsParam.getSearchBeginTime(), powerStatisticsParam.getSearchEndTime());
        //如果的确没有统计到数据则默认
        if (rActivePowerRangePO == null) {
            rActivePowerRangePO = new RActivePowerRangeVO();
        }
        PowerStatisticsVO powerStatisticsVO = new PowerStatisticsVO();
        BeanUtils.copyProperties(rActivePowerRangePO, powerStatisticsVO);
        //计算各区间占比
        int total = powerStatisticsVO.getMinsNum0() + powerStatisticsVO.getMinsNum1() + powerStatisticsVO.getMinsNum2()
                + powerStatisticsVO.getMinsNum3() + powerStatisticsVO.getMinsNum4() + powerStatisticsVO.getMinsNum5()
                + powerStatisticsVO.getMinsNum6() + powerStatisticsVO.getMinsNum7() + powerStatisticsVO.getMinsNum8()
                + powerStatisticsVO.getMinsNum9();
        if (total != 0) {
            BigDecimal totalData = BigDecimal.valueOf(total);
            powerStatisticsVO.setProportion0(BigDecimal.valueOf(powerStatisticsVO.getMinsNum0() * 100.0).divide(totalData, 4, RoundingMode.HALF_UP).doubleValue());
            powerStatisticsVO.setProportion1(BigDecimal.valueOf(powerStatisticsVO.getMinsNum1() * 100.0).divide(totalData, 4, RoundingMode.HALF_UP).doubleValue());
            powerStatisticsVO.setProportion2(BigDecimal.valueOf(powerStatisticsVO.getMinsNum2() * 100.0).divide(totalData, 4, RoundingMode.HALF_UP).doubleValue());
            powerStatisticsVO.setProportion3(BigDecimal.valueOf(powerStatisticsVO.getMinsNum3() * 100.0).divide(totalData, 4, RoundingMode.HALF_UP).doubleValue());
            powerStatisticsVO.setProportion4(BigDecimal.valueOf(powerStatisticsVO.getMinsNum4() * 100.0).divide(totalData, 4, RoundingMode.HALF_UP).doubleValue());
            powerStatisticsVO.setProportion5(BigDecimal.valueOf(powerStatisticsVO.getMinsNum5() * 100.0).divide(totalData, 4, RoundingMode.HALF_UP).doubleValue());
            powerStatisticsVO.setProportion6(BigDecimal.valueOf(powerStatisticsVO.getMinsNum6() * 100.0).divide(totalData, 4, RoundingMode.HALF_UP).doubleValue());
            powerStatisticsVO.setProportion7(BigDecimal.valueOf(powerStatisticsVO.getMinsNum7() * 100.0).divide(totalData, 4, RoundingMode.HALF_UP).doubleValue());
            powerStatisticsVO.setProportion8(BigDecimal.valueOf(powerStatisticsVO.getMinsNum8() * 100.0).divide(totalData, 4, RoundingMode.HALF_UP).doubleValue());
            powerStatisticsVO.setProportion9(BigDecimal.valueOf(powerStatisticsVO.getMinsNum9() * 100.0).divide(totalData, 4, RoundingMode.HALF_UP).doubleValue());
        }
        return powerStatisticsVO;
    }

    @Override
    public List<PowerStatisticsTargetVO> getTargetLimitById(PowerStatisticsParam powerStatisticsParam) {
        List<PowerStatisticsTargetVO> record = new ArrayList<>();
        RActivePowerRangeVO rActivePowerRangePO = rActivePowerRangeService.getDataByLineId(powerStatisticsParam.getLineId(),
                powerStatisticsParam.getSearchBeginTime(), powerStatisticsParam.getSearchEndTime());
        if (ObjectUtil.isNotNull(rActivePowerRangePO)) {
            //获取电压数据
            LineCountEvaluateParam lineParam = new LineCountEvaluateParam();
            lineParam.setLineId(Collections.singletonList(rActivePowerRangePO.getLineId()));
            LineCountEvaluateParam lineCountEvaluateParam = new LineCountEvaluateParam();
            lineCountEvaluateParam.setLineId(Collections.singletonList(rActivePowerRangePO.getLineId()));
            lineCountEvaluateParam.setStartTime(powerStatisticsParam.getSearchBeginTime());
            lineCountEvaluateParam.setEndTime(powerStatisticsParam.getSearchEndTime());
            //获取超标数据


            List<DataLimitRateDetailTimeDto> dtoList = irStatLimitRateDetailDService.getLimitRateDetailTime(lineCountEvaluateParam);
            Map<String, DataLimitRateDetailTimeDto> timeDateMap = dtoList.stream().collect(Collectors.toMap(x -> x.getTime(), Function.identity()));
            List<String> timeId = rActivePowerRangePO.getTimeId();
            String times = reflexObjValue(rActivePowerRangePO, "minsTime" + powerStatisticsParam.getField()).toString().replace("null", "");
            List<String> timeList = JSONArray.parseArray(times, String.class);
            for (int i = 0; i < timeId.size(); i++) {
                String date = timeId.get(i);
                List<String> strings = JSONArray.parseArray(timeList.get(i), String.class);
                if (CollUtil.isNotEmpty(strings)) {
                    for (String time : strings) {
                        PowerStatisticsTargetVO vo = new PowerStatisticsTargetVO();
                        String t = date + " " + time;
                        vo.setTime(t);
                        if (timeDateMap.containsKey(date)) {
                            DataLimitRateDetailTimeDto rateDetail = timeDateMap.get(date);
                            vo.setVoltageOffset(isCross(time, rateDetail.getVoltageDevOvertime()));
                            vo.setSequenceCurrentUnbalance(isCross(time, rateDetail.getINegOvertime()));
                            vo.setUbalance(isCross(time, rateDetail.getUbalanceOvertime()));
                            vo.setFlicker(isCross(time, rateDetail.getFlickerOvertime()));
                            vo.setVTimes(isCross(time, rateDetail.getUharmOvertime()));
                            vo.setITimes(isCross(time, rateDetail.getIharmOvertime()));
                            vo.setInterHarmonic(isCross(time, rateDetail.getInuharmOvertime()));
                        }
                        record.add(vo);
                    }
                }
            }
        }
        return record.stream().sorted(Comparator.comparing(PowerStatisticsTargetVO::getTime)).collect(Collectors.toList());
    }

    @Override
    public List<ThdDataVO> getTargetByTimeDetail(PowerStatisticsParam param) {
        List<ThdDataVO> info = new ArrayList<>();
        //获取越线详情
        List<Overlimit> data = overLimitClient.getOverLimitByLineIds(Collections.singletonList(param.getLineId())).getData();
        Overlimit overlimit = data.get(0);
        LineCountEvaluateParam evaluateParam = new LineCountEvaluateParam();
        evaluateParam.setLineId(Collections.singletonList(param.getLineId()));
        evaluateParam.setStartTime(param.getSearchBeginTime());
        evaluateParam.setEndTime(param.getSearchEndTime());
        String time = param.getSearchBeginTime();
        if ("1".equals(param.getStatisticalId())) {
            //电压数据
            List<DataVDto> dataVAllTime = dataV.getRawData(evaluateParam);
            //闪变数据
            List<DataPltDto> dataFlickerAllTime = dataPlt.getRawData(evaluateParam);
            //电流数据
            List<DataIDto> dataIList = dataI.getRawData(evaluateParam);
            //电压偏差
            if ("Dev".equals(param.getCode()) || StrUtil.isBlank(param.getCode())) {
                addThdData(info, overlimit.getVoltageDev(), "vuDev", dataVAllTime, "电压上偏差","%",time);
                addThdData(info, overlimit.getUvoltageDev(), "vlDev", dataVAllTime, "电压下偏差","%",time);
            }
            //电压不平横度
            if ("Unbalance".equals(param.getCode()) || StrUtil.isBlank(param.getCode())) {
                addThdData(info, overlimit.getUbalance(), "vUnbalance", dataVAllTime, "三相电压不平横度","%",time);
            }
            //闪变
            if ("plt".equals(param.getCode()) || StrUtil.isBlank(param.getCode())) {
                addThdData(info, overlimit.getFlicker(), "plt", dataFlickerAllTime, "长时闪变","",time);
            }
            //负序电流不平衡度
            if ("iNeg".equals(param.getCode()) || StrUtil.isBlank(param.getCode())) {
                addThdData(info, overlimit.getINeg(), "iNeg", dataIList, "电流不平衡度","",time);
            }
            //频率偏差越限
            if ("freq".equals(param.getCode()) || StrUtil.isBlank(param.getCode())) {
                addThdData(info, overlimit.getINeg(), "freqDev", dataVAllTime, "频率偏差","Hz",time);
            }
            //电压谐波畸变率
            if ("freq".equals(param.getCode()) || StrUtil.isBlank(param.getCode())) {
                addThdData(info, overlimit.getINeg(), "vThd", dataVAllTime, "电压谐波畸变率","%",time);
            }

        }
        if ("2".equals(param.getStatisticalId())) {
            //谐波数据
            List<DataHarmDto> dataVHarmList = dataHarmRateV.getRawData(evaluateParam);
            addThdData(info, overlimit, "getUharm", "v", 2, 26, dataVHarmList, "谐波电压","%",time);
        }
        if ("3".equals(param.getStatisticalId())) {
            //电流数据
            List<DataIDto> dataIList = dataI.getRawData(evaluateParam);
            addThdData(info, overlimit, "getUharm", "i", 2, 26, dataIList, "谐波电流","A",time);
        }
        if ("4".equals(param.getStatisticalId())) {
            //间谐波数据
            List<DataHarmDto> dataVInHarmList = dataInHarmV.getRawData(evaluateParam);
            addThdData(info, overlimit, "getInuharm", "v", 1, 17, dataVInHarmList, "间谐波电压","%",time);
        }
        return info;
    }

    private void addThdData(List<ThdDataVO> info, Overlimit overlimit, String limitField, String fieldName, Integer start, Integer end, List<?> dataAllTime, String anotherName,String unit,String time) {
        if (CollUtil.isNotEmpty(dataAllTime)) {
            for (int i = start; i < end; i++) {
                int finalI = i;
                List<String> collect = dataAllTime.stream().map(x -> data(x, fieldName + finalI)).filter(x -> ObjectUtil.isNotNull(x)).collect(Collectors.toList());
                if (CollUtil.isNotEmpty(collect)) {
                    ThdDataVO vo = new ThdDataVO();
                    try {
                        Method getUharmMethod = Overlimit.class.getMethod(limitField + i);
                        float limit = (float) getUharmMethod.invoke(overlimit);
                        vo.setLimit(limit);
                    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                    vo.setAnotherName(anotherName);
                    vo.setUnit(unit);
                    vo.setTime(time);
                    if ("getInuharm".equals(limitField)) {
                        vo.setAnotherName(i - 0.5 + "次");
                    } else {
                        vo.setAnotherName(i + "次");
                    }
                    for (String s : collect) {
                        setDataV(vo, s);
                    }
                    info.add(vo);
                }
            }

        }
    }

    private void addThdData(List<ThdDataVO> info, Float limit, String fieldName, List<?> dataAllTime, String anotherName,String unit,String time) {
        if (CollUtil.isNotEmpty(dataAllTime)) {
            List<String> collect = dataAllTime.stream().map(x -> data(x, fieldName)).filter(x -> ObjectUtil.isNotNull(x)).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(collect)) {
                ThdDataVO vo = new ThdDataVO();
                vo.setLimit(limit);
                vo.setAnotherName(anotherName);
                vo.setUnit(unit);
                vo.setTime(time);
                for (String s : collect) {
                    setDataV(vo, s);
                }
                info.add(vo);
            }
        }
    }

    private void setDataV(ThdDataVO dto, String fieldName) {
        String[] split = fieldName.split("&");
        if (split.length > 1) {
            try {
                Class clazz = dto.getClass();
                Field field = clazz.getDeclaredField(split[0]);
                field.setAccessible(true);
                field.set(dto, NumberUtil.round(split[1],4)+"");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private String data(Object dto, String fieldName) {
        try {
            Class clazz = dto.getClass();
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            Object o = field.get(dto);
            if (ObjectUtil.isNotNull(o)) {
                Field phasicType = clazz.getDeclaredField("phasicType");
                phasicType.setAccessible(true);
                String phasic = phasicType.get(dto)+"";
                Field valueType = clazz.getDeclaredField("valueType");
                valueType.setAccessible(true);
                Object value = valueType.get(dto);
                if(ObjectUtil.isNotNull(value)){
                    return value.toString().toUpperCase() + "Phase" + phasic + "&" + o;
                }else{
                    return  "AVGPhase" + phasic + "&" + o;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private Integer isCross(String time, List<String> timeCross) {
        if (timeCross.contains(time)) {
            return 1;
        }
        return 0;
    }

    /**
     * @param obj
     * @param fieldName
     * @Description: 反射获取值
     * @return: java.lang.Object
     * @Author: wr
     * @Date: 2025/3/26 19:24
     */
    private Object reflexObjValue(Object obj, String fieldName) {
        try {
            Class clazz = obj.getClass();
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public void exportExcelRangTemplate(PowerStatisticsParam powerStatisticsParam, HttpServletResponse response) {
        PowerStatisticsVO powerStatisticsVO = getDataByLineId(powerStatisticsParam);
        //开始组装excel所需数据
        List<PowerStatisticsExcelRangVO> powerStatisticsVOS = new ArrayList<>();
        ExportParams exportParams = new ExportParams(powerStatisticsParam.getSearchValue(), "区间数据");
        //各区间数量
        PowerStatisticsExcelRangVO num = new PowerStatisticsExcelRangVO();
        num.setMinsNum0(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getMinsNum0() + "");
        num.setMinsNum1(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getMinsNum1() + "");
        num.setMinsNum2(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getMinsNum2() + "");
        num.setMinsNum3(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getMinsNum3() + "");
        num.setMinsNum4(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getMinsNum4() + "");
        num.setMinsNum5(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getMinsNum5() + "");
        num.setMinsNum6(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getMinsNum6() + "");
        num.setMinsNum7(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getMinsNum7() + "");
        num.setMinsNum8(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getMinsNum8() + "");
        num.setMinsNum9(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getMinsNum9() + "");
        num.setText("个数");
        powerStatisticsVOS.add(num);
        //各区间是否越限
        PowerStatisticsExcelRangVO isOrNot = new PowerStatisticsExcelRangVO();
        isOrNot.setMinsNum0(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getIsOrNot0() > 0 ? "是" : "否");
        isOrNot.setMinsNum1(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getIsOrNot1() > 0 ? "是" : "否");
        isOrNot.setMinsNum2(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getIsOrNot2() > 0 ? "是" : "否");
        isOrNot.setMinsNum3(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getIsOrNot3() > 0 ? "是" : "否");
        isOrNot.setMinsNum4(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getIsOrNot4() > 0 ? "是" : "否");
        isOrNot.setMinsNum5(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getIsOrNot5() > 0 ? "是" : "否");
        isOrNot.setMinsNum6(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getIsOrNot6() > 0 ? "是" : "否");
        isOrNot.setMinsNum7(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getIsOrNot7() > 0 ? "是" : "否");
        isOrNot.setMinsNum8(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getIsOrNot8() > 0 ? "是" : "否");
        isOrNot.setMinsNum9(powerStatisticsVO.getLineId() == null ? "" : powerStatisticsVO.getIsOrNot9() > 0 ? "是" : "否");
        isOrNot.setText("是否越限");
        powerStatisticsVOS.add(isOrNot);
        //各区间占比
        PowerStatisticsExcelRangVO proportion = new PowerStatisticsExcelRangVO();
        int total = 0;
        if (powerStatisticsVO.getLineId() != null) {
            total = powerStatisticsVO.getMinsNum0() + powerStatisticsVO.getMinsNum1() + powerStatisticsVO.getMinsNum2()
                    + powerStatisticsVO.getMinsNum3() + powerStatisticsVO.getMinsNum4() + powerStatisticsVO.getMinsNum5()
                    + powerStatisticsVO.getMinsNum6() + powerStatisticsVO.getMinsNum7() + powerStatisticsVO.getMinsNum8()
                    + powerStatisticsVO.getMinsNum9();
        }
        if (total != 0) {
            proportion.setMinsNum0(dftwo.format((double) powerStatisticsVO.getMinsNum0().intValue() / total * 100) + "%");
            proportion.setMinsNum1(dftwo.format((double) powerStatisticsVO.getMinsNum1().intValue() / total * 100) + "%");
            proportion.setMinsNum2(dftwo.format((double) powerStatisticsVO.getMinsNum2().intValue() / total * 100) + "%");
            proportion.setMinsNum3(dftwo.format((double) powerStatisticsVO.getMinsNum3().intValue() / total * 100) + "%");
            proportion.setMinsNum4(dftwo.format((double) powerStatisticsVO.getMinsNum4().intValue() / total * 100) + "%");
            proportion.setMinsNum5(dftwo.format((double) powerStatisticsVO.getMinsNum5().intValue() / total * 100) + "%");
            proportion.setMinsNum6(dftwo.format((double) powerStatisticsVO.getMinsNum6().intValue() / total * 100) + "%");
            proportion.setMinsNum7(dftwo.format((double) powerStatisticsVO.getMinsNum7().intValue() / total * 100) + "%");
            proportion.setMinsNum8(dftwo.format((double) powerStatisticsVO.getMinsNum8().intValue() / total * 100) + "%");
            proportion.setMinsNum9(dftwo.format((double) powerStatisticsVO.getMinsNum9().intValue() / total * 100) + "%");
        } else {
            proportion.setMinsNum0("");
            proportion.setMinsNum1("");
            proportion.setMinsNum2("");
            proportion.setMinsNum3("");
            proportion.setMinsNum4("");
            proportion.setMinsNum5("");
            proportion.setMinsNum6("");
            proportion.setMinsNum7("");
            proportion.setMinsNum8("");
            proportion.setMinsNum9("");
        }
        proportion.setText("占比");
        powerStatisticsVOS.add(proportion);
        //数据准备完毕开始渲染excel
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, PowerStatisticsExcelRangVO.class, powerStatisticsVOS);
        Sheet sheet = workbook.getSheet("区间数据");
        Font redFont = workbook.createFont();
        redFont.setColor(IndexedColors.RED.getIndex());
        //将数据的表格加边框
        for (int i = 0; i <= 4; i++) {
            Row row = sheet.getRow(i);
            for (int j = 0; j <= 10; j++) {
                Cell cell = row.getCell(j);
                CellStyle style = workbook.createCellStyle();
                style.setBorderTop(BorderStyle.THIN);
                style.setBorderBottom(BorderStyle.THIN);
                style.setBorderLeft(BorderStyle.THIN);
                style.setBorderRight(BorderStyle.THIN);
                style.setAlignment(HorizontalAlignment.CENTER);
                style.setVerticalAlignment(VerticalAlignment.CENTER);
                //如果当前表格为是(是否越限:是)则红色字体显示
                if (cell.getStringCellValue().equals("是")) {
                    style.setFont(redFont);
                }
                cell.setCellStyle(style);
            }
        }
        //设置统一列宽
        for (int j = 0; j <= 10; j++) {
            sheet.setColumnWidth(j, 4295);
        }
        //第一行第一列设置斜杠
        CreationHelper helper = workbook.getCreationHelper();
        XSSFDrawing xssfDrawing = (XSSFDrawing) sheet.createDrawingPatriarch();
        ClientAnchor anchor = helper.createClientAnchor();
        //设置斜线的开始位置,我这里是从第0行到第1行，从第0列到第1列
        anchor.setCol1(0);
        anchor.setCol2(1);
        anchor.setRow1(1);
        anchor.setRow2(2);
        XSSFSimpleShape simpleShape = xssfDrawing.createSimpleShape((XSSFClientAnchor) anchor);
        //设置形状类型未线型
        simpleShape.setShapeType(ShapeTypes.LINE);
        //设置线宽
        simpleShape.setLineWidth(0.5);
        //设置线的风格
        simpleShape.setLineStyle(0);
        //设置线的颜色
        simpleShape.setLineStyleColor(0, 0, 0);
        //删除第一行title
        //sheet.removeRow(sheet.getRow(0));
        //上移(因为删除一行只是将数据清空了留下空行)
        //sheet.shiftRows(1, sheet.getLastRowNum(), -1);
        PoiUtil.exportFileByWorkbook(workbook, powerStatisticsParam.getSearchValue() + ".xlsx", response);
    }

    @Override
    public void exportExcelListTemplate(PowerStatisticsParam powerStatisticsParam, HttpServletResponse response) {
        //获取各个区间的详细数据(就是调用getTargetByTime方法在进行某些指标的名称的处理)
        List<ThdDataVOExcel> time0 = getExcelListRecord(powerStatisticsParam.getTime0(), powerStatisticsParam.getLineId());
        List<ThdDataVOExcel> time1 = getExcelListRecord(powerStatisticsParam.getTime1(), powerStatisticsParam.getLineId());
        List<ThdDataVOExcel> time2 = getExcelListRecord(powerStatisticsParam.getTime2(), powerStatisticsParam.getLineId());
        List<ThdDataVOExcel> time3 = getExcelListRecord(powerStatisticsParam.getTime3(), powerStatisticsParam.getLineId());
        List<ThdDataVOExcel> time4 = getExcelListRecord(powerStatisticsParam.getTime4(), powerStatisticsParam.getLineId());
        List<ThdDataVOExcel> time5 = getExcelListRecord(powerStatisticsParam.getTime5(), powerStatisticsParam.getLineId());
        List<ThdDataVOExcel> time6 = getExcelListRecord(powerStatisticsParam.getTime6(), powerStatisticsParam.getLineId());
        List<ThdDataVOExcel> time7 = getExcelListRecord(powerStatisticsParam.getTime7(), powerStatisticsParam.getLineId());
        List<ThdDataVOExcel> time8 = getExcelListRecord(powerStatisticsParam.getTime8(), powerStatisticsParam.getLineId());
        List<ThdDataVOExcel> time9 = getExcelListRecord(powerStatisticsParam.getTime9(), powerStatisticsParam.getLineId());
        try {
            //10个区间10个sheet10组数据
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), ThdDataVOExcel.class).build();
            WriteSheet sheet0 = EasyExcel.writerSheet(0, times.get(0)).head(ThdDataVOExcel.class).build();
            excelWriter.write(time0, sheet0);
            WriteSheet sheet1 = EasyExcel.writerSheet(1, times.get(1)).head(ThdDataVOExcel.class).build();
            excelWriter.write(time1, sheet1);
            WriteSheet sheet2 = EasyExcel.writerSheet(2, times.get(2)).head(ThdDataVOExcel.class).build();
            excelWriter.write(time2, sheet2);
            WriteSheet sheet3 = EasyExcel.writerSheet(3, times.get(3)).head(ThdDataVOExcel.class).build();
            excelWriter.write(time3, sheet3);
            WriteSheet sheet4 = EasyExcel.writerSheet(4, times.get(4)).head(ThdDataVOExcel.class).build();
            excelWriter.write(time4, sheet4);
            WriteSheet sheet5 = EasyExcel.writerSheet(5, times.get(5)).head(ThdDataVOExcel.class).build();
            excelWriter.write(time5, sheet5);
            WriteSheet sheet6 = EasyExcel.writerSheet(6, times.get(6)).head(ThdDataVOExcel.class).build();
            excelWriter.write(time6, sheet6);
            WriteSheet sheet7 = EasyExcel.writerSheet(7, times.get(7)).head(ThdDataVOExcel.class).build();
            excelWriter.write(time7, sheet7);
            WriteSheet sheet8 = EasyExcel.writerSheet(8, times.get(8)).head(ThdDataVOExcel.class).build();
            excelWriter.write(time8, sheet8);
            WriteSheet sheet9 = EasyExcel.writerSheet(9, times.get(9)).head(ThdDataVOExcel.class).build();
            excelWriter.write(time9, sheet9);
            excelWriter.finish();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private List<ThdDataVOExcel> getExcelListRecord(List<String> times, String lineId) {
        List<ThdDataVO> thdDataVOS = new ArrayList<>();
        List<ThdDataVOExcel> thdDataVOExcels = new ArrayList<>();
        PowerStatisticsParam powerStatisticsParam = new PowerStatisticsParam();
        powerStatisticsParam.setLineId(lineId);
        for (String time : times) {
            powerStatisticsParam.setSearchBeginTime(time);
            powerStatisticsParam.setSearchEndTime(time);
            for (int i = 1; i < 5; i++) {
                powerStatisticsParam.setStatisticalId(i + "");
                List<ThdDataVO> thdDataVOList = getTargetByTimeDetail(powerStatisticsParam);
                if (i != 1) {
                    for (ThdDataVO thdDataVO : thdDataVOList) {
                        switch (i) {
                            case 2:
                                thdDataVO.setAnotherName("谐波电压" + thdDataVO.getAnotherName());
                                break;
                            case 3:
                                thdDataVO.setAnotherName("谐波电流" + thdDataVO.getAnotherName());
                                break;
                            case 4:
                                thdDataVO.setAnotherName("间谐波电压" + thdDataVO.getAnotherName());
                                break;
                        }
                    }
                }
                thdDataVOS.addAll(thdDataVOList);
            }
        }
        for (ThdDataVO thdDataVO : thdDataVOS) {
            ThdDataVOExcel thdDataVOExcel = new ThdDataVOExcel();
            BeanUtils.copyProperties(thdDataVO, thdDataVOExcel);
            thdDataVOExcels.add(thdDataVOExcel);
        }
        return thdDataVOExcels;
    }
}
