package com.ag.util;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/5/22 0022.
 */
public class StrUtil extends StringUtils {
    /**
     * 去空格
     *
     * @param str
     * @return
     */
    public static String trimString(String str) {
        return str == null ? "" : str.trim();
    }

    /**
     * 去除左边从0开始到第一个匹配字符
     *
     * @param str
     *            ="1,2,3,4,"
     * @param leftFlag
     *            =","
     * @return "2,3,4,"
     */
    public static String subLeftString(String str, String leftFlag) {
        if (str == null || str.equals("") || !str.contains(leftFlag)) {
            return "";
        }
        return str.substring(str.indexOf(leftFlag) + 1, str.length());
    }

    /**
     * 去除右边第一个匹配字符开始到末尾
     *
     * @param str
     *            ="1,2,3,4,"
     * @param rightFlag
     *            =","
     * @return "1,2,3,4"
     */
    public static String subRightString(String str, String rightFlag) {
        if (isEmpty(str)){
            return "";
        }
        if (!str.contains(rightFlag)) {
            return str;
        }
        return str.substring(0, str.lastIndexOf(rightFlag));
    }

    /**
     * 新旧字符串编号比较，移除其中相同的编号
     *
     * @param oldStr
     *            旧字符串编号
     * @param newStr
     *            新字符串编号
     * @see return oldStr 需要删除的对象编号
     * @see return newStr 需要新增的对象编号
     * @see ：
     *      {@code StringBuilder oldStr = new StringBuilder("1,2,3,4,0,");//老编号
     * StringBuilder newStr = new StringBuilder("4,2,6,8,9,10,");//新编号}
     * @see return oldStr = "1,3,0," oldStr = "6,8,9,10,"
     */
    public static void removeDu(StringBuilder oldStr, StringBuilder newStr) {
        String a = oldStr.toString();
        String b = newStr.toString();
        String[] arr1 = a.split(",");
        String[] arr2 = b.split(",");
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i].equals(arr2[j])) {
                    arr1[i] = "";
                    arr2[j] = "";
                }
            }
        }
        oldStr.delete(0, oldStr.length());
        newStr.delete(0, newStr.length());
        for (String str : arr1) {
            if (!"".equals(str)) {
                oldStr.append(str).append(",");
            }
        }
        for (String str : arr2) {
            if (!"".equals(str)) {
                newStr.append(str).append(",");
            }
        }
    }



    /**
     * 将格式为a-b,c-d,e-f,......截取得到a,c,e,.....
     *
     * @param regedSerialNo_groupCodeStr
     * @return
     */
    public static String getPartStr(String regedSerialNo_groupCodeStr) {
        String[] regedSerialNo_groupCodeStrs = regedSerialNo_groupCodeStr
                .split(",");
        String regedSerialNoStr = "";
        for (String str : regedSerialNo_groupCodeStrs) {
            regedSerialNoStr += str.split("-")[0] + ",";
        }
        return regedSerialNoStr.substring(0, regedSerialNoStr.length() - 1);
    }

    /**
     * 新旧分组组合项目对比 把新分组内的组合项目分为两类 : 一类是之前旧分组中存在的组合项目 另一类是新添加的组合项目
     *
     * @param newGroupCombStr
     * @param oldGroupCombStr
     * @return
     */
    public static List<String> getNewAndOldGroupCombStr(String newGroupCombStr,
                                                        String oldGroupCombStr) {
        System.out.println("newGroupCombStr:" + newGroupCombStr);
        System.out.println("oldGroupCombStr:" + oldGroupCombStr);
        List<String> NewAndOldGroupCombLst = new ArrayList<String>();
        // 旧组合项目数组
        String[] newGroupCombArray = newGroupCombStr.split(",");
        // 新组合项目中仍存在的就组合项目信息
        String oldGroupCombStr1 = "";
        // 要删除的组合项目信息
        String oldGroupCombStr2 = "";
        // 新添加的组合项目信息
        String newGroupCombStr1 = "";
        for (String str : newGroupCombArray) {
            if (oldGroupCombStr.contains(str)) {
                oldGroupCombStr1 += str + ",";
            } else {
                newGroupCombStr1 += str + ",";
            }
        }
        // 旧组合项目数组
        String[] oldGroupCombArray = oldGroupCombStr.split(",");
        for(String str : oldGroupCombArray){
            if(!oldGroupCombStr1.contains(str)){
                oldGroupCombStr2 += str + ",";
            }
        }
        System.out.println("oldGroupCombStr1:::" + oldGroupCombStr1);
        System.out.println("newGroupCombStr1:::" + newGroupCombStr1);
        NewAndOldGroupCombLst.add(oldGroupCombStr2);
        NewAndOldGroupCombLst.add(newGroupCombStr1);
        return NewAndOldGroupCombLst;
    }

    public static boolean isEmpty(Object object){
        return object == null || "".equals(object.toString());
    }

    public static String firstToLower(String param) {
        return join(param.substring(0, 1).toLowerCase() , param.substring(1, param.length()));
    }
    public static String firstToUpper(String param) {
        return join(param.substring(0, 1).toUpperCase() , param.substring(1, param.length()));
    }

    public static String converIdsToSql(String ids){
        if (StrUtil.isEmpty(ids)){
            return "";
        }
        String[] arr = ids.split(",");
        Set<String> set = new HashSet<>();
        for (String str : arr){
            if (isEmpty(str)){
                continue;
            }
            if (set.contains(str)){
                continue;
            }
            set.add(str);
        }
        String target = "";
        for (String str : set){
            target = join(target, "'", str, "',");
        }
        if (isEmpty(target)){
            return "''";
        }
        return target.substring(0, target.length()-1);
    }

    public static String toUpperCol(String param) {
        param = StrUtil.join(param.substring(0, 1).toLowerCase(), param.substring(1, param.length()));
        StringBuilder sb = new StringBuilder();
        char[] col = param.toCharArray();
        for (char ch : col) {
            if (Character.isUpperCase(ch)) {
                sb.append("_").append(Character.toLowerCase(ch));
            } else {
                sb.append(ch);
            }
        }
        return sb.toString().toUpperCase();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(converIdsToSql(",b,b,c"));
        StringBuilder str1 = new StringBuilder("12,22,32,42,20,");// 老编号
        StringBuilder str2 = new StringBuilder("42,22,61,87,9,10,");// 新编号
        StrUtil.removeDu(str1, str2);
        System.out.println(str1);// 剩下是需要移除对象的编号
        System.out.println(str2);// 剩下是需要新建对象的编号
        String str = "1,2,3,";
        System.out.println(str.lastIndexOf(","));
        System.out.println(str.length());
        String message = "ORA-12899: 列 \"JT_YWDT_N\".\"WB_USER\".\"USE_MESSAGE\" 的值太大 (实际值: 2, 最大值: 1)";
        System.out.println(getStrByPattern(message));

    }

    public static String getStrByPattern(String input){
        Pattern pattern = Pattern.compile("\"(.*?)\"");
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }
}
