farm模块-区域多选转换工具类
This commit is contained in:
@@ -0,0 +1,73 @@
|
|||||||
|
package cn.iocoder.yudao.module.farm.framework.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 地区代码转换工具类
|
||||||
|
* 用于在数据库字符串(逗号分隔)和Java集合之间转换
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class AreaCodeUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分隔符 - 用于分隔不同的地区代码组合
|
||||||
|
*/
|
||||||
|
private static final String SEPARATOR = "|";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将地区代码集合转换为数据库存储的字符串
|
||||||
|
*
|
||||||
|
* @param areaCodeList 地区代码集合
|
||||||
|
* @return 逗号分隔的字符串,如果集合为空则返回null
|
||||||
|
*/
|
||||||
|
public static String toDatabaseString(List<String> areaCodeList) {
|
||||||
|
if (areaCodeList == null || areaCodeList.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// 过滤空字符串并去重
|
||||||
|
List<String> filteredList = areaCodeList.stream()
|
||||||
|
.filter(StrUtil::isNotBlank)
|
||||||
|
.distinct()
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
if (filteredList.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return String.join(SEPARATOR, filteredList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将数据库字符串转换为地区代码集合
|
||||||
|
* 注意:这里使用特殊的分隔符 "|" 来分隔不同的地区代码组合
|
||||||
|
* 因为地区代码本身包含逗号,如 "120000,120100,120118"
|
||||||
|
*
|
||||||
|
* @param databaseString 数据库中的字符串
|
||||||
|
* @return 地区代码集合,如果字符串为空则返回空集合
|
||||||
|
*/
|
||||||
|
public static List<String> toAreaCodeList(String databaseString) {
|
||||||
|
if (StrUtil.isBlank(databaseString)) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 使用 "|" 作为分隔符,因为地区代码本身包含逗号
|
||||||
|
return Arrays.stream(databaseString.split("\\|"))
|
||||||
|
.map(String::trim)
|
||||||
|
.filter(StrUtil::isNotBlank)
|
||||||
|
.distinct()
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("转换地区代码集合失败,原始字符串: {}", databaseString, e);
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package cn.iocoder.yudao.module.farm.framework.util;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.farm.controller.admin.modelmanagement.vo.ModelManagementRespVO;
|
||||||
|
import cn.iocoder.yudao.module.farm.dal.dataobject.modelmanagement.ModelManagementDO;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模型管理转换工具类
|
||||||
|
* 确保所有接口返回格式统一
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
public class ModelManagementConvertUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 ModelManagementDO 转换为 ModelManagementRespVO
|
||||||
|
* 确保 suitablePlantingAreas 字段格式统一
|
||||||
|
*/
|
||||||
|
public static ModelManagementRespVO convertToRespVO(ModelManagementDO modelManagementDO) {
|
||||||
|
if (modelManagementDO == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ModelManagementRespVO respVO = new ModelManagementRespVO();
|
||||||
|
|
||||||
|
// 手动设置基础字段,避免BeanUtils处理复杂类型
|
||||||
|
respVO.setId(modelManagementDO.getId());
|
||||||
|
respVO.setModelName(modelManagementDO.getModelName());
|
||||||
|
respVO.setSpeciesId(modelManagementDO.getSpeciesId());
|
||||||
|
respVO.setSpeciesName(modelManagementDO.getSpeciesName());
|
||||||
|
respVO.setCycle(modelManagementDO.getCycle());
|
||||||
|
respVO.setHarvest(modelManagementDO.getHarvest());
|
||||||
|
respVO.setDescription(modelManagementDO.getDescription());
|
||||||
|
respVO.setCreateTime(modelManagementDO.getCreateTime());
|
||||||
|
respVO.setBaseId(modelManagementDO.getBaseId());
|
||||||
|
respVO.setBaseName(modelManagementDO.getBaseName());
|
||||||
|
respVO.setPlotId(modelManagementDO.getPlotId());
|
||||||
|
respVO.setPlotName(modelManagementDO.getPlotName());
|
||||||
|
respVO.setTenantId(modelManagementDO.getTenantId());
|
||||||
|
respVO.setIsRelease(modelManagementDO.getIsRelease());
|
||||||
|
respVO.setQuoteCount(modelManagementDO.getQuoteCount());
|
||||||
|
respVO.setSpeciesImage(modelManagementDO.getSpeciesImage());
|
||||||
|
respVO.setReleaseTime(modelManagementDO.getReleaseTime());
|
||||||
|
|
||||||
|
// 设置suitablePlantingAreas字段,转换为前端期望的对象格式
|
||||||
|
List<String> areasList = modelManagementDO.getSuitablePlantingAreasList();
|
||||||
|
respVO.setSuitablePlantingAreasFromStringList(areasList);
|
||||||
|
|
||||||
|
// 设置speciesImageList字段,触发自动转换
|
||||||
|
respVO.setSpeciesImageList(modelManagementDO.getSpeciesImageList());
|
||||||
|
|
||||||
|
return respVO;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user