之前我们写过三个set方法,删除也需要删除三个del方法,不然会导致数据不一致
set为了去重,hash为了方便查看预约,list为了算方便算长度(预约数)
dao层
RedisDao 新增三个方法
@Override
public Integer hashDel(String key, String hkey) {
return redisTemplate.opsForHash().delete(key, hkey).intValue();
}
@Override
public Integer setDel(String key, String value) {
return redisTemplate.opsForSet().remove(key, value).intValue();
}
@Override
public Integer listDel(String key, String value) {
return redisTemplate.opsForList().remove(key,1,value).intValue();
}
service层
BOokingServiceImpl
@Transactional
public Integer delOneBooking(Booking booking) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
String date = fmt.format(booking.getBookingDate());
Integer doctorId = booking.getDoctor().getDoctorId();
Integer patientId = booking.getPatient().getPatientId();
String key = "patient:" + patientId;
redisDao.hashDel(key, date + "@" + booking.getVisitTime());
key = String.format("patient-set:%s", date);
redisDao.setDel(key,patientId.toString());
key = String.format("doctor:%d:%s", doctorId, date);
System.out.println(key);
Integer integer = redisDao.listDel(key,doctorId.toString());
return 1;
}
controller层
BookingController
//页面获取doctorid 因为好多键名需要它
@RequestMapping("del_booking")
@ResponseBody
public Map<String, Object> delOneBooking(Booking booking,Integer doctorId, HttpSession session) {
Map<String, Object> result = new HashMap<>();
Patient user = (Patient)session.getAttribute("user");
Doctor doctor = new Doctor();
doctor.setDoctorId(doctorId);//修改doctorId
booking.setDoctor(doctor);
booking.setPatient(user);
if (bookingService.delOneBooking(booking) > 0) {
result.put("success", true);
}
else {
result.put("success", false);
}
return result;
}
页面
ajax
function delBooking(obj) {
var bookingDate = $(obj).parent().prev().prev().text();
var visitTime = $(obj).parent().prev().text();
var doctorId = $(obj).parent().parent().find("input").val(); //从隐藏input标签获取医生id
$.ajax({
url: 'del_booking?bookingDate='+bookingDate+"&visitTime="+visitTime+"&doctorId="+doctorId,
type: 'post',
dataType: "json",
success: function (data) {
if (data.success) {
//删除本行数据
$(obj).parent().parent().remove();
}
else {
alert("取消失败");
}
},
error: function(error){ //失败后回调
alert("服务器连接失败");
}
});
}
页面循环列表 写一个隐藏的input标签 估计是最笨的方法了吧 ~
<#list bookings as b>
<tr>
<input type="hidden" value="${b.doctor.doctorId}">
<td>${b_index + 1}</td>
<td>${b.doctor.office.officeName}</td>
<td>${b.doctor.name}</td>
<td>${b.bookingDate?string("yyyy-MM-dd")}</td>
<td>${b.visitTime}</td>
<td><a href="javascript:void(0)" onclick="delBooking(this)">取消预约</a></td>
</tr>
</#list>