package com.njcn.device.pms.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.njcn.system.pojo.po.DictData;
import lombok.SneakyThrows;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wr
 * @description
 * @date 2023/11/23 19:48
 */
public class PubUtil {

    /**
     * 导入模拟判断是否为空
     * @param object
     * @return
     */
    @SneakyThrows
    public static String isObjNull(Object object) {
        Field[] fields = object.getClass().getSuperclass().getDeclaredFields();
        StringBuilder builder = new StringBuilder();
        for (Field field : fields) {
            //设置可访问
            field.setAccessible(true);
            //属性的值
            Object fieldValue = field.get(object);
            //是否包含必填校验注解
            boolean isExcelValid = field.isAnnotationPresent(ExcelValid.class);
            if (isExcelValid && Objects.isNull(fieldValue)) {
                builder.append(field.getAnnotation(ExcelValid.class).message() + "; ");
            }
        }
        return builder.toString();
    }

    public static String subString(String name){
        String replace ;
        if(name.equals("古冶")){
            replace = name
                    .replace("古冶","古冶供电中心");
        }else{
            replace = name.replace("国网冀北", "")
                    .replace("唐山市","")
                    .replace("双桥","双桥区")
                    .replace("张家口","国网张家口")
                    .replace("唐山","国网唐山")
                    .replace("国网昌黎县供电公司","昌黎县供电公司")
                    .replace("双滦客服中心","双滦区供电中心")
                    .replace("营子客服中心","营子区供电中心")
                    .replace("安次客户服务分中心","安次供电中心")
                    .replace("广阳客户服务分中心","广阳供电中心")
                    .replace("国网卢龙县供电公司","卢龙县供电公司")
                    .replace("国网青龙县供电公司","青龙县供电公司")
                    .replace("广阳客户服务分中心","广阳供电中心")
                    .replace("国网秦皇岛市抚宁区供电公司","抚宁县供电公司")
                    .replace("滦县供电公司","滦南县供电公司")
                    .replace("下花园客户服务分中心","下花园区供电中心")
                    .replace("宣化客户服务分中心","宣化区供电中心")
                    .replace("客户服务中心","供电公司")
                    .replace("有限","")
                    .replace("分","");
        }
        return replace;
    }

    public static String getDicById(String name, List<DictData> dictData) {
        if(StrUtil.isNotBlank(name)){
            //先匹配有没有完全吻合的
            List<DictData> compareDictDataList = dictData.stream().filter(x -> x.getName().equalsIgnoreCase(name)).collect(Collectors.toList());
            if(CollUtil.isNotEmpty(compareDictDataList)){
                return compareDictDataList.get(0).getId();
            }
            //如果没有匹配到，则判断是否有包含的
            List<DictData> dictDataList = dictData.stream().filter(x -> x.getName().contains(name)).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(dictDataList)) {
                return dictDataList.get(0).getId();
            }
        }
        //实在没有匹配到则返回空
        return "";
    }

    public static void exportExcel(String fileName, List<?> list,Class<?> t, HttpServletResponse response) {
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            fileName = URLEncoder.encode(fileName, CharsetUtil.UTF_8);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/octet-stream;charset=UTF-8");
            EasyExcel.write(outputStream,t).sheet("sheet").doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
