package cn.com.countrygarden.bi.main.controller.ck;

import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.dto.ck.CkCleanerFieldConfigVo;
import cn.com.countrygarden.bi.main.dto.ck.CkCleanerQueyr;
import cn.com.countrygarden.bi.main.dto.ck.CkCleanerTableVo;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.ck.CkFiledValueVo;
import cn.com.countrygarden.bi.main.model.ck.CkRegionFiledValueVo;
import cn.com.countrygarden.bi.main.service.ck.CkCleanerExpandService;
import cn.com.countrygarden.bi.main.service.ck.CkDataExportHandler;
import cn.com.countrygarden.bi.main.service.ck.CkGreenExpandService;
import cn.com.countrygarden.bi.main.service.ck.CkKshExpandService;
import cn.com.countrygarden.bi.main.util.ExcelCopyUtil;
import cn.com.countrygarden.bi.main.util.export.CExcelExportEntity;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static cn.com.countrygarden.bi.main.util.export.DynamicExcelUtil.buildWorkBook;


/**
 * 基础数据表-业务逻辑层实现
 *
 * @author 李森林
 * @email www.justintoforest@gamail.com
 * @url
 * @date 2020-04-13
 */
@RestController("CkDataExportController")
@RequestMapping("/api/ckData")
public class CkDataExportController extends BaseController {
    @Autowired
    private CkCleanerExpandService ckCleanerService;

    @Autowired
    private CkGreenExpandService ckGreenService;

    @Autowired
    private Map<String,CkDataExportHandler> ckDataExportHandlerMap;

    @Autowired
    private CkKshExpandService ckKshExpandService;
    /**
     * 区域导出
     * @param cleanerQueyr
     * @param role
     * @param type
     */
    @GetMapping(value = "/bi/region/export", produces = "application/json;charset=utf-8")
    public void biRegionExport(CkCleanerQueyr cleanerQueyr,
                              @RequestParam(value = "role", required = false) String role,
                              @RequestParam(value = "type", required = false) String type) {
        List<CkCleanerFieldConfigVo> configVos = (List<CkCleanerFieldConfigVo>) ckCleanerService.getDTableConfig(role, getAccessToken()).getData();
        ArrayList<CkCleanerFieldConfigVo> configVos1 = new ArrayList<>(configVos);
//      sheet1
//      过滤出可读写的属性
        List<String> propSortList=CollUtil.newArrayList();
        configVos = parseFieldConfig(configVos);
        List<CExcelExportEntity> colList = new ArrayList<>();
//      固定列
        colList.add(new CExcelExportEntity("区域编码","orgCode",0));
        colList.add(new CExcelExportEntity("区域名称","orgName",0));
//        colList.add(new CExcelExportEntity("年份","year",0));
//        colList.add(new CExcelExportEntity("月份","month",0));
//      动态列
        buildCol(configVos,colList,propSortList);
//      导出配置
        ExportParams params = new ExportParams(null, "sheet1", ExcelType.XSSF);
        XSSFWorkbook workbook = (XSSFWorkbook) buildWorkBook(colList, CollUtil.newArrayList(), params);
//      组合导出数据
        List<CkRegionFiledValueVo> data = ckCleanerService.getRegionData(cleanerQueyr, getAccessToken());
        List<List<String>> rowsDatas=new ArrayList<>();
        if(CollUtil.isEmpty(data)){
            data=new ArrayList<>();
        }
        for (CkRegionFiledValueVo tableVo : data) {
            List<String> rowData = new ArrayList<String>();
            rowData.add(StrUtil.isNotBlank(tableVo.getOrgCode())?tableVo.getOrgCode():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getOrgName())?tableVo.getOrgName():"");
//          设置动态列
            for (String fieldCode : propSortList) {
                rowData.add(getDynamicValue2(tableVo,fieldCode));
            }
            rowsDatas.add(rowData);
        }
        XSSFWorkbook wb = getWb(configVos1, cleanerQueyr);
        XSSFWorkbook wb2 = writerData2(type, rowsDatas, workbook, true);
        wb.createSheet("总部合计");
        ExcelCopyUtil.copySheet(wb,wb2.getSheetAt(0),wb.getSheetAt(1),true);
//      写出数据
        downloadExcelData(wb, "导出数据");
    }


    /**
     * 可视化区域导出
     * @param cleanerQueyr
     * @param role
     * @param type
     */
    @GetMapping(value = "/bi/region/exportKsh", produces = "application/json;charset=utf-8")
    public void biRegionExportKsh(CkCleanerQueyr cleanerQueyr,
                               @RequestParam(value = "role", required = false) String role,
                               @RequestParam(value = "type", required = false) String type) {
        List<CkCleanerFieldConfigVo> configVos = (List<CkCleanerFieldConfigVo>) ckKshExpandService.getDTableConfig(role, getAccessToken()).getData();
        ArrayList<CkCleanerFieldConfigVo> configVos1 = new ArrayList<>(configVos);
//      sheet1
//      过滤出可读写的属性
        List<String> propSortList=CollUtil.newArrayList();
        configVos = parseFieldConfig(configVos);
        List<CExcelExportEntity> colList = new ArrayList<>();
//      固定列
        colList.add(new CExcelExportEntity("区域编码","orgCode",0));
        colList.add(new CExcelExportEntity("区域名称","orgName",0));
//        colList.add(new CExcelExportEntity("年份","year",0));
//        colList.add(new CExcelExportEntity("月份","month",0));
//      动态列
        buildCol(configVos,colList,propSortList);
//      导出配置
        ExportParams params = new ExportParams(null, "sheet1", ExcelType.XSSF);
        XSSFWorkbook workbook = (XSSFWorkbook) buildWorkBook(colList, CollUtil.newArrayList(), params);
//      组合导出数据
        List<CkRegionFiledValueVo> data = ckKshExpandService.getRegionData(cleanerQueyr, getAccessToken());
        List<List<String>> rowsDatas=new ArrayList<>();
        if(CollUtil.isEmpty(data)){
            data=new ArrayList<>();
        }
        for (CkRegionFiledValueVo tableVo : data) {
            List<String> rowData = new ArrayList<String>();
            rowData.add(StrUtil.isNotBlank(tableVo.getOrgCode())?tableVo.getOrgCode():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getOrgName())?tableVo.getOrgName():"");
//          设置动态列
            for (String fieldCode : propSortList) {
                rowData.add(getDynamicValue2(tableVo,fieldCode));
            }
            rowsDatas.add(rowData);
        }
        XSSFWorkbook wb = getWbKsh(configVos1, cleanerQueyr);
        XSSFWorkbook wb2 = writerData2(type, rowsDatas, workbook, true);
        wb.createSheet("总部合计");
        ExcelCopyUtil.copySheet(wb,wb2.getSheetAt(0),wb.getSheetAt(1),true);
