| | |
| | | */ |
| | | package org.sxkj.gd.workorder.wrapper; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springblade.core.mp.support.BaseEntityWrapper; |
| | | import org.springblade.core.tool.utils.BeanUtil; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | import org.sxkj.gd.workorder.dto.GdTaskResultDTO; |
| | | import org.sxkj.gd.workorder.entity.GdTaskResultEntity; |
| | | import org.sxkj.gd.workorder.vo.GdTaskResultVO; |
| | |
| | | * @author lw |
| | | * @since 2026-01-14 |
| | | */ |
| | | @Slf4j |
| | | public class GdTaskResultWrapper extends BaseEntityWrapper<GdTaskResultEntity, GdTaskResultVO> { |
| | | |
| | | public static GdTaskResultWrapper build() { |
| | |
| | | |
| | | /** |
| | | * 将单个DTO转换为Entity |
| | | * 处理geojson字段:将字符串数组格式转换为正确的JSON对象数组格式 |
| | | * |
| | | * @param dto DTO对象 |
| | | * @return Entity对象 |
| | |
| | | private GdTaskResultEntity convertDtoToEntity(GdTaskResultDTO dto) { |
| | | GdTaskResultEntity entity = BeanUtil.copy(dto, GdTaskResultEntity.class); |
| | | |
| | | // 步骤1:设置附件类型 |
| | | if (dto.getAttachmentType() != null) { |
| | | entity.setAttachmentType(dto.getAttachmentType()); |
| | | } else if (dto.getDateType() != null) { |
| | | entity.setAttachmentType(dto.getDateType()); |
| | | } |
| | | |
| | | // 步骤2:处理geojson字段,将字符串数组转换为正确的JSON格式 |
| | | String processedGeojson = processGeojson(dto.getGeojson()); |
| | | entity.setGeojson(processedGeojson); |
| | | |
| | | return entity; |
| | | } |
| | | |
| | | /** |
| | | * 处理geojson字段,将字符串数组格式转换为正确的JSON对象数组格式 |
| | | * 解决问题:["{\"score\":0.88}"] -> [{"score":0.88}] |
| | | * |
| | | * @param geojson 原始geojson字符串 |
| | | * @return 处理后的geojson字符串,如果处理失败则返回原值 |
| | | */ |
| | | private String processGeojson(String geojson) { |
| | | // 步骤1:空值检查 |
| | | if (StringUtil.isEmpty(geojson)) { |
| | | return geojson; |
| | | } |
| | | |
| | | try { |
| | | // 步骤2:尝试解析为JSONArray |
| | | com.alibaba.fastjson.JSONArray jsonArray = JSON.parseArray(geojson); |
| | | if (jsonArray == null || jsonArray.isEmpty()) { |
| | | return geojson; |
| | | } |
| | | |
| | | // 步骤3:检查第一个元素是否为字符串(说明是字符串数组格式) |
| | | Object firstElement = jsonArray.get(0); |
| | | if (firstElement instanceof String) { |
| | | // 步骤4:这是一个字符串数组,需要解析每个字符串元素 |
| | | com.alibaba.fastjson.JSONArray resultArray = new com.alibaba.fastjson.JSONArray(); |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | String elementStr = jsonArray.getString(i); |
| | | if (StringUtil.hasText(elementStr)) { |
| | | try { |
| | | // 步骤5:解析字符串为JSON对象 |
| | | com.alibaba.fastjson.JSONObject jsonObj = JSON.parseObject(elementStr); |
| | | if (jsonObj != null) { |
| | | resultArray.add(jsonObj); |
| | | } |
| | | } catch (Exception e) { |
| | | // 单个元素解析失败,记录日志但继续处理其他元素 |
| | | log.warn("解析geojson数组元素失败: {}", elementStr); |
| | | } |
| | | } |
| | | } |
| | | // 步骤6:返回正确的JSON数组格式 |
| | | return resultArray.toJSONString(); |
| | | } |
| | | |
| | | // 如果不是字符串数组,直接返回原值 |
| | | return geojson; |
| | | } catch (Exception e) { |
| | | // 解析失败,返回原值 |
| | | log.warn("处理geojson失败,返回原值: {}", geojson, e); |
| | | return geojson; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将文件类型字符串转换为附件类型编码 |
| | | * |
| | | * @param fileType 文件类型字符串(图片/视频/照片) |