完整代码
//根据医院编号 和 科室编号 ,查询排班规则数据@Overridepublic Map<String, Object> getRuleSchedule(long page, long limit, String hoscode, String depcode) {//1 根据医院编号 和 科室编号 查询Criteria criteria = Criteria.where("hoscode").is(hoscode).and("depcode").is(depcode);//2 根据工作日workDate期进行分组Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria),//匹配条件Aggregation.group("workDate")//分组字段.first("workDate").as("workDate")//3 统计号源数量.count().as("docCount").sum("reservedNumber").as("reservedNumber").sum("availableNumber").as("availableNumber"),//排序Aggregation.sort(Sort.Direction.DESC,"workDate"),//4 实现分页Aggregation.skip((page-1)*limit),Aggregation.limit(limit));//调用方法,最终执行AggregationResults<BookingScheduleRuleVo> aggResults =mongoTemplate.aggregate(agg, Schedule.class, BookingScheduleRuleVo.class);List<BookingScheduleRuleVo> bookingScheduleRuleVoList = aggResults.getMappedResults();//分组查询的总记录数Aggregation totalAgg = Aggregation.newAggregation(Aggregation.match(criteria),Aggregation.group("workDate"));AggregationResults<BookingScheduleRuleVo> totalAggResults =mongoTemplate.aggregate(totalAgg, Schedule.class, BookingScheduleRuleVo.class);int total = totalAggResults.getMappedResults().size();//把日期对应星期获取for(BookingScheduleRuleVo bookingScheduleRuleVo:bookingScheduleRuleVoList) {Date workDate = bookingScheduleRuleVo.getWorkDate();String dayOfWeek = this.getDayOfWeek(new DateTime(workDate));bookingScheduleRuleVo.setDayOfWeek(dayOfWeek);}//设置最终数据,进行返回Map<String, Object> result = new HashMap<>();result.put("bookingScheduleRuleList",bookingScheduleRuleVoList);result.put("total",total);//获取医院名称String hosName = hospitalService.getHospName(hoscode);//其他基础数据Map<String, String> baseMap = new HashMap<>();baseMap.put("hosname",hosName);result.put("baseMap",baseMap);return result;}