2023.6.15

1. 大框架基本完成
This commit is contained in:
KaiyuanOSG 2023-06-15 20:34:47 +08:00
parent 8bd543e9ba
commit f8723d51fc
5 changed files with 136 additions and 28 deletions

View File

@ -1,5 +1,6 @@
package com.sdut.labex.controller; package com.sdut.labex.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.sdut.labex.Factory.BorrowInfoFactory; import com.sdut.labex.Factory.BorrowInfoFactory;
import com.sdut.labex.entity.BorrowInfo; import com.sdut.labex.entity.BorrowInfo;
@ -8,6 +9,10 @@ import com.sdut.labex.utils.ResVo;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* File: BorrowInfoController * File: BorrowInfoController
@ -24,14 +29,49 @@ public class BorrowInfoController {
@PostMapping("/borrowInfo") @PostMapping("/borrowInfo")
public ResVo addBorrowInfo(@RequestBody JSONObject jsonObject) { public ResVo addBorrowInfo(@RequestBody JSONObject jsonObject) {
BorrowInfo borrowInfo = BorrowInfoFactory.createBorrowInfo(jsonObject); Map<String, String> map = new HashMap<>();
return borrowInfoService.addBorrowInfo(borrowInfo); 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") @PostMapping("/borrowInfo/notBorrowedYet")
public ResVo updateBorrowInfo(@RequestBody JSONObject jsonObject) { public ResVo notBorrowedYet(@RequestBody JSONObject jsonObject) {
BorrowInfo borrowInfo = BorrowInfoFactory.createBorrowInfo(jsonObject); JSONArray jsonArray = jsonObject.getJSONArray("time");
return borrowInfoService.updateBorrowInfo(borrowInfo);
String date = jsonObject.getString("date");
List<String> timeList = new ArrayList<>();
for (Object item : jsonArray) {
timeList.add(item.toString());
}
return borrowInfoService.notBorrowedYet(timeList, date);
} }
@DeleteMapping("/borrowInfo/{id}") @DeleteMapping("/borrowInfo/{id}")

View File

@ -3,7 +3,9 @@ package com.sdut.labex.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdut.labex.entity.BorrowInfo; import com.sdut.labex.entity.BorrowInfo;
import com.sdut.labex.entity.Room;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -18,9 +20,15 @@ import java.util.Map;
public interface BorrowInfoMapper extends BaseMapper<BorrowInfo> { public interface BorrowInfoMapper extends BaseMapper<BorrowInfo> {
public Page<BorrowInfo> queryBorrowInfoByOptions(Page<BorrowInfo> page, BorrowInfo borrowInfo); public Page<BorrowInfo> queryBorrowInfoByOptions(Page<BorrowInfo> page, BorrowInfo borrowInfo);
public BorrowInfo borrow(Map<String, String> map);
public void addTimeTable(Map<String, String> map); public void addTimeTable(Map<String, String> map);
public List<BorrowInfo> listAllByDateAndRoom(String date, String room); public List<BorrowInfo> listAllByDateAndRoom(String date, String room);
public BorrowInfo isBorrowed(@Param("roomName") String roomName, @Param("time") String time, @Param("date") String date);
public List<Room> notBorrowedYet(@Param("timeList") List<String> timeList, @Param("date") String date);
} }

View File

@ -4,6 +4,9 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.sdut.labex.entity.BorrowInfo; import com.sdut.labex.entity.BorrowInfo;
import com.sdut.labex.utils.ResVo; import com.sdut.labex.utils.ResVo;
import java.util.List;
import java.util.Map;
/** /**
* @author springforest * @author springforest
* @description 针对表borrowInfo(借用记录)的数据库操作Service * @description 针对表borrowInfo(借用记录)的数据库操作Service
@ -11,11 +14,15 @@ import com.sdut.labex.utils.ResVo;
*/ */
public interface BorrowInfoService extends IService<BorrowInfo> { public interface BorrowInfoService extends IService<BorrowInfo> {
public ResVo addBorrowInfo(BorrowInfo borrowInfo); public ResVo borrow(Map<String, String> map);
public ResVo deleteBorrowInfoById(Integer id); public ResVo deleteBorrowInfoById(Integer id);
public ResVo updateBorrowInfo(BorrowInfo borrowInfo);
public ResVo getBorrowInfo(BorrowInfo borrowInfo, int pageNum, int pageSize); public ResVo getBorrowInfo(BorrowInfo borrowInfo, int pageNum, int pageSize);
public ResVo notBorrowedYet(List<String> timeList, String date);
public int isBorrowed(Map<String, String> map);
} }

View File

@ -3,14 +3,19 @@ package com.sdut.labex.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.labex.entity.BorrowInfo; import com.sdut.labex.entity.BorrowInfo;
import com.sdut.labex.entity.Room;
import com.sdut.labex.mapper.BorrowInfoMapper; import com.sdut.labex.mapper.BorrowInfoMapper;
import com.sdut.labex.service.BorrowInfoService; import com.sdut.labex.service.BorrowInfoService;
import com.sdut.labex.utils.ResVo; 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 org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
/** /**
* @author springforest * @author springforest
@ -24,16 +29,21 @@ public class BorrowInfoServiceImpl extends ServiceImpl<BorrowInfoMapper, BorrowI
@Resource @Resource
private BorrowInfoMapper borrowInfoMapper; private BorrowInfoMapper borrowInfoMapper;
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Override @Override
public ResVo addBorrowInfo(BorrowInfo borrowInfo) { public ResVo borrow(Map<String, String> map) {
try { try {
borrowInfoMapper.insert(borrowInfo); synchronized (this) {//线程锁
//默认为通过审核(0未审核1通过2拒绝)
map.put("isAdmit", "1");
borrowInfoMapper.borrow(map);
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return ResVo.error("借用失败"); return ResVo.error("借用失败");
} }
return ResVo.ok("借用成功"); return ResVo.ok("借用成功");
} }
@ -52,21 +62,6 @@ public class BorrowInfoServiceImpl extends ServiceImpl<BorrowInfoMapper, BorrowI
return ResVo.ok("删除成功"); return ResVo.ok("删除成功");
} }
@Override
public ResVo updateBorrowInfo(BorrowInfo borrowInfo) {
if (borrowInfoMapper.selectById(borrowInfo.getId()) == null) {
return ResVo.error("该借用记录不存在");
}
try {
borrowInfoMapper.updateById(borrowInfo);
} catch (Exception e) {
e.printStackTrace();
return ResVo.error("更新失败");
}
return ResVo.ok("更新成功");
}
@Override @Override
public ResVo getBorrowInfo(BorrowInfo borrowInfo, int pageNum, int pageSize) { public ResVo getBorrowInfo(BorrowInfo borrowInfo, int pageNum, int pageSize) {
Page<BorrowInfo> page = new Page<>(pageNum, pageSize); Page<BorrowInfo> page = new Page<>(pageNum, pageSize);
@ -77,6 +72,40 @@ public class BorrowInfoServiceImpl extends ServiceImpl<BorrowInfoMapper, BorrowI
res.put("list", page.getRecords()); res.put("list", page.getRecords());
return ResVo.ok(res); return ResVo.ok(res);
} }
@Override
public ResVo notBorrowedYet(List<String> timeList, String date) {
List<Room> RBI;
String key = "notBorrowedYet:" + timeList.toString() + "_" + date;
ValueOperations<String, Object> 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<String, Object> 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<String, String> 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;
}
}
} }

View File

@ -17,6 +17,10 @@
insert into borrow_info (apply_user, time, date, apply_date, is_admit, room_name) insert into borrow_info (apply_user, time, date, apply_date, is_admit, room_name)
VALUES (#{applyUser}, #{time}, #{date}, #{applyDate}, 1, #{roomName}); VALUES (#{applyUser}, #{time}, #{date}, #{applyDate}, 1, #{roomName});
</insert> </insert>
<insert id="borrow" parameterType="map">
insert into borrow_info(name, time, date, applyDate, isAdmit, room_name)
values (#{name}, #{time}, #{date}, #{applyDate}, #{isAdmit}, #{roomName})
</insert>
<select id="queryBorrowInfoByOptions" resultType="com.sdut.labex.entity.BorrowInfo"> <select id="queryBorrowInfoByOptions" resultType="com.sdut.labex.entity.BorrowInfo">
select * select *
@ -35,4 +39,24 @@
</if> </if>
</where> </where>
</select> </select>
<select id="isBorrowed" resultType="com.sdut.labex.entity.BorrowInfo">
select *
from borrow_info
where date = #{date}
and time = #{time}
and roomName = #{roomName};
</select>
<select id="notBorrowedYet" resultType="com.sdut.labex.entity.Room">
SELECT *
FROM room
WHERE name NOT IN (
SELECT room_name
FROM borrow_info
WHERE is_admit = 1 AND date = #{date}
AND time IN
<foreach collection="timeList" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
)
</select>
</mapper> </mapper>