add 通用分页查询逻辑

This commit is contained in:
liao
2023-08-23 18:32:38 +08:00
parent 0c96ba1859
commit 94ff888a4f
7 changed files with 286 additions and 14 deletions
@@ -0,0 +1,31 @@
package com.jero.modules.laws.common.mapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jero.modules.tag.entity.LawsTag;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.data.repository.query.Param;
import java.util.List;
import java.util.Map;
/**
* @Author: liao
* @Date: 2023/8/23 16:49
* @Description: 通用
*/
@Mapper
public interface CommonMapper {
/**
* @Author: liao
* @Date: 2023/8/23 17:02
* @Description: 根据表名获取该表全部字段
**/
List<LawsTag> getFieldList(@Param("tableName") String tableName);
IPage<Map<String,Object>> getInfoPage(@org.apache.ibatis.annotations.Param("page") IPage<Map<String,Object>> page,
@org.apache.ibatis.annotations.Param("table") String table,
@org.apache.ibatis.annotations.Param("field") String field,
@org.apache.ibatis.annotations.Param("condition") String condition);
}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jero.modules.laws.common.mapper.CommonMapper">
<select id="getFieldList" resultType="com.jero.modules.tag.entity.LawsTag">
select *
from laws_tag tag
left join laws_area area on tag.show_area = area.id
where tag.table_name = #{tableName}
and tag.is_delete = 0
order by area.sort asc, tag.order_num asc, tag.create_time asc
</select>
<select id="getInfoPage" resultType="java.util.Map">
select ${field} from ${table} ${condition}
</select>
</mapper>
@@ -0,0 +1,21 @@
package com.jero.modules.laws.common.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.util.Map;
/**
* @Author: liao
* @Date: 2023/8/23 16:54
* @Description: 通用
*/
public interface ICommonService {
/**
* @Author: liao
* @Date: 2023/8/23 16:56
* @Description: 通用分页查询
**/
IPage<Map<String,Object>> getCommonPage(Map<String, Object> parameterMap);
}
@@ -0,0 +1,204 @@
package com.jero.modules.laws.common.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jero.common.constant.enums.LanguageEnum;
import com.jero.common.constant.enums.YesOrNoEnum;
import com.jero.common.util.MessageUtils;
import com.jero.modules.document.enums.FieldTypeEnum;
import com.jero.modules.laws.common.mapper.CommonMapper;
import com.jero.modules.laws.common.service.ICommonService;
import com.jero.modules.tag.entity.LawsTag;
import com.jero.modules.tag.enums.FileTableIdEnum;
import com.spire.ms.System.Collections.ArrayList;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @Author: liao
* @Date: 2023/8/23 16:54
* @Description: 通用
*/
@Component
public class CommonServiceImpl implements ICommonService {
@Autowired
private CommonMapper commonMapper;
/**
* @Author: liao
* @Date: 2023/8/23 16:57
* @Description: 通用分页查询
**/
@Override
public IPage<Map<String, Object>> getCommonPage(Map<String, Object> parameterMap) {
// 获取操作模块
String module = parameterMap.get("module").toString();
// 获取要操作的表名
String tableName = FileTableIdEnum.getTableName(module);
// 获取操作语言
LanguageEnum language = MessageUtils.getLanguage();
// 获取分页参数
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(parameterMap, tableName);
// 封装查询条件
String selectCondition = getSelectCondition(parameterMap, tableName);
// 获取分页查询结果
IPage<Map<String, Object>> infoPage = commonMapper.getInfoPage(page, tableName, selectField, selectCondition);
// 对一些字段做翻译(如文件ids、树型选择ids)
return infoPage;
}
/**
* @Author: liao
* @Date: 2023/8/23 17:10
* @Description: 封装查询返回字段
**/
private String getSelectField(Map<String, Object> parameterMap, String tableName) {
// 获取全部字段
List<LawsTag> fieldList = commonMapper.getFieldList(tableName);
// 筛选出查询列表显示的字段
fieldList = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue()
.equals(lawsTag.getIsShowList().toString())).collect(Collectors.toList());
// 筛选获取日期类型字段
List<String> dateFieldList = fieldList.stream().filter(lawsTag -> FieldTypeEnum.DATE_SINGLE
.equals(lawsTag.getFieldShowType())).collect(Collectors.toList())
.stream().map(LawsTag::getDbFieldName).collect(Collectors.toList());
// 获取全部的字段列表
List<String> tempFieldList = fieldList.stream().map(LawsTag::getDbFieldName).collect(Collectors.toList());
// 创建一个新的用于返回的字段列表
List<String> selectFieldList = new ArrayList();
// 查询主键
selectFieldList.add("id");
tempFieldList.forEach(tempField -> {
String endField;
// 对日期格式和普通格式做处理
if (dateFieldList.contains(tempField)) {
// 如果是日期格式
String newField = "date_format(" + tempField + ", '%Y-%m-%d')";
endField = "case when " + newField + " is null then \"\" else " + newField + " end " + tempField;
selectFieldList.add(endField);
} else {
// 如果是普通格式
endField = "case when " + tempField + " is null then \"\" else " + tempField + " end " + tempField;
selectFieldList.add(endField);
}
});
return StringUtils.join(selectFieldList, ",");
}
/**
* @Author: liao
* @Date: 2023/8/23 17:16
* @Description: 封装查询条件
**/
private String getSelectCondition(Map<String, Object> parameterMap, String tableName) {
// 获取全部字段
List<LawsTag> fieldList = commonMapper.getFieldList(tableName);
// 筛选出用于查询的字段
fieldList = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue()
.equals(lawsTag.getIsQuery().toString())).collect(Collectors.toList());
//查询条件
StringBuilder selectCondition = new StringBuilder("where 1 = 1");
// 删除pageNo和pageSize,不参与条件查询
parameterMap.remove("pageNo");
parameterMap.remove("pageSize");
// 查询条件处理
for (Map.Entry<String, Object> entryMap : parameterMap.entrySet()) {
String key = entryMap.getKey();
String value = "";
if (!Objects.isNull(entryMap.getValue())) {
value = entryMap.getValue().toString();
}
// 如果value不为null和"",则拼接查询条件
if (StringUtils.isNotBlank(value)) {
jointSelectCondition(fieldList, selectCondition, key, value);
}
}
//处理列表表头排序
if (StringUtils.isNotBlank((String) parameterMap.get("orderByField"))) {
selectCondition.append(" order by " + parameterMap.get("orderByField"));
orderBySort(parameterMap, selectCondition);
} else {
selectCondition.append(" order by create_time desc");
}
return selectCondition.toString();
}
/**
* @Author: liao
* @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");//倒序
}
}
/**
* @Author: liao
* @Date: 2023/8/23 17:57
* @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 (FieldTypeEnum.TEXT_STRING.getValue().equals(fieldShowType)
|| FieldTypeEnum.TEXT_NUMBER.getValue().equals(fieldShowType)
|| FieldTypeEnum.STANDARD.getValue().equals(fieldShowType)) {
if (value.contains("%")) {
// 字段内容可能包含%,是转义字符,做一下处理
value = value.replace("%", "/%");
}
selectCondition.append(" and " + key + " like concat(concat('%','" + value + "'),'%')");
} else if (FieldTypeEnum.PULL_SINGLE.getValue().equals(fieldShowType)
|| FieldTypeEnum.PULL_MORE.getValue().equals(fieldShowType)
|| FieldTypeEnum.TREE.getValue().equals(fieldShowType)) {
StringBuilder valueSb = new StringBuilder();
StringBuilder condition = new StringBuilder();
for (String valueTemp : value.split(",")) {
valueSb.append("'" + valueTemp + "',");
condition.append(" or " + key + " like binary concat(concat('%','" + valueTemp + "'),'%')");
}
condition.append(")");
String substring = valueSb.substring(0, valueSb.length() - 1);
//下拉单选或下拉多选
selectCondition.append(" and (" + key + " in(" + substring + ")" + condition);
} else if (FieldTypeEnum.DATE_SINGLE.getValue().equals(fieldShowType)) {
//日期(区分单日期还时间范围)
if (value.contains(",")) {
selectCondition.append(" and " + "date_format(" + key + ",'%Y-%m-%d') >= '" + value.split(",")[0] + "'"
+ " and " + "date_format(" + key + ",'%Y-%m-%d') <= '" + value.split(",")[1] + "'");
} else {
selectCondition.append(" and " + "date_format(" + key + ",'%Y-%m-%d') = '" + value + "'");
}
} else if (FieldTypeEnum.PERSON.getValue().equals(fieldShowType)) {
if (value.contains("%")) {
// 字段内容可能包含%,是转义字符,做一下处理
value = value.replace("%", "/%");
}
selectCondition.append(" and " + key + " like concat(concat('%','" + value + "'),'%')");
}
}
}
}
}
@@ -1,13 +0,0 @@
package com.jero.modules.laws.component;
import org.springframework.stereotype.Component;
/**
* @Author: liao
* @Date: 2023/8/22 16:25
* @Description: 查询组件
*/
@Component
public class SearchComponent {
}
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jero.common.api.vo.Result;
import com.jero.common.aspect.annotation.AutoLog;
import com.jero.common.system.base.controller.JeroController;
import com.jero.modules.laws.common.service.ICommonService;
import com.jero.modules.laws.regulation.entity.LawsBasicInfo;
import com.jero.modules.laws.regulation.service.ILawsBasicInfoService;
import io.swagger.annotations.Api;
@@ -67,4 +68,12 @@ public class LawsBasicInfoController extends JeroController<LawsBasicInfo, ILaws
return Result.OK(list);
}
@Autowired
private ICommonService commonService;
@PostMapping(value = "/getCommon")
public Result<IPage<Map<String,Object>>> getCommon(@RequestBody Map<String,Object> parameter) {
return Result.OK(commonService.getCommonPage(parameter));
}
}
@@ -419,7 +419,9 @@ public class LawsBasicInfoServiceImpl extends ServiceImpl<LawsBasicInfoMapper, L
if (key.equals(fieldName)) {
//字段类型(判断是输入框还是下拉,等等)
String fieldType = (String) map.get("field_show_type");//字段类型
if (FieldTypeEnum.TEXT_STRING.getValue().equals(fieldType) || FieldTypeEnum.TEXT_NUMBER.getValue().equals(fieldType)|| FieldTypeEnum.STANDARD.getValue().equals(fieldType)) {
if (FieldTypeEnum.TEXT_STRING.getValue().equals(fieldType)
|| FieldTypeEnum.TEXT_NUMBER.getValue().equals(fieldType)
|| FieldTypeEnum.STANDARD.getValue().equals(fieldType)) {
if (value.contains("%")) {
// 字段内容可能包含%,是转义字符,做一下处理
value = value.replace("%", "/%");