package nckd.jxccl.swc.hsas.business.outdata;

import com.google.common.collect.Lists;
import nckd.jxccl.base.common.utils.DateTimeUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.exception.ErrorCode;
import kd.bos.exception.KDException;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QFilter;
import kd.bos.orm.util.CollectionUtils;
import nckd.jxccl.base.sit.helper.SITExportDataHelper;
import nckd.jxccl.base.swc.helper.SWCHelper;
import nckd.jxccl.sit.hcsi.business.importtaskguide.utils.ImportTaskUtils;
import nckd.jxccl.sit.hcsi.common.constant.enums.DataTypeEnum;
import nckd.jxccl.sit.hcsi.common.constant.enums.SITShowType;
import nckd.jxccl.swc.constants.SwcConstant;
import nckd.jxccl.swc.hsas.business.utils.OutImportTaskUtils;
import nckd.jxccl.swc.hsas.common.OutImpPresetItem;
import nckd.jxccl.swc.hsas.formplugin.web.outdata.helper.OutImpTemplateHelper;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.streaming.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;

import java.text.MessageFormat;
import java.util.*;

/**
 * Tyx 2025-10-21
 * 导出Service，nckd_taskguideimport
 */
public class OutImportTaskGuideExportService {

    private static final Log logger = LogFactory.getLog(OutImportTaskGuideExportService.class);
    private static final int PAGE_SIZE = 500;
    private static final String TPLERROR = "1";
    private static final String IMPORT_ERROR_TEMPLATE = "外部数据导入任务_数据导入失败结果_{0}";
    private static final String DATA_IMPORT_SHEET = "数据导入模板";
    private static final String ERROR_CODE = "getImportErrorExcelFile_error";
    private static final int COLUMN_WIDTH_MULTIPLIER = 3;
    private static final int FIRST_COLUMN_WIDTH_MULTIPLIER = 4;

    /**
     * 导入模板方法
     *
     * @param view
     */
    public void exportExcelTemplate(IFormView view) {
        logger.info("-------- begin exportExcelTemplate --------");

        try {
            SXSSFWorkbook wb = new SXSSFWorkbook(500);
            Throwable exp = null;
            try {
                FormShowParameter parameter = view.getFormShowParameter();
                // 外部系统数据导入模板
                Long tplId = (Long) parameter.getCustomParam("outimpTplId");
                // 导入任务ID
                Long importTaskId = (Long) parameter.getCustomParam("importTaskId");
                String dd = DateTimeUtils.format(new Date(), "yyyyMMdd");
                String exportFileName = MessageFormat.format(ResManager.loadKDString("外单位数据导入任务_数据导入模板_{0}", "ImportTaskGuideExportService_1", "swc-hsas-business", new Object[0]), dd);
                createImportTaskDataSheet(tplId, wb, importTaskId);
                flushAllAndDownload(wb, exportFileName, view);
            } catch (Throwable var18) {
                exp = var18;
                throw var18;
            } finally {
                if (wb != null) {
                    if (exp != null) {
                        try {
                            wb.close();
                        } catch (Throwable e1) {
                            exp.addSuppressed(e1);
                        }
                    } else {
                        wb.close();
                    }
                }

            }
        } catch (Exception e2) {
            logger.error("ImportTaskGuideExportService  error : ", e2);
            String str = ResManager.loadKDString("下载导入模板失败。", "ImportTaskGuideExportService_0", "swc-hsas-business", new Object[0]);
            view.showErrorNotification(str);
        }
        logger.info("end exportExcelTemplate");
    }

