package org.springblade.modules.mjkj.common.config.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springblade.modules.mjkj.common.cgform.entity.CgformEnhanceJava;
import org.springblade.modules.mjkj.common.cgform.entity.CgformField;
import org.springblade.modules.mjkj.common.cgform.entity.CgformHead;
import org.springblade.modules.mjkj.common.cgform.entity.CgformIndex;
import org.springblade.modules.mjkj.common.cgform.enums.CgformValidPatternEnum;
import org.springblade.modules.mjkj.common.cgform.mapper.CategoryMapper;
import org.springblade.modules.mjkj.common.cgform.model.DictModel;
import org.springblade.modules.mjkj.common.cgform.model.FieldModel;
import org.springblade.modules.mjkj.common.cgform.model.SysPermissionDataRuleModel;
import org.springblade.modules.mjkj.common.cgform.model.query.MatchTypeEnum;
import org.springblade.modules.mjkj.common.cgform.model.query.QueryGenerator;
import org.springblade.modules.mjkj.common.cgform.model.query.QueryRuleEnum;
import org.springblade.modules.mjkj.common.cgform.service.ICgformFieldService;
import org.springblade.modules.mjkj.common.cgform.service.ISysDictItemService;
import org.springblade.modules.mjkj.common.cgform.service.ISysDictService;
import org.springblade.modules.mjkj.common.utils.MjkjUtils;
import org.springblade.modules.mjkj.common.config.exception.DBException;
import org.springblade.modules.mjkj.common.config.util.jsonschema.BaseColumn;
import org.springblade.modules.mjkj.common.config.util.jsonschema.CommonProperty;
import org.springblade.modules.mjkj.common.config.util.jsonschema.JsonSchemaDescrip;
import org.springblade.modules.mjkj.common.config.util.jsonschema.JsonschemaUtil;
import org.springblade.modules.mjkj.common.config.util.jsonschema.validate.*;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Slf4j
public class SqlSymbolUtil {
/**
* 获取id
*
* @return
*/
public static Long getIdWorkerId() {
long id = IdWorker.getId();
return id;
}
/**
* 获取查询sql
*
* @param tableName
* @param onlCgformFields
* @param map
* @return
*/
public static String getSelectSql(String tableName, List<CgformField> onlCgformFields, Map<String, Object> map) {
StringBuffer whereSb = new StringBuffer();//where条件字段
StringBuffer selectSb = new StringBuffer();//查询字段
for (CgformField onlCgformField : onlCgformFields) {
String dbFieldName = onlCgformField.getDbFieldName();
String dbType = onlCgformField.getDbType();
if (onlCgformField.getIsShowList() == 1) {
selectSb.append("," + dbFieldName);
}
if (Func.isNotEmpty(onlCgformField.getMainField())) {
boolean flag = !DataTypeUtil.isNumberType(dbType);
String sql = QueryGenerator.getSingleQueryConditionSql(dbFieldName, "", map.get(dbFieldName), flag);
if (!"".equals(sql)) {
whereSb.append(" AND " + sql);
}
}
if (onlCgformField.getIsQuery() != 1) {//是否查询条件0否 1是
continue;
}
if ("single".equals(onlCgformField.getQueryMode())) {//查询模式
if (map.get(dbFieldName) != null) {
boolean flag = DataTypeUtil.isNotNumberType(dbType);
String sql = QueryGenerator.getSingleQueryConditionSql(dbFieldName, "", map.get(dbFieldName), flag);
if (!"".equals(sql)) {
whereSb.append(" AND " + sql);
}
}
} else {
Object beginObj = map.get(dbFieldName + "_begin");
if (beginObj != null) {
whereSb.append(" AND " + dbFieldName + ">=");
if (DataTypeUtil.isNumberType(dbType)) {
whereSb.append(beginObj.toString());
} else {
whereSb.append("'" + beginObj.toString() + "'");
}
}
Object endObj = map.get(dbFieldName + "_end");
if (endObj != null) {
whereSb.append(" AND " + dbFieldName + "<=");
if (DataTypeUtil.isNumberType(dbType)) {
whereSb.append(endObj.toString());
} else {
whereSb.append("'" + endObj.toString() + "'");
}
}
}
}
return "SELECT id" + selectSb.toString() + " FROM " + getSubstring(tableName) + " where is_deleted =0 " + whereSb.toString();
}
public static boolean equals(Object object1, Object object2) {
if (Func.isEmpty(object1) && Func.isEmpty(object2)) {
return true;
} else {
return Func.isNotEmpty(object1) && object1.equals(object2);
}
}
/**
* 判断表名称和表说明是否一样
*
* @param onlCgformHead
* @param newOnlCgformHead
* @return
*/
public static boolean onlCgformHeadEquals(CgformHead onlCgformHead, CgformHead newOnlCgformHead) {
return !equals(onlCgformHead.getTableName(), newOnlCgformHead.getTableName()) || !equals(onlCgformHead.getTableTxt(), newOnlCgformHead.getTableTxt());
}
/**
* 判断新字段和旧字段是否一致
*
* @param field
* @param newField
* @return
*/
public static boolean fieldEquals(CgformField field, CgformField newField) {
return !equals(field.getDbFieldName(), newField.getDbFieldName())
|| !equals(field.getDbFieldTxt(), newField.getDbFieldTxt())
|| !equals(field.getDbLength(), newField.getDbLength())
|| !equals(field.getDbPointLength(), newField.getDbPointLength())
|| !equals(field.getDbType(), newField.getDbType())
|| !equals(field.getDbIsNull(), newField.getDbIsNull())
|| !equals(field.getDbIsKey(), newField.getDbIsKey())
|| !equals(field.getDbDefaultVal(), newField.getDbDefaultVal());
}
/**
* 判断新索引和旧索引是否一致
*
* @param index
* @param newIndex
* @return
*/
public static boolean indexEquals(CgformIndex index, CgformIndex newIndex) {
return !equals(index.getIndexName(), newIndex.getIndexName())
|| !equals(index.getIndexField(), newIndex.getIndexField())
|| !equals(index.getIndexType(), newIndex.getIndexType());
}
/**
* java增强,判断是否是java类
*
* @param onlCgformEnhanceJava
* @return
*/
public static boolean isExistJava(CgformEnhanceJava onlCgformEnhanceJava) {
String javaType = onlCgformEnhanceJava.getCgJavaType();
String javaValue = onlCgformEnhanceJava.getCgJavaValue();
if (Func.isEmpty(javaValue)) {
return true;
}
try {
if ("class".equals(javaType)) {
Class clazz = Class.forName(javaValue);
if (clazz == null || clazz.newInstance() == null) {
return false;
}
} else if ("spring".equals(javaType)) {
Object bean = SpringContextUtils.getBean(javaValue);
if (bean == null) {
return false;
}
}
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}
public static String getSubstring(String s) {
return Pattern.matches("^[a-zA-z].*\\$\\d+$", s) ? s.substring(0, s.lastIndexOf("$")) : s;
}
/**
* 将表字段转为json
*
* @param availableFieldsList
* @param disabledFieldsLiast
* @param fieldModel
* @return
*/
public static JSONObject getFiledJson(List<CgformField> availableFieldsList, List<String> disabledFieldsLiast, FieldModel fieldModel) {
List<String> mustAddList = new ArrayList<>();//必填字段
List<CommonProperty> hiddenList = new ArrayList<>();//隐藏列表
List<String> arrayList2 = new ArrayList<>();
ISysDictService dictService = SpringContextUtils.getBean(ISysDictService.class);
ISysDictItemService dictItemService = SpringContextUtils.getBean(ISysDictItemService.class);
CategoryMapper categoryMapper = SpringContextUtils