//      写出数据
        downloadExcelData(wb, "导出数据");
    }


    private XSSFWorkbook getWb(List<CkCleanerFieldConfigVo> configVos, CkCleanerQueyr cleanerQueyr){
//      过滤出可读写的属性
        List<CExcelExportEntity> colList = new ArrayList<>();
//      过滤出可读写的属性
        List<String> propSortList=CollUtil.newArrayList();
        configVos = parseFieldConfig(configVos);
//      固定列
        colList.add(new CExcelExportEntity("区域编码","orgCode",0));
        colList.add(new CExcelExportEntity("区域名称","orgName",0));
        colList.add(new CExcelExportEntity("项目编码","commCode",0));
        colList.add(new CExcelExportEntity("项目名称","commName",0));
        colList.add(new CExcelExportEntity("年份","year",0));
        colList.add(new CExcelExportEntity("月份","month",0));
//      动态列
        buildCol(configVos,colList,propSortList);
//      导出配置
        ExportParams params = new ExportParams(null, "项目明细", ExcelType.XSSF);
        XSSFWorkbook workbook = (XSSFWorkbook) buildWorkBook(colList, CollUtil.newArrayList(), params);
//      组合导出数据
        cleanerQueyr.setPage(1);
        cleanerQueyr.setSize(Integer.MAX_VALUE);
        PageData data = (PageData) ckCleanerService.queryPageList(cleanerQueyr, getAccessToken()).getData();
        List<CkCleanerTableVo> list = (List<CkCleanerTableVo>) data.getData();
        List<List<String>> rowsDatas=new ArrayList<>();
        for (CkCleanerTableVo tableVo : list) {
            List<String> rowData = new ArrayList<String>();
            rowData.add(StrUtil.isNotBlank(tableVo.getOrgCode())?tableVo.getOrgCode():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getOrgName())?tableVo.getOrgName():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getCommCode())?tableVo.getCommCode():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getCommName())?tableVo.getCommName():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getYear())?tableVo.getYear():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getMonth())?tableVo.getMonth():"");