    /**
     * 创建Excel
     *
     * @param tplId
     * @param wb
     * @param importTaskId
     */
    private void createImportTaskDataSheet(Long tplId, SXSSFWorkbook wb, Long importTaskId) {
        DynamicObject sinsurData = OutImportTaskUtils.getTplData(tplId, "id,number,nckd_startline", (QFilter) null);
        int startLine = sinsurData.getInt("nckd_startline");
        String sheetName = ResManager.loadKDString("数据导入模板", "ImportTaskGuideExportService_2", "swc-hsas-business", new Object[0]);
        SXSSFSheet sheet = wb.createSheet(sheetName);
        sheet.setRandomAccessWindowSize(-1);
        SXSSFDrawing drawingPatriarch = sheet.createDrawingPatriarch();
        SXSSFRow headRow = sheet.createRow(startLine - 2);
        // 创建表头样式
        CellStyle headDateStyle = getColumnStyle(true, 0, 0, SITShowType.DATE.getCode(), HorizontalAlignment.LEFT, sheet.getWorkbook(), HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex(), true);
        CellStyle headTextStyle = getColumnStyle(true, 0, 0, SITShowType.TEXT.getCode(), HorizontalAlignment.LEFT, sheet.getWorkbook(), HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex(), true);
        CellStyle headAmountStyle = getColumnStyle(true, 0, 0, SITShowType.AMOUNT.getCode(), HorizontalAlignment.LEFT, sheet.getWorkbook(), HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex(), true);
        CellStyle headNumStyle = getColumnStyle(true, 0, 0, SITShowType.NUM.getCode(), HorizontalAlignment.LEFT, sheet.getWorkbook(), HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex(), true);
        CellStyle headRequriedNumStyle = getColumnStyle(true, 0, 0, SITShowType.NUM.getCode(), HorizontalAlignment.LEFT, sheet.getWorkbook(), HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex(), true);
        CellStyle headRequiredDateStyle = getColumnStyle(true, 0, 0, SITShowType.DATE.getCode(), HorizontalAlignment.LEFT, sheet.getWorkbook(), HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex(), true);
        CellStyle headRequiredAmountStyle = getColumnStyle(true, 0, 0, SITShowType.AMOUNT.getCode(), HorizontalAlignment.LEFT, sheet.getWorkbook(), HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex(), true);
        CellStyle headRequrriedTextStyle = getColumnStyle(true, 0, 0, SITShowType.TEXT.getCode(), HorizontalAlignment.LEFT, sheet.getWorkbook(), HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex(), true);
        // 必填项样式
        Font font = sheet.getWorkbook().createFont();
        font.setColor((short) 10);
        headRequiredDateStyle.setFont(font);
        headRequiredAmountStyle.setFont(font);
        headRequrriedTextStyle.setFont(font);
        headRequriedNumStyle.setFont(font);
        Map<Long, OutImpPresetItem> itemEnumMap = OutImpTemplateHelper.getPresetItemEnumMap();
        List<Map<String, String>> columnHeadList = OutImportTaskUtils.getColumnHeadList(tplId, importTaskId);
        List<Integer> textIndexList = new ArrayList(columnHeadList.size());
        Set<Integer> allIndexSet = new HashSet(columnHeadList.size());
        OutImpPresetItem presetItemEnum = null;
        for (int i = 0; i < columnHeadList.size(); ++i) {
            Map<String, String> columnHead = columnHeadList.get(i);
            String columnIndexStr = (String) columnHead.get("columnIndex");
            String dataTypeIdStr = (String) columnHead.get("datatypeid");
            String name = (String) columnHead.get("name");
            String itemType = (String) columnHead.get("itemType");
            String comment = (String) columnHead.get("comment");
            int columnIndex = Integer.parseInt(columnIndexStr);
            if (allIndexSet.add(columnIndex) && !textIndexList.contains(columnIndex)) {
                if (Long.parseLong(dataTypeIdStr) == DataTypeEnum.STRING.getDbId()) {
                    textIndexList.add(columnIndex);
                }

                if ("0".equals(itemType)) {
                    CellStyle style = this.getFixCellStyle(true, Long.valueOf(columnHead.get("datatypeid")), headDateStyle,  headTextStyle,  headAmountStyle,  headNumStyle,
                            headRequiredDateStyle,  headRequrriedTextStyle,  headRequiredAmountStyle,  headRequriedNumStyle);
                    this.createCell(comment, headRow, columnIndex, name, true, style, dataTypeIdStr, drawingPatriarch);
                } else {
                    CellStyle style = this.getFixCellStyle(false, Long.valueOf(columnHead.get("datatypeid")), headDateStyle,  headTextStyle,  headAmountStyle,  headNumStyle,
                            headRequiredDateStyle,  headRequrriedTextStyle,  headRequiredAmountStyle,  headRequriedNumStyle);
                    this.createCell(comment, headRow, columnIndex, name, false, style, dataTypeIdStr, drawingPatriarch);
                }
            }
        }

        Iterator<Integer> it = allIndexSet.iterator();
        while (it.hasNext()) {
            int index = (Integer) it.next();
            int columnIndex = sheet.getColumnWidth(index);
            sheet.setColumnWidth(index, columnIndex * 3);
        }

        // 添加数据验证（下拉列表）
        this.addDataValidations(sheet, columnHeadList, startLine - 1);

        this.batchSetDefaulColumnStyle(sheet, textIndexList, this.getTextCellStyle(sheet));
    }

