1。需求为页面中的内容,由多个vo类组成,此时我们查询出来的多个vo类都为list集合,需要把里面的参数根据多个vo类中查询出来的相同字段进行拼接
List<UserCntDto> userCntDtos = mapper.****();
List<GroupVo> groupVoList = mapper.****();
List<IndicatorByGroupResultVo> indicatorByGroupResultVos = mapper.***();
List<IndicatorValueDTO> indicatorValueDTOS = mapper.***();
2.拼接上面四个list集合,四个list的公共字段为一个groupId,以groupID为key,四个长度的数组为value,转换成map,根据不同的groupId把第一个list个放在数组的第一位,第二个同样如此
public List<IndividualIndicatorVo> queryIndicatorDetail (List<UserCntDto> userCntDtos, List<GroupVo> groupVoList,
List<IndicatorValueDTO> indicatorValueDTOS,
List<IndicatorByGroupResultVo> indicatorByGroupResultVos) {
Map<Long, Integer[]> map= new HashMap<>();
for (int i = 0; i<groupVoList.size(); i++) {
GroupVo groupVo = groupVoList.get(i);
if (!map.containsKey(groupVo.getId())) {
map.put(groupVo.getId(), new Integer[4]);
}
map.get(groupVo.getId())[0] = i;
}
for (int i = 0; i<indicatorValueDTOS.size(); i++) {
IndicatorValueDTO indicatorValueDTO = indicatorValueDTOS.get(i);
if (!map.containsKey(indicatorValueDTO.getGroupId())) {
map.put(indicatorValueDTO.getGroupId(), new Integer[4]);
}
map.get(indicatorValueDTO.getGroupId())[1] = i;
}
for (int i = 0; i<indicatorByGroupResultVos.size(); i++) {
IndicatorByGroupResultVo indicatorByGroupResultVo = indicatorByGroupResultVos.get(i);
if (!map.containsKey(indicatorByGroupResultVo.getGroupId())) {
map.put(indicatorByGroupResultVo.getGroupId(), new Integer[4]);
}
map.get(indicatorByGroupResultVo.getGroupId())[2] = i;
}
for (int i = 0; i<userCntDtos.size(); i++) {
UserCntDto userCntDto = userCntDtos.get(i);
if (!map.containsKey(userCntDto.getGroupId())) {
map.put(userCntDto.getGroupId(), new Integer[4]);
}
map.get(userCntDto.getGroupId())[3] = i;
}
Integer[] integers = map.get(1);
System.out.println(integers);
return iterMapForValue(map, userCntDtos, groupVoList, indicatorValueDTOS, indicatorByGroupResultVos);
}
3.再遍历map,根据自己需要的vo去组装参数,完成转换
public List<IndividualIndicatorVo> iterMapForValue (Map<Long, Integer[]> map, List<UserCntDto> userCntDtos,
List<GroupVo> groupVoList,
List<IndicatorValueDTO> indicatorValueDTOS,
List<IndicatorByGroupResultVo> indicatorByGroupResultVos) {
List<Long> groupIds = groupVoList.stream().map(GroupVo::getId).collect(Collectors.toList());
List<IndividualIndicatorVo> individualIndicatorVos = new ArrayList<>();
for (Map.Entry<Long, Integer[]> entry : map.entrySet()) {
IndividualIndicatorVo individualIndicatorVo = new IndividualIndicatorVo();
Integer[] value = entry.getValue();
if (value[0] != null) {
GroupVo groupVo = groupVoList.get(value[0]);
individualIndicatorVo.setGroupId(groupVo.getId());
individualIndicatorVo.setGroupName(groupVo.getName());
}
if (value[1] != null) {
IndicatorValueDTO indicatorValue = indicatorValueDTOS.get(value[1]);
IndicatorResultVo indicatorResultVo = new IndicatorResultVo();
indicatorResultVo.setId(indicatorValue.getId());
indicatorResultVo.setName(indicatorValue.getName());
indicatorResultVo.setValue(indicatorValue.getValue());
individualIndicatorVo.setIndicator(indicatorResultVo);
}
if (value[2] != null) {
IndicatorByGroupResultVo indicatorByGroupResultVo = indicatorByGroupResultVos.get(value[2]);
individualIndicatorVo.setConfidentInterval(indicatorByGroupResultVo.getConfidentInterval());
individualIndicatorVo.setPValue(indicatorByGroupResultVo.getPValue());
}
if (value[3] != null) {
UserCntDto userCntDto = userCntDtos.get(value[3]);
UpGradeInfoDto upGradeInfoDto = new UpGradeInfoDto();
upGradeInfoDto.setTotalUsers(userCntDto.getTotal());
upGradeInfoDto.setUpgradeUsers(userCntDto.getUpgradeCnt());
individualIndicatorVo.setUpgradeInfo(upGradeInfoDto);
}
if (listContains(groupIds,individualIndicatorVo.getGroupId())){
individualIndicatorVos.add(individualIndicatorVo);
}
}
return individualIndicatorVos;
}