Commit ca3eb3612859e48b0255e15aabefc5b75726c788
1 parent
dcbf3fe9
养护计划优化提交
Showing
5 changed files
with
108 additions
and
15 deletions
urbanops-framework/urbanops-spring-boot-starter-mybatis/src/main/java/com/zteits/urbanops/framework/mybatis/core/type/StringArrayToListTypeHandler.java
0 → 100644
| 1 | +package com.zteits.urbanops.framework.mybatis.core.type; | |
| 2 | +import org.apache.ibatis.type.BaseTypeHandler; | |
| 3 | +import org.apache.ibatis.type.JdbcType; | |
| 4 | + | |
| 5 | +import java.sql.*; | |
| 6 | +import java.util.ArrayList; | |
| 7 | +import java.util.Collections; | |
| 8 | +import java.util.List; | |
| 9 | +import java.util.regex.Pattern; | |
| 10 | + | |
| 11 | +public class StringArrayToListTypeHandler extends BaseTypeHandler<List<String>> { | |
| 12 | + | |
| 13 | + // 匹配引号(单或双) | |
| 14 | + private static final Pattern SPLIT_PATTERN = Pattern.compile(",\\s*"); | |
| 15 | + private static final Pattern TRIM_QUOTES = Pattern.compile("^(['\"])(.*)\\1$"); | |
| 16 | + | |
| 17 | + @Override | |
| 18 | + public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException { | |
| 19 | + if (parameter == null || parameter.isEmpty()) { | |
| 20 | + ps.setString(i, "[]"); | |
| 21 | + return; | |
| 22 | + } | |
| 23 | + // 统一用双引号序列化为 JSON 格式写入 | |
| 24 | + StringBuilder sb = new StringBuilder(); | |
| 25 | + sb.append("["); | |
| 26 | + for (int idx = 0; idx < parameter.size(); idx++) { | |
| 27 | + if (idx > 0) sb.append(","); | |
| 28 | + sb.append("\"").append(parameter.get(idx)).append("\""); | |
| 29 | + } | |
| 30 | + sb.append("]"); | |
| 31 | + ps.setString(i, sb.toString()); | |
| 32 | + } | |
| 33 | + | |
| 34 | + @Override | |
| 35 | + public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException { | |
| 36 | + return parse(rs.getString(columnName)); | |
| 37 | + } | |
| 38 | + | |
| 39 | + @Override | |
| 40 | + public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException { | |
| 41 | + return parse(rs.getString(columnIndex)); | |
| 42 | + } | |
| 43 | + | |
| 44 | + @Override | |
| 45 | + public List<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { | |
| 46 | + return parse(cs.getString(columnIndex)); | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * 解析 ['a','b'] 或 ["x","y"] 等格式 | |
| 51 | + */ | |
| 52 | + private List<String> parse(String value) { | |
| 53 | + if (value == null || (value = value.trim()).length() == 0 || "null".equalsIgnoreCase(value)) { | |
| 54 | + return Collections.emptyList(); | |
| 55 | + } | |
| 56 | + | |
| 57 | + // 去掉首尾的 [ 和 ] | |
| 58 | + if (!value.startsWith("[") || !value.endsWith("]")) { | |
| 59 | + return Collections.emptyList(); // 非法格式 | |
| 60 | + } | |
| 61 | + value = value.substring(1, value.length() - 1).trim(); | |
| 62 | + | |
| 63 | + // 空数组 | |
| 64 | + if (value.isEmpty()) { | |
| 65 | + return Collections.emptyList(); | |
| 66 | + } | |
| 67 | + | |
| 68 | + List<String> result = new ArrayList<>(); | |
| 69 | + String[] items = SPLIT_PATTERN.split(value); | |
| 70 | + | |
| 71 | + for (String item : items) { | |
| 72 | + item = item.trim(); | |
| 73 | + if (item.length() == 0) continue; | |
| 74 | + | |
| 75 | + // 移除首尾的单引号或双引号 | |
| 76 | + if ((item.startsWith("'") && item.endsWith("'")) || | |
| 77 | + (item.startsWith("\"") && item.endsWith("\""))) { | |
| 78 | + item = item.substring(1, item.length() - 1); | |
| 79 | + } | |
| 80 | + | |
| 81 | + result.add(item); | |
| 82 | + } | |
| 83 | + | |
| 84 | + return result; | |
| 85 | + } | |
| 86 | +} | ... | ... |
urbanops-module-garden/src/main/java/com/zteits/urbanops/module/garden/controller/admin/inspectionplan/vo/InspectionPlanCommitRespVO.java
| 1 | 1 | package com.zteits.urbanops.module.garden.controller.admin.inspectionplan.vo; |
| 2 | 2 | |
| 3 | +import com.baomidou.mybatisplus.annotation.TableField; | |
| 4 | +import com.zteits.urbanops.framework.mybatis.core.type.StringListTypeHandler; | |
| 3 | 5 | import io.swagger.v3.oas.annotations.media.Schema; |
| 4 | 6 | import lombok.*; |
| 5 | 7 | import java.util.*; |
| ... | ... | @@ -57,11 +59,8 @@ public class InspectionPlanCommitRespVO { |
| 57 | 59 | |
| 58 | 60 | @Schema(description = "巡检照片") |
| 59 | 61 | @ExcelProperty("巡检照片") |
| 60 | - private String img; | |
| 61 | - | |
| 62 | - @Schema(description = "巡检照片") | |
| 63 | - @ExcelProperty("巡检照片") | |
| 64 | - private List<String> imgList; | |
| 62 | + @TableField(typeHandler = StringListTypeHandler.class) | |
| 63 | + private List<String> img; | |
| 65 | 64 | |
| 66 | 65 | @Schema(description = "巡检结果: 1:正常;2:异常") |
| 67 | 66 | @ExcelProperty("巡检结果: 1:正常;2:异常") | ... | ... |
urbanops-module-garden/src/main/java/com/zteits/urbanops/module/garden/dal/dataobject/inspectionplan/InspectionPlanCommitDO.java
| 1 | 1 | package com.zteits.urbanops.module.garden.dal.dataobject.inspectionplan; |
| 2 | 2 | |
| 3 | +import com.zteits.urbanops.framework.mybatis.core.type.StringListTypeHandler; | |
| 3 | 4 | import lombok.*; |
| 4 | 5 | import java.time.LocalDateTime; |
| 6 | +import java.util.List; | |
| 7 | + | |
| 5 | 8 | import com.baomidou.mybatisplus.annotation.*; |
| 6 | 9 | import com.zteits.urbanops.framework.mybatis.core.dataobject.BaseDO; |
| 7 | 10 | |
| ... | ... | @@ -61,7 +64,8 @@ public class InspectionPlanCommitDO extends BaseDO { |
| 61 | 64 | /** |
| 62 | 65 | * 巡检照片 |
| 63 | 66 | */ |
| 64 | - private String img; | |
| 67 | + @TableField(typeHandler = StringListTypeHandler.class) | |
| 68 | + private List<String> img; | |
| 65 | 69 | /** |
| 66 | 70 | * 巡检结果: 1:正常;2:异常 |
| 67 | 71 | */ | ... | ... |
urbanops-module-garden/src/main/java/com/zteits/urbanops/module/garden/service/inspectionplan/InspectionPlanCommitServiceImpl.java
| ... | ... | @@ -132,7 +132,7 @@ public class InspectionPlanCommitServiceImpl implements InspectionPlanCommitServ |
| 132 | 132 | .batchNo(inspectionPlanDO.getBatchNo()) |
| 133 | 133 | .planNo(createReqVO.getPlanNo()).busiLine(inspectionPlanDO.getBusiLine()) |
| 134 | 134 | .companyId(inspectionPlanDO.getCompanyId()).deptId(inspectionPlanDO.getDeptId()) |
| 135 | - .roadId(inspectionPlanDO.getRoadId()).img(JSONObject.toJSONString(createReqVO.getImgList())) | |
| 135 | + .roadId(inspectionPlanDO.getRoadId()).img(createReqVO.getImgList()) | |
| 136 | 136 | .inspectionState(createReqVO.getInspectionState()).transState(createReqVO.getTransState()) |
| 137 | 137 | .transWorkNo(createReqVO.getTransWorkNo()) |
| 138 | 138 | .userId(userId).userName(userName) |
| ... | ... | @@ -195,7 +195,7 @@ public class InspectionPlanCommitServiceImpl implements InspectionPlanCommitServ |
| 195 | 195 | IPage<InspectionPlanCommitRespVO> page = inspectionPlanCommitMapper.selectForPage(mpPage, pageReqVO); |
| 196 | 196 | addDataInspectionPlan(page.getRecords()); |
| 197 | 197 | // 转换返回 |
| 198 | - return new PageResult<>(imgProcess(page.getRecords()), page.getTotal()); | |
| 198 | + return new PageResult<>(page.getRecords(), page.getTotal()); | |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | @Override |
| ... | ... | @@ -242,11 +242,11 @@ public class InspectionPlanCommitServiceImpl implements InspectionPlanCommitServ |
| 242 | 242 | }); |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | - private List<InspectionPlanCommitRespVO> imgProcess(List<InspectionPlanCommitRespVO> list) { | |
| 246 | - list.forEach(e -> { | |
| 247 | - e.setImgList(StrUtils.imgProcess(e.getImg(), "")); | |
| 248 | - }); | |
| 249 | - return list; | |
| 250 | - } | |
| 245 | +// private List<InspectionPlanCommitRespVO> imgProcess(List<InspectionPlanCommitRespVO> list) { | |
| 246 | +// list.forEach(e -> { | |
| 247 | +// e.setImgList(StrUtils.imgProcess(e.getImg(), "")); | |
| 248 | +// }); | |
| 249 | +// return list; | |
| 250 | +// } | |
| 251 | 251 | |
| 252 | 252 | } |
| 253 | 253 | \ No newline at end of file | ... | ... |
urbanops-module-garden/src/main/resources/mapper/inspectionplan/InspectionPlanCommitMapper.xml
| ... | ... | @@ -2,6 +2,10 @@ |
| 2 | 2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| 3 | 3 | <mapper namespace="com.zteits.urbanops.module.garden.dal.mysql.inspectionplan.InspectionPlanCommitMapper"> |
| 4 | 4 | |
| 5 | + | |
| 6 | + <resultMap id="BaseResultMap" type="com.zteits.urbanops.module.garden.controller.admin.inspectionplan.vo.InspectionPlanCommitRespVO"> | |
| 7 | + <result property="img" column="img" typeHandler="com.zteits.urbanops.framework.mybatis.core.type.StringArrayToListTypeHandler"/> | |
| 8 | + </resultMap> | |
| 5 | 9 | <!-- |
| 6 | 10 | 一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。 |
| 7 | 11 | 无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。 |
| ... | ... | @@ -9,7 +13,7 @@ |
| 9 | 13 | 文档可见:https://www.iocoder.cn/MyBatis/x-plugins/ |
| 10 | 14 | --> |
| 11 | 15 | <!-- 复杂联表分页查询 --> |
| 12 | - <select id="selectForPage" resultType="com.zteits.urbanops.module.garden.controller.admin.inspectionplan.vo.InspectionPlanCommitRespVO"> | |
| 16 | + <select id="selectForPage" resultMap="BaseResultMap"> | |
| 13 | 17 | SELECT |
| 14 | 18 | a.plan_no, |
| 15 | 19 | b.plan_name, | ... | ... |