package com.house365.test;

import com.alibaba.druid.support.json.JSONParser;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.dubbo.common.json.JSON;
import com.alibaba.dubbo.common.json.JSONObject;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.house365.web.interceptor.UrlParamAuthInterceptor;
import org.apache.poi.util.SystemOutLogger;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CharmyinTest {
    int i;

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    public static void main(String[] args) throws ParseException {
        // TODO Auto-generated method stub
        //        Random r = new Random();
        //        Float  flost = r.nextFloat();
        //        System.out.println(flost%0.42);
        //
        //
        //
        //        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //        String date = sdf.format(new Date())+"-01";
        //        sdf = new SimpleDateFormat("yyyy-MM-dd");
        //        Date newDate = sdf.parse(date);
        //        System.out.println(newDate);
        //
        //        CharmyinTest test =  new CharmyinTest();
        //        System.out.println(test.i+1);
        //
        //
        //        Object aaa = null;
        //        System.out.println((String)aaa);

        //时间间隔
        //        Calendar fromCalendar = Calendar.getInstance();
        //        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        //        fromCalendar.set(Calendar.MINUTE, 0);
        //        fromCalendar.set(Calendar.SECOND, 0);
        //        fromCalendar.set(Calendar.MILLISECOND, 0);
        //        fromCalendar.set(Calendar.MONTH, Calendar.DECEMBER);
        //        fromCalendar.set(Calendar.DAY_OF_MONTH, 31);
        //        fromCalendar.set(Calendar.YEAR, 2015);
        //
        //        Calendar toCalendar = Calendar.getInstance();
        //        toCalendar.setTime(new Date());
        //        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        //        toCalendar.set(Calendar.MINUTE, 0);
        //        toCalendar.set(Calendar.SECOND, 0);
        //        toCalendar.set(Calendar.MILLISECOND, 0);
        //
        //        //和最后一次签到间隔天数
        //        long day = (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24);
        //        System.out.printf("day :" + day);

        //        Calendar fromCalendar = Calendar.getInstance();
        //        fromCalendar.set(Calendar.DAY_OF_WEEK,2);
        //        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        //        fromCalendar.set(Calendar.MINUTE, 0);
        //        fromCalendar.set(Calendar.SECOND, 0);
        //        fromCalendar.set(Calendar.MILLISECOND, 0);
        //        System.out.printf("day :" + fromCalendar.getTime());

        //CRM 编码
        //        try {
        //            String name = java.net.URLEncoder.encode("测试", "GBK");
        //            System.out.println(name);
        //            System.out.println(java.net.URLDecoder.decode(name, "GBK"));
        //        } catch (UnsupportedEncodingException e) {
        //            e.printStackTrace();
        //        }
        //CRM 编码


        //        Calendar calendar = Calendar.getInstance();
        //        calendar.add(Calendar.MONTH, -24);
        //        Date date = calendar.getTime();
        //        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        //        System.out.println(sdf.format(date));

        // 签名测试
        //        UrlParamAuthInterceptor ok = new UrlParamAuthInterceptor();
        //        String str = "serviceCode=Cm&serviceName=CommentHouse&houseId=10&userId=1581005&criticContent=woaipu.com阿斯顿发斯zxxww.com芬蛤蟆大是大非&criticPictures=111111111,2222222222&priceScore=3&placeScore=4&trafficScore=5&infrastructureScore=3&environmentScore=4.5&isAnonymous=1&cityKey=nj&source=1&useridType=1&userCityKey=nj&userName=xxx&channel=1";
        //        String std = null;
        //        try {
        //            std = ok.encrypt(str, true);
        //        } catch (UnsupportedEncodingException e) {
        //            e.printStackTrace();
        //        }
        //        System.out.println(std);


        //        List<Map<String, String>> list = new ArrayList<>();
        //        for (int i = 1; i < 3; i++) {
        //            Map<String, String> map = new HashMap<>();
        //            map.put("value", "v" + i);
        //            map.put("name", "n" + i);
        //            list.add(map);
        //        }
        //
        //        try {
        //            String json = JSON.json(list);
        //            System.out.print("json ===" + json);
        //        } catch (IOException e) {
        //            e.printStackTrace();
        //        }
        //
        //        List<String> cityList = new ArrayList<>();
        //        cityList.add("123");
        //        cityList.add("234");
        //        cityList.add("345");
        //
        //        try {
        //            String json = JSON.json(cityList);
        //            System.out.print("json ===" + json);
        //        } catch (IOException e) {
        //            e.printStackTrace();
        //        }

        //        System.out.println(24*60*60);
        //        System.out.println(TimeUnit.DAYS.toSeconds(1));

        //        String url = "123456";
        //        System.out.println(url.indexOf("/")+"--"+url.substring(url.lastIndexOf("/") + 1));

        //        long beginTime = System.currentTimeMillis();
        //        try {
        //            Thread.sleep(3000);
        //        } catch (InterruptedException e) {
        //            e.printStackTrace();
        //        }
        //        System.out.println(System.currentTimeMillis() - beginTime);


        //        Calendar fromCalendar = Calendar.getInstance();
        //        fromCalendar.add(Calendar.MINUTE, 50);
        //        Date date = new Date();
        //        long remainHours = (fromCalendar.getTime().getTime() - date.getTime()) / 1000 / 60 / 60;
        //        int remainMinutes = Integer.valueOf(
        //            "" + ((fromCalendar.getTime().getTime() - date.getTime()) / 1000 / 60 % 60));
        //        System.out.println("remainHours is :" + remainHours + ",remainMinutes is :" + remainMinutes);
        //        System.out.println("result is :" + (remainHours == 0 ? (remainMinutes > 0 ? 1 : remainHours) : remainHours));

        //        String num = "1.24";
        //        BigDecimal bd = new BigDecimal(num);
        //        String str1 = String.valueOf(bd.setScale(1, BigDecimal.ROUND_UP));
        //        String str2 = String.valueOf(bd.setScale(1, BigDecimal.ROUND_HALF_UP));
        //
        //        System.out.println("str1:"+str1+",str2:"+str2);

        //        List<Integer> list = new ArrayList<>();
        //        for(int i=0;i<1001;i++){
        //            list.add(i);
        //        }
        //        List<List<Integer>> parts = Lists.partition(list, 1000);
        //        for(List<Integer> part:parts){
        //            System.out.println(Joiner.on(",").join(part));
        //        }

        //        String regex = "\\[{1}(.+)\\]{1}";
        //        String str = "短信中包含1234567890122以下敏感字：[fuck,shit,bitch]并且号码中包含以下黑名单：张三-13701234567，李四-18701234567";
        //        System.out.print("敏感字如下：");
        //        System.out.println(getKeyWords(regex, str));
        //        System.out.print("黑名单号码如下：");
        //        System.out.println(getMobiles(str));
        //        System.out.println("contain:"+containMobiles(str));
        //
        //        System.out.println("字符替换：" + replaceStr("我喜欢红色"));
        //
        //        String testMobile = "test1377676566Tee";
        //        Pattern pattern = Pattern.compile("^((13[0-9])|(14[5,7])|(15[^4,\\D])|(17[6-8])|(18[0-9]))\\d{8}$");
        //        Matcher matcher = pattern.matcher(testMobile);
        //        System.out.println(getMobiles(testMobile));
        //        while (matcher.find()) {
        //            System.out.println(matcher.group(0));
        //        }

        //        int times = 3400;
        //        System.out.println(times/1000);

        // 参数加密
        //        UrlParamAuthInterceptor ok = new UrlParamAuthInterceptor();
        //        String str = "serviceName=HouseCommentReplyPraise&serviceCode=Cm&praise=1&commentId=4862&useridType=1&api_key=android&v=6.3.0&version=6.3.0&city=wh&client=tf&signTime=1477615742795&sign=ECD476BDABDABAB5F414D665ACDF3E1F";
        //        String std = null;
        //        try {
        //            std = ok.encrypt(str, true);
        //        } catch (UnsupportedEncodingException e) {
        //            e.printStackTrace();
        //        }
        //        System.out.println(std);

        // lottery rate
        for(int i=0;i<20;i++) {
            Random random = new Random();
            Float currentRandom = random.nextFloat();
            Float lastRandom = currentRandom * 100 % 3.7f;
            System.out.println("currentRandom :" + currentRandom + ", lastRandom:" + lastRandom);
        }
    }

    //提取 中括号中关键字
    public static String getKeyWords(String regex, String str) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        if (m.find()) {
            return m.group(1);
        }
        return null;
    }

    public static boolean containMobiles(String str) {
        boolean flag = false;
        Pattern p = Pattern.compile("(\\+86|)?(\\d{11})");
        Matcher m = p.matcher(str);
        while (m.find()) {
            flag = true;
            break;
        }
        return flag;
    }

    //提取字符串中的手机号码
    public static String getMobiles(String str) {
        Pattern p = Pattern.compile("(\\+86|)?(\\d{11})");
        Matcher m = p.matcher(str);
        StringBuilder sb = new StringBuilder();
        while (m.find()) {
            if (sb.length() > 0)
                sb.append(",");
            sb.append(m.group(2));
        }
        /*
        * 不加"()"也能将手机号码输出 添加"()"是为了筛选数据添加上去的，
        * 第一对"()"是为了获取字符串"+86"，代码是System.out.println(m.group(1));，
        * 第二对"()"是获取11位纯数字电话号码， 本次的输出的手机号码中包含了"+86"，如果只要11位数字号码，
        * 可将代码改为System.out.println(m.group(2));
        */
        //System.out.println(m.groupCount());// 该行代码是输出有几对"()"，即捕获组个数，本次输出结果是2，因为有两对"()"
        return sb.toString();
    }

    //替换字符
    public static String replaceStr(String str) {

        String regex = "红";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);

        return m.replaceAll("绿");
    }

}
