package com.tuoheng.admin.utils.excel;

import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.springframework.util.CollectionUtils;

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

/**
 * @Title:
 * @description: 自动列宽
 * @version: 1.0
 **/
public class CustomCellWriteWidthHandler extends AbstractColumnWidthStyleStrategy {
	// 增加缓存 避免重复计算，影响性能
    private Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<>();

    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    	// 头部或者是数据行时
        boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
        if (needSetWidth) {
        	// 先尝试从缓存中获取
            Map<Integer, Integer> maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo());
            if (maxColumnWidthMap == null) {
                maxColumnWidthMap = new HashMap<>();
                CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
            }
            Integer columnWidth = 20;
            Integer columnIndex = cell.getColumnIndex();
            if (0 == columnIndex) {
                columnWidth = 15;
            } else if (2 == columnIndex || 3 == columnIndex) {
                columnWidth = 60;
            } else if (4 == columnIndex || 5 == columnIndex || 6 == columnIndex) {
                columnWidth = 35;
            }
            maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
            writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
        }
    }

    private Integer dataLength(List<WriteCellData<?>> cellDataList, Cell cell, Boolean isHead) {
        if (isHead) {
//            return cell.getStringCellValue().getBytes().length;
            return 20;
        } else {
            CellData cellData = cellDataList.get(0);
            CellDataTypeEnum type = cellData.getType();
            if (type == null) {
                return -1;
            } else {
            	// 根据数据类型，定义不同的单元格宽度
                switch (type) {
                    case STRING:
//                        return cellData.getStringValue().getBytes().length;
                        return 20;
                    case BOOLEAN:
//                        return cellData.getBooleanValue().toString().getBytes().length;
                        return 20;
                    case NUMBER:
//                        return cellData.getNumberValue().toString().getBytes().length;
                        return 20;
                    default:
                        return -1;
                }
            }
        }
    }
}