    private void addDataValidations(SXSSFSheet sheet, List<Map<String, String>> columnHeadList, int firstDataRowIndex) {
        for (int i = 0; i < columnHeadList.size(); i++) {
            Map<String, String> columnHead = columnHeadList.get(i);
            String itemType = columnHead.get("itemType");

            // 如果是预设项（itemType为"0"）
            if ("0".equals(itemType)) {
                Long itemId = Long.valueOf(columnHead.get("id"));
                OutImpPresetItem presetItem = OutImpTemplateHelper.getPresetItemEnumMap().get(itemId);

                // 检查是否有预定义的下拉选项
                if (presetItem != null && hasDropdownOptions(presetItem)) {
                    String[] options = getDropdownOptions(presetItem);
                    if (options != null && options.length > 0) {
                        // 创建数据验证
                        int columnIndex = Integer.parseInt(columnHead.get("columnIndex"));
                        setDataValidation(sheet, options, columnIndex, firstDataRowIndex, true);
                    }
                }
                // 特殊处理二级单位
                else if (presetItem != null && presetItem.getItemNumber().equals("R1")) {
                    String[] options = getOrgDropdownOptions();
                    if (options != null && options.length > 0) {
                        // 创建数据验证
                        int columnIndex = Integer.parseInt(columnHead.get("columnIndex"));
                        setDataValidation(sheet, options, columnIndex, firstDataRowIndex, false);
                    }
                }
            }
        }
    }


    /**
     * 判断是否有下拉项 【nckd_outimportitem】
     * @param presetItem
     * @return
     */
    private boolean hasDropdownOptions(OutImpPresetItem presetItem) {
        Long itemId = presetItem.getItemId();
        DynamicObject item = SwcConstant.OUTIMPORTITEM_ENTITY.loadOne(itemId);
        DynamicObjectCollection entryCols = item.getDynamicObjectCollection("nckd_entryentity");
        if(entryCols.size() > 0) {
            return true;
        }
        else {
            return false;
        }
    }

    private String[] getDropdownOptions(OutImpPresetItem presetItem) {
        Long itemId = presetItem.getItemId();
        DynamicObject item = SwcConstant.OUTIMPORTITEM_ENTITY.loadOne(itemId);
        DynamicObjectCollection entryCols = item.getDynamicObjectCollection("nckd_entryentity");
        return entryCols.stream()
                .map(entry -> entry.getString("nckd_validation"))
                .toArray(String[]::new);
    }

    /**
     * 针对二级单位特殊处理，获取系统内所有二级组织
     * @return
     */
    private String[] getOrgDropdownOptions() {
        DynamicObjectCollection cols = SWCHelper.getAllSecondOrg();
        String[] orgNames = cols.stream().map(col -> col.getString("name")).toArray(String[]::new);
        return orgNames;
    }


