add 多日期排序与查询

This commit is contained in:
liao
2023-08-25 13:58:17 +08:00
parent c7743f9abb
commit 013ce2dc87
@@ -23,6 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
@@ -50,21 +51,74 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
String module = parameterMap.get("module").toString();
// 获取要操作的表名
String tableName = FileTableIdEnum.getTableName(module);
// 查询全部字段列表
List<LawsTag> fieldList = lawsCommonMapper.getFieldList(tableName);
// 筛选出查询列表显示的字段(不传id查列表)
List<LawsTag> fieldListSelect = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue()
.equals(lawsTag.getIsShowList().toString())).collect(Collectors.toList());
// 筛选出用于查询的字段
List<LawsTag> fieldListCondition = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue()
.equals(lawsTag.getIsQuery().toString())).collect(Collectors.toList());
// 获取分页参数
Integer pageNo = Integer.valueOf(parameterMap.get("pageNo").toString());
Integer pageSize = Integer.valueOf(parameterMap.get("pageSize").toString());
IPage<Map<String,Object>> page = new Page<>(pageNo, pageSize);
// 封装查询返回字段
String selectField = getSelectField(tableName, null);
String selectField = getSelectField(fieldListSelect);
// 封装查询条件
String selectCondition = getSelectCondition(parameterMap, tableName);
String selectCondition = getSelectCondition(parameterMap, fieldListCondition);
// 获取分页查询结果
IPage<Map<String, Object>> infoPage = lawsCommonMapper.getCommonPage(page, tableName, selectField, selectCondition);
// 对一些字段做翻译(如文件ids、树型选择ids)
infoPage.getRecords().forEach(map -> {
// 对返回的数据做处理
processResultMap(map, fieldListSelect);
});
// 待处理
return infoPage;
}
/**
* @Author: liao
* @Date: 2023/8/25 11:52
* @Description: 对返回的数据做处理
**/
private void processResultMap(Map<String, Object> map, List<LawsTag> fieldList) {
for (Map.Entry<String, Object> entryMap : map.entrySet()) {
String key = entryMap.getKey();
String value = "";
if (!Objects.isNull(entryMap.getValue())) {
value = entryMap.getValue().toString();
}
// 如果value不为null和"",对返回结果做处理
if (StringUtils.isNotBlank(value)) {
// 对返回的字段值做处理
processResultFieldValue(key, value, fieldList);
}
}
}
/**
* @Author: liao
* @Date: 2023/8/25 11:55
* @Description: 对返回的字段值做处理
**/
private String processResultFieldValue(String key, String value, List<LawsTag> fieldList) {
for (LawsTag lawsTag : fieldList) {
if (key.equals(lawsTag.getDbFieldName())) {
if (LawsFieldTypeEnum.DATE_MANY.getValue().equals(lawsTag.getFieldShowType())) {
// 如果是多日期格式,做去重处理
List<String> valueList = Arrays.asList(value.split(","))
.stream().distinct().collect(Collectors.toList());
value = StringUtils.join(valueList, ",");
}
}
}
return value;
}
/**
* @Author: liao
* @Date: 2023/8/24 9:09
@@ -273,12 +327,19 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
public Map<String, Object> getByIdAndModule(String id, String module) {
// 获取要操作的表名
String tableName = FileTableIdEnum.getTableName(module);
// 获取全部字段
List<LawsTag> fieldList = lawsCommonMapper.getFieldList(tableName);
// 筛选表单显示的字段(传id查表单)
fieldList = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue()
.equals(lawsTag.getIsShowForm().toString())).collect(Collectors.toList());
// 封装查询显示的字段
String selectField = getSelectField(tableName, id);
String selectField = getSelectField(fieldList);
// 封装查询条件
Map<String, Object> resultMap = lawsCommonMapper.getByIdAndModule(tableName, selectField, id);
// 对一些字段做翻译(如文件ids、树型选择ids)
// ...
// 对返回的字段做处理
processResultMap(resultMap, fieldList);
return resultMap;
}
@@ -321,6 +382,37 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
}
}
/**
* @Author: liao
* @Date: 2023/8/25 11:22
* @Description: 处理多日期类型值
**/
private String processDateManyTypeValue(String value, String fieldShowType) {
// 如果不是多日期则直接返回
if (!LawsFieldTypeEnum.DATE_MANY.getValue().equals(fieldShowType)) {
return value;
}
// 对多日期进行去重
List<String> valueList = Arrays.asList(value.split(","))
.stream().distinct().collect(Collectors.toList());
if (valueList.size() == 1) {
// 如果只有一个日期值,则复制成两个,用于查询和排序
return value + "," + value;
}
List<LocalDate> dateList = valueList.stream()
.map(LocalDate::parse) // 解析日期字符串为 LocalDate 对象
.sorted() // 按照日期大小进行排序
.collect(Collectors.toList());
// 将排序后的 LocalDate 对象转换为字符串
List<String> sortedDateStrList = dateList.stream()
.map(LocalDate::toString)
.collect(Collectors.toList());
return String.join(",", sortedDateStrList);
}
/**
* @Author: liao
* @Date: 2023/8/24 9:53
@@ -346,6 +438,8 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
// 如果是单日期格式,做格式处理
value = "str_to_date('" + value + "','%Y-%m-%d %H:%i:%s')";
} else {
// 如果是多日期格式做处理
value = processDateManyTypeValue(value, fieldShowType);
// 字符串做处理
value = "'" + value + "'";
}
@@ -373,18 +467,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
* @Date: 2023/8/23 17:10
* @Description: 封装查询返回字段
**/
private String getSelectField(String tableName, String id) {
// 获取全部字段
List<LawsTag> fieldList = lawsCommonMapper.getFieldList(tableName);
if (StringUtils.isBlank(id)) {
// 筛选出查询列表显示的字段(不传id查列表)
fieldList = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue()
.equals(lawsTag.getIsShowList().toString())).collect(Collectors.toList());
} else {
// 筛选表单显示的字段(传id查表单)
fieldList = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue()
.equals(lawsTag.getIsShowForm().toString())).collect(Collectors.toList());
}
private String getSelectField(List<LawsTag> fieldList) {
// 筛选获取日期类型字段
List<String> dateFieldList = fieldList.stream().filter(lawsTag -> LawsFieldTypeEnum.DATE_SINGLE.getValue()
.equals(lawsTag.getFieldShowType())).collect(Collectors.toList())
@@ -417,12 +500,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
* @Date: 2023/8/23 17:16
* @Description: 封装查询条件
**/
private String getSelectCondition(Map<String, Object> parameterMap, String tableName) {
// 获取全部字段
List<LawsTag> fieldList = lawsCommonMapper.getFieldList(tableName);
// 筛选出用于查询的字段
fieldList = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue()
.equals(lawsTag.getIsQuery().toString())).collect(Collectors.toList());
private String getSelectCondition(Map<String, Object> parameterMap, List<LawsTag> fieldList) {
//查询条件
StringBuilder selectCondition = new StringBuilder("where 1 = 1");
// 删除pageNo和pageSize,不参与条件查询
@@ -441,9 +519,18 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
}
}
//处理列表表头排序
if (StringUtils.isNotBlank((String) parameterMap.get("orderByField"))) {
selectCondition.append(" order by " + parameterMap.get("orderByField"));
orderBySort(parameterMap, selectCondition);
String orderByField = (String) parameterMap.get("orderByField");
if (StringUtils.isNotBlank(orderByField)) {
// 排序字段的类型
String fieldShowType = fieldList.stream().filter(lawsTag -> orderByField
.equals(lawsTag.getDbFieldName())).collect(Collectors.toList()).get(0).getFieldShowType();
if (LawsFieldTypeEnum.DATE_MANY.getValue().equals(fieldShowType)) {
// 多日期的排序逻辑不一样
orderBySortDateMany(parameterMap, selectCondition);
} else {
selectCondition.append(" order by " + parameterMap.get("orderByField"));
orderBySort(parameterMap, selectCondition);
}
} else {
selectCondition.append(" order by create_time desc");
}
@@ -455,31 +542,64 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
* @Date: 2023/8/22 11:41
* @Description: 给拼装的查询条件排序
**/
private void orderBySort(Map<String, Object> parameter, StringBuilder conditionSb) {
if ("1".equals(parameter.get("orderBy"))) {
conditionSb.append(" asc");//正序
} else if ("2".equals(parameter.get("orderBy"))) {
conditionSb.append(" desc");//倒序
private void orderBySort(Map<String, Object> parameterMap, StringBuilder selectCondition) {
if ("1".equals(parameterMap.get("orderBy"))) {
selectCondition.append(" asc"); //正序
} else if ("2".equals(parameterMap.get("orderBy"))) {
selectCondition.append(" desc"); //倒序
}
}
/**
* @Author: liao
* @Date: 2023/8/22 11:41
* @Description: 给拼装的查询条件排序(多日期)
**/
private void orderBySortDateMany(Map<String, Object> parameterMap, StringBuilder selectCondition) {
if ("1".equals(parameterMap.get("orderBy"))) {
selectCondition.append(" order by STR_TO_DATE(SUBSTRING_INDEX(" +
parameterMap.get("orderByField") + ", ',', 1), '%Y-%m-%d')");
selectCondition.append(" asc"); //正序
} else if ("2".equals(parameterMap.get("orderBy"))) {
selectCondition.append(" order by STR_TO_DATE(SUBSTRING_INDEX(" +
parameterMap.get("orderByField") + ", ',', -1), '%Y-%m-%d')");
selectCondition.append(" desc"); //倒序
}
}
/**
* @Author: liao
* @Date: 2023/8/25 10:58
* @Description: 拼接查询条件
**/
private void jointSelectCondition(List<LawsTag> fieldList, StringBuilder selectCondition, String key, String value) {
for (LawsTag lawsTag : fieldList) {
String dbFieldName = lawsTag.getDbFieldName();
if (Objects.equals(key, dbFieldName)) {
String fieldShowType = lawsTag.getFieldShowType();
if (FieldCommon.TYPE_LIKE.contains(fieldShowType)) {
// 模糊查询拼接
processLikeTypeCondition(selectCondition, key, value);
} else if (FieldCommon.TYPE_LIKE_IN.contains(fieldShowType)) {
// 模糊查询+in查询
processLikeInTypeCondition(selectCondition, key, value);
} else if (LawsFieldTypeEnum.DATE_SINGLE.getValue().equals(fieldShowType)) {
// 单日期查询
processDateSingleTypeCondition(selectCondition, key, value);
}else if (LawsFieldTypeEnum.DATE_MANY.getValue().equals(fieldShowType)) {
// 多日期查询
processDateManyTypeCondition(selectCondition, key, value);
}
// 处理其他类型
}
}
}
/**
* @Author: liao
* @Date: 2023/8/25 10:59
* @Description: 模糊查询拼接
**/
private void processLikeTypeCondition(StringBuilder selectCondition, String key, String value) {
if (value.contains("%")) {
value = value.replace("%", "/%");
@@ -487,6 +607,11 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
selectCondition.append(" and " + key + " like concat(concat('%','" + value + "'),'%')");
}
/**
* @Author: liao
* @Date: 2023/8/25 11:00
* @Description: 模糊查询+in查询
**/
private void processLikeInTypeCondition(StringBuilder selectCondition, String key, String value) {
StringBuilder valueSb = new StringBuilder();
StringBuilder condition = new StringBuilder();
@@ -499,6 +624,11 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
selectCondition.append(" and (").append(key).append(" in(").append(substring).append(")").append(condition);
}
/**
* @Author: liao
* @Date: 2023/8/25 11:00
* @Description: 单日期查询
**/
private void processDateSingleTypeCondition(StringBuilder selectCondition, String key, String value) {
if (value.contains(",")) {
String startDate = value.split(",")[0];
@@ -510,5 +640,20 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
}
}
/**
* @Author: liao
* @Date: 2023/8/25 11:01
* @Description: 多日期查询
**/
private void processDateManyTypeCondition(StringBuilder selectCondition, String key, String value) {
if (value.contains(",")) {
String startDate = value.split(",")[0];
String endDate = value.split(",")[1];
selectCondition.append(" and date_format(SUBSTRING_INDEX(").append(key).append(", ',', 1),'%Y-%m-%d') >= '").append(startDate)
.append("' and date_format(SUBSTRING_INDEX(").append(key).append(", ',', -1),'%Y-%m-%d') <= '").append(endDate).append("'");
} else {
selectCondition.append(" and " + key + " like concat(concat('%','" + value + "'),'%')");
}
}
}