返回顶部
扩大
缩小

Yeap

获取数据,封装成对象的方法

@Override
public List info(Long groundId) throws JsonProcessingException {
// 根据场地id,获取所有场次对象
List groundTimeTables = groundTimeTableRepository.findByGroundId(groundId);

if (groundTimeTables != null && groundTimeTables.size() >0) {
  List<Long> companyIds = groundTimeTables.stream().map(GroundTimeTable::getCompanyId).collect(Collectors.toList());
  Long companyId = companyIds.get(0);
  DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
  // map集合,key存放时间和价格拼接字符串  value存放营业日的数组
  Map<String, List<String>> groundTimesMap = new HashMap<>();
  for (GroundTimeTable timeTable : groundTimeTables) {
    String start = TIME_FORMATTER.format(timeTable.getStartTime());
    String end = TIME_FORMATTER.format(timeTable.getEndTime());
    // 时间段价格
    String price = timeTable.getPrice();
    String tmpTime = start + SPLIT_SPACE + end + SPLIT_SPACE + price;
    if (groundTimesMap.get(tmpTime) != null) {
      groundTimesMap.get(tmpTime).add(timeTable.getOpeningDay());
    } else {
      List<String> list = new ArrayList<>();
      list.add(timeTable.getOpeningDay());
      groundTimesMap.put(tmpTime, list);
    }
  }
  // key是营业日拼接的字符串,value的GroundTimeObj对象的集合,返回时根据营业日作为单独对象进行返回
  Map<String, List<GroundTimeObj>> openDayMap = new HashMap<>();
  Iterator<String> iterator = groundTimesMap.keySet().iterator();
  while (iterator.hasNext()) {
    String next = iterator.next();
    String[] split = next.split(SPLIT_SPACE);
    // todo检验场次是否属于同一营业日   根据key获取营业日 value 的数组
    List<String> openDays = groundTimesMap.get(next);
    String openDaysStr = JsonUtils.obj2JsonString(openDays);
    GroundTimeObj build =
        GroundTimeObj.builder()
            .startTime(LocalTime.parse(split[0]))
            .endTime(LocalTime.parse(split[1]))
            .price(split[2])
            .build();
    if (openDayMap.get(openDaysStr) != null) {
      List<GroundTimeObj> timeObjs = openDayMap.get(openDaysStr);
      timeObjs.add(build);
    } else {
      List<GroundTimeObj> timeObjs = new ArrayList<>();
      timeObjs.add(build);
      openDayMap.put(openDaysStr, timeObjs);
    }
  }

  // 最终返回数据是一个GroundTimeTableReq对象的集合  根据营业日区分对象
  List<GroundTimeTableReq> groundTimeTableReqs = new ArrayList<>();

  Iterator<String> iteratorOpenDays = openDayMap.keySet().iterator();
  while (iteratorOpenDays.hasNext()) {
    String next = iteratorOpenDays.next();
    GroundTimeTableReq req =
        GroundTimeTableReq.builder()
            .commpanyId(companyId)
            .groundId(groundId)
            .openDays(JsonUtils.jsonString2List(next, String.class))
            .groundTimes(openDayMap.get(next))
            .build();
    // 组装对象
    groundTimeTableReqs.add(req);
  }

  return groundTimeTableReqs;

}
return null;
}

posted on 2021-08-30 14:26  YP泡泡  阅读(187)  评论(0)    收藏  举报

导航