    /**
     * 为Excel工作表的指定列设置数据验证（下拉列表）约束
     *
     * @param sheet Excel工作表对象
     * @param options 下拉列表的选项数组
     * @param columnIndex 要设置下拉列表的列索引（从0开始）
     * @param firstDataRowIndex 数据起始行索引
     * @param isAllowOutRange 是否允许输入列表外的值，true表示允许输入自定义值，false表示只能选择列表项
     */
    private void setDataValidation(SXSSFSheet sheet, String[] options, int columnIndex, int firstDataRowIndex, boolean isAllowOutRange) {
        // 检查选项总长度是否超过Excel的255字符限制
        String optionsString = String.join(",", options);
        if (optionsString.length() > 255) {
            // 如果超过255字符限制，则不创建下拉列表或采用其他方式处理
            logger.warn("Dropdown options exceed 255 characters limit, skipping validation for column: " + columnIndex);
            return;
        }

        // 创建下拉列表约束
        DataValidationHelper validationHelper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(options);

        // 设置应用范围（从第一行数据开始到最后一行）
        CellRangeAddressList addressList = new CellRangeAddressList(
                firstDataRowIndex, 65535, columnIndex, columnIndex);

        // 创建数据验证
        DataValidation validation = validationHelper.createValidation(constraint, addressList);

        // 允许输入列表外的文本
        if(isAllowOutRange) {
            validation.setShowErrorBox(false);  // 不显示错误框
            validation.setEmptyCellAllowed(true); // 允许空值
            validation.setShowPromptBox(true); // 可选：添加输入提示
            validation.createPromptBox("输入提示", "可选择下拉选项或输入自定义值");
        }
        // 添加到工作表
        sheet.addValidationData(validation);
    }

    private CellStyle getFixCellStyle(boolean isRequired, long dataTypeId, CellStyle headDateStyle, CellStyle headTextStyle, CellStyle headAmountStyle, CellStyle headNumStyle,
                                      CellStyle headRequiredDateStyle, CellStyle headRequrriedTextStyle, CellStyle headRequiredAmountStyle, CellStyle headRequriedNumStyle) {
        if(isRequired) {
            if (dataTypeId == DataTypeEnum.AMOUNT.getDbId()) {
                return headRequiredAmountStyle;
            }
            else if(dataTypeId == DataTypeEnum.NUMBERIC.getDbId()) {
                return headRequriedNumStyle;
            }
            else if(dataTypeId == DataTypeEnum.STRING.getDbId()) {
                return headRequrriedTextStyle;
            }
            else if(dataTypeId == DataTypeEnum.DATE.getDbId()) {
                return headRequiredDateStyle;
            }
            else return headRequrriedTextStyle;
        }
        else {
            if (dataTypeId == DataTypeEnum.AMOUNT.getDbId()) {
                return headAmountStyle;
            }
            else if(dataTypeId == DataTypeEnum.NUMBERIC.getDbId()) {
                return headNumStyle;
            }
            else if(dataTypeId == DataTypeEnum.STRING.getDbId()) {
                return headTextStyle;
            }
            else if(dataTypeId == DataTypeEnum.DATE.getDbId()) {
                return headDateStyle;
            }
            else return headTextStyle;
        }
    }

    private void createCell(String comment, SXSSFRow headRow, int cellIndex, String name, boolean isRequried, CellStyle cellStyle, String dataType, SXSSFDrawing drawingPatriarch) {
        SXSSFCell cell = headRow.createCell(cellIndex);
        String value = "";
        if (isRequried) {
            value = "*" + name;
        } else {
            value = name;
        }
        cell.setCellValue(value);
        cell.setCellStyle(cellStyle);
        if (dataType != null) {
            cell.setCellComment(this.getComment(comment, drawingPatriarch, cell));
        }
    }

    private Comment getComment(String commentStr, SXSSFDrawing drawingPatriarch, SXSSFCell cell) {
        Comment comment = drawingPatriarch.createCellComment(new XSSFClientAnchor(cell.getColumnIndex(), cell.getRowIndex(), cell.getColumnIndex() + 1, cell.getRowIndex() + 2, cell.getColumnIndex(), cell.getRowIndex(), cell.getColumnIndex() + 1, cell.getRowIndex() + 2));
        comment.setString(new XSSFRichTextString(commentStr));
        return comment;
    }

