解决高并发-springboot-redis-mysql医院预约系统项目超详细讲解--半个小时教你如何使用springboot完成预约项目-----第四章:预约挂号

dao层
redisDaoImpl
三个方法
预约
获取医生预约次数
去重

    @Autowired
    private StringRedisTemplate redisTemplate; //对redis操作的对象
    //尾部添加数据
    //预约往redis储存一个list类型,键名为 doctor:1:20210109 ,doctor:id:日期,值为患者id
    @Override
    public Integer listRpush(String key, String value) {
        return redisTemplate.opsForList().rightPush(key,value).intValue();
    }
  
    //获取list的长度,list长度证明,被预约过多少次,医生每天看病总数-预约总数等于剩余预约数
    @Override
    public Integer listLen(String key) {
        return redisTemplate.opsForList().size(key).intValue();
    }
  //预约,去重,往set加值,预约成功加值,加不进去证明加过值了=预约失败
    @Override
    public Integer setAdd(String key, String value) {
        return redisTemplate.opsForSet().add(key, value).intValue();
    }

service层
BookingServiceImpl
两个方法
预约
获取list长度

   @Autowired
    private RedisDao redisDao;

    @Override
    public Integer addOneBooking(Booking booking) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        String date = fmt.format(booking.getBookingDate()); //格式化date

        Integer patientId = booking.getPatient().getPatientId();
        Integer doctorId = booking.getDoctor().getDoctorId();
        String key = String.format("patient-set:%s", date);
		//key=patient-set:yyyyMMdd
        //若已挂号
        if (redisDao.setAdd(key, patientId.toString()) == 0) {
            return 0; //set加入不进值,预约过了,结束方法
        }

         key = String.format("doctor:%d:%s", doctorId, date);
		//	list的key=doctor:医生id:日期 

        return redisDao.listRpush(key, patientId.toString());//往list加入患者id

    }

    @Override
    public Integer getBookingCount(Integer doctorId, Date bookingDate) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        String date = fmt.format(bookingDate);

        String key = String.format("doctor:%d:%s", doctorId, date);
      	//拼接list的key名
		
        return redisDao.listLen(key);
    }

controller层
预约方法:
与页面ajax绑定函数
假设医生12:00-14:00为休息时间算出看病时间

    @Autowired
    private BookingService bookingService;
    @Autowired
    private DoctorService doctorService;
    @RequestMapping("add_booking")
    @ResponseBody
     public Map<String, Object> addOneBooking(Integer doctorId,
                                                    @DateTimeFormat(pattern = "yyyy-MM-dd") Date bookingDate,
                                                    HttpSession session) {
        HashMap<String, Object> result = new HashMap<>();
        Doctor doctor = doctorService.getDoctorById(doctorId);
        Patient user = (Patient)session.getAttribute("user");
        if (user==null){ //没有登陆
            result.put("success", false);
            result.put("message", "请登录后再预约");
            return result;
        }
        //查询医生当天被预约多少次
        Integer bookingCount = bookingService.getBookingCount(doctorId, bookingDate);
        //医生还剩余number次
        Integer number = doctor.getTotal()-bookingCount;
        if (number<=0){ // 没有剩余预约次数了
            result.put("success", false);
            result.put("message", "当天预约已满");
            return result;
        }

        //计算预约时间
        Integer length = (8 * 60) / doctor.getTotal();
        Integer waitTime = bookingCount * length; //需等待时间
        Integer hour = 0, minute = 0;

        if (waitTime < 240) {
            hour = 8 + waitTime / 60;
            minute = waitTime % 60;
        }
        else {
            hour = 14 + (waitTime - 240) / 60;
            minute = (waitTime - 240) % 60;
        }
		//看病时间
        String visitTime = String.format("%02d:%02d:00", hour, minute);
        Booking booking = new Booking();
        booking.setDoctor(doctor);
        booking.setPatient(user);
        booking.setBookingDate(bookingDate);
        booking.setVisitTime(visitTime);
        if (bookingService.addOneBooking(booking) > 0) {
        bookingService.addOneBooking(booking);
         result.put("success", true);
            result.put("message", number - 1); //预约数-1
            result.put("time", visitTime);
        }
        else {
            result.put("success", false);
            result.put("message", "不能重复预约。"); 
        }
        return result;
    }

页面ajax

按钮绑定onclick事件

	function addBooking(doctorId) {
				var bookingDate = $("#booking-date").val();	//id选择器获取val

				$.ajax({
					url: 'add_booking?doctorId='+doctorId+"&bookingDate="+bookingDate, 
					type: 'post',
					dataType: "json",
					success: function (data) {
						if (data.success) {
							$("#total-"+doctorId).text(data.message);
							if (data.message <= 0) {
								$("#btn-booking").attr("disabled", "disabled");
							}
							alert("预约成功,请于"+data.time+"前来就诊");
						}
						else {
							alert(data.message);
						}
					},
					error: function(error){    //失败后回调
						alert("服务器连接失败");
					}
				});
			}

效果
点击前
解决高并发-springboot-redis-mysql医院预约系统项目超详细讲解--半个小时教你如何使用springboot完成预约项目-----第四章:预约挂号
点击后
解决高并发-springboot-redis-mysql医院预约系统项目超详细讲解--半个小时教你如何使用springboot完成预约项目-----第四章:预约挂号
解决高并发-springboot-redis-mysql医院预约系统项目超详细讲解--半个小时教你如何使用springboot完成预约项目-----第四章:预约挂号

上一篇:eclipse项目发布到tomcat后的默认部署路径


下一篇:iOS7 中的statusbar的隐藏和样式更改