package com.czg.handel;

import cn.hutool.core.util.ArrayUtil;
import cn.idev.excel.metadata.Head;
import cn.idev.excel.metadata.data.WriteCellData;
import cn.idev.excel.write.handler.CellWriteHandler;
import cn.idev.excel.write.metadata.holder.WriteSheetHolder;
import cn.idev.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;

import java.util.List;

/**
 * 台桌统计退单单元格处理器
 *
 * @author tankaikai
 * @since 2025-04-02 09:50
 */
public class TableRefundCellHandel implements CellWriteHandler {
    // 要处理的列索引数组
    private int[] yellowColumnIndex = {11, 12};

    public TableRefundCellHandel() {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        // 当前行索引
        int curRowIndex = cell.getRowIndex();
        // 当前列索引
        int curColIndex = cell.getColumnIndex();
        if (curRowIndex == 0) {
            return;
        }
        // 如果是指定要处理的列，则要进行加黄操作
        if (ArrayUtil.contains(yellowColumnIndex, curColIndex)) {
            Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
            if (preCell.getCellType() == CellType.NUMERIC) {
                if (preCell.getNumericCellValue() < 0) {
                    //System.out.println("设置黄色背景：" + curRowIndex + "行，" + curColIndex + "列" + preCell.getNumericCellValue());
                    CellStyle style = preCell.getSheet().getWorkbook().createCellStyle();
                    style.cloneStyleFrom(preCell.getCellStyle());
                    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
                    // 设置水平居中
                    style.setAlignment(HorizontalAlignment.CENTER);
                    // 设置垂直居中
                    style.setVerticalAlignment(VerticalAlignment.CENTER);
                    preCell.setCellStyle(style);
                }
            }
        }
    }
}
