package com.product.admin.util;
|
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
import org.springframework.stereotype.Component;
|
|
import com.alibaba.druid.util.StringUtils;
|
import com.product.admin.config.SystemCode;
|
import com.product.util.config.SystemParamSet;
|
import com.product.common.lang.DateUtils;
|
import com.product.core.entity.FieldSetEntity;
|
import com.product.core.exception.BaseException;
|
import com.product.core.service.support.AbstractBaseService;
|
import com.product.core.spring.context.SpringMVCContextHolder;
|
|
/**
|
* 系统参数替换
|
*
|
* @author Mr.Xu
|
*
|
*/
|
|
public class SystemParamReplace extends AbstractBaseService {
|
|
public static void main(String[] args) throws BaseException {
|
paramEXCELError("13", 10, 10, "12");
|
}
|
|
/**
|
* 数据导入错误提示信息
|
*
|
* @param paramText 错误通用文本
|
* @param x 错误所在行
|
* @param y 错误所在列
|
* @param dataValue 错误数据
|
* @return
|
*/
|
public static String paramError(String paramText, int x, int y, String dataValue) {
|
|
String xs = (String) excelColIndexToStr(x);
|
|
// 验证正则表达式
|
Pattern p = Pattern.compile("(\\{\\{)([\\w]+)(\\}\\})");
|
Matcher m = p.matcher(paramText);
|
StringBuffer sb = new StringBuffer();
|
while (m.find()) {
|
String group = m.group(2);
|
if ("x".equals(group)) {
|
m.appendReplacement(sb, "" + xs);
|
} else if ("y".equals(group)) {
|
m.appendReplacement(sb, "" + y);
|
} else if ("dataValue".equals(group)) {
|
m.appendReplacement(sb, dataValue);
|
}
|
}
|
m.appendTail(sb);
|
return sb.toString();
|
}
|
|
/**
|
* 错误信息替换
|
*
|
* @param paramText 原错误文本
|
* @param x 替换参数一
|
* @param y 替换参数二
|
* @param dataValue 错误数据
|
* @return
|
*/
|
public static String paramError(String paramText, String x, String y, String dataValue) {
|
// 验证正则表达式
|
Pattern p = Pattern.compile("(\\{\\{)([\\w]+)(\\}\\})");
|
Matcher m = p.matcher(paramText);
|
StringBuffer sb = new StringBuffer();
|
while (m.find()) {
|
String group = m.group(2);
|
if ("x".equals(group)) {
|
m.appendReplacement(sb, "" + x);
|
} else if ("y".equals(group)) {
|
m.appendReplacement(sb, "" + y);
|
} else if ("dataValue".equals(group)) {
|
m.appendReplacement(sb, dataValue);
|
}
|
}
|
m.appendTail(sb);
|
return sb.toString();
|
}
|
|
/**
|
* 错误信息替换(列号转为EXCEL列号)
|
*
|
* @param paramText 原错误文本
|
* @param x 替换参数一
|
* @param y 替换参数二
|
* @param dataValue 错误数据
|
* @return
|
*/
|
public static String paramEXCELError(String paramText, int x, int y, String dataValue) {
|
|
String xs = (String) excelColIndexToStr(x);
|
y=y+3;
|
|
// 验证正则表达式
|
Pattern p = Pattern.compile("(\\{\\{)([\\w]+)(\\}\\})");
|
Matcher m = p.matcher(paramText);
|
StringBuffer sb = new StringBuffer();
|
while (m.find()) {
|
String group = m.group(2);
|
if ("x".equals(group)) {
|
m.appendReplacement(sb, xs);
|
} else if ("y".equals(group)) {
|
m.appendReplacement(sb, "" + y);
|
} else if ("dataValue".equals(group)) {
|
m.appendReplacement(sb, dataValue);
|
}
|
}
|
m.appendTail(sb);
|
return sb.toString();
|
}
|
|
// 将数字转换为EXCEL列号字母
|
public static String excelColIndexToStr(int columnIndex) {
|
// 列索引从0开始
|
|
String columnStr = "";
|
do {
|
if (columnStr.length() > 0) {
|
columnIndex--;
|
}
|
columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr;
|
columnIndex =((columnIndex - columnIndex % 26) / 26);
|
} while (columnIndex > 0);
|
return columnStr;
|
}
|
|
/**
|
*
|
* @param args 需要替换的String字符串
|
* @param pageFieldset 表单FieldSetEntity 参数 替换规则 {表单参数} 、 [系统参数]
|
* @return
|
* @throws BaseException
|
*/
|
public static String paramReplace(String args, FieldSetEntity pageFieldset) throws BaseException {
|
Pattern p = Pattern.compile("(\\[)([\\w]+)(\\])");
|
Matcher m = p.matcher(args);
|
StringBuffer sb = new StringBuffer();
|
while (m.find()) {
|
String group = m.group(2);
|
if (SystemParamSet.SYSTEM_PARAMS.get(group) != null) {
|
m.appendReplacement(sb, replaceSystemParameter(group));
|
}
|
|
}
|
m.appendTail(sb);
|
return replacePageParameter(sb.toString(), pageFieldset);
|
}
|
|
/**
|
*
|
* @param args 需要替换的String字符串 高级参照专用
|
* @return
|
* @throws BaseException
|
*/
|
public static String paramReplacePrompt(String args) throws BaseException {
|
if (args == null || "".equals(args)) {
|
return null;
|
}
|
Pattern p = Pattern.compile("(\\{\\{)([\\w]+)(\\}\\})");
|
Matcher m = p.matcher(args);
|
StringBuffer sb = new StringBuffer();
|
while (m.find()) {
|
String group = m.group(2);
|
if (SystemParamSet.SYSTEM_PARAMS.get(group) != null) {
|
m.appendReplacement(sb, replaceSystemParameter(group));
|
}
|
|
}
|
m.appendTail(sb);
|
return sb.toString();
|
}
|
|
/**
|
* 替换业务数据
|
*
|
* @param args
|
* @param fs
|
* @return
|
* @throws BaseException
|
*/
|
private static String replacePageParameter(String args, FieldSetEntity fs) throws BaseException {
|
if (fs != null) {
|
Pattern p = Pattern.compile("(\\{)([\\w]+)(\\})");
|
Matcher m = p.matcher(args);
|
StringBuffer sb = new StringBuffer();
|
while (m.find()) {
|
String group = m.group(2);
|
if (fs.getString(group) != null) {
|
m.appendReplacement(sb, fs.getString(group));
|
}else{
|
m.appendReplacement(sb, "");
|
}
|
}
|
m.appendTail(sb);
|
return sb.toString();
|
}
|
return args;
|
}
|
|
/**
|
* 替换文本参数
|
*
|
* @param paramText 原文本
|
* @param fseData 业务数据
|
* @return 新文本
|
* @throws BaseException
|
*/
|
public static String replaceParamText(String paramText, FieldSetEntity fseData) throws BaseException {
|
String str = paramText;
|
// 人员参数{{参数值:staff}}
|
Pattern p1 = Pattern.compile("(\\{\\{)([\\w]+)(:staff\\}\\})");
|
// 邮件参数{{参数值:email}}
|
Pattern p2 = Pattern.compile("(\\{\\{)([\\w]+)(:email\\}\\})");
|
// 系统参数{{参数名:sys}}
|
Pattern p3 = Pattern.compile("(\\{\\{)([\\w]+)(:sys\\}\\})");
|
// 普通业务参数{{参数名}}
|
Pattern p4 = Pattern.compile("(\\{\\{)([\\w]+)(\\}\\})");
|
|
StringBuffer sb1 = new StringBuffer();
|
Matcher m = p1.matcher(str);
|
while (m.find()) {
|
String group = m.group(2);
|
m.appendReplacement(sb1, group);
|
}
|
m.appendTail(sb1);
|
|
StringBuffer sb2 = new StringBuffer();
|
m = p2.matcher(sb1);
|
while (m.find()) {
|
String group = m.group(2);
|
m.appendReplacement(sb2, group);
|
}
|
m.appendTail(sb2);
|
|
StringBuffer sb3 = new StringBuffer();
|
m = p3.matcher(sb2);
|
while (m.find()) {
|
String group = m.group(2);
|
String replaceValue=replaceSystemParameter(group);
|
if(StringUtils.isEmpty(replaceValue)) {
|
continue;
|
}
|
m.appendReplacement(sb3, replaceValue);
|
}
|
m.appendTail(sb3);
|
|
StringBuffer sb4 = new StringBuffer();
|
if (fseData != null) {
|
m = p4.matcher(sb3);
|
while (m.find()) {
|
String group = m.group(2);
|
String value = fseData.getString(group);
|
if (null != value) {
|
m.appendReplacement(sb4, value);
|
}
|
}
|
m.appendTail(sb4);
|
}
|
return sb4.toString();
|
|
}
|
|
/**
|
* 获取系统参数值
|
*
|
* @param key 系统参数名
|
* @return
|
*/
|
public static String replaceSystemParameter(String key) throws BaseException {
|
if (SpringMVCContextHolder.getCurrentUser() == null) {
|
return "";
|
}
|
try {
|
switch (key) {
|
case "YEAR": // 年
|
return DateUtils.getYear();
|
case "MONTH": // 月
|
return DateUtils.getMonth();
|
case "DAY": // 天
|
return DateUtils.getDay();
|
case "STAFF_NAME": // 用户名称
|
return SpringMVCContextHolder.getCurrentUser().getCurrentStaff().getString("staff_name");
|
case "STAFF_UUID": // 用户uuid
|
return SpringMVCContextHolder.getCurrentUser().getCurrentStaff().getString("uuid");
|
case "STAFF_CODE": // 用户编码
|
return SpringMVCContextHolder.getCurrentUser().getCurrentStaff().getString("tricode");
|
case "USER_UUID": // user 表uuid
|
return SpringMVCContextHolder.getCurrentUser().getUuid();
|
case "USER_ID": // user 表id
|
return String.valueOf(SpringMVCContextHolder.getCurrentUser().getUser_id());
|
case "CLIENT_UUID": // 客户uuid
|
return SpringMVCContextHolder.getCurrentUser().getClient_uuid();
|
case "CLIENT_NAME": // 客户uuid
|
return SpringMVCContextHolder.getCurrentUser().getClientName();
|
case "COMPANY_CODE": // 公司编码
|
return SpringMVCContextHolder.getCurrentUser().getOrg_level_code();
|
case "COMPANY_NAME": // 公司名称
|
return SpringMVCContextHolder.getCurrentUser().getOrg_level_name();
|
case "COMPANY_UUID": // 公司uuid
|
return SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid();
|
case "DEPT_CODE": // 部门编码
|
return SpringMVCContextHolder.getCurrentUser().getCurrentDept().getString("org_level_code");
|
case "DEPT_NAME": // 部门名称
|
return SpringMVCContextHolder.getCurrentUser().getCurrentDept().getString("org_level_name");
|
case "DEPT_UUID": // 部门uuid
|
return SpringMVCContextHolder.getCurrentUser().getDept_uuid();
|
case "POST_UUID": // 岗位uuid
|
return SpringMVCContextHolder.getCurrentUser().getPost();
|
case "ROLE_UUID": // 角色UUID
|
return SpringMVCContextHolder.getCurrentUser().getRoles();
|
default:
|
return key;
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new BaseException(SystemCode.SYSTEM_SYS_PARAM_ACQUIRE_FAIL.getText(),
|
SystemCode.SYSTEM_SYS_PARAM_ACQUIRE_FAIL.getValue(), SystemParamReplace.class,
|
"replaceSystemParameter");
|
}
|
}
|
}
|