//          设置动态列
            for (String fieldCode : propSortList) {
                rowData.add(getDynamicValue(tableVo,fieldCode));
            }
            rowsDatas.add(rowData);
        }
//      因层级过于复杂，easypoi形式写入方式舍弃，采用简易数据写入
        ExcelWriter writer = new ExcelWriter(workbook, "项目明细");
        StyleSet style=new StyleSet(workbook);
        style.setBorder(BorderStyle.NONE, IndexedColors.AQUA);
        writer.setStyleSet(style);
//      跳过标题行
        writer.passRows(workbook.getSheet("项目明细").getLastRowNum()+1);
        for (List<String> rowData : rowsDatas) {
            writer.writeRow(rowData,false);
        }
        return workbook;
    }

    private XSSFWorkbook getWbKsh(List<CkCleanerFieldConfigVo> configVos, CkCleanerQueyr cleanerQueyr){
//      过滤出可读写的属性
        List<CExcelExportEntity> colList = new ArrayList<>();
//      过滤出可读写的属性
        List<String> propSortList=CollUtil.newArrayList();
        configVos = parseFieldConfig(configVos);
//      固定列
        colList.add(new CExcelExportEntity("区域编码","orgCode",0));
        colList.add(new CExcelExportEntity("区域名称","orgName",0));
        colList.add(new CExcelExportEntity("项目编码","commCode",0));
        colList.add(new CExcelExportEntity("项目名称","commName",0));
        colList.add(new CExcelExportEntity("年份","year",0));
        colList.add(new CExcelExportEntity("月份","month",0));
        colList.add(new CExcelExportEntity("常规实际进驻时间","actualEnterTime",0));
        colList.add(new CExcelExportEntity("合同收楼时间","contractProperty",0));
        colList.add(new CExcelExportEntity("是否填报完成","isFillin",0));
        colList.add(new CExcelExportEntity("是否申请解锁","isUnlocking",0));
        colList.add(new CExcelExportEntity("是否对外显示","isDisplay",0));
//      动态列
        buildCol(configVos,colList,propSortList);
//      导出配置
        ExportParams params = new ExportParams(null, "项目明细", ExcelType.XSSF);
        XSSFWorkbook workbook = (XSSFWorkbook) buildWorkBook(colList, CollUtil.newArrayList(), params);
//      组合导出数据
        cleanerQueyr.setPage(1);
        cleanerQueyr.setSize(Integer.MAX_VALUE);
        PageData data = (PageData) ckKshExpandService.queryPageList(cleanerQueyr, getAccessToken()).getData();
        List<CkCleanerTableVo> list = (List<CkCleanerTableVo>) data.getData();
        List<List<String>> rowsDatas=new ArrayList<>();
        for (CkCleanerTableVo tableVo : list) {
            List<String> rowData = new ArrayList<String>();
            rowData.add(StrUtil.isNotBlank(tableVo.getOrgCode())?tableVo.getOrgCode():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getOrgName())?tableVo.getOrgName():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getCommCode())?tableVo.getCommCode():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getCommName())?tableVo.getCommName():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getYear())?tableVo.getYear():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getMonth())?tableVo.getMonth():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getActualEnterTime())?tableVo.getActualEnterTime():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getContractProperty())?tableVo.getContractProperty():"");
            if(tableVo.getIsFillin()==null){
                rowData.add("");
            }else {
                rowData.add(tableVo.getIsFillin().equals(1)?"是":"否");
            }
            if(tableVo.getIsUnlocking()==null){
                rowData.add("");
            }else {
                rowData.add(tableVo.getIsUnlocking().equals(1)?"是":"否");
            }
            if(tableVo.getIsDisplay()==null){
                rowData.add("");
            }else {
                rowData.add(tableVo.getIsDisplay().equals(1)?"是":"否");
            }

//          设置动态列
            for (String fieldCode : propSortList) {
                rowData.add(getDynamicValue(tableVo,fieldCode));
            }
            rowsDatas.add(rowData);
        }
//      因层级过于复杂，easypoi形式写入方式舍弃，采用简易数据写入
        ExcelWriter writer = new ExcelWriter(workbook, "项目明细");
        StyleSet style=new StyleSet(workbook);
        style.setBorder(BorderStyle.NONE, IndexedColors.AQUA);
        writer.setStyleSet(style);