    private void batchSetDefaulColumnStyle(SXSSFSheet sheet, List<Integer> indexList, CellStyle cellStyle) {
        Iterator var4 = indexList.iterator();
        while (var4.hasNext()) {
            int index = (Integer) var4.next();
            sheet.setDefaultColumnStyle(index, cellStyle);
        }
    }

    private CellStyle getTextCellStyle(SXSSFSheet sheet) {
        CellStyle textCellStyle = sheet.getWorkbook().createCellStyle();
        textCellStyle.setDataFormat((short) 49);
        return textCellStyle;
    }

    private String getDataType(OutImpPresetItem presetItemEnum) {
        if (presetItemEnum.getDataTypeId() == DataTypeEnum.DATE.getDbId()) {
            return SITShowType.DATE.getCode();
        } else {
            return presetItemEnum.getDataTypeId() == DataTypeEnum.NUMBERIC.getDbId() ? SITShowType.NUM.getCode() : SITShowType.TEXT.getCode();
        }
    }

    private void flushAllAndDownload(SXSSFWorkbook wb, String fileName, IFormView view) {
        String url = SITExportDataHelper.storeFile(wb, fileName);
        if (StringUtils.isNotEmpty(url)) {
            view.download(url);
        }
    }

    public String getImportErrorExcelFile(List<Map<Integer, String>> dataHeadList, List<Map<Integer, String>> dataRowList, List<Map<String, String>> errDataList,
                                          int headIndex, String writeTaskType, Map<Integer, List<Map<String, String>>> columnIndexMap, boolean hasWorkStartDate) {
        logger.info("getImportErrorExcelFile begin");
        String url = "";
        int rowNum = headIndex + dataRowList.size() + 2;

        try {
            SXSSFWorkbook wb = new SXSSFWorkbook(rowNum);

            try {
                String dd = DateTimeUtils.format(new Date(), "yyyyMMdd");
                String exportFileName = MessageFormat.format(ResManager.loadKDString("历史数据迁移任务_数据导入失败结果_{0}", "ImportTaskGuideExportService_3", "swc-hsas-business", new Object[0]), dd);
                String sheetName = ResManager.loadKDString("数据导入模板", "ImportTaskGuideExportService_2", "swc-hsas-business", new Object[0]);
                SXSSFSheet sheet = wb.createSheet(sheetName);
                sheet.setRandomAccessWindowSize(-1);
                SXSSFDrawing drawingPatriarch = sheet.createDrawingPatriarch();
                this.createHeadRow(dataHeadList, headIndex, sheet, drawingPatriarch, writeTaskType);
                this.batchSetDefaulColumnStyle(sheet, this.getTextColumnIndexList(columnIndexMap), this.getTextCellStyle(sheet));
                Set<Integer> allIndexSet = new HashSet(columnIndexMap.size() + 12);
                Iterator var18 = columnIndexMap.keySet().iterator();

                while (true) {
                    int index;
                    if (!var18.hasNext()) {
                        allIndexSet.addAll(Lists.newArrayList(new Integer[]{1, 2, 3, 4}));
                        sheet.setColumnWidth(0, sheet.getColumnWidth(0) * 4);
                        var18 = allIndexSet.iterator();

                        while (var18.hasNext()) {
                            index = (Integer) var18.next();
                            int columnWidth = sheet.getColumnWidth(index);
                            sheet.setColumnWidth(index, columnWidth * 3);
                        }

                        this.addDataRow(dataRowList, errDataList, headIndex, wb, sheet);
                        url = SITExportDataHelper.storeFile(wb, exportFileName);
                        break;
                    }

                    index = (Integer) var18.next();
                    allIndexSet.add(index + 1);
                }
            } catch (Throwable e) {
                throw e;
            }
        } catch (Exception e) {
            logger.error("getImportErrorExcelFile  error : ", e);
            throw new KDException(e, new ErrorCode("getImportErrorExcelFile_error", e.getMessage()), new Object[0]);
        }

        logger.info("getImportErrorExcelFile end");
        return url;
    }

