package cn.report.excel.easyexcel.custom;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import cn.report.excel.easyexcel.api.metadata.Head;
import cn.report.excel.easyexcel.api.write.merge.AbstractMergeStrategy;
import cn.report.excel.easyexcel.custom.base.RowRange;

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

/**
 *      String fileName ="D:\\data\\EasyTest.xlsx";
		ExcelWriter excelWriter = null;
	
		// 内容样式
		WriteCellStyle contentWriteCellStyle = EasyExcelUtil.getContentStyle();
		HorizontalCellStyleStrategy horizontalCellStyleStrategy =
				new HorizontalCellStyleStrategy(EasyExcelUtil.getHeadStyle(), contentWriteCellStyle);
		
		List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();
		Map<String,Object> cellMap =  new HashMap<String,Object>();
		cellMap.put("startRow", 3+"");
		cellMap.put("startCol", 0);
		listMap.add(cellMap);
		cellMap =  new HashMap<String,Object>();
		cellMap.put("startRow", 0+"");
		cellMap.put("startCol", 1);
		listMap.add(cellMap);
		
		Map<String, List<RowRangeDto>> strategyMap = 
				EasyExcelUtil.addMerStrategy(EasyExcelUtil.content(dataM()),
						listMap,2,dataM().size()
				);
		
	    excelWriter = EasyExcel.write(fileName)
				//定制excel显示风格
				.registerWriteHandler(new SamCellWidthWriteHandler())
				.head(EasyExcelUtil.head(header))
				// 第一行大标题样式设置
				.registerWriteHandler(new SamTitleWriteHandler(bigtitle1,header ))
				//设置默认样式及写入头信息开始的行数
				//.useDefaultStyle(true)
				.relativeHeadRowIndex(1)
				// 表头、内容样式设置
				.registerWriteHandler(horizontalCellStyleStrategy)
			    .registerWriteHandler(new SamCellMergeStrategy(strategyMap,3))
				.build();
    	
        WriteSheet writeSheet = EasyExcel.writerSheet(0,"模板").build();
		excelWriter.write(EasyExcelUtil.content(dataM()),writeSheet);
 */
public class SamCellMergeStrategy extends AbstractMergeStrategy 
{
    private Map<String, List<RowRange>> strategyMap;
    private int startRow = 1; 
    // private Sheet sheet;
    
    public SamCellMergeStrategy(Map<String, List<RowRange>> strategyMap) {
        this.strategyMap = strategyMap;
    }
    //startRow单元格合并起始行
    public SamCellMergeStrategy(Map<String, List<RowRange>> strategyMap,int startRow) {
        this.strategyMap = strategyMap;
        this.startRow = startRow;
    }
 
   /** 每隔2行会合并 把eachColumn 设置成 3 也就是数据的长度，所以就第一列会合并
   *  LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0,4);
   */
    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer integer)
    {
    	//this.sheet = sheet;
        //单元格合并起始行即数据行开始行。主要看标题行数。
        if (cell.getRowIndex() == startRow && cell.getColumnIndex() == 0) {
            /**
             * 保证每个cell被合并一次，如果不加上面的判断，因为是一个cell一个cell操作的，
             * 例如合并A2:A3,当cell为A2时，合并A2,A3，但是当cell为A3时，又是合并A2,A3，
             * 但此时A2,A3已经是合并的单元格了
             */
            for (Map.Entry<String, List<RowRange>> entry : strategyMap.entrySet()) {
                Integer columnIndex = Integer.valueOf(entry.getKey());
                entry.getValue().forEach(rowRange -> {
                    //添加一个合并请求
                    sheet.addMergedRegionUnsafe(new CellRangeAddress(rowRange.getStart(),
                            rowRange.getEnd(), columnIndex, columnIndex));
                });
            }
        }
    }
}