1. 组织编码操作
/**
* 按长度拆分
*
* @param str
* @param length
* @return
*/
public static String[] split(String str, int length) {
int count = (int) Math.ceil((double) str.length() / length); // 计算分割后的子串个数
String[] subStrs = new String[count];
for (int i = 0; i < count; i++) {
if (i == count - 1) {
subStrs[i] = str.substring(i * length);
} else {
subStrs[i] = str.substring(i * length, (i + 1) * length);
}
}
return subStrs;
}
/**
* 按长度拆分OrgCode
*
* @param str
* @param length
* @return
*/
public static String[] splitOrgCode(String str, int length) {
if(StringUtils.isEmpty(str)){
return new String[1];
}
if(str.length()<length){
return new String[1];
}
int count = (int) Math.ceil((double) str.length() / length); // 计算分割后的子串个数
String[] subStrs = new String[count-1];
for (int i = 0; i < count; i++) {
String valStr = str.substring(0,str.length()-3*(i+1));
if(StringUtils.isEmpty(valStr)){
break;
}
subStrs[i] = valStr;
}
return subStrs;
}
/**
* 获取子节点所有
* @param rootOrgCode 根节点code
* @param childOrgCode 子节点code
* @return
*/
private static List<String> getOrgTreePath(String rootOrgCode,String childOrgCode) {
int num = (childOrgCode.length() - rootOrgCode.length())/3;
List<String> orgCodes = new ArrayList<>();
for (int i = 0; i < num; i++) {
orgCodes.add(childOrgCode.substring(0,(i+1)*3));
}
return orgCodes;
}
/**
* 拼接 url
*
* @param url
* @param urlParams
* @return
*/
public static String builderUrl(String url, Map<String, Object> urlParams) {
StringBuilder urlBuilder = new StringBuilder(url);
if (!urlParams.isEmpty()) {
urlBuilder.append("?");
int index = 0;
for (Map.Entry entry : urlParams.entrySet()) {
index++;
urlBuilder.append(entry.getKey()).append("=").append(entry.getValue());
if (index <= urlParams.size() - 1) {
urlBuilder.append("&");
}
}
}
return urlBuilder.toString();
}
/**
* @param channelCode 1000004$1$0$0
* @return str[0] 设备编码 str[1] 单元类型 str[2] 单元序号(默认0) str[3] 通道序号
*/
public static String[] splitChannelCode(String channelCode) {
return channelCode.split("\\$");
}
private static final String SEPERATOR = "$";
public static String toChannelIdWithoutUnitType(String deviceCode, Integer unitSeq, Integer channelSeq) {
return deviceCode + SEPERATOR + unitSeq + SEPERATOR + channelSeq;
}
/**
* getSortedString 对参数按照Key进行ASCII排序
* @param jsonObject 请求参数
* @return 排序拼装后的字符串
*/
public static String getSortedString(JSONObject jsonObject) {
SortedMap<String, Object> sortMap = new TreeMap<>();
StringBuilder sbf = new StringBuilder();
for (Map.Entry<String, Object> objectEntry : jsonObject.entrySet()) {
String key = objectEntry.getKey();
Object value = objectEntry.getValue();
if(StringUtils.isEmpty(key) || ObjectUtil.isNull(value)){
continue;
}
if ("sign".equals(key)) {
continue;
}
sortMap.put(key, value);
}
Set<Map.Entry<String, Object>> mapEntrySet = sortMap.entrySet();
for (Map.Entry entry : mapEntrySet) {
String k = (String) entry.getKey();
Object v = entry.getValue();
sbf.append(k).append("=").append(v).append("&");
}
String sbfString = sbf.toString();
// System.out.println("排序后的字符串:" + sbfString.substring(0, sbfString.length() - 1));
return sbfString.substring(0, sbfString.length() - 1);
}
2.其他操作