package com.ads.common.util;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author yuqiang
 * @Description 通用方法（优先使用hutool中的方法）
 * @Date 2020/3/18 16:13
 */
@Slf4j
public class Util {

    /**
     * @description 获取UUID
     * @author yuqiang
     * @date 2020/4/9 16:16
     * @return java.lang.String
     **/
    public static String generateUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
    }

    /**
     * @description 获取异常详细信息
     * @author yuqiang
     * @date 2020/4/10 9:23
     * @param ex:
     * @return java.lang.String
     **/
    public static String getExceptionInfo(Exception ex) {
        String sOut = "";
        sOut += ex.getMessage() + "\r\n";
        StackTraceElement[] trace = ex.getStackTrace();
        for (StackTraceElement s : trace) {
            sOut += "\tat " + s + "\r\n";
        }
        return sOut;
    }

    /**
     * @description 获取对象的自定义注解Field
     * @author yuqiang
     * @date 2020/5/18 13:55
     * @param clazz:
     * @return T
     **/
    public static <T> T initFieldName(Class<T> clazz) {
        Field[] sourceFields = clazz.getDeclaredFields();
        try {
            T newClazz = clazz.newInstance();
            Field[] targetFields = newClazz.getClass().getDeclaredFields();
            Map<String, String> sourceMap = new HashMap<>();
            for (Field sourceField : sourceFields) {
                if (sourceField.isAnnotationPresent(com.ads.common.annotation.Field.class)) {
                    com.ads.common.annotation.Field field = sourceField.getAnnotation(com.ads.common.annotation.Field.class);
                    String name = field.name();
                    sourceMap.put(sourceField.getName(), name);
                }
            }
            for (Field targetField : targetFields) {
                targetField.setAccessible(true);
                targetField.set(newClazz, sourceMap.get(targetField.getName()));
            }
            return newClazz;
        } catch (Exception e) {
            log.error("反射操作异常---", e);
        }
        return null;
    }

    /**
     * @description 获取用户初始密码
     * @author yuqiang
     * @date 2020/5/18 13:55
     * @return java.lang.String
     **/
    public static String getUserPassword() {
        StringBuilder builder = new StringBuilder();
        int numberCount = 0;
        int lowerCount = 0;
        int upperCount = 0;
        for (int i=0 ;i< 8 ;i ++) {
            if (i == 6 || i == 7) {
                // 第7位
                if (numberCount == 0) {
                    builder.append(RandomUtil.randomNumbers(1));
                    numberCount ++;
                    continue;
                }
                if (lowerCount == 0) {
                    builder.append(RandomUtil.randomChar(RandomUtil.BASE_CHAR));
                    lowerCount ++;
                    continue;
                }
                if (upperCount == 0) {
                    builder.append(StrUtil.toString(RandomUtil.randomChar(RandomUtil.BASE_CHAR)).toUpperCase());
                    upperCount ++;
                    continue;
                }
            }
            int index = Integer.valueOf(RandomUtil.randomNumbers(1));
            if (index >= 7) {
                builder.append(StrUtil.toString(RandomUtil.randomChar(RandomUtil.BASE_CHAR)).toUpperCase());
                upperCount ++;
                continue;
            }
            if (index >= 3) {
                builder.append(RandomUtil.randomNumbers(1));
                numberCount ++;
                continue;
            }
            builder.append(RandomUtil.randomChar(RandomUtil.BASE_CHAR));
            lowerCount ++;
        }
        return builder.toString();
    }

    /**
     * @description 字符串加1
     * @author yuqiang
     * @date 2020/6/22 15:10
     * @param str:
     * @return java.lang.String
     **/
    public static String strAddOne(String str) {
        int length = str.length();
        int value = Integer.valueOf(str);
        value = value + 1;
        return String.format("%04d", value);
    }

    public static void main(String[] args) {
        System.out.println(strAddOne("0009"));
    }


}
