test: es
This commit is contained in:
@@ -37,19 +37,113 @@ import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.stereotype.Service;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@EnableAsync
|
||||
public class ElasticArchiveDao {
|
||||
|
||||
public static RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
|
||||
|
||||
/**
|
||||
* 驼峰转蛇形命名
|
||||
* 例如: userName -> user_name
|
||||
*/
|
||||
private String camelToSnake(String camelCase) {
|
||||
if (camelCase == null || camelCase.isEmpty()) {
|
||||
return camelCase;
|
||||
}
|
||||
StringBuilder snakeCase = new StringBuilder();
|
||||
snakeCase.append(Character.toLowerCase(camelCase.charAt(0)));
|
||||
|
||||
for (int i = 1; i < camelCase.length(); i++) {
|
||||
char c = camelCase.charAt(i);
|
||||
if (Character.isUpperCase(c)) {
|
||||
snakeCase.append('_').append(Character.toLowerCase(c));
|
||||
} else {
|
||||
snakeCase.append(c);
|
||||
}
|
||||
}
|
||||
return snakeCase.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 蛇形转驼峰命名
|
||||
* 例如: user_name -> userName
|
||||
*/
|
||||
private String snakeToCamel(String snakeCase) {
|
||||
if (snakeCase == null || snakeCase.isEmpty()) {
|
||||
return snakeCase;
|
||||
}
|
||||
StringBuilder camelCase = new StringBuilder();
|
||||
boolean nextUpperCase = false;
|
||||
|
||||
for (int i = 0; i < snakeCase.length(); i++) {
|
||||
char c = snakeCase.charAt(i);
|
||||
if (c == '_') {
|
||||
nextUpperCase = true;
|
||||
} else {
|
||||
if (nextUpperCase) {
|
||||
camelCase.append(Character.toUpperCase(c));
|
||||
nextUpperCase = false;
|
||||
} else {
|
||||
camelCase.append(Character.toLowerCase(c));
|
||||
}
|
||||
}
|
||||
}
|
||||
return camelCase.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查两个字段名是否相等(支持驼峰和蛇形命名转换)
|
||||
* @param field1 字段名1
|
||||
* @param field2 字段名2
|
||||
* @return 是否相等
|
||||
*/
|
||||
private boolean isFieldEqual(String field1, String field2) {
|
||||
if (field1 == null || field2 == null) {
|
||||
return field1 == field2;
|
||||
}
|
||||
|
||||
// 直接比较
|
||||
if (field1.equals(field2)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 驼峰转蛇形比较
|
||||
String field1Snake = camelToSnake(field1);
|
||||
String field2Snake = camelToSnake(field2);
|
||||
if (field1Snake.equals(field2Snake)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 蛇形转驼峰比较
|
||||
String field1Camel = snakeToCamel(field1);
|
||||
String field2Camel = snakeToCamel(field2);
|
||||
if (field1Camel.equals(field2Camel)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 交叉比较:field1驼峰转蛇形 vs field2原值,field2驼峰转蛇形 vs field1原值
|
||||
if (field1Snake.equals(field2) || field2Snake.equals(field1)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (field1Camel.equals(field2) || field2Camel.equals(field1)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// 初始化api客户端
|
||||
/* public static RestHighLevelClient client = new RestHighLevelClient(
|
||||
RestClient.builder(new HttpHost("localhost", 9200, "http")));*/
|
||||
@@ -128,25 +222,7 @@ public class ElasticArchiveDao {
|
||||
}
|
||||
|
||||
// 关键字搜索 指定匹配类型
|
||||
public AjaxJson searchProductList(String indexName,
|
||||
String type,
|
||||
String fieldName,
|
||||
String keyword,
|
||||
String fieldName2,
|
||||
String keyword2,
|
||||
String fieldName3,
|
||||
String keyword3,
|
||||
String fieldName4,
|
||||
String keyword4,
|
||||
String fieldName5,
|
||||
String keyword5,
|
||||
String fieldName6,
|
||||
String keyword6,
|
||||
Boolean bool,
|
||||
boolean ordinaryRole,
|
||||
String entityIds,
|
||||
Integer page,
|
||||
Integer limit) throws Exception {
|
||||
public AjaxJson searchProductList(String indexName, String type, String fieldName, String keyword, String fieldName2, String keyword2, String fieldName3, String keyword3, String fieldName4, String keyword4, String fieldName5, String keyword5, String fieldName6, String keyword6, Boolean bool, boolean ordinaryRole, String entityIds, Integer page, Integer limit) throws Exception {
|
||||
AjaxJson ajaxJson = new AjaxJson();
|
||||
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().trackTotalHits(true);
|
||||
|
||||
@@ -238,9 +314,69 @@ public class ElasticArchiveDao {
|
||||
SearchResponse searchResponse = client.search(searchRequest, options.build());
|
||||
SearchHits hits = searchResponse.getHits();
|
||||
List<Map> json = new ArrayList();
|
||||
for (SearchHit hit : hits.getHits()) {
|
||||
json.add(hit.getSourceAsMap());
|
||||
|
||||
OpenControlExample example0 = new OpenControlExample();
|
||||
example0.createCriteria().andOpenTypeEqualTo(1);
|
||||
List<OpenControl> openControls = openControlMapper.selectByExample(example0);
|
||||
|
||||
// 记录开放控制规则数量
|
||||
log.info("应用开放控制规则数量: {}", openControls.size());
|
||||
|
||||
// 打印所有开放控制规则详情
|
||||
for (OpenControl oc : openControls) {
|
||||
log.info("开放控制规则 - ID: {}, 字段: {}, 值: {}",
|
||||
oc.getId(), oc.getFieldEg(), oc.getValue());
|
||||
}
|
||||
|
||||
int processedHits = 0;
|
||||
int hiddenFieldsCount = 0;
|
||||
|
||||
for (SearchHit hit : hits.getHits()) {
|
||||
Map<String, Object> res = new HashMap<>();
|
||||
String hitId = hit.getId();
|
||||
Map<String, Object> sourceMap = hit.getSourceAsMap();
|
||||
|
||||
log.info("处理文档ID: {}, 文档内容: {}", hitId, sourceMap);
|
||||
|
||||
AtomicInteger currentHitHiddenFields = new AtomicInteger();
|
||||
|
||||
sourceMap.forEach((key, value) -> {
|
||||
boolean fieldHidden = false;
|
||||
for (OpenControl openControl : openControls) {
|
||||
String controlField = openControl.getFieldEg();
|
||||
String controlValue = openControl.getValue();
|
||||
|
||||
log.debug("检查字段 - 文档ID: {}, 字段名: {}, 字段值: {}, 控制字段: {}, 控制值: {}",
|
||||
hitId, key, value, controlField, controlValue);
|
||||
|
||||
// 使用字段名相等性检查(支持驼峰和蛇形命名转换)
|
||||
if (isFieldEqual(key, controlField) && value != null && value.equals(controlValue)) {
|
||||
res.put(key, "");
|
||||
fieldHidden = true;
|
||||
currentHitHiddenFields.getAndIncrement();
|
||||
// 记录字段被隐藏的详细信息
|
||||
log.info("字段隐藏处理 - 文档ID: {}, 字段名: {}, 字段值: {}, 控制规则ID: {}, 控制字段: {}, 控制值: {}",
|
||||
hitId, key, value, openControl.getId(), controlField, controlValue);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!fieldHidden) {
|
||||
res.put(key, value);
|
||||
}
|
||||
});
|
||||
|
||||
if (currentHitHiddenFields.get() > 0) {
|
||||
log.info("文档ID: {} 共隐藏了 {} 个字段", hitId, currentHitHiddenFields);
|
||||
res.put("controlIdentifier", "隐藏");
|
||||
}
|
||||
|
||||
json.add(res);
|
||||
processedHits++;
|
||||
}
|
||||
|
||||
log.info("开放控制处理完成 - 处理文档数: {}, 总隐藏字段数: {}", processedHits, hiddenFieldsCount);
|
||||
|
||||
|
||||
ajaxJson.put("json", json);
|
||||
ajaxJson.put("total", searchResponse.getHits().getTotalHits().value);
|
||||
|
||||
@@ -275,22 +411,22 @@ public class ElasticArchiveDao {
|
||||
}
|
||||
|
||||
|
||||
public AjaxJson advancedSearch(Map<String,Object> param){
|
||||
public AjaxJson advancedSearch(Map<String, Object> param) {
|
||||
AjaxJson json = new AjaxJson();
|
||||
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().trackTotalHits(true);
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
try {
|
||||
String indexName = StringUtil.formatMap(param,"indexName");
|
||||
String type = StringUtil.formatMap(param,"type");
|
||||
Integer page = Integer.parseInt(StringUtil.formatMap(param,"page"));
|
||||
Integer limit = Integer.parseInt(StringUtil.formatMap(param,"limit"));
|
||||
List<Map<String,Object>> conditions = (List<Map<String,Object>>) param.get("conditions");
|
||||
if(CollectionUtils.isNotEmpty(conditions)){
|
||||
for (Map<String,Object> item : conditions) {
|
||||
String relation = StringUtil.formatMap(item,"relation");
|
||||
String searchCondition = StringUtil.formatMap(item,"condition");
|
||||
String fetchName =StringUtil.formatMap(item,"tagName");
|
||||
String tagValue =StringUtil.formatMap(item,"tagValue");
|
||||
String indexName = StringUtil.formatMap(param, "indexName");
|
||||
String type = StringUtil.formatMap(param, "type");
|
||||
Integer page = Integer.parseInt(StringUtil.formatMap(param, "page"));
|
||||
Integer limit = Integer.parseInt(StringUtil.formatMap(param, "limit"));
|
||||
List<Map<String, Object>> conditions = (List<Map<String, Object>>) param.get("conditions");
|
||||
if (CollectionUtils.isNotEmpty(conditions)) {
|
||||
for (Map<String, Object> item : conditions) {
|
||||
String relation = StringUtil.formatMap(item, "relation");
|
||||
String searchCondition = StringUtil.formatMap(item, "condition");
|
||||
String fetchName = StringUtil.formatMap(item, "tagName");
|
||||
String tagValue = StringUtil.formatMap(item, "tagValue");
|
||||
BoolQueryBuilder currentConditionQuery = QueryBuilders.boolQuery();
|
||||
switch (searchCondition) {
|
||||
case "equal":
|
||||
@@ -322,7 +458,7 @@ public class ElasticArchiveDao {
|
||||
// 根据 relation 的值决定如何添加当前条件到主查询构建器中
|
||||
if ("or".equalsIgnoreCase(relation)) {
|
||||
boolQueryBuilder.should(currentConditionQuery);
|
||||
}else{
|
||||
} else {
|
||||
boolQueryBuilder.must(currentConditionQuery);
|
||||
}
|
||||
|
||||
@@ -353,7 +489,7 @@ public class ElasticArchiveDao {
|
||||
}
|
||||
json.put("json", data);
|
||||
json.put("total", searchResponse.getHits().getTotalHits().value);
|
||||
}else {
|
||||
} else {
|
||||
json = AjaxJson.returnExceptionInfo("请输出查询条件");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -369,10 +505,7 @@ public class ElasticArchiveDao {
|
||||
|
||||
|
||||
// 关键字搜索 指定匹配类型
|
||||
public AjaxJson searchAll(String indexName,
|
||||
String type,
|
||||
Integer page,
|
||||
Integer limit) throws IOException {
|
||||
public AjaxJson searchAll(String indexName, String type, Integer page, Integer limit) throws IOException {
|
||||
AjaxJson ajaxJson = new AjaxJson();
|
||||
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().trackTotalHits(true);
|
||||
|
||||
@@ -597,10 +730,7 @@ public class ElasticArchiveDao {
|
||||
|
||||
|
||||
//openType:0-开放,1-不开放
|
||||
public String selectOpenControlByEntity(Integer entityId,
|
||||
Integer type,
|
||||
String archiveNo,
|
||||
String tableName) {
|
||||
public String selectOpenControlByEntity(Integer entityId, Integer type, String archiveNo, String tableName) {
|
||||
String result = "隐藏";
|
||||
String conditionSql = "";
|
||||
if (type == 1) {
|
||||
@@ -608,6 +738,10 @@ public class ElasticArchiveDao {
|
||||
} else {
|
||||
conditionSql = " 1=1 and folder_no in ('" + archiveNo + "') ";
|
||||
}
|
||||
|
||||
log.info("开放控制检查开始 - entityId: {}, type: {}, archiveNo: {}, tableName: {}, conditionSql: {}",
|
||||
entityId, type, archiveNo, tableName, conditionSql);
|
||||
|
||||
//得到数据列表
|
||||
Map<String, Object> parasMap = new HashMap<String, Object>();
|
||||
parasMap.put("tableName", tableName);
|
||||
@@ -617,22 +751,66 @@ public class ElasticArchiveDao {
|
||||
//开放
|
||||
OpenControlExample example0 = new OpenControlExample();
|
||||
example0.setOrderByClause("sort");
|
||||
example0.createCriteria()
|
||||
.andEntityIdEqualTo(entityId)
|
||||
.andOpenTypeEqualTo(0);
|
||||
example0.createCriteria().andEntityIdEqualTo(entityId).andOpenTypeEqualTo(0);
|
||||
|
||||
List<OpenControl> openControlList0 = openControlMapper.selectByExample(example0);
|
||||
|
||||
log.info("查询到数据记录数: {}, 开放控制规则数: {}", dataList.size(), openControlList0.size());
|
||||
|
||||
// 打印所有数据记录的详细信息
|
||||
for (int i = 0; i < dataList.size(); i++) {
|
||||
Map<String, Object> map = dataList.get(i);
|
||||
log.info("数据记录[{}]: {}", i, map);
|
||||
}
|
||||
|
||||
// 打印所有开放控制规则
|
||||
for (OpenControl oc : openControlList0) {
|
||||
log.info("开放控制规则 - ID: {}, 字段: {}, 值: {}",
|
||||
oc.getId(), oc.getFieldEg(), oc.getValue());
|
||||
}
|
||||
|
||||
for (Map<String, Object> map : dataList) {
|
||||
log.debug("检查数据记录: {}", map);
|
||||
|
||||
for (OpenControl openControl : openControlList0) {
|
||||
if (map.get(openControl.getFieldEg()).equals(openControl.getValue())) {
|
||||
String controlField = openControl.getFieldEg();
|
||||
String controlValue = openControl.getValue();
|
||||
|
||||
// 遍历数据记录的所有字段,查找匹配的控制字段
|
||||
boolean fieldMatched = false;
|
||||
Object matchedFieldValue = null;
|
||||
String matchedFieldName = null;
|
||||
|
||||
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||
String dataField = entry.getKey();
|
||||
Object dataValue = entry.getValue();
|
||||
|
||||
// 使用字段名相等性检查(支持驼峰和蛇形命名转换)
|
||||
if (isFieldEqual(dataField, controlField)) {
|
||||
fieldMatched = true;
|
||||
matchedFieldValue = dataValue;
|
||||
matchedFieldName = dataField;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("比较字段 - 控制字段: {}, 匹配的数据字段: {}, 字段值: {}, 控制值: {}, 规则ID: {}",
|
||||
controlField, matchedFieldName, matchedFieldValue, controlValue, openControl.getId());
|
||||
|
||||
if (fieldMatched && matchedFieldValue != null && matchedFieldValue.equals(controlValue)) {
|
||||
result = "开放";
|
||||
log.info("开放控制匹配成功 - 数据记录: {}, 控制字段: {}, 匹配字段: {}, 字段值: {}, 控制值: {}, 规则ID: {}",
|
||||
map, controlField, matchedFieldName, matchedFieldValue, controlValue, openControl.getId());
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ("开放".equals(result)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
log.info("开放控制检查结果: {} (entityId: {}, archiveNo: {})", result, entityId, archiveNo);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -640,12 +818,7 @@ public class ElasticArchiveDao {
|
||||
try {
|
||||
String indexName0 = "xinchuang";
|
||||
String type0 = "dangan";
|
||||
AjaxJson json = searchAll(
|
||||
indexName0,
|
||||
type0,
|
||||
1,
|
||||
2000000
|
||||
);
|
||||
AjaxJson json = searchAll(indexName0, type0, 1, 2000000);
|
||||
|
||||
List<Map> list = (List<Map>) json.getBody().get("json");
|
||||
for (Map map : list) {
|
||||
@@ -655,12 +828,7 @@ public class ElasticArchiveDao {
|
||||
String archiveNo = StringUtil.formatMap(map, "archiveNo");
|
||||
String tableName = StringUtil.formatMap(map, "tableName");
|
||||
|
||||
String kaifang = selectOpenControlByEntity(
|
||||
entityId,
|
||||
type,
|
||||
archiveNo,
|
||||
tableName
|
||||
);
|
||||
String kaifang = selectOpenControlByEntity(entityId, type, archiveNo, tableName);
|
||||
|
||||
MyArchives product = new MyArchives();
|
||||
product.setOpen(kaifang);
|
||||
|
||||
Reference in New Issue
Block a user