package com.zcxk.util;

import cn.hutool.core.collection.CollectionUtil;
import com.zcxk.dto.ReportDto;
import com.zcxk.dto.ReportTitleDto;

import java.util.*;

/**
 * @ClassName ReportExcelUtil
 * @Description: 报表导出excel
 * @Author lihui
 * @Date 2021/5/17
 * @Version V1.0
 **/
public class ReportExcelUtil {

    static Map<Integer,String[]> TITLE_MAP = new HashMap<>();

    static {
        TITLE_MAP.put(1, new String[]{"NO.","名称","时间","取水量(万m³)","耗电量(万KWH)"});
        TITLE_MAP.put(2, new String[]{"NO.","名称","时间","进水量(万m³)","制水量(万m³)","耗电量(万KWH)","耗药量(KG)"});
        TITLE_MAP.put(3, new String[]{"NO.","名称","时间","供水量(m³)","耗电量(KWH)"});
        TITLE_MAP.put(4, new String[]{"NO.","名称","时间"});
    }

    /**
    * @Author lihui
    * @Description 按顺序转换导出的数据
    * @Date 17:10 2021/5/17
    * @Param [list]
    * @return java.util.List<java.util.List<java.lang.String>>
    **/
    public static List<List<String>> toReportExcelDto(List<ReportDto> list, int titleType){
        int number = 1;
        List<List<String>> result = new ArrayList<>();
        if (CollectionUtil.isEmpty(list)){
            return result;
        }
        for (ReportDto reportDto : list) {
            List<String>  dataList = new ArrayList<>();
            dataList.add((number++) +"");
            dataList.add(reportDto.getParentSceneName());
            dataList.add(reportDto.getCollectDate());
            if (titleType == 1) {
                dataList.add(doubleToString(reportDto.getIntakeWaterUsage()));
                dataList.add(doubleToString(reportDto.getPowerUsage()));
            }
            if (titleType == 2) {
                dataList.add(doubleToString(reportDto.getIntakeWaterUsage()));
                dataList.add(doubleToString(reportDto.getYieldWaterUsage()));
                dataList.add(doubleToString(reportDto.getPowerUsage()));
                dataList.add(doubleToString(reportDto.getDrugUsage()));
            }
            if (titleType == 3) {
                dataList.add(doubleToString(reportDto.getYieldWaterUsage()));
                dataList.add(doubleToString(reportDto.getPowerUsage()));
            }
            List<Map<String,Object>> childList = reportDto.getDataMapValues();
            if (CollectionUtil.isEmpty(childList)) {
                continue;
            }
            for (Map<String,Object> childMap: childList) {
                int ok = 0;
                for (Map.Entry<String,Object> entry : childMap.entrySet()) {
                    if (ok == 0){
                        ok++;
                        continue;
                    }
                    dataList.add(entry.getValue() != null ? entry.getValue().toString() : "");
                }
            }
            result.add(dataList);
        }
        return result;
    }
    /**
     * @Author lihui
     * @Description 按顺序转换导出的数据
     * @Date 17:10 2021/5/17
     * @Param [list]
     * @return java.util.List<java.util.List<java.lang.String>>
     **/
    public static List<List<String>> toHisoryDataReportExcelDto(List<ReportDto> list, int titleType) {
        int number = 1;
        List<List<String>> result = new ArrayList<>();
        if (CollectionUtil.isEmpty(list)) {
            return result;
        }
        for (ReportDto reportDto : list) {
            List<String> dataList = new ArrayList<>();
            dataList.add((number++) + "");
            if (titleType==4) {
                dataList.add(reportDto.getDeviceName());
            } else {
                dataList.add(reportDto.getParentSceneName());
            }
            dataList.add(reportDto.getCollectDate());
            List<Map<String, Object>> childList = reportDto.getDataMapValues();
            if (CollectionUtil.isEmpty(childList)) {
                continue;
            }
            for (Map<String, Object> childMap : childList) {
                int ok = 0;
                for (Map.Entry<String, Object> entry : childMap.entrySet()) {
                    if (ok == 0) {
                        ok++;
                        continue;
                    }
                    dataList.add(entry.getValue() != null ? entry.getValue().toString() : "");
                }
            }
            result.add(dataList);
        }
        return result;
    }




    /**
    * @Author lihui
    * @Description 按顺序组装导出的标题
    * @Date 17:11 2021/5/17
    * @Param [titleDtos, titleType]
    * @return java.util.List<java.util.List<java.lang.String>>
    **/
    public static List<List<String>> toExcelTitleList(List<ReportTitleDto> titleDtos, int titleType,int reportType){
        List<List<String>> titleList = toTitleList(titleDtos,reportType);
        String[] shuZu  = TITLE_MAP.get(titleType);
        if (shuZu == null) {
            return titleList;
        }
        boolean isEmpty = CollectionUtil.isEmpty(titleList);
        List<List<String>> result = new ArrayList<>();
        for (String string : shuZu) {
            List<String> listString = new ArrayList<>();
            listString.add(string);
            if (!isEmpty){
                listString.add(string);
                listString.add(string);
            }
            result.add(listString);
        }
        if (!isEmpty) {
            result.addAll(titleList);
        }
        return result;
    }

    /**
     * @Author lihui
     * @Description 按顺序组装导出的标题
     * @Date 17:11 2021/5/17
     * @Param [titleDtos, titleType]
     * @return java.util.List<java.util.List<java.lang.String>>
     **/
    public static List<List<String>> toHistoryDataExcelTitleList(List<ReportTitleDto> titleDtos, int titleType,int reportType){
        List<List<String>> titleList = toTitleList(titleDtos,reportType);
        String[] shuZu  = TITLE_MAP.get(4);
        if (shuZu == null) {
            return titleList;
        }
        boolean isEmpty = CollectionUtil.isEmpty(titleList);
        List<List<String>> result = new ArrayList<>();
        for (String string : shuZu) {
            List<String> listString = new ArrayList<>();
            listString.add(string);
            if (!isEmpty){
                listString.add(string);
                listString.add(string);
            }
            result.add(listString);
        }
        if (!isEmpty) {
            result.addAll(titleList);
        }
        return result;
    }

    private static List<List<String>> toTitleList(List<ReportTitleDto> list,int reportType) {
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        String preDimensionName = null;
        String dimensionName = null;
        String kongGe = " ";
        List<List<String>> result = new ArrayList<>();
        for (ReportTitleDto reportTitleDto : list) {
            for (ReportTitleDto.DeviceChildren deviceChildren : reportTitleDto.getDeviceChildren()) {
                for (ReportTitleDto.StatisticalDimension dimensionList : deviceChildren.getDimensionList()) {
                    List<String> child = new ArrayList<>();
                    child.add(reportTitleDto.getClassify().indexOf("isNull") != -1 ? deviceChildren.getName() : reportTitleDto.getClassify());
                    child.add(deviceChildren.getName());
                    if (reportType == 0) {
                        // 避免第三行，在邻列的情况下相同值被合并
                        if (preDimensionName != null && preDimensionName.equals(dimensionList.getDimensionName())) {
                            dimensionName = kongGe + dimensionList.getDimensionName();
                        } else {
                            dimensionName = dimensionList.getDimensionName();
                        }
                        child.add(dimensionName);
                    }
                    result.add(child);
                    preDimensionName = dimensionName;
                }
            }
        }
        return result;
    }

    private static String doubleToString(Double doubleValue){
        return doubleValue == null ? "" : doubleValue.toString();
    }
}
