package com.hc.core.utils.tool;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

/**
 * 说明：常用工具
 */
public class ResultUtils {
    private static SecureRandom random = new SecureRandom();
    private static final String DATE_FORMAT = "yyyyMMddHHmmssSSS";
    private static final int LENGTH = 20;
    private static final int RANDOM_START = 100000; // 随机数字起始值
    /**
     * 生成25位的全球唯一UUID
     *
     * @return UUID
     */
    public static String getUUID() {
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String localDate = LocalDateTime.now().format(ofPattern);
        //随机数
        String randomNumeric = RandomStringUtils.randomNumeric(8);
        return localDate + randomNumeric;
    }
    public static String getDocNo() {
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String localDate = LocalDateTime.now().format(ofPattern);
        return localDate;
    }

    public static String generateUniqueNumber() {
        String timePart = new SimpleDateFormat(DATE_FORMAT).format(new Date());
        Random random = new Random();
        int randomPart = random.nextInt(900000) + RANDOM_START; // 生成5位数随机数
        String uniqueNumber = timePart + randomPart;
        return uniqueNumber;
    }

    /**
     * 随机生成六位数验证码
     *
     * @return
     */
    public static int getRandomNum6() {
        Random r = new Random();
        return r.nextInt(900000) + 100000;//(Math.random()*(999999-100000)+100000)
    }

    /**
     * 随机生成四位数验证码
     *
     * @return
     */
    public static int getRandomNum4() {
        Random r = new Random();
        return r.nextInt(9000) + 1000;
    }



    /**
     * 对url字符串进行编码.
     *
     * @param url 要编码的url字符串
     * @return 编码后的字符串
     */
    public static String urlEncoder(String url) {
        if (StringUtils.isEmpty(url)) {
            return null;
        }
        try {
            return java.net.URLEncoder.encode(url, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 对url字符串进行解码.
     *
     * @param url 要解码的url字符串
     * @return 解码后的字符串
     */
    public static String urlDecoder(String url) {
        if (StringUtils.isEmpty(url)) {
            return null;
        }
        try {
            return URLDecoder.decode(url, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 生成随机密码
     *
     * @param pwd_len 生成的密码的总长度
     * @return 密码的字符串
     */
    public static String genRandomNum(int pwd_len) {
        // 35是因为数组是从0开始的，26个字母+10个数字
        final int maxNum = 36;
        int i; // 生成的随机数
        int count = 0; // 生成的密码的长度
        char[] str = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
                'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
                'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while (count < pwd_len) {
            // 生成随机数，取绝对值，防止生成负数，
            i = Math.abs(r.nextInt(maxNum)); // 生成的数最大为36-1
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count++;
            }
        }
        return pwd.toString();
    }


    public static String getOrderStatusName(Integer orderStatus)
    {
        if(orderStatus==null)
        {
            return "";
        }
        switch (orderStatus)
        {
            case 0:return "待支付";
            case 1:return "支付成功";
            case 2:return "退费";
            case 3:return "部分退费";
            default:return "";
        }
    }

    public static String getTypeName(Integer type)
    {
        if(type==null)
        {
            return "";
        }
        switch (type)
        {
            case 0:return "支付宝";
            case 1:return "微信";
            case 2:return "微信移动端";
            case 3:return "对公汇款";
            default:return "";
        }
    }

    //-1不开只记录， 0申请中 10申请成功，20开票完成，30开票失败，40开票成功签章失败， 50作废申请，60未作废 70作废中 80作废失败 90作废成功
    public static String getSyncStatusName(Integer syncStatus)
    {
        if(syncStatus==null)
        {
            return "";
        }
        switch (syncStatus)
        {
            case -1:return "不开只记录";
            case 0:return "申请中";
            case 10:return "申请成功";
            case 20:return "开票完成";
            case 30:return "开票失败";
            case 40:return "开票成功签章失败";
            case 50:return "作废申请";
            case 60:return "未作废";
            case 70:return "作废中";
            case 80:return "作废失败";
            case 90:return "作废成功";
            default:return "";
        }
    }
    //状态0123审核中，拒绝，通过，处理完毕
    public static String getCheckStatusName(Integer status)
    {
        if(status==null)
        {
            return "";
        }
        switch (status)
        {
            case 0:return "审核中";
            case 1:return "拒绝";
            case 2:return "通过";
            case 3:return "处理完毕";
            default:return "";
        }
    }

    public static boolean isNullOrEmpty(String str)
    {
        if (str == null || str.equals("")) {
            // 字符串是null或者空字符串
            return true;
        }
        return false;
    }
}
