package cn.com.countrygarden.bi.main.util.export;

import cn.com.countrygarden.bi.main.util.export.CExcelExportUtil;
import cn.hutool.core.collection.CollUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.entity.vo.BaseEntityTypeConstants;

import java.util.*;

/**
 * 动态Excel工具类
 *
 * @ClassName:DynamicExcelUtil.java
 * @Description:功能描述
 * @author: lisenlin
 * @date: 2020/9/11
 */
public abstract class DynamicExcelUtil {

    public static List<ExcelExportEntity> buildExcelExportEntity(Map<String, DynamicExcelData> paramInfo) {
        //表头的集合，用于添加表头
        List<ExcelExportEntity> entityList = new ArrayList<>();
        //参数信息--[用于动态拼接列头]
        final Iterator<String> iterator = paramInfo.keySet().iterator();
        while (iterator.hasNext()) {
            final String paramKeyStr = iterator.next();
            final String paramNameStr = paramInfo.get(paramKeyStr).getName();
            ExcelExportEntity exportEntity = new ExcelExportEntity(paramNameStr, paramKeyStr, 30);
            exportEntity.setType(paramInfo.get(paramKeyStr).getType());
            //列头由参数汉字名称,参数key为列key
            entityList.add(exportEntity);
        }
        return entityList;
    }

    public static List<Map<String, Object>> buildExportDynamicData(Map<String, DynamicExcelData> colMap, List<List<DynamicExcelValue>> staticData) {
        //数据列
        final Set<String> statisParamKey = colMap.keySet();
        //最终的数据
        List<Map<String, Object>> datas = new ArrayList<>();
//          遍历每行数据
        for (int i = 0; i < staticData.size(); i++) {
//           遍历动态列数据
            List<DynamicExcelValue> colLists = staticData.get(i);
            Map<String, Object> hashMap = new LinkedHashMap<>(10);
            for (String paramKey : statisParamKey) {
                hashMap.put(paramKey,0);
            }
            for (int j = 0; j < colLists.size(); j++) {
                //参数的验证
                for (String paramKey : statisParamKey) {
                    DynamicExcelValue dto = colLists.get(j);
                    if (paramKey.equals(dto.getKey())) {
                        hashMap.put(dto.getKey(), dto.getValue());
                    }
                }
            }
            datas.add(hashMap);
        }
        return datas;
    }

    public static Workbook buildWorkBook(List<CExcelExportEntity> colList, List<Map<String, Object>> valList, ExportParams params) {
        if (CollUtil.isEmpty(colList)) {
            return null;
        }
        final Workbook workbook = CExcelExportUtil.exportExcel(params, colList, valList);
        //动态合并纵列[mergeMap key列索引(从0开始),value依赖的列,没有传空,startRow 开始行(从零开始)]
        return workbook;
    }
    /**
     * 动态数据
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class DynamicExcelData {

        /**
         * 参数名称
         */
        private String name;

        /**
         * 参数key
         */
        private String key;

        private Integer type= BaseEntityTypeConstants.StringType;

    }

    /**
     * 动态值
     */
    @Data
    @AllArgsConstructor
    public static class DynamicExcelValue {

        /**
         * 参数的key
         */
        private String Key;

        @Excel(name = "统计数据", orderNum = "2")
        private Object value;

    }

}
