package cn.iocoder.yudao.module.member.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.util.ObjectUtils;

import java.util.*;

public class CustomMergeStrategy {

    /**
     * 分组，每几行合并一次
     */
    private final List<List<Integer>> mergeColDataGroupCountList;

    /**
     * 目标合并列index
     */
    private final Map<Integer, Integer> targetColumnIndex;
    /**
     * 需要开始合并单元格的首行index
     */
    private Integer rowIndex;
    //存放合并的
    private Map<Integer,List<Integer>> aaa=new HashMap<>();

    /**
     * mergeColDataList为待合并目标列的值
     */
    public CustomMergeStrategy(List<List<String>> mergeColDataList, Map<Integer, Integer> targetColumnIndex) {
        this.mergeColDataGroupCountList = getGroupCountList(mergeColDataList);
        this.targetColumnIndex = targetColumnIndex;
    }

    public void merge(Sheet sheet,int i) {
        rowIndex = 1; // 假设从第二行开始合并，第一行为表头
        // 遍历所有行
        Iterator<Row> rowIterator = sheet.rowIterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            int i1=0;
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                if(i1>i){
                    return;
                }
                Cell cell = cellIterator.next();
                if (null == rowIndex) {
                    rowIndex = cell.getRowIndex();
                }
                // 仅从首行以及目标列的单元格开始合并，忽略其他
                if (targetColumnIndex.get(cell.getColumnIndex())!=null) {
                    //找到对应的需要合并的列
                    mergeGroupColumn(sheet, targetColumnIndex.get(cell.getColumnIndex()),cell.getColumnIndex());
                }
                i1++;
            }
        }
    }

    private void mergeGroupColumn(Sheet sheet, Integer index,Integer i) {
        int rowCount = rowIndex;
        for (Integer count : mergeColDataGroupCountList.get(index)) {
            if (count == 1) {
                rowCount += count;
                continue;
            }
            // 合并单元格
            CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCount, rowCount + count - 1,
                    i, i);
                sheet.addMergedRegion(cellRangeAddress);

            rowCount += count;
        }
    }

    /**
     * 该方法将目标列根据值是否相同连续可合并，存储可合并的行数
     */
    private List<List<Integer>> getGroupCountList(List<List<String>> exportDataList) {
        if (ObjectUtils.isEmpty(exportDataList)) {
            return new ArrayList<>();
        }
        List<List<Integer>> groupCountListList = new ArrayList<>();
        for (List<String> dataList : exportDataList) {
            List<Integer> groupCountList = new ArrayList<>();
            int count = 1;
            for (int i = 1; i < dataList.size(); i++) {
                if (dataList.get(i).equals(dataList.get(i - 1))) {
                    count++;
                } else {
                    groupCountList.add(count);
                    count = 1;
                }
            }
            // 处理完最后一条后
            groupCountList.add(count);
            groupCountListList.add(groupCountList);
        }
        return groupCountListList;
    }
}
