解决获取景区名时出现一个景区名对应多个id的问题
This commit is contained in:
parent
2f51e6bd46
commit
504fbba046
@ -4,6 +4,7 @@ import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class RouteStat {
|
||||
private Long scenicId;
|
||||
private String scenicName;//景区名称,通过路线id获得景区id
|
||||
private String routeName;//路线名称
|
||||
private Long routeId ;//路线id
|
||||
|
@ -447,16 +447,35 @@ public class OrderAnalysisServiceImpl implements OrderAnalysisService {
|
||||
.collect(Collectors.toMap(CpRoute::getId, route -> route));
|
||||
|
||||
// 提取所有景区ID
|
||||
Set<Long> scenicIds = routes.stream()
|
||||
List<Long> scenicIds = routes.stream()
|
||||
.map(CpRoute::getScenicId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 批量查询所有景区名称确保不为空
|
||||
// Map<Long, String> scenicNameMap = emScenicMapper.getScenicNameMap(new ArrayList<>(scenicIds));
|
||||
Map<Long, String> scenicNameMap = Collections.emptyMap();
|
||||
if (!scenicIds.isEmpty()) {
|
||||
scenicNameMap = emScenicMapper.getScenicNameMap(new ArrayList<>(scenicIds));
|
||||
// 获取景区id和景区名对应的map
|
||||
List<Map<String, Object>> scenicList = emScenicMapper.getScenicNameMap(scenicIds);
|
||||
// 处理空集合(MyBatis无结果时返回空List)
|
||||
if (scenicList == null) {
|
||||
scenicList = new ArrayList<>();
|
||||
}
|
||||
|
||||
// 转换为 Map<String, String>(ID为键,名称为值)
|
||||
Map<Long, String> scenicNameMap = new HashMap<>();
|
||||
for (Map<String, Object> item : scenicList) { // 泛型修正为 Map<String, Object>
|
||||
// 1. 获取scenicId:先转Number(兼容Integer/Long),再转Long
|
||||
Number idNum = (Number) item.get("scenicId"); // 键是字符串"scenicId"(与XML别名一致)
|
||||
Long scenicId = null;
|
||||
if (idNum != null) {
|
||||
scenicId = idNum.longValue(); // 安全转为Long,避免类型错误
|
||||
}
|
||||
|
||||
// 2. 获取scenicName:直接转为String(与XML别名"scenicName"一致)
|
||||
String scenicName = (String) item.get("scenicName");
|
||||
|
||||
// 3. 非空校验后存入(避免存null键/值,导致后续getOrDefault失效)
|
||||
if (scenicId != null && scenicName != null) {
|
||||
scenicNameMap.put(scenicId, scenicName);
|
||||
}
|
||||
}
|
||||
|
||||
// 组装结果
|
||||
@ -474,6 +493,8 @@ public class OrderAnalysisServiceImpl implements OrderAnalysisService {
|
||||
|
||||
Long scenicId = route.getScenicId();
|
||||
if (scenicId != null) {
|
||||
//wenti
|
||||
stat.setScenicId(scenicId);
|
||||
stat.setScenicName(scenicNameMap.getOrDefault(scenicId, "未知景区"));
|
||||
} else {
|
||||
stat.setScenicName("未知景区");
|
||||
@ -486,7 +507,6 @@ public class OrderAnalysisServiceImpl implements OrderAnalysisService {
|
||||
routeDistribution.add(stat);
|
||||
}
|
||||
}
|
||||
|
||||
return routeDistribution;
|
||||
}
|
||||
/**
|
||||
|
@ -518,10 +518,10 @@ public class OrderDetailAnalysisServiceImpl implements OrderDetailAnalysisServic
|
||||
.collect(Collectors.toMap(CpRoute::getId, route -> route));
|
||||
|
||||
// 提取所有景区ID(用于批量查询景区名称)
|
||||
Set<Long> scenicIds = routes.stream()
|
||||
List<Long> scenicIds = routes.stream()
|
||||
.map(CpRoute::getScenicId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 批量查询景区名称
|
||||
Map<Long, String> scenicNameMap = new HashMap<>();
|
||||
@ -545,6 +545,7 @@ public class OrderDetailAnalysisServiceImpl implements OrderDetailAnalysisServic
|
||||
stat.setRouteName(route.getName());
|
||||
Long scenicId = route.getScenicId();
|
||||
if (scenicId != null) {
|
||||
stat.setScenicId(scenicId);
|
||||
stat.setScenicName(scenicNameMap.getOrDefault(scenicId, "未知景区"));
|
||||
} else {
|
||||
stat.setScenicName("未知景区");
|
||||
|
@ -8,6 +8,7 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -35,9 +36,9 @@ public interface EmScenicMapper extends BaseMapper<EmScenic> {
|
||||
* @return
|
||||
*/
|
||||
List<AreaNumStatisVo> countByAreaId();
|
||||
List<Map<String, Object>> getScenicNameMap(@Param("scenicIds") List<Long> scenicIds);
|
||||
// Map<Long, String> getScenicNameMap(ArrayList<Long> scenicIds);
|
||||
|
||||
Map<Long, String> getScenicNameMap(ArrayList<Long> scenicIds);
|
||||
|
||||
@Select("SELECT * FROM em_scenic WHERE id IN (#{ids})")
|
||||
// @Select("SELECT * FROM em_scenic WHERE id IN (#{ids})")
|
||||
List<EmScenic> listByIds(ArrayList<Long> ids);
|
||||
}
|
||||
|
@ -27,14 +27,34 @@
|
||||
group by area_id
|
||||
</select>
|
||||
<select id="getScenicNameMap" resultType="java.util.HashMap">
|
||||
SELECT id AS "key", name AS "value"
|
||||
SELECT
|
||||
id AS "scenicId", -- 更明确的别名
|
||||
name AS "scenicName"
|
||||
FROM em_scenic
|
||||
WHERE del_flag = 0
|
||||
<if test="scenicIds != null and scenicIds.size() > 0">
|
||||
<!-- 确保参数正确传递,并且当scenicIds为空时的处理逻辑 -->
|
||||
<if test="scenicIds != null">
|
||||
<if test="scenicIds.size() > 0">
|
||||
AND id IN
|
||||
<foreach collection="scenicIds" item="id" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</if>
|
||||
<if test="scenicIds.size() == 0">
|
||||
<!-- 当传入空列表时,确保不返回任何结果 -->
|
||||
AND 1 = 0
|
||||
</if>
|
||||
</if>
|
||||
<if test="scenicIds == null">
|
||||
AND 1 = 0
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<select id="listByIds" resultType="com.aircraft.modules.system.domain.EmScenic">
|
||||
SELECT * FROM em_scenic
|
||||
WHERE id IN
|
||||
<foreach collection="list" item="id" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</select>
|
||||
</mapper>
|
||||
|
Loading…
Reference in New Issue
Block a user