package cn.superdesk.uniorder.common.excel.biz;

import cn.hutool.json.JSONUtil;
import com.alibaba.excel.write.handler.RowWriteHandler;
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;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
public class CommentWriteHandler implements RowWriteHandler {

    private List<Integer> hidderRows;

    public CommentWriteHandler(List<Integer> hidderRows) {
        this.hidderRows = hidderRows;
    }
    @Override
    public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer integer, Integer integer1, Boolean aBoolean) {
    }

    @Override
    public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean aBoolean) {
        log.info("row-index1:{}", JSONUtil.toJsonStr(row));
        if (!CollectionUtils.isEmpty(hidderRows)) {
            // 删除列
            writeSheetHolder.getSheet().removeRow(row);
        }
    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean aBoolean) {
        log.info("row-index2:{}", JSONUtil.toJsonStr(row));
        if (!CollectionUtils.isEmpty(hidderRows) && hidderRows.contains(row.getRowNum())) {
            // 删除列
            writeSheetHolder.getSheet().removeRow(row);
        }
    }
}