//      跳过标题行
        writer.passRows(workbook.getSheet("项目明细").getLastRowNum()+1);
        for (List<String> rowData : rowsDatas) {
            writer.writeRow(rowData,false);
        }
        return workbook;
    }


    private String getDynamicValue2(CkRegionFiledValueVo tableVo, String fieldCode) {
        for (CkFiledValueVo valueVo : tableVo.getChildren()) {
            if(valueVo==null||valueVo.getFieldCode()==null){
                continue;
            }
            if(valueVo.getFieldCode().equalsIgnoreCase(fieldCode)){
                return valueVo.getValue();
            }
        }
        return "";
    }


    /**
     * 通用化数据导出
     * @param cleanerQueyr
     * @param role
     * @param type
     */
    @GetMapping(value = "/export", produces = "application/json;charset=utf-8")
    public void export(CkCleanerQueyr cleanerQueyr,
                              @RequestParam(value = "role", required = false) String role,
                              @RequestParam(value = "type", required = false) String type) {
        List<CkCleanerFieldConfigVo> configVos = (List<CkCleanerFieldConfigVo>) ckCleanerService.getDTableConfig(role, getAccessToken()).getData();
//      过滤出可读写的属性
        List<String> propSortList=CollUtil.newArrayList();
        configVos = parseFieldConfig(configVos);
        List<CExcelExportEntity> colList = new ArrayList<>();
//      固定列
        //colList.add(new CExcelExportEntity("区域编码","orgCode",0));
        colList.add(new CExcelExportEntity("区域名称","orgName",0));
        //colList.add(new CExcelExportEntity("项目编码","commCode",0));
        colList.add(new CExcelExportEntity("项目名称","commName",0));
        colList.add(new CExcelExportEntity("年份","year",0));
        colList.add(new CExcelExportEntity("月份","month",0));
//      动态列
        buildCol(configVos,colList,propSortList);
//      导出配置
        ExportParams params = new ExportParams(null, "sheet1", ExcelType.XSSF);
        XSSFWorkbook workbook = (XSSFWorkbook) buildWorkBook(colList, CollUtil.newArrayList(), params);
//      组合导出数据
        cleanerQueyr.setPage(1);
        cleanerQueyr.setSize(Integer.MAX_VALUE);
        PageData data = (PageData) ckCleanerService.queryPageList(cleanerQueyr, getAccessToken()).getData();
        List<CkCleanerTableVo> list = (List<CkCleanerTableVo>) data.getData();
        List<List<String>> rowsDatas=new ArrayList<>();
        for (CkCleanerTableVo tableVo : list) {
            List<String> rowData = new ArrayList<String>();
           // rowData.add(StrUtil.isNotBlank(tableVo.getOrgCode())?tableVo.getOrgCode():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getOrgName())?tableVo.getOrgName():"");
           // rowData.add(StrUtil.isNotBlank(tableVo.getCommCode())?tableVo.getCommCode():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getCommName())?tableVo.getCommName():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getYear())?tableVo.getYear():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getMonth())?tableVo.getMonth():"");
//          设置动态列
            for (String fieldCode : propSortList) {
                rowData.add(getDynamicValue(tableVo,fieldCode));
            }
            rowsDatas.add(rowData);
        }
//      写出数据
        writerData(type,rowsDatas,workbook, true);
    }

    /**
     * 绿化数据导出
     * @param cleanerQueyr
     * @param role
     * @param type
     */
    @GetMapping(value = "/ckGreenExport", produces = "application/json;charset=utf-8")
    public void ckGreenExport(CkCleanerQueyr cleanerQueyr,
                       @RequestParam(value = "role", required = false) String role,
                       @RequestParam(value = "type", required = false) String type) {
        List<CkCleanerFieldConfigVo> configVos = (List<CkCleanerFieldConfigVo>) ckCleanerService.getDTableConfig(role, getAccessToken()).getData();
//      过滤出可读写的属性
        List<String> propSortList=CollUtil.newArrayList();
        configVos = parseFieldConfig(configVos);
        List<CExcelExportEntity> colList = new ArrayList<>();
//      固定列
        colList.add(new CExcelExportEntity("区域编码","orgCode",0));
        colList.add(new CExcelExportEntity("区域名称","orgName",0));
        colList.add(new CExcelExportEntity("项目编码","commCode",0));
        colList.add(new CExcelExportEntity("项目名称","commName",0));
        colList.add(new CExcelExportEntity("年份","year",0));
        colList.add(new CExcelExportEntity("月份","month",0));
//      动态列
        buildCol(configVos,colList,propSortList);
//      导出配置
        ExportParams params = new ExportParams(null, "sheet1", ExcelType.XSSF);
        XSSFWorkbook workbook = (XSSFWorkbook) buildWorkBook(colList, CollUtil.newArrayList(), params);
//      组合导出数据
        cleanerQueyr.setPage(1);
        cleanerQueyr.setSize(Integer.MAX_VALUE);
        PageData data = (PageData) ckGreenService.queryPageList(cleanerQueyr, getAccessToken()).getData();
        List<CkCleanerTableVo> list = (List<CkCleanerTableVo>) data.getData();
        List<List<String>> rowsDatas=new ArrayList<>();
        for (CkCleanerTableVo tableVo : list) {
            List<String> rowData = new ArrayList<String>();
            rowData.add(StrUtil.isNotBlank(tableVo.getOrgCode())?tableVo.getOrgCode():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getOrgName())?tableVo.getOrgName():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getCommCode())?tableVo.getCommCode():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getCommName())?tableVo.getCommName():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getYear())?tableVo.getYear():"");
            rowData.add(StrUtil.isNotBlank(tableVo.getMonth())?tableVo.getMonth():"");
