首页>代码>基于SpringBoot+thymeleaf实现的大学生自习室座位预定系统>/seat-subscribe-sys/src/main/java/com/cxs/controller/AdminController.java
package com.cxs.controller;

import com.cxs.dto.AdminAddRoomDTO;
import com.cxs.enums.OpenTypeEnum;
import com.cxs.model.Admin;
import com.cxs.model.Classroom;
import com.cxs.model.Seat;
import com.cxs.model.Student;
import com.cxs.service.AdminService;
import com.cxs.service.ClassroomService;
import com.cxs.service.SeatOrderService;
import com.cxs.service.SeatService;
import com.cxs.sourceBo.ClazzBo;
import com.cxs.vo.AdminLoginVO;
import com.cxs.vo.ClassroomVO;
import com.cxs.vo.Result;
import com.cxs.vo.SeatOrderVO;
import com.cxs.vo.StudentLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @Project:seat-subscribe-sys
 * @Author:cxs
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private ClassroomService classroomService;

    @Autowired
    private SeatService seatService;

    @Autowired
    private SeatOrderService seatOrderService;


    @RequestMapping("/")
    public String toIndex(HttpSession session, Model model) {
        Object user = session.getAttribute("loginUser");
        if (null != user) {
            if (user instanceof AdminLoginVO) {
                AdminLoginVO loginVO = (AdminLoginVO) user;
                model.addAttribute("user", loginVO);
                return "admin/index";
            }else {
                return "redirect:/auth/login";
            }
        } else {
            return "redirect:/auth/login";
        }
    }

    @RequestMapping("/toAdminList")
    public String toAdminList(Model model) {
        List<Admin> list = adminService.list(null);
        model.addAttribute("adminList", list);
        return "admin/adminList";
    }

    @RequestMapping("/toEditAdmin")
    public String toEditAdmin(@RequestParam("id") Integer id, Model model) {
        Admin admin = adminService.getById(id);
        model.addAttribute("editUser", admin);
        return "admin/editAdmin";
    }

    @RequestMapping("/deleteAdmin")
    public String deleteAdmin(@RequestParam("id") Integer id,Model model) {
        adminService.removeById(id);
        List<Admin> list = adminService.list(null);
        model.addAttribute("adminList", list);
        return "admin/adminList";
    }

    @RequestMapping("/toAddAdmin")
    public String toAddAdmin(Model model) {
        return "admin/addAdmin";
    }

    @RequestMapping("/toEditRoom")
    public String toEditRoom(@RequestParam("id") Integer id,Model model) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        Classroom classroom = classroomService.getById(id);
        ClassroomVO vo = new ClassroomVO();
        BeanUtils.copyProperties(classroom, vo);
        vo.setOpenTimeEndStr(formatter.format(classroom.getOpenTimeEnd()));
        vo.setOpenTimeStartStr(formatter.format(classroom.getOpenTimeStart()));
        vo.setOpenTimeRange(vo.getOpenTimeStartStr() + "-" + vo.getOpenTimeEndStr());
        vo.setOpenDesc(OpenTypeEnum.getDesc(classroom.getOpenType()));
        model.addAttribute("room", vo);
        return "admin/editRoom";
    }

    @RequestMapping("/updateAdmin")
    @ResponseBody
    public Map<String, Object> updateAdmin(Admin admin, Model model) {
        Map<String, Object> res = new HashMap<>();
        res.put("code", 200);
        res.put("msg", "操作成功");
        boolean b = adminService.updateById(admin);
        if (!b) {
            res.put("code", -1);
            res.put("msg", "操作失败");
        }
        return res;
    }

    @RequestMapping("/registerAdmin")
    @ResponseBody
    public Map<String, Object> registerAdmin(Admin admin, Model model) {
        Map<String, Object> res = new HashMap<>();
        res.put("code", 200);
        res.put("msg", "操作成功");
        boolean b = adminService.save(admin);
        if (!b) {
            res.put("code", -1);
            res.put("msg", "操作失败");
        }
        return res;
    }

    @RequestMapping("/toAddRoom")
    public String toAddRoom(Model model) {
        return "admin/addRoom";
    }

    @RequestMapping("/addRoom")
    @ResponseBody
    public Map<String, Object> addRoom(AdminAddRoomDTO room, Model model) {
        Map<String, Object> res = new HashMap<>();
        res.put("code", 200);
        res.put("msg", "操作成功");
        if (!addRoomCheck(room, res)) {
            return res;
        }
        Classroom classroom = new Classroom();
        BeanUtils.copyProperties(room, classroom);
        String timeRange = room.getTimeRange();
        String[] split = timeRange.split("-");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        classroom.setOpenTimeStart(LocalTime.parse(split[0].trim(), formatter));
        classroom.setOpenTimeEnd(LocalTime.parse(split[1].trim(), formatter));
        boolean b = classroomService.save(classroom);
        if (!b) {
            res.put("code", -1);
            res.put("msg", "操作失败");
        } else {
            List<Seat> seats = new ArrayList<>();
            for (int i = 1; i <= classroom.getSeatNum(); i++) {
                Seat seat = new Seat();
                seat.setSeatNo(i);
                seat.setClassroomId(classroom.getId());
                seats.add(seat);
            }
           seatService.saveBatch(seats);
        }
        return res;
    }

    @RequestMapping("/editRoom")
    @ResponseBody
    public Map<String, Object> editRoom(AdminAddRoomDTO room, Model model) {
        Map<String, Object> res = new HashMap<>();
        res.put("code", 200);
        res.put("msg", "操作成功");

        Classroom classroom = new Classroom();
        classroom.setId(room.getId());
        classroom.setName(room.getName());
        boolean b = classroomService.updateById(classroom);
        if (!b) {
            res.put("code", -1);
            res.put("msg", "操作失败");
        }
        return res;
    }

    @RequestMapping("/deleteRoom")
    public String deleteRoom(@RequestParam("id") Integer id,Model model) {
        classroomService.removeById(id);
        return "redirect:/room/toRoomList";
    }

    @RequestMapping("/toReservationList")
    public String toReservationList(HttpSession session, Model model) {
        List<SeatOrderVO> list = seatOrderService.getReservationBySno(null, null);
        model.addAttribute("list", list);
        return "admin/reservationList";
    }

    @RequestMapping("/toMyInfo")
    public String toMyInfo(HttpSession session, Model model) {
        Object user = session.getAttribute("loginUser");
        AdminLoginVO loginVO = (AdminLoginVO) user;
        Admin admin = adminService.getById(loginVO.getId());
        model.addAttribute("user", admin);
        return "admin/myInfo";
    }

    @RequestMapping("/updateInfo")
    @ResponseBody
    public Result updateInfo(Admin admin, HttpSession session, Model model) {
        Result vo = new Result(200, "OK");
        adminService.updateById(admin);
        return vo;
    }

    private boolean addRoomCheck(AdminAddRoomDTO room, Map<String, Object> res) {

        Integer seatNum = room.getSeatNum();
        if (seatNum > 50) {
            res.put("code", -1);
            res.put("msg", "座位数最大为50");
            return false;
        }
        String timeRange = room.getTimeRange();
        if (!StringUtils.hasLength(timeRange)) {
            res.put("code", -1);
            res.put("msg", "时间有误");
            return false;
        }

        String[] split = timeRange.split("-");
        if (null == split || split.length != 2) {
            res.put("code", -1);
            res.put("msg", "时间有误");
            return false;
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalTime s = LocalTime.parse(split[0].trim(), formatter);
        LocalTime e = LocalTime.parse(split[1].trim(), formatter);
        if (s.isAfter(e)) {
            res.put("code", -1);
            res.put("msg", "时间范围有误");
            return false;
        }
        return true;
    }
}
最近下载更多
wlax99  LV12 11月1日
yayacui  LV2 10月28日
lijianMark  LV6 9月23日
Peny_ZH  LV5 9月22日
ma406805131  LV15 6月15日
thsgli  LV8 6月13日
来一杯西瓜冰咩  LV6 5月11日
zolscy  LV12 3月24日
a318888331  LV13 3月10日
最近浏览更多
wlax99  LV12 10月31日
shunlun8855  LV1 10月29日
yayacui  LV2 10月28日
ckqlxm  LV1 10月25日
zwn258000 10月24日
暂无贡献等级
lijianMark  LV6 9月23日
Peny_ZH  LV5 9月21日
17558420274  LV16 9月15日
abdkfksdkf  LV16 8月25日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友