package com.jay.monitor.data.server.utils;

import cn.hutool.core.util.ObjectUtil;
import com.cat.file.message.internal.DefaultMessageTree;
import com.jay.monitor.data.core.model.serializable.base.BaseDTO;
import com.jay.monitor.data.core.model.serializable.base.CommonDataDTO;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Module 工具类
 * @Description 数据处理
 * @Author liukaixiong
 * @Date 2020/10/23 16:28
 */
public class DataUtils {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");

    /**
     * 获取String的值,没有则获取默认值
     *
     * @param value        值
     * @param defaultValue 默认值
     * @return
     */
    public static String getStringValue(String value, String defaultValue) {
        if (StringUtils.isEmpty(value)) {
            return defaultValue;
        }
        return value;
    }

    public static <T> T getDefaultValue(T value, T defaultValue) {
        if (ObjectUtil.isEmpty(value)) {
            return defaultValue;
        }
        return value;
    }

    /**
     * 格式化成特定的日期字符串
     *
     * @param date
     * @return
     */
    public static String parseDayString(Date date) {
        if (date == null) {
            return sdf.format(new Date());
        }
        return sdf.format(date);
    }


    public static Map<String, Object> parseFieldAnnotation(Object obj, Class<? extends Annotation> annotation, String annotationName) {
        Map<String, Object> fieldMap = new LinkedHashMap<>();
        try {
            Field[] declaredFields = obj.getClass().getDeclaredFields();
            for (int i = 0; i < declaredFields.length; i++) {
                Field declaredField = declaredFields[i];
                String name = declaredField.getName();
                Annotation declaredAnnotation = declaredField.getDeclaredAnnotation(annotation);
                Object defaultValue = AnnotationUtils.getValue(declaredAnnotation, annotationName);
                if (defaultValue != null) {
                    String propertyValue = BeanUtils.getProperty(obj, name);
                    fieldMap.put(defaultValue.toString(), propertyValue);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fieldMap;
    }

    public static DefaultMessageTree getJsonContentMessageTree(BaseDTO baseDTO, Integer id) {
        if (baseDTO instanceof CommonDataDTO) {
            CommonDataDTO commonDataDTO = (CommonDataDTO) baseDTO;
            DefaultMessageTree messageTree = new DefaultMessageTree(commonDataDTO.getApplicationName(), commonDataDTO.getIp(), id, commonDataDTO.getCreateDate(), commonDataDTO.getJsonContent());
            return messageTree;
        }
        return null;
    }

    public static DefaultMessageTree getErrorMessageTree(BaseDTO baseDTO, Integer id) {
        if (baseDTO instanceof CommonDataDTO) {
            CommonDataDTO commonDataDTO = (CommonDataDTO) baseDTO;
            DefaultMessageTree messageTree = new DefaultMessageTree(PrefixUtils.prefixErrorMessage(commonDataDTO.getApplicationName()), commonDataDTO.getIp(), id, commonDataDTO.getCreateDate(), commonDataDTO.getErrorStackTrace());
            return messageTree;
        }
        return null;
    }

}
