package com.hz.pm.api.performance.util;

/**
 * @author CMM
 * @since 2023/08/17 20:59
 */

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

import cn.hutool.core.collection.CollUtil;
import com.hz.pm.api.performance.constant.ProjectBizConst;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.google.common.collect.Lists;
import com.hz.pm.api.performance.model.entity.ExcelHead;

/**
 * excel 工具类
 * @return
 * @author CMM
 * @since 2023/08/17 21:07
 */
public class ExcelUtils {
    /**
     * 构造excel基础样式
     *
     * @return 样式
     */
    public static WriteCellStyle writeCellStyle() {
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        writeCellStyle.setWrapped(true);
        writeCellStyle.setBorderBottom(BorderStyle.THIN);
        writeCellStyle.setBorderLeft(BorderStyle.THIN);
        writeCellStyle.setBorderRight(BorderStyle.THIN);
        writeCellStyle.setBorderTop(BorderStyle.THIN);
        return writeCellStyle;
    }

    /**
     * 表内容数据转换
     *
     * @param headList 表头数据
     * @param dataList 表内容数据转换
     * @return List<List<Object>>
     */
    public static List<List<Object>> convertData(List<ExcelHead<Object>> headList, List<Map<String, Object>> dataList) {
        List<List<Object>> result = Lists.newArrayList();
        //对 dataList 转为 easyExcel 的数据格式
        for (Map<String, Object> data : dataList) {
            List<Object> row = Lists.newArrayList();
            for (ExcelHead<Object> h : headList) {
                Object o = data.get(h.getFieldName());
                //需要对null的处理，转换为空字符串
                row.add(handler(o, h.getNullValue()));
            }
            result.add(row);
        }
        return result;
    }
    /**
     * null 值处理
     * @param nullValue 默认值
     * @return 默认值
     */
    private static Object handler(Object o, Object nullValue) {
        return o != null ? o : nullValue;
    }

    public static List<List<String>> getHead(List<String> reUserList,List<String> verifyUserList) {

        List<List<String>> list = Lists.newArrayList();
        list.add(Lists.newArrayList(ProjectBizConst.FIRST_INDEX_TITLE));
        list.add(Lists.newArrayList(ProjectBizConst.SECOND_INDEX_TITLE));
        list.add(Lists.newArrayList(ProjectBizConst.THIRD_INDEX_TITLE));
        list.add(Lists.newArrayList(ProjectBizConst.INDEX_SCORE_TITLE));
        list.add(Lists.newArrayList(ProjectBizConst.INDEX_DETAIL_TITLE));
        list.add(Lists.newArrayList(ProjectBizConst.GRADE_DETAIL_TITLE));
        list.add(Lists.newArrayList(ProjectBizConst.SUPPORT_MATERIAL_TITLE));
        list.add(Lists.newArrayList(ProjectBizConst.SELF_APPRAISAL_BASIS_TITLE));
        list.add(Lists.newArrayList(ProjectBizConst.SELF_APPRAISAL_SCORE_TITLE));
        if (CollUtil.isNotEmpty(reUserList)) {
            List<String> reUserHead = Lists.newArrayList();
            reUserHead.add(ProjectBizConst.RE_APPRAISAL_BASIS_TITLE);
            reUserHead.add(ProjectBizConst.RE_APPRAISAL_SCORE_TITLE);
            for (String reUser : reUserList) {
                for (String h : reUserHead) {
                    List<String> reUsers = Lists.newArrayList();
                    reUsers.add(reUser + ProjectBizConst.RE_APPRAISAL);
                    reUsers.add(h);
                    list.add(reUsers);
                }
            }
        }

        if (CollUtil.isNotEmpty(verifyUserList)) {
            List<String> verifyUserHead = Lists.newArrayList();
            verifyUserHead.add(ProjectBizConst.VERIFY_BASIS_TITLE);
            verifyUserHead.add(ProjectBizConst.VERIFY_SCORE_TITLE);
            for (String verifyUser : reUserList) {
                for (String h : verifyUserHead) {
                    List<String> reUsers = Lists.newArrayList();
                    reUsers.add(verifyUser + ProjectBizConst.VERIFY);
                    reUsers.add(h);
                    list.add(reUsers);
                }
            }
        }
        list.add(Lists.newArrayList(ProjectBizConst.IS_ADDITIONAL_TITLE));
        return list;
    }
}