我们要做成这样的效果.
分析
1.首先我们要根据参数在mongodb中读出医院,然后根据医院编号和参数读出科室。同时在医院中有预约规则根据预约规则。计算出周期和要显示的天数。
service中的getBookingScheduleRule方法
//根据医院编号获取预约规则
Hospital hospital = hospitalService.getByHoscode(hoscode);
if (ObjectUtils.isEmpty(hospital)) {
throw new YyghException(ResultCodeEnum.DATA_ERROR);
}
BookingRule bookingRule = hospital.getBookingRule();
//获取可预约日期的数据(分页)
IPage iPage = this.getListDate(page, limit, bookingRule);
//获取当前可预约的时间
List dateList = iPage.getRecords();
获取可预约时间的方法
//获取可预约日期的数据(分页)
private IPage getListDate(Integer page, Integer limit, BookingRule bookingRule) {
//当天放号时间
DateTime releaseTime = this.getDateTime(new Date(), bookingRule.getReleaseTime());
//预约周期
Integer cycle = bookingRule.getCycle();
//如果当天放号时间已过,则预约周期后一天为即将放号时间,周期加1
if (releaseTime.isAfterNow()) {
cycle += 1;
}
//可预约所有日期,最后一天显示即将放号倒计时
List dateList = new ArrayList<>();
for (int i = 0; i < cycle; i++) {
//计算当前预约日期
DateTime curDateTime = new DateTime().plusDays(i);
String dateString = curDateTime.toString("yyyy-MM-dd");
dateList.add(new DateTime(dateString).toDate());
}
//因为周期不同,每页显示的日期最多7天时间,超过七天分页
List pageDateList = new ArrayList<>();
int start = (page - 1) * limit;
int end = (page - 1) * limit + limit;
//如果可以显示的数据小于7,直接显示
if (end > dateList.size()) {
end = dateList.size();
}
for (int i = start; i < end; i++) {
pageDateList.add(dateList.get(i));
}
//如果大于7,进行分页
IPage iPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page, 7, dateList.size());
iPage.setRecords(pageDateList);
return iPage;
}
private DateTime getDateTime(Date date, String timeString) {
String dateTimeString = new DateTime(date).toString("yyyy-MM-dd") + " " + timeString;
DateTime dateTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm").parseDateTime(dateTimeString);
return dateTime;
}
2.根据计算出来的可预约时间找到对应的排班信息,同时利用mongoTemplate.aggregate的sum方法,来计算可用的预约数,为了方便我们之后演示我们需要建立一个map,key是工作时间value是预约规则和剩余数量
Criteria criteria = Criteria.where("hoscode").is(hoscode).and("depcode").is(depcode).and("workDate").in(dateList);
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(criteria),
Aggregation.group("workDate").first("workDate").as("workDate")
.count().as("docCount")
.sum("availableNumber").as("availableNumber")
.sum("reservedNumber").as("reservedNumber")
);
AggregationResults bookingScheduleRuleVos = mongoTemplate.aggregate(agg, Schedule.class, BookingScheduleRuleVo.class);
List scheduleVoList = bookingScheduleRuleVos.getMappedResults();
HashMap scheduleRuleVoMap = new HashMap<>();
if (!CollectionUtils.isEmpty(scheduleVoList)) {
scheduleRuleVoMap = (HashMap) scheduleVoList.stream().collect(Collectors.toMap(BookingScheduleRuleVo::getWorkDate,
BookingScheduleRuleVo -> BookingScheduleRuleVo));
}
3.根据日期对应排班的map,整理出可预约的
//获取可预约的排班规则 private ArrayListgetScheduleRuleVos(Integer page, BookingRule bookingRule, IPage iPage, List dateList, HashMap scheduleRuleVoMap) { ArrayList bookingScheduleRuleVoList = new ArrayList<>(); for (int i = 0, len = dateList.size(); i < len; i++) { Date date = dateList.get(i); BookingScheduleRuleVo bookingScheduleRuleVo = scheduleRuleVoMap.get(toDate(date)); if (null == bookingScheduleRuleVo) { // 说明当天没有排班医生 bookingScheduleRuleVo = new BookingScheduleRuleVo(); //就诊医生人数 bookingScheduleRuleVo.setDocCount(0); //科室剩余预约数 -1表示无号 bookingScheduleRuleVo.setAvailableNumber(-1); } bookingScheduleRuleVo.setWorkDate(date); bookingScheduleRuleVo.setWorkDateMd(date); //计算当前预约日期为周几 String dayOfWeek = this.getDayOfWeek(new DateTime(date)); bookingScheduleRuleVo.setDayOfWeek(dayOfWeek); //最后一页最后一条记录为即将预约 状态 0:正常 1:即将放号 -1:当天已停止挂号 if (i == len - 1 && page == iPage.getPages()) { bookingScheduleRuleVo.setStatus(1); } else { bookingScheduleRuleVo.setStatus(0); } //当天预约如果过了停号时间, 不能预约 if (i == 0 && page == 1) { DateTime stopTime = this.getDateTime(new Date(), bookingRule.getStopTime()); if (stopTime.isBeforeNow()) { //停止预约 bookingScheduleRuleVo.setStatus(-1); } } bookingScheduleRuleVoList.add(bookingScheduleRuleVo); } return bookingScheduleRuleVoList; }
4.把数据都封装到map里面返回
//可预约日期规则数据
result.put("bookingScheduleList", bookingScheduleRuleVoList);
result.put("total", iPage.getTotal());
//其他基础数据
Map baseMap = new HashMap<>();
//医院名称
baseMap.put("hosname", hospitalService.getHospName(hoscode));
//科室
Department department = departmentService.getDepartment(hoscode, depcode);
//大科室名称
baseMap.put("bigname", department.getBigname());
//科室名称
baseMap.put("depname", department.getDepname());
//月
baseMap.put("workDateString", new DateTime().toString("yyyy年MM月"));
//放号时间
baseMap.put("releaseTime", bookingRule.getReleaseTime());
//停号时间
baseMap.put("stopTime", bookingRule.getStopTime());
result.put("baseMap", baseMap);
controller
@ApiOperation(value = "获取排班数据")
@GetMapping("auth/findScheduleList/{hoscode}/{depcode}/{workDate}")
public Result findScheduleList(
@ApiParam(name = "hoscode", value = "医院code", required = true)
@PathVariable String hoscode,
@ApiParam(name = "depcode", value = "科室code", required = true)
@PathVariable String depcode,
@ApiParam(name = "workDate", value = "排班日期", required = true)
@PathVariable String workDate) {
return Result.ok(scheduleService.getScheduleDetail(hoscode, depcode, workDate));
}
@GetMapping("getSchedule/{scheduleId}")
public Result getSchedule(@PathVariable String scheduleId) {
return Result.ok(scheduleService.getSchedule(scheduleId));
}
3.1封装api请求
在/api/hosp.js添加方法
getBookingScheduleRule(page, limit, hoscode, depcode) {
return request({
url: `${api_name}/auth/getBookingScheduleRule/${page}/${limit}/${hoscode}/${depcode}`,
method: 'get'
})
},
findScheduleList(hoscode, depcode, workDate) {
return request({
url: `${api_name}/auth/findScheduleList/${hoscode}/${depcode}/${workDate}`,
method: 'get'
})
},
3.2 页面展示
创建/pages/hospital/schedule.vue组件
预约确认
Controller
@GetMapping("inner/getScheduleOrderVo/{scheduleId}")
public ScheduleOrderVo getScheduleOrderVo(@PathVariable String scheduleId) {
return scheduleService.getScheduleOrderVo(scheduleId);
}
service
@Override
public ScheduleOrderVo getScheduleOrderVo(String scheduleId) {
ScheduleOrderVo scheduleOrderVo = new ScheduleOrderVo();
//获取排班信息
Schedule schedule = scheduleRepository.findById(scheduleId).get();
if (ObjectUtils.isEmpty(schedule)) {
throw new YyghException(ResultCodeEnum.PARAM_ERROR);
}
//获取预约挂号的规则
Hospital hoscode = hospitalService.getByHoscode(schedule.getHoscode());
if (ObjectUtils.isEmpty(hoscode)) {
throw new YyghException(ResultCodeEnum.PARAM_ERROR);
}
BookingRule bookingRule = hoscode.getBookingRule();
if (ObjectUtils.isEmpty(bookingRule)) {
throw new YyghException(ResultCodeEnum.PARAM_ERROR);
}
//把获取的数据设置到scheduleOrderVo
BeanUtils.copyProperties(schedule, scheduleOrderVo);
scheduleOrderVo.setHosname(hospitalService.getHospName(schedule.getHoscode()));
scheduleOrderVo.setDepname(departmentService.getDepName(schedule.getHoscode(), schedule.getDepcode()));
//退号截止天数(如:就诊前一天为-1,当天为0)
int quitDay = bookingRule.getQuitDay();
DateTime startTime = getDateTime(scheduleOrderVo, schedule, bookingRule, quitDay);
scheduleOrderVo.setStartTime(startTime.toDate());
return scheduleOrderVo;
}
private DateTime getDateTime(ScheduleOrderVo scheduleOrderVo, Schedule schedule, BookingRule bookingRule, int quitDay) {
DateTime quitTime = this.getDateTime(new DateTime(schedule.getWorkDate()).plusDays(quitDay).toDate(), bookingRule.getQuitTime());
scheduleOrderVo.setQuitTime(quitTime.toDate());
//预约开始时间
DateTime startTime = this.getDateTime(new Date(), bookingRule.getReleaseTime());
scheduleOrderVo.setStartTime(startTime.toDate());
//预约截止时间
DateTime endTime = this.getDateTime(new DateTime().plusDays(bookingRule.getCycle()).toDate(), bookingRule.getStopTime());
scheduleOrderVo.setEndTime(endTime.toDate());
//当天停止挂号时间
DateTime stopTime = this.getDateTime(new Date(), bookingRule.getStopTime());
return startTime;
}
前端
在/api/hosp/hospital.js添加方法
getSchedule(id) {
return request({
url: `${api_name}/getSchedule/${id}`,
method: 'get'
})
}
创建/pages/hospital/booking.vue组件



