package com.palmnest.application.core.func;

import java.text.DecimalFormat;

import org.apache.commons.lang.StringUtils;

/**
 * 扩展jstl
 *
 * @author cyq
 */
public class ZddFunction {
	/**
	 * 分割字符串，保留指定长度的前几位和后几位，中间用指定替代字符串表示；
	 * 若原字符串长度不大于首尾长度总和，则只保留首长度+替代字符；
	 * 若原字符串不大于首长度，则直接返回原字符串
	 *
	 * @param source
	 * @param replaceString
	 * @param beginLength
	 * @param endLength
	 * @return
	 */
	public static String splitString(String source, String replaceString, Integer beginLength, Integer endLength) {
		String rs = source;

		if (endLength > 0 && source.length() > (beginLength + endLength)) {
			rs = source.substring(0, beginLength) + (StringUtils.isNotEmpty(replaceString) ? replaceString : "") + source.substring(source.length() - endLength);
		} else if (source.length() > beginLength) {
			rs = source.substring(0, beginLength) + (StringUtils.isNotEmpty(replaceString) ? replaceString : "");
		}
		return rs;
	}

	/**
	 * @param source        源字符串
	 * @param length        需要显示的长度
	 * @param replaceString 如果源字符串超过需要显示的长度，后面接的字符串，如“...”
	 * @return 如果源字符串长度<=   需要显示的长度   ，   则直接返回   ，   否则截取需要的长度   ，   再拼上后缀
                    */
	public static String cutString(String source, Integer length, String replaceString) {
		String rs = source;
		if (rs == null) {
			return null;
		}
		if (source.length() <= length) {
			return rs;
		}
		rs = source.substring(0, length) + (StringUtils.isNotEmpty(replaceString) ? replaceString : "");
		return rs;
	}

	public static void main(String[] args) {
		System.out.println(cutString("abcdefghijklmn", 5, "..."));
		Float total = 468.9F;
		String[] upCase = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
		String[] updegit = {"零", "零", "零", "零", "零", "零"};
		DecimalFormat df = new DecimalFormat("0000.00");
		String totalStr = df.format(total).replace(".", "");
		String[] ss = totalStr.split("");
		for (int i = 1, len = ss.length; i < len; i++) {
			Integer idx = Integer.valueOf(ss[i]);
			updegit[6 - (len - i)] = upCase[idx];
		}
		System.out.println(updegit);
	}
}
