From 013ce2dc871f3a0b63647a95132f75c343f42656 Mon Sep 17 00:00:00 2001 From: liao Date: Fri, 25 Aug 2023 13:58:17 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=A4=9A=E6=97=A5=E6=9C=9F=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E4=B8=8E=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/LawsCommonServiceImpl.java | 209 +++++++++++++++--- 1 file changed, 177 insertions(+), 32 deletions(-) diff --git a/jero-boot-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java b/jero-boot-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java index 64d88448..9571b045 100644 --- a/jero-boot-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java +++ b/jero-boot-modules/src/main/java/com/jero/modules/laws/common/service/impl/LawsCommonServiceImpl.java @@ -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 fieldList = lawsCommonMapper.getFieldList(tableName); + // 筛选出查询列表显示的字段(不传id查列表) + List fieldListSelect = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue() + .equals(lawsTag.getIsShowList().toString())).collect(Collectors.toList()); + // 筛选出用于查询的字段 + List 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> 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> 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 map, List fieldList) { + for (Map.Entry 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 fieldList) { + for (LawsTag lawsTag : fieldList) { + if (key.equals(lawsTag.getDbFieldName())) { + if (LawsFieldTypeEnum.DATE_MANY.getValue().equals(lawsTag.getFieldShowType())) { + // 如果是多日期格式,做去重处理 + List 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 getByIdAndModule(String id, String module) { // 获取要操作的表名 String tableName = FileTableIdEnum.getTableName(module); + + // 获取全部字段 + List 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 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 valueList = Arrays.asList(value.split(",")) + .stream().distinct().collect(Collectors.toList()); + + if (valueList.size() == 1) { + // 如果只有一个日期值,则复制成两个,用于查询和排序 + return value + "," + value; + } + List dateList = valueList.stream() + .map(LocalDate::parse) // 解析日期字符串为 LocalDate 对象 + .sorted() // 按照日期大小进行排序 + .collect(Collectors.toList()); + + // 将排序后的 LocalDate 对象转换为字符串 + List 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 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 fieldList) { // 筛选获取日期类型字段 List 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 parameterMap, String tableName) { - // 获取全部字段 - List fieldList = lawsCommonMapper.getFieldList(tableName); - // 筛选出用于查询的字段 - fieldList = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue() - .equals(lawsTag.getIsQuery().toString())).collect(Collectors.toList()); + private String getSelectCondition(Map parameterMap, List 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 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 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 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 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 + "'),'%')"); + } + } }