package com.sifang.modules.sunshine.SystemSetting.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.lang3.RandomStringUtils;

import com.jfinal.kit.HashKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class RandomUtil {

    public static String getPositiveRandomWithRang(int max, int min) {
        String r = "1234";
        if (max > 0 && min > 0 && max >= min) {
            Random random = new Random();
            int s = random.nextInt(max) % (max - min + 1) + min;
            r = String.valueOf(s);
        }
        return r;
    }

    public static String getPositiveRandomWithRang(int count) {
        StringBuffer sb = new StringBuffer();
        String str = "0123456789";
        Random r = new Random();
        for (int i = 0; i < count; i++) {
            int num = r.nextInt(str.length());
            sb.append(str.charAt(num));
            str = str.replace((str.charAt(num) + ""), "");
        }
        return sb.toString();
    }

    /**
     * 获取一定范围内的值
     *
     * @param start 起始值
     * @param end   结束值
     * @return
     */
    public static int getRandomMoney(int start, int end) {
        Random rand = new Random();
        int randNum = rand.nextInt(end - 1) + start;
        return randNum;
    }

    public static String randomOrderNumber() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
        String s = RandomStringUtils.randomNumeric(5);
        return sdf.format(new Date()) + s;
    }
    
    public static String SiteGoodsProfitNumber() {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
    	String s = RandomStringUtils.randomNumeric(5);
    	return sdf.format(new Date()) + s;
    }

	/**
	 * 生成安全的密码，生成随机的16位salt并经过1024次 sha-1 hash
	 */
	public static String entryptPassword(String plainPassword) {
		return HashKit.md5(plainPassword);
	}

	// 获取服务商结账编号，结账编号：年月日时分秒+随机四位（编号不可重复）,例如1904101224257567
	public static String getBillingNumber() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strdate = format.format(new Date());
		String s = RandomStringUtils.randomNumeric(4);
		String str = strdate.substring(2, strdate.length());
		return str.replace(" ", "").replace("-", "").replace(":", "") + s;
	}


}