    private void createHeadRow(List<Map<Integer, String>> dataHeadList, int headIndex, SXSSFSheet sheet, SXSSFDrawing drawingPatriarch, String writeTaskType) {
        SXSSFRow headRow = sheet.createRow(headIndex);
        CellStyle headTextStyle = getColumnStyle(true, 0, 0, SITShowType.TEXT.getCode(), HorizontalAlignment.LEFT, sheet.getWorkbook(), HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex(), true);
        SXSSFCell errMsgCell = headRow.createCell(0);
        errMsgCell.setCellValue(ResManager.loadKDString("失败原因", "CalResultCoverImportService_58", "swc-hsas-business", new Object[0]));
        errMsgCell.setCellStyle(headTextStyle);
        Font font = sheet.getWorkbook().createFont();
        CellStyle requrriedStyle = getColumnStyle(true, 0, 0, SITShowType.TEXT.getCode(), HorizontalAlignment.LEFT, sheet.getWorkbook(), HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex(), true);
        font.setColor((short)10);
        requrriedStyle.setFont(font);
        Set<Integer> requireFixColumnSet = ImportTaskUtils.getExportTplRequireColumnSet();
        Map<Integer, String> headMap = (Map)dataHeadList.get(0);
        CellStyle style = null;

        Map.Entry entry;
        for(Iterator var15 = headMap.entrySet().iterator(); var15.hasNext(); this.createCell("", headRow, (Integer)entry.getKey() + 1, (String)entry.getValue(), false, style, (String)null, drawingPatriarch)) {
            entry = (Map.Entry)var15.next();
            if (requireFixColumnSet.contains(entry.getKey())) {
                style = requrriedStyle;
            } else {
                style = headTextStyle;
            }
        }
    }

    private List<Integer> getTextColumnIndexList(Map<Integer, List<Map<String, String>>> columnIndexMap) {
        List<Integer> textIndexList = new ArrayList(columnIndexMap.size() + 12);
        textIndexList.addAll(Lists.newArrayList(new Integer[]{1, 2, 3, 4}));
        Iterator var5 = columnIndexMap.entrySet().iterator();
        while(var5.hasNext()) {
            Map.Entry<Integer, List<Map<String, String>>> entry = (Map.Entry)var5.next();
            List<Map<String, String>> paramMapList = (List)entry.getValue();
            if (!CollectionUtils.isEmpty(paramMapList)) {
                String dataTypeIdStr = (String)((Map)paramMapList.get(0)).get("datatypeid");
                if (!StringUtils.isEmpty(dataTypeIdStr) && Long.parseLong(dataTypeIdStr) == DataTypeEnum.STRING.getDbId()) {
                    textIndexList.add((Integer)entry.getKey() + 1);
                }
            }
        }
        return textIndexList;
    }

    private void addDataRow(List<Map<Integer, String>> dataRowList, List<Map<String, String>> errDataList, int headIndex, SXSSFWorkbook wb, SXSSFSheet sheet) {
        Map<Integer, List<String>> errorMap = this.getErrorMap(errDataList);
        boolean isTplError = "1".equals(((Map)errDataList.get(0)).get("isAll"));
        int tplIndex = headIndex++;
        CellStyle errColumnStyle = getColumnStyle(true, 0, 0, "text", HorizontalAlignment.LEFT, wb, (short)9, true);
        Font font = wb.createFont();
        font.setColor((short)10);
        errColumnStyle.setFont(font);
        CellStyle columnStyle = getColumnStyle(true, 0, 0, "text", HorizontalAlignment.LEFT, wb, (short)9, true);
        List<String> errorList = null;
        Map<Integer, String> rowMap = null;
        SXSSFRow dataRow = null;
        SXSSFCell cell = null;
        int index = 0;

        for(int length = dataRowList.size(); index < length; ++index) {
            errorList = (List)errorMap.get(index);
            if (isTplError) {
                errorList = (List)errorMap.get(tplIndex);
            }

            if (errorList != null) {
                rowMap = (Map)dataRowList.get(index);
                dataRow = sheet.createRow(headIndex);
                ++headIndex;
                cell = dataRow.createCell(0);
                cell.setCellStyle(errColumnStyle);
                cell.setCellValue(this.getErrorMsg(errorList));
                Iterator var18 = rowMap.entrySet().iterator();

                while(var18.hasNext()) {
                    Map.Entry<Integer, String> entry = (Map.Entry)var18.next();
                    cell = dataRow.createCell((Integer)entry.getKey() + 1);
                    cell.setCellStyle(columnStyle);
                    cell.setCellValue((String)entry.getValue());
                }
            }
        }
    }

