diff --git a/src/main/java/com/sdut/labex/controller/BorrowInfoController.java b/src/main/java/com/sdut/labex/controller/BorrowInfoController.java index 4d421bf..86cf168 100644 --- a/src/main/java/com/sdut/labex/controller/BorrowInfoController.java +++ b/src/main/java/com/sdut/labex/controller/BorrowInfoController.java @@ -1,5 +1,6 @@ package com.sdut.labex.controller; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.sdut.labex.Factory.BorrowInfoFactory; import com.sdut.labex.entity.BorrowInfo; @@ -8,6 +9,10 @@ import com.sdut.labex.utils.ResVo; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * File: BorrowInfoController @@ -24,14 +29,49 @@ public class BorrowInfoController { @PostMapping("/borrowInfo") public ResVo addBorrowInfo(@RequestBody JSONObject jsonObject) { - BorrowInfo borrowInfo = BorrowInfoFactory.createBorrowInfo(jsonObject); - return borrowInfoService.addBorrowInfo(borrowInfo); + Map map = new HashMap<>(); + map.put("name", jsonObject.getString("username")); + map.put("date", jsonObject.getString("date")); + map.put("reason", jsonObject.getString("reason")); + map.put("applyDate", jsonObject.getString("applyDate")); + map.put("roomName", jsonObject.getString("roomName")); + + JSONArray timeList = jsonObject.getJSONArray("time"); + + boolean isAnyRoomBorrowed = false; + + for (Object item : timeList) { + map.put("time", item.toString()); + int code = borrowInfoService.isBorrowed(map); + if (code == 1) { + return ResVo.error("借用失败,所选时段教室被占用"); + } else if (code == 0) { + isAnyRoomBorrowed = true; + } + } + + if (isAnyRoomBorrowed) { + for (Object item : timeList) { + map.put("time", item.toString()); + borrowInfoService.borrow(map); + } + } + + return ResVo.ok("借用成功"); } - @PutMapping("/borrowInfo") - public ResVo updateBorrowInfo(@RequestBody JSONObject jsonObject) { - BorrowInfo borrowInfo = BorrowInfoFactory.createBorrowInfo(jsonObject); - return borrowInfoService.updateBorrowInfo(borrowInfo); + @PostMapping("/borrowInfo/notBorrowedYet") + public ResVo notBorrowedYet(@RequestBody JSONObject jsonObject) { + JSONArray jsonArray = jsonObject.getJSONArray("time"); + + String date = jsonObject.getString("date"); + + List timeList = new ArrayList<>(); + for (Object item : jsonArray) { + timeList.add(item.toString()); + } + + return borrowInfoService.notBorrowedYet(timeList, date); } @DeleteMapping("/borrowInfo/{id}") diff --git a/src/main/java/com/sdut/labex/mapper/BorrowInfoMapper.java b/src/main/java/com/sdut/labex/mapper/BorrowInfoMapper.java index b8eb316..a3f4a75 100644 --- a/src/main/java/com/sdut/labex/mapper/BorrowInfoMapper.java +++ b/src/main/java/com/sdut/labex/mapper/BorrowInfoMapper.java @@ -3,7 +3,9 @@ package com.sdut.labex.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.sdut.labex.entity.BorrowInfo; +import com.sdut.labex.entity.Room; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; @@ -18,9 +20,15 @@ import java.util.Map; public interface BorrowInfoMapper extends BaseMapper { public Page queryBorrowInfoByOptions(Page page, BorrowInfo borrowInfo); + public BorrowInfo borrow(Map map); + public void addTimeTable(Map map); public List listAllByDateAndRoom(String date, String room); + + public BorrowInfo isBorrowed(@Param("roomName") String roomName, @Param("time") String time, @Param("date") String date); + + public List notBorrowedYet(@Param("timeList") List timeList, @Param("date") String date); } diff --git a/src/main/java/com/sdut/labex/service/BorrowInfoService.java b/src/main/java/com/sdut/labex/service/BorrowInfoService.java index 87edbce..97d69c4 100644 --- a/src/main/java/com/sdut/labex/service/BorrowInfoService.java +++ b/src/main/java/com/sdut/labex/service/BorrowInfoService.java @@ -4,6 +4,9 @@ import com.baomidou.mybatisplus.extension.service.IService; import com.sdut.labex.entity.BorrowInfo; import com.sdut.labex.utils.ResVo; +import java.util.List; +import java.util.Map; + /** * @author springforest * @description 针对表【borrowInfo(借用记录)】的数据库操作Service @@ -11,11 +14,15 @@ import com.sdut.labex.utils.ResVo; */ public interface BorrowInfoService extends IService { - public ResVo addBorrowInfo(BorrowInfo borrowInfo); + public ResVo borrow(Map map); public ResVo deleteBorrowInfoById(Integer id); - public ResVo updateBorrowInfo(BorrowInfo borrowInfo); - public ResVo getBorrowInfo(BorrowInfo borrowInfo, int pageNum, int pageSize); + + public ResVo notBorrowedYet(List timeList, String date); + + public int isBorrowed(Map map); + + } diff --git a/src/main/java/com/sdut/labex/service/impl/BorrowInfoServiceImpl.java b/src/main/java/com/sdut/labex/service/impl/BorrowInfoServiceImpl.java index 7771f44..e04f11f 100644 --- a/src/main/java/com/sdut/labex/service/impl/BorrowInfoServiceImpl.java +++ b/src/main/java/com/sdut/labex/service/impl/BorrowInfoServiceImpl.java @@ -3,14 +3,19 @@ package com.sdut.labex.service.impl; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.sdut.labex.entity.BorrowInfo; +import com.sdut.labex.entity.Room; import com.sdut.labex.mapper.BorrowInfoMapper; import com.sdut.labex.service.BorrowInfoService; import com.sdut.labex.utils.ResVo; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; /** * @author springforest @@ -24,16 +29,21 @@ public class BorrowInfoServiceImpl extends ServiceImpl redisTemplate; + @Override - public ResVo addBorrowInfo(BorrowInfo borrowInfo) { + public ResVo borrow(Map map) { try { - borrowInfoMapper.insert(borrowInfo); + synchronized (this) {//线程锁 + //默认为通过审核(0未审核,1通过,2拒绝) + map.put("isAdmit", "1"); + borrowInfoMapper.borrow(map); + } } catch (Exception e) { e.printStackTrace(); return ResVo.error("借用失败"); } - - return ResVo.ok("借用成功"); } @@ -52,21 +62,6 @@ public class BorrowInfoServiceImpl extends ServiceImpl page = new Page<>(pageNum, pageSize); @@ -77,6 +72,40 @@ public class BorrowInfoServiceImpl extends ServiceImpl timeList, String date) { + List RBI; + String key = "notBorrowedYet:" + timeList.toString() + "_" + date; + ValueOperations valueOperations = redisTemplate.opsForValue(); + //拆箱的 'redisTemplate.hasKey(key)' 可能产生 'java.lang.NullPointerException + if (Boolean.TRUE.equals(redisTemplate.hasKey(key))) { + //缓存命中 + return (ResVo) valueOperations.get(key); + } else { + //缓存未命中,先添加缓存 + RBI = borrowInfoMapper.notBorrowedYet(timeList, date); + Map res = new HashMap<>(); + res.put("list", RBI); + //存入缓存 + valueOperations.set(key, ResVo.ok(res)); + //设置过期时间 + redisTemplate.expire(key, 12, TimeUnit.HOURS); + return ResVo.ok(res); + } + } + + @Override + public int isBorrowed(Map map) { + String date = map.get("date"); + String time = map.get("time"); + String roomName = map.get("roomName"); + if (borrowInfoMapper.isBorrowed(roomName, time, date) != null) { + return 1; + } else { + return 0; + } + } } diff --git a/src/main/resources/mapper/BorrowInfoMapper.xml b/src/main/resources/mapper/BorrowInfoMapper.xml index ca14b9c..fc172bd 100644 --- a/src/main/resources/mapper/BorrowInfoMapper.xml +++ b/src/main/resources/mapper/BorrowInfoMapper.xml @@ -17,6 +17,10 @@ insert into borrow_info (apply_user, time, date, apply_date, is_admit, room_name) VALUES (#{applyUser}, #{time}, #{date}, #{applyDate}, 1, #{roomName}); + + insert into borrow_info(name, time, date, applyDate, isAdmit, room_name) + values (#{name}, #{time}, #{date}, #{applyDate}, #{isAdmit}, #{roomName}) + + +