package cn.autoform.util.form;
|
|
import cn.autoform.bean.form.CpnType;
|
import cn.autoform.bean.seache.SeacheConditions;
|
import cn.autoform.util.tool.ObjectUtil;
|
import com.alibaba.fastjson.JSON;
|
import net.sf.json.JSONObject;
|
import org.apache.commons.lang3.StringUtils;
|
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
|
/**
|
* 自定义条件工具类
|
* @author WangYX
|
*
|
*/
|
@SuppressWarnings("unchecked")
|
public class AutoConditionUtil {
|
|
private static final Map<CpnType, Condition> CONDITION = new HashMap<>();
|
|
static{
|
// id
|
CONDITION.put(CpnType.id, (fildName, valueLeft, valueRight)->{
|
if(StringUtils.isNotBlank(Objects.toString(valueLeft, "")) && StringUtils.isNotBlank(Objects.toString(valueRight, ""))){
|
return String.format("%s >= %s AND %s <= %s", fildName, escapeSql(valueLeft), fildName, escapeSql(valueRight));
|
}
|
if(StringUtils.isNoneBlank(Objects.toString(valueLeft, ""))){
|
return String.format("%s >= %s", fildName, escapeSql(valueLeft));
|
}
|
else {
|
return String.format("%s <= %s", fildName, escapeSql(valueRight));
|
}
|
});
|
|
// 单行文本
|
CONDITION.put(CpnType.text, (fildName, valueLeft, valueRight)->{
|
return String.format("%s LIKE '%%%s%%'", fildName, escapeSql(Objects.toString(valueLeft, "")));
|
});
|
|
// 生成编号
|
CONDITION.put(CpnType.auto, (fildName, valueLeft, valueRight)->{
|
return String.format("%s LIKE '%%%s%%'", fildName, escapeSql(Objects.toString(valueLeft, "")));
|
});
|
|
// 文本域
|
CONDITION.put(CpnType.textarea, CONDITION.get(CpnType.text));
|
// 金额控件
|
CONDITION.put(CpnType.number, (fildName, valueLeft, valueRight)->{
|
if(StringUtils.isNotBlank(Objects.toString(valueLeft, "")) && StringUtils.isNotBlank(Objects.toString(valueRight, ""))){
|
return String.format("cast(%s as DECIMAL(65,30)) >= cast('%s' as DECIMAL(65,30)) AND cast(%s as DECIMAL(65,30)) <= cast('%s' as DECIMAL(65,30))", fildName, escapeSql(valueLeft), fildName, escapeSql(valueRight));
|
}
|
if(StringUtils.isNoneBlank(Objects.toString(valueLeft, ""))){
|
return String.format("cast(%s as DECIMAL(65,30)) >= cast('%s' as DECIMAL(65,30))", fildName, escapeSql(valueLeft));
|
}
|
else {
|
return String.format("cast(%s as DECIMAL(65,30)) <= cast('%s' as DECIMAL(65,30))", fildName, escapeSql(valueRight));
|
}
|
});
|
// 日期
|
CONDITION.put(CpnType.calendar, (fildName, valueLeft, valueRight)->{
|
String type = "%Y-%m-%d %H:%i";
|
String val = Objects.toString(StringUtils.isNoneBlank(Objects.toString(valueLeft, "")) ? valueLeft : valueRight, "");
|
if(val.indexOf('-') >= 1) {
|
if (val.length() <= 4) {
|
type = "%Y";
|
} else if (val.length() <= 7) {
|
type = "%Y-%m";
|
} else if (val.length() <= 10) {
|
type = "%Y-%m-%d";
|
}
|
}else{
|
type = "%H:%i";
|
}
|
|
if(StringUtils.isNotBlank(Objects.toString(valueLeft, "")) && StringUtils.isNotBlank(Objects.toString(valueRight, ""))){
|
return String.format("str_to_date(%s, '%s') >= str_to_date('%s', '%s') AND str_to_date(%s, '%s') <= str_to_date('%s', '%s')"
|
, fildName, type, escapeSql(valueLeft), type, fildName, type, escapeSql(valueRight), type);
|
}
|
if(StringUtils.isNoneBlank(Objects.toString(valueLeft, ""))){
|
return String.format("str_to_date(%s, '%s') >= str_to_date('%s', '%s')", fildName, type, escapeSql(valueLeft), type);
|
}
|
else {
|
return String.format("str_to_date(%s, '%s') <= str_to_date('%s', '%s')", fildName, type, escapeSql(valueRight), type);
|
}
|
});
|
// 单选按钮
|
CONDITION.put(CpnType.radio, (fildName, valueLeft, valueRight)->{
|
return String.format("%s = '%%%s%%'", fildName, escapeSql(valueLeft));
|
});
|
// 复选按钮
|
CONDITION.put(CpnType.checkbox, (fildName, valueLeft, valueRight)->{
|
if(valueLeft instanceof String){
|
return String.format("FIND_IN_SET('%s',%s)", escapeSql(valueLeft), fildName);
|
}else if(valueLeft instanceof List){
|
StringBuilder sql = new StringBuilder("(");
|
((List)valueLeft).stream()
|
.forEach(val->{
|
sql.append(CONDITION.get(CpnType.checkbox).getSql(fildName, val, null)).append(" OR ");
|
});
|
sql.delete(sql.length()-" OR ".length(), sql.length());
|
return sql.append(')').toString();
|
}else{
|
return "";
|
}
|
});
|
// 下拉框
|
CONDITION.put(CpnType.dropdownlist, CONDITION.get(CpnType.checkbox));
|
// 数额控件
|
CONDITION.put(CpnType.amount, CONDITION.get(CpnType.number));
|
//树控件
|
CONDITION.put(CpnType.tree,CONDITION.get(CpnType.checkbox));
|
|
}
|
|
public static String getConditionSql(List<SeacheConditions> conditions){
|
if(conditions == null || conditions.size() <= 0)
|
return null;
|
StringBuilder sql = new StringBuilder();
|
conditions.stream().forEach(condition->{
|
|
String where = CONDITION.get(condition.getType()).getCondition(condition);
|
if(StringUtils.isBlank(where))
|
return;
|
sql.append(where).append(" AND ");
|
});
|
if(sql.length() == 0)
|
return null;
|
sql.delete(sql.length()-" AND ".length(), sql.length());
|
return sql.toString();
|
}
|
|
public static String escapeSql(Object obj){
|
String sql = Objects.toString(obj, "");
|
sql = sql.replace("\\", "");
|
sql = sql.replace("'", "\\'");
|
return sql;
|
}
|
|
|
/**
|
* 过滤选项卡。
|
* @param
|
*/
|
public static void filterOption(JSONObject filedset) {
|
String[] tabAndTags = (String[]) filedset.values().stream().filter(AutoConditionUtil::cpnTypeIsTabOrTag).map(val->Objects.toString(((Map)val).get("columnOrderNum"), "")).toArray(String[]::new);
|
filedset.entrySet().stream().forEach(val->{
|
Map info = ((Map)((Map.Entry)val).getValue());
|
String parentsubFormNum = Objects.toString(info.get("parentsubFormNum"));
|
if(isContains(tabAndTags, parentsubFormNum)) {
|
info.put("parentsubFormNum", 0);
|
}
|
});
|
for(String tabAndTag : tabAndTags) {
|
filedset.remove("itemId_"+tabAndTag);
|
}
|
}
|
public static String filterOption(String filedset) {
|
com.alibaba.fastjson.JSONObject filedsetObj = JSON.parseObject(filedset);
|
String[] tabAndTags = (String[]) filedsetObj.values().stream().filter(AutoConditionUtil::cpnTypeIsTabOrTag).map(val->Objects.toString(((Map)val).get("columnOrderNum"), "")).toArray(String[]::new);
|
filedsetObj.entrySet().stream().forEach(val->{
|
Map info = ((Map)((Map.Entry)val).getValue());
|
String parentsubFormNum = Objects.toString(info.get("parentsubFormNum"));
|
if(isContains(tabAndTags, parentsubFormNum)) {
|
info.put("parentsubFormNum", 0);
|
}
|
});
|
for(String tabAndTag : tabAndTags) {
|
filedsetObj.remove("itemId_"+tabAndTag);
|
}
|
return filedsetObj.toJSONString();
|
}
|
|
public static String toInVal(String val){
|
if(ObjectUtil.empty(val)){
|
return "''";
|
}
|
val = escapeSql(val).replace(",", "','");
|
return String.format("'%s'", val);
|
}
|
|
public static void main(String... args){
|
System.out.println(toInVal("a',b"));
|
}
|
|
public static boolean isContains(String[] arrays, String str) {
|
for(String array : arrays) {
|
if(Objects.equals(array, str)) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
/**
|
* 当前控件是选项卡或者选项卡标签
|
* @param val
|
* @return
|
*/
|
public static boolean cpnTypeIsTabOrTag(Object val) {
|
String type = Objects.toString(((Map)val).get("tag_Type"));
|
return Objects.equals(CpnType.tab.toString(), type);
|
}
|
|
|
@FunctionalInterface
|
public static interface Condition{
|
|
/**
|
* 获得条件语句
|
* @param
|
* @param
|
* @param
|
* @return
|
*/
|
default String getCondition(SeacheConditions condition){
|
if(StringUtils.isBlank(Objects.toString(condition.getValueLeft(), "")) && StringUtils.isBlank(Objects.toString(condition.getValueRight(), "")))
|
return "";
|
return getSql(condition.getFieldName(), condition.getValueLeft(), condition.getValueRight());
|
}
|
|
String getSql(String fildName, Object valueLeft, Object valueRight);
|
}
|
}
|