package jnpf.attendance.excel;

import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;

/**
 * 组合写入处理器
 * 用于同时应用多个Handler（样式Handler + 合并Handler）
 *
 * @author Generated
 * @date 2026-04-29
 */
@Slf4j
public class CompositeWriteHandler implements RowWriteHandler, CellWriteHandler {

    private final RowWriteHandler rowHandler;
    private final CellWriteHandler cellHandler;

    public CompositeWriteHandler(RowWriteHandler rowHandler, CellWriteHandler cellHandler) {
        this.rowHandler = rowHandler;
        this.cellHandler = cellHandler;
    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        // 执行样式Handler
        if (rowHandler != null) {
            try {
                rowHandler.afterRowDispose(writeSheetHolder, writeTableHolder, row, relativeRowIndex, isHead);
            } catch (Exception e) {
                log.error("RowHandler执行失败: {}", e.getMessage(), e);
            }
        }
    }

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        // 执行单元格Handler（包含合并逻辑）
        if (cellHandler != null) {
            try {
                cellHandler.afterCellDispose(context);
            } catch (Exception e) {
                log.error("CellHandler执行失败: {}", e.getMessage(), e);
            }
        }
    }
}
