package com.example.servlet;
import com.example.pojo.ClassAll;
import com.example.pojo.ClassInfo;
import com.example.pojo.User;
import com.example.service.ClassAllService;
import com.example.service.ClassInfoService;
import com.example.service.UserService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@WebServlet("/Index")
public class Index extends HttpServlet {// 首页
// 设置放监听的Map
private Map<String, ScheduledExecutorService> schedulerMap = new HashMap<>();
// schedulerMap的获取方法
public Map<String, ScheduledExecutorService> getSchedulerMap() {
return schedulerMap;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
req.setCharacterEncoding("UTF-8");
String type = req.getParameter("type");// 前端传过来的type
ClassAllService classAllService = new ClassAllService();
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
int user_id = -1;
if(req.getSession().getAttribute("user_id") != null){
user_id = (int)req.getSession().getAttribute("user_id"); // 用户已登录,获取session中存的用户id
}
if("class_all".equals(type)) {
List<ClassAll> class_all = null;// 场地表
HashMap<Object, Object> dataMap = new HashMap<>();
try {
class_all = classAllService.getAllClassAll();// 获取所有场地表
dataMap.put("class_all", class_all);
dataMap.put("user_id", user_id);
}catch (SQLException e) {
throw new RuntimeException(e);
}
resp.setContentType("text/json;charset=utf-8");
PrintWriter pw = resp.getWriter();
pw.print(gson.toJson(dataMap));
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String type = req.getParameter("type");// 前端传过来的type
ClassInfoService classInfoService = new ClassInfoService();
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
int user_id = -1;
if(req.getSession().getAttribute("user_id") != null){
user_id = (int)req.getSession().getAttribute("user_id"); // 用户已登录,获取session中存的用户id
}
if("updateClass_Info".equals(type)) {
String message = null;
// 1.获取请求体传过来的值
String class_all_id_temp = req.getParameter("class_all_id");
String radioValue = req.getParameter("radioValue");
String checkboxValues = req.getParameter("checkboxValues");
JsonArray jsonArray = gson.fromJson(checkboxValues, JsonArray.class);
int class_all_id = Integer.parseInt(class_all_id_temp);
// 对checkboxValues进行处理
String[] names = new String[jsonArray.size()];
if(jsonArray.size() > 2){ //后端再次判断单次预约是否超过2小时
message = "单次预约不能超过2小时!哥们修改js是没用滴。";
}else{
for(int i = 0;i < jsonArray.size();i++){ // 数据处理,class_info表的name字段标准化
int checkboxValue = jsonArray.get(i).getAsInt();
// 处理names[i]为数据库对应格式
if(radioValue.equals("today")){
// 正常不用处理
}else if(radioValue.equals("tomorrow")){
checkboxValue = checkboxValue + 12;
}else{
checkboxValue = checkboxValue + 24;
}
names[i] = radioValue + "_th" + checkboxValue;
System.out.println(names[i]); // 输出测试
}
// 2.循环查询是否已经被预约
boolean is_right = true;
for(int i = 0;i < jsonArray.size();i++){
ClassInfo classInfo = null;
try {
classInfo = classInfoService.getClassInfo(class_all_id, names[i]);
System.out.println("state: " + classInfo.getClass_state() + ", user_id: " + classInfo.getUser_id());
if(classInfo.getClass_state() != 0 || classInfo.getUser_id() != 0){
is_right = false;
break;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if(!is_right){ // 3.返回提示已经被预约
message = "失败!您选择的场地已经被预约了!";
}else{ // 3.预约
is_right = true;
// 检查用户当前grade为多少,是否够预约
UserService userService = new UserService();
User user = null;
List<ClassInfo> user_classInfo = null;
// 查询用户等级可以预约的数量
int user_count = 0;
try {
user = userService.getUserById(user_id);
if(user.getGrade() == 0){
user_count = 2;
}else{
user_count = 6;
}
// 查询用户当前预约了多少小时
user_classInfo = classInfoService.getClassInfoByUser_id(user_id);
if(user_classInfo.size() + jsonArray.size() > user_count){ // 准备预约的和已有的加起来大于限制
message = "抱歉,超出用户当前级别最大可预约的数量!";
is_right = false;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
if(is_right){
// 循环update
int i;
for(i = 0;i < names.length;i++){
ClassInfo classInfo = new ClassInfo(-1, "", names[i], 1,user_id, class_all_id);
try {
int count = classInfoService.updateClassInfo(classInfo);
if(count != 1) {
message = "预约内部程序处理错误!";
break;
}
// --------超时自动取消部分-监听
// 2.根据class_info的name值转成时间,监听时间值每小时是否相等
String now_day = names[i].split("_th")[0]; // 日期
LocalDate today_temp = LocalDate.now();
没有合适的资源?快使用搜索试试~ 我知道了~
场地预约(javaweb实训项目)的所有代码
共179个文件
class:36个
jpg:24个
java:17个
需积分: 0 0 下载量 5 浏览量
2023-12-31
13:58:25
上传
评论
收藏 68.21MB ZIP 举报
温馨提示
场地预约(javaweb实训项目)的整体代码
资源推荐
资源详情
资源评论
收起资源包目录
场地预约(javaweb实训项目)的所有代码 (179个子文件)
Index.class 11KB
Index.class 11KB
myCenter.class 8KB
myCenter.class 8KB
FindPwd.class 7KB
FindPwd.class 7KB
UserDAO.class 5KB
UserDAO.class 5KB
login.class 5KB
login.class 5KB
register.class 5KB
register.class 5KB
ClassInfoDAO.class 4KB
ClassInfoDAO.class 4KB
ClassAllDAO.class 3KB
ClassAllDAO.class 3KB
User.class 2KB
User.class 2KB
MyFilter.class 2KB
MyFilter.class 2KB
ClassInfo.class 2KB
ClassInfo.class 2KB
UserService.class 2KB
UserService.class 2KB
DruidUtil.class 2KB
DruidUtil.class 2KB
ClassInfoService.class 1KB
ClassInfoService.class 1KB
ClassAllService.class 1KB
ClassAllService.class 1KB
ClassAll.class 1KB
ClassAll.class 1KB
FindPwd$1.class 985B
FindPwd$1.class 985B
My_Result.class 909B
My_Result.class 909B
mvnw.cmd 7KB
bootstrap.css 141KB
bootstrap.css 141KB
register.css 5KB
register.css 5KB
find_pwd.css 4KB
find_pwd.css 4KB
login.css 4KB
login.css 4KB
index.css 3KB
index.css 3KB
add.css 3KB
add.css 3KB
mycenter.css 2KB
mycenter.css 2KB
icomoon.eot 7KB
icomoon.eot 7KB
.gitignore 490B
.gitignore 190B
add.html 5KB
add.html 5KB
register.html 3KB
register.html 3KB
find_pwd.html 3KB
find_pwd.html 3KB
login.html 3KB
login.html 3KB
mycenter.html 2KB
mycenter.html 2KB
login.html 1KB
index.html 1KB
index.html 1KB
test.html 396B
index.html 346B
index.html 346B
图标.ico 4KB
favicon.ico 4KB
图标.ico 4KB
favicon.ico 4KB
druid-1.2.18.jar 3.71MB
mysql-connector-j-8.0.32.jar 2.37MB
protobuf-java-3.21.9.jar 1.59MB
javax.mail-1.6.2.jar 644KB
junit-jupiter-params-5.8.1.jar 562KB
gson-2.10.1.jar 277KB
junit-jupiter-engine-5.8.1.jar 224KB
javax.mail-api-1.6.2.jar 214KB
junit-jupiter-api-5.8.1.jar 189KB
junit-platform-engine-1.8.1.jar 181KB
junit-platform-commons-1.8.1.jar 98KB
activation-1.1.jar 62KB
opentest4j-1.2.0.jar 7KB
apiguardian-api-1.1.2.jar 7KB
junit-jupiter-5.8.1.jar 6KB
Index.java 14KB
myCenter.java 7KB
FindPwd.java 7KB
UserDAO.java 6KB
login.java 4KB
ClassInfoDAO.java 3KB
register.java 3KB
ClassAllDAO.java 2KB
User.java 2KB
ClassInfo.java 2KB
共 179 条
- 1
- 2
资源评论
0白露
- 粉丝: 2
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- projectData
- SQL Server 性能监视器,它旨在提供开箱即用的全面监控,并作为您自己的项目或应用程序的监控框架 它在本地数据库中收集性能
- Python 程序语言设计模式思路-并发模式:线程池模式:管理线程池,优化线程创建和销毁
- 股事汇投资工具-实时新闻、财经日历、市场快讯、持仓查询、外汇兑换、换算工具、大盘云图、江恩工具、指标检测等
- webrtc-streamer
- html+css+'青春献礼二十大 红色旅游助乡村'为主题的网页设计 2022年参与学校网页设计比赛时完成的
- Unity 模型布尔运算插件
- eCANtools工具,CAN资料包
- 129335283047061xiazaigongjuxiang(去重软件).apk
- Android环境检测工具,检测ksu,lsp,magisk等
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功