//          设置动态列
            for (String fieldCode : propSortList) {
                rowData.add(getDynamicValue(tableVo,fieldCode));
            }
            rowsDatas.add(rowData);
        }
//      写出数据
        writerData(type,rowsDatas,workbook, true);
    }

    private void writerData(String type, List<List<String>> rowsDatas, XSSFWorkbook workbook, boolean isUseBulid) {
//      因层级过于复杂，easypoi形式写入方式舍弃，采用简易数据写入
        ExcelWriter writer = new ExcelWriter(workbook, "sheet1");
        StyleSet style=new StyleSet(workbook);
        style.setBorder(BorderStyle.NONE, IndexedColors.AQUA);
        writer.setStyleSet(style);
//      跳过标题行
        writer.passRows(workbook.getSheet("sheet1").getLastRowNum()+1);
        if(!isUseBulid){
            for (List<String> rowData : rowsDatas) {
                writer.writeRow(rowData,false);
            }
            downloadExcelData(workbook, "导出数据");
            return;
        }
//      策略注入调整最终输出数据
        CkDataExportHandler exportHandler = ckDataExportHandlerMap.get(type);
        if(exportHandler!=null){
            exportHandler.buildData(rowsDatas);
        }
        if(CollUtil.isEmpty(rowsDatas)){
            downloadExcelData(workbook, "测试");
            return;
        }
        for (List<String> rowData : rowsDatas) {
            writer.writeRow(rowData,false);
        }
//      excel数据表最终调整
        if(exportHandler!=null){
            exportHandler.buildWorkBook(workbook);
        }
        downloadExcelData(workbook, "导出数据");
    }


    private XSSFWorkbook writerData2(String type, List<List<String>> rowsDatas, XSSFWorkbook workbook, boolean isUseBulid) {
//      因层级过于复杂，easypoi形式写入方式舍弃，采用简易数据写入
        ExcelWriter writer = new ExcelWriter(workbook, "sheet1");
        StyleSet style=new StyleSet(workbook);
        style.setBorder(BorderStyle.NONE, IndexedColors.AQUA);
        writer.setStyleSet(style);
//      跳过标题行
        writer.passRows(workbook.getSheet("sheet1").getLastRowNum()+1);
        if(!isUseBulid){
            for (List<String> rowData : rowsDatas) {
                writer.writeRow(rowData,false);
            }
            downloadExcelData(workbook, "导出数据");
            return workbook;
        }
//      策略注入调整最终输出数据
        CkDataExportHandler exportHandler = ckDataExportHandlerMap.get(type);
        if(CollUtil.isEmpty(rowsDatas)){
            return workbook;
        }
        if(exportHandler!=null){
            exportHandler.buildData(rowsDatas);
        }
        for (List<String> rowData : rowsDatas) {
            writer.writeRow(rowData,false);
        }
//      excel数据表最终调整
        if(exportHandler!=null){
            exportHandler.buildWorkBook(workbook);
        }
        return workbook;
    }

    private String getDynamicValue(CkCleanerTableVo tableVo, String fieldCode) {
        for (int i = 0; i < tableVo.getChildren().size(); i++) {
            CkCleanerFieldConfigVo configVo = tableVo.getChildren().get(i);
            if(configVo==null||configVo.getFieldCode()==null){
                continue;
            }
            if(configVo.getFieldCode().equalsIgnoreCase(fieldCode)){
                return StrUtil.isBlank(configVo.getValue())||configVo.getValue()==null?"":configVo.getValue();
            }
        }
        return "";
    }


    /**
     * 构建动态列
     * @param list
     * @param dataList
     * @param configVos
     * @param colList
     * @param propSortList
     */
    private void buildCol(List<CkCleanerFieldConfigVo> configVos, List<CExcelExportEntity> colList, List<String> propSortList) {
        int level=0;
        for (int i = 0; i < configVos.size(); i++) {
            CkCleanerFieldConfigVo configVo = configVos.get(i);
//          如果为空目录
            if(configVo.getType()==0&&CollUtil.isEmpty(configVo.getChildren())){
                continue;
            }
            if(configVo.getType()==1){
                propSortList.add(configVo.getFieldCode());
            }
            CExcelExportEntity exportEntity = new CExcelExportEntity(configVo.getFieldName(), configVo.getFieldCode(),level);
//                    configVo.getShowWidth()!=null?configVo.getShowWidth().intValue()/3:30
            colList.add(exportEntity);
            exportEntity.setTotalChildCount(0);
            if(CollUtil.isNotEmpty(configVo.getChildren())){
                List<CExcelExportEntity> entities = buildChildCol(configVo.getChildren(), level + 1, propSortList);
                exportEntity.setTotalChildCount(countChild(entities));
                exportEntity.setList(entities);
            }
        }
    }

    /**
     * 过滤需要的字段集合
     * @param configVos
     * @return
     */
    private List<CkCleanerFieldConfigVo> parseFieldConfig(List<CkCleanerFieldConfigVo> configVos) {
        List<CkCleanerFieldConfigVo> result=new ArrayList<>();
        for (int i = 0; i < configVos.size(); i++) {
            CkCleanerFieldConfigVo configVo = configVos.get(i);
//          如果为字段并且可读
            if(configVo.getType()==1&&configVo.getIsWrite()!=null){
                result.add(configVo);
                continue;
            }
//          如果目录并且子级为空则跳过
            if(configVo.getType()==0&&CollUtil.isEmpty(configVo.getChildren())){
                continue;
            }
//          如果为目录并且有下级
            List<CkCleanerFieldConfigVo> vos = parseFieldConfig(configVo.getChildren());
            if(CollUtil.isNotEmpty(vos)){
                result.add(configVo);
            }
        }
        return result;
    }


    /**
     * 构建目录下的子字段
     * @param configVos
     * @param propSortList
     * @return
     */
    private List<CExcelExportEntity> buildChildCol(List<CkCleanerFieldConfigVo> configVos, int level, List<String> propSortList) {
        List<CExcelExportEntity> colList=new ArrayList<>();
        for (int i = 0; i < configVos.size(); i++) {
            CkCleanerFieldConfigVo configVo = configVos.get(i);
//          如果为空目录
            if(configVo.getType()==0&&CollUtil.isEmpty(configVo.getChildren())){
                continue;
            }
            if(configVo.getType()==1){
                propSortList.add(configVo.getFieldCode());
            }
            CExcelExportEntity exportEntity = new CExcelExportEntity(configVo.getFieldName(), configVo.getFieldCode(),level);
//                    configVo.getShowWidth()!=null?configVo.getShowWidth().intValue()/3:30
            exportEntity.setTotalChildCount(0);
            if(CollUtil.isNotEmpty(configVo.getChildren())){
                List<CExcelExportEntity> entities = buildChildCol(configVo.getChildren(), level + 1, propSortList);
                exportEntity.setTotalChildCount(countChild(entities));
                exportEntity.setList(entities);
            }
            colList.add(exportEntity);
        }
        return CollUtil.isEmpty(colList)?null:colList;
    }

    public int countChild(List<CExcelExportEntity> entities){
        int result=0;
        for (CExcelExportEntity entity : entities) {
            if(entity.getTotalChildCount()!=0){
                result+=entity.getTotalChildCount();
            }else{
                result+=1;
                if(CollUtil.isNotEmpty(entity.getList())){
                    result+=entity.getList().size();
                }
            }
        }
        return result;
    }



}