package com.yem.ensign.common;

import java.text.MessageFormat;
import java.util.*;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;

/**
 * 字符串工具类
 */
public class StringUtils {

    /**
     * 判断字符串是否为空
     * @param string 字符串
     * @return 是，否
     */
	public static boolean isEmpty(String string) {
        return isNull(string) || isBlank(string);
    }

    /**
     * 判断字符串是否不为空
     * @param string 字符串
     * @return 是，否
     */
    public static boolean isNotEmpty(String string) {
        return !isEmpty(string);
    }

    /**
     * 返回空字符串
     * @return
     */
    public static String getEmpty() {
        return "";
    }

    /**
     * 判断自负是否为null
     * @param string 字符串
     * @return 是，否
     */
    public static boolean isNull(String string) {
        return string == null;
    }

    /**
     * 判断字符串是否不为null
     * @param string 字符串
     * @return 是，否
     */
    public static boolean isNotNull(String string) {
        return !isNull(string);
    }

    /**
     * 判断字符串是否为空白
     * @param string 字符串
     * @return 是，否
     */
    public static boolean isBlank(String string) {
        return isNotNull(string) && string.trim().length() == 0;
    }

    /**
     * 去除字符串中的首尾空格
     * @param string 处理前字符串
     * @return 处理后字符串
     */
    public static String trim(String string) {
        return isNull(string) ? getEmpty() : string.trim();
    }

    /**
     * 根据占位符替换值 替换 字符串中的占位符
     * @param msg 包含占位符的字符串
     * @param params 占位符替换值
     * @return 处理后字符串
     */
    public static String formatMessage(String msg, Object[] params) {
        return MessageFormat.format(msg, params);
    }

    /**
     * 根据Set值转字符串
     * @param valueSet Set值
     * @return 转换后字符串
     */
    public static String setToString(Set valueSet) {
        StringBuffer returnValue = new StringBuffer("(");
        if (valueSet != null && valueSet.size() != 0) {
            Iterator it = valueSet.iterator();
            boolean flag = false;
            String tempValue = null;
            Object obj = null;
            while (it.hasNext()) {
                obj = it.next();
                if (obj instanceof String) {
                    tempValue = (String)obj;
                }
                else {
                    tempValue = obj.toString();
                }
                if (tempValue != null) {
                    if (tempValue.trim().length() == 0) {
                        continue;
                    }
                    if (flag) {
                        returnValue.append(",");
                    }
                    returnValue.append("'");
                    returnValue.append(tempValue);
                    returnValue.append("'");
                    flag = true;
                }
            }
        }
        else {
            returnValue.append("'null'");
        }
        returnValue.append(")");
        return returnValue.toString();
    }

    /**
     * 将对象集合中的指定字段拼接成字符串
     * @param value 对象集合
     * @param field 字段标识
     * @return 拼接后的字符串，例：('A1','A2','A3')
     */
    public static String setToString(DynamicObjectCollection value, String field) {
        StringBuffer returnValue = new StringBuffer("(");
        if (value != null && value.size() != 0) {
            boolean flag = false;
            for (DynamicObject obj : value) {
                if (flag) {
                    returnValue.append(",");
                }
                returnValue.append("'");
                returnValue.append(obj.getString(field));
                returnValue.append("'");
                flag = true;
            }
        }
        else {
            returnValue.append("'null'");
        }
        returnValue.append(")");
        return returnValue.toString();
    }

    /**
     * 将字符串插入字符串中指定位置
     * @param number 被插入字符串
     * @param step 指定位置
     * @param split 插入字符串
     * @return 处理后字符串
     */
    public static String splitString(String number, int step, String split) {
        String value = null;
        if(isEmpty(split)){
            value = number;
        }else if(!isEmpty(number)){
            int length = number.length();
            if(step<0){
                value = split + number;
            }else if(step>length){
                value = number + split;
            }else{
                value = number.substring(0, step);
                value = value + split + number.substring(step, length);
            }
        }
        return value;
    }

    /**
     * 字符串数组 按','拼接符 拼接成字符串
     * @param fliedList 字符串List数组
     * @return 拼接后字符串，例：A,B,C
     */
    public static String listToString(List<String> fliedList) {
        StringBuffer sb = new StringBuffer();
        if (null == fliedList || fliedList.size() == 0) {
            throw new IllegalArgumentException("List数组为空！");
        }else if (fliedList.size() == 1) {
            sb.append(fliedList.get(0));
        }else {
            for (String flied:fliedList) {
                if(sb.length()==0){
                    sb.append(flied);
                }else{
                    sb.append(",").append(flied);
                }
            }
        }
        return sb.toString();
    }

