package com.changhong.epc.config.tool;
|
|
import com.changhong.ssc.data.autho.base.struct.Condition;
|
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.math.NumberUtils;
|
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
import java.util.Objects;
|
|
/**
|
* 数据权限运算符
|
* @author WangYX
|
*
|
*/
|
@Slf4j
|
public enum Operator {
|
/* 等于 */
|
eq ("%s = %s" , 0 , Objects::equals),
|
/* 不等于 */
|
notEq ("%s != %s" , 0 , (resultObj, powerObj)->!eq.operation(resultObj, powerObj)),
|
/* 大于 */
|
gt ("%s > %s" , 0 , (resultObj, powerObj)->NumberUtils.toDouble(resultObj.toString()) > NumberUtils.toDouble(powerObj.toString())),
|
/* 大于或等于 */
|
gtEq ("%s >= %s" , 0 , (resultObj, powerObj)->NumberUtils.toDouble(resultObj.toString()) >= NumberUtils.toDouble(powerObj.toString())),
|
/* 小于 */
|
lt ("%s < %s" , 0 , (resultObj, powerObj)->NumberUtils.toDouble(resultObj.toString()) < NumberUtils.toDouble(powerObj.toString())),
|
/* 小于或等于 */
|
ltEq ("%s <= %s" , 0 , (resultObj, powerObj)->NumberUtils.toDouble(resultObj.toString()) <= NumberUtils.toDouble(powerObj.toString())),
|
/* 为空 */
|
empty ("%s IS NULL" , null , (resultObj, powerObj)->resultObj == null),
|
/* 不为空 */
|
notEmpty ("%s IS NOT NULL" , null , (resultObj, powerObj)->!empty.operation(resultObj, powerObj)),
|
/* 包含 */
|
like ("%s like '%%%s%%'" , 1 , (resultObj, powerObj)->resultObj.toString().indexOf(powerObj.toString()) >= 0),
|
/* 不包含 */
|
notLike ("%s NOT like '%%%s%%'" , 1 , (resultObj, powerObj)->!like.operation(resultObj, powerObj)),
|
/* 在...中 */
|
in ("%s IN(%s)" , 1 , (resultObj, powerObj)->{
|
if(powerObj instanceof String){
|
powerObj = Arrays.asList(powerObj);
|
}
|
return ((List)powerObj).stream().filter(obj->Objects.equals(resultObj, obj)).count() > 0;
|
}),
|
/* 不在...中 */
|
notIn ("%s NOT IN(%s)" , 1 , (resultObj, powerObj)->!in.operation(resultObj, powerObj));
|
|
/**
|
* 类型
|
*/
|
private Integer type;
|
|
/**
|
* 格式化
|
*/
|
private String format;
|
|
/**
|
* 执行运算
|
*/
|
private JavaOperation oper;
|
|
Operator(String format, Integer type, JavaOperation oper){
|
this.type = type;
|
this.format = format;
|
this.oper = oper;
|
}
|
|
public boolean operation(Object resultObj,Object powerObj){
|
try{
|
return oper.execute(resultObj, powerObj);
|
}catch (Exception e) {
|
log.error(e.getMessage());
|
return false;
|
}
|
}
|
|
/**
|
* 获得sql
|
* @param condition
|
* @param alias
|
* @return
|
*/
|
public String getSql(Condition condition, String alias, String tableName){
|
/* 字段值 */
|
String val = getString(condition),
|
/* 字段名称 */
|
column = ObjectUtil.empty(alias) ? condition.getColumn() : alias+"."+condition.getColumn();
|
if(type == null){
|
return String.format(this.format, column);
|
}else{
|
return String.format(this.format, column, val);
|
}
|
}
|
|
/**
|
* 字符串转参数
|
* @param condition
|
* @return
|
*/
|
public String getString(Condition condition){
|
return String.format("'%s'", escape(Objects.toString(condition.getValue(), "").replace(",", "','")));
|
}
|
|
/**
|
* 转义解决sql注入
|
* @param param
|
* @return
|
*/
|
public String escape(Object param){
|
return Objects.toString(param, "").replace("'", "\\'");
|
}
|
|
@FunctionalInterface
|
public interface JavaOperation{
|
|
boolean execute(Object resultObj, Object powerObj);
|
|
/**
|
* 转字符串
|
* @param obj
|
* @return
|
*/
|
default String getString(Object obj){
|
if(obj == null){
|
return null;
|
}
|
if(obj instanceof String){
|
return (String) obj;
|
}
|
return obj.toString();
|
}
|
|
/**
|
* 转数组
|
* @param obj
|
* @return
|
*/
|
default List<?> getArray(Object obj){
|
if(obj == null){
|
return null;
|
}
|
if(obj instanceof List){
|
return (List<?>) obj;
|
}
|
if(obj instanceof String){
|
return Arrays.asList(obj.toString().split(","));
|
}
|
if(obj.getClass().isArray()){
|
return Arrays.asList((Object[])obj);
|
}
|
return new ArrayList<>();
|
}
|
}
|
|
}
|