    private Map<Integer, List<String>> getErrorMap(List<Map<String, String>> errDataList) {
        Map<Integer, List<String>> errorMap = new HashMap(16);
        List<String> tempList = null;
        Iterator var4 = errDataList.iterator();

        while(var4.hasNext()) {
            Map<String, String> tempMap = (Map)var4.next();
            tempList = (List)errorMap.getOrDefault(Integer.valueOf((String)tempMap.get("dataIndex")), new ArrayList());
            tempList.add(tempMap.get("errMsg"));
            errorMap.put(Integer.valueOf((String)tempMap.get("dataIndex")), tempList);
        }

        return errorMap;
    }

    private String getErrorMsg(List<String> errorList) {
        StringBuilder key = new StringBuilder();
        int index = 1;

        for(Iterator var4 = errorList.iterator(); var4.hasNext(); ++index) {
            String msg = (String)var4.next();
            key.append(index).append('.').append(' ').append(msg).append(' ');
        }

        return key.toString();
    }


    public static CellStyle getColumnStyle(boolean isTmpl, int precision, int scale, String columnType, HorizontalAlignment alignment, SXSSFWorkbook wb, short colorIndex, boolean needBoard) {
        CellStyle headColumnStyle = wb.createCellStyle();
        DataFormat dataFormat = wb.createDataFormat();
        short format;
        if (!isTmpl) {
            if (StringUtils.equals(SITShowType.DATE.getCode(), columnType)) {
                format = dataFormat.getFormat("yyyy-MM-dd");
            } else {
                String amountFormat;
                if (StringUtils.equals(SITShowType.AMOUNT.getCode(), columnType)) {
                    amountFormat = getAmountFormat(precision);
                    format = dataFormat.getFormat(amountFormat);
                } else if (StringUtils.equals(SITShowType.NUM.getCode(), columnType)) {
                    amountFormat = getAmountFormat(scale);
                    format = dataFormat.getFormat(amountFormat);
                } else {
                    format = dataFormat.getFormat("text");
                }
            }
        } else if (StringUtils.equals(SITShowType.DATE.getCode(), columnType)) {
            format = dataFormat.getFormat("yyyy-MM-dd");
        } else {
            format = dataFormat.getFormat("text");
        }

        headColumnStyle.setDataFormat(format);
        if (needBoard) {
            headColumnStyle.setBorderBottom(BorderStyle.HAIR);
            headColumnStyle.setBorderTop(BorderStyle.HAIR);
            headColumnStyle.setBorderLeft(BorderStyle.HAIR);
            headColumnStyle.setBorderRight(BorderStyle.HAIR);
        }

        headColumnStyle.setFillForegroundColor(colorIndex);
        headColumnStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headColumnStyle.setAlignment(alignment);
        headColumnStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headColumnStyle.setHidden(true);
        return headColumnStyle;
    }

    private static String getAmountFormat(int precision) {
        StringBuilder format = new StringBuilder("0");
        if (precision > 0) {
            format.append('.');

            for(int i = 0; i < precision; ++i) {
                format.append('0');
            }
        }

        return format.toString();
    }


}