    /**
     * 字符串数组 按指定拼接符 拼接成字符串
     * @param fliedList 字符串List数组
     * @param symbol 拼接符
     * @return 拼接后字符串
     */
    public static String listToString(List<String> fliedList,String symbol) {
        StringBuffer sb = new StringBuffer();
        if (null == fliedList || fliedList.size() == 0) {
            throw new IllegalArgumentException("List数组为空！");
        }else if (fliedList.size() == 1) {
            sb.append(fliedList.get(0));
        }else {
            for (String flied:fliedList) {
                if(sb.length()==0){
                    sb.append(flied);
                }else{
                    sb.append(symbol).append(flied);
                }
            }
        }
        return sb.toString();
    }

    /**
     * 获取对象数组的主键数组
     * @param objArray 对象数组
     * @return 主键数组
     */
    public static Object[] getPks(DynamicObject[] objArray) {
        if (objArray == null || objArray.length == 0) {
            return new Object[0];
        }
        Object[] pks = new Object[objArray.length];
        for (int i = 0; i < objArray.length; ++i) {
            pks[i] = objArray[i].getPkValue();
        }
        return pks;
    }

    /**
     * 获取对象集合的主键数组
     * @param objArray 对象集合
     * @return 主键数组
     */
    public static Object[] getPks(DynamicObjectCollection objArray) {
        if (objArray == null || objArray.size() == 0) {
            return new Object[0];
        }
        Object[] pks = new Object[objArray.size()];
        for (int i = 0; i < objArray.size(); ++i) {
            pks[i] = ((DynamicObject)objArray.get(i)).getPkValue();
        }
        return pks;
    }

    /**
     * 转换泛型List数组
     * @param e
     * @return
     * @param <T>
     */
    public static <T> List<T> makeList(T... e) {
        List<T> statusList = new ArrayList<T>();
        for (int i = 0; i < e.length; ++i) {
            statusList.add(e[i]);
        }
        return statusList;
    }

    /**
     * 字符串数组 按','拼接符 拼接成字符串
     * @param valueSet 字符串Set数组
     * @return 拼接后字符串
     */
    public static String setToSelectorString(Set<String> valueSet) {
        StringBuffer returnValue = new StringBuffer("");
        if (valueSet != null && valueSet.size() != 0) {
            Iterator<String> it = valueSet.iterator();
            boolean flag = false;
            String tempValue = null;
            Object obj = null;
            while (it.hasNext()) {
                obj = it.next();
                if (obj instanceof String) {
                    tempValue = (String)obj;
                }
                else {
                    tempValue = obj.toString();
                }
                if (tempValue != null) {
                    if (tempValue.trim().length() == 0) {
                        continue;
                    }
                    if (flag) {
                        returnValue.append(",");
                    }
                    returnValue.append(tempValue);
                    flag = true;
                }
            }
        }
        return returnValue.toString();
    }

    /**
     * 字符串数组 按指定拼接符 拼接成字符串
     * @param valueSet 字符串Set数组
     * @return 拼接后字符串
     */
    public static String setToSelectorString(Set<String> valueSet,String symbol) {
        StringBuffer returnValue = new StringBuffer("");
        if (valueSet != null && valueSet.size() != 0) {
            Iterator<String> it = valueSet.iterator();
            boolean flag = false;
            String tempValue = null;
            Object obj = null;
            while (it.hasNext()) {
                obj = it.next();
                if (obj instanceof String) {
                    tempValue = (String)obj;
                }
                else {
                    tempValue = obj.toString();
                }
                if (tempValue != null) {
                    if (tempValue.trim().length() == 0) {
                        continue;
                    }
                    if (flag) {
                        returnValue.append(symbol);
                    }
                    returnValue.append(tempValue);
                    flag = true;
                }
            }
        }
        return returnValue.toString();
    }

    /**
     * String左对齐
     * @param src 字符串
     * @param len 字符串总长度
     * @param ch char值
     * @return 处理后字符串
     */
    public static String padLeft(String src, int len, char ch) {
      int diff = len - src.length();
      if (diff <= 0) {
        return src;
      }
     
      char[] charr = new char[len];
      System.arraycopy(src.toCharArray(), 0, charr, 0, src.length());
      for (int i = src.length(); i < len; i++) {
        charr[i] = ch;
      }
      return new String(charr);
    }

    /**
     * String右对齐
     * @param src 字符串
     * @param len 字符串总长度
     * @param ch char值
     * @return 处理后字符串
     */
    public static String padRight(String src, int len, char ch) {
      int diff = len - src.length();
      if (diff <= 0) {
        return src;
      }
     
      char[] charr = new char[len];
      System.arraycopy(src.toCharArray(), 0, charr, diff, src.length());
      for (int i = 0; i < diff; i++) {
        charr[i] = ch;
      }
      return new String(charr);
    }
}
