教务管理系统之controller层

AllController

package com.example.academicadministration.controller;


import com.example.academicadministration.service.StudentService;
import com.example.academicadministration.service.TeacherService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class AllController {

    @SneakyThrows
    @RequestMapping("/login")
    public void login(String id, String pwd, String kind, @Param("sub") String sub, HttpServletRequest request, HttpServletResponse response){
        if (sub==null||sub.equals("注册")){
            switch (kind){
                case "student":
                    request.getRequestDispatcher("/student/toRegister").forward(request,response);
                    return;
                case "teacher":
                    request.getRequestDispatcher("/teacher/toRegister").forward(request,response);
                    return;
                default:
                   response.getWriter().print("管理员无法被注册");
                    return;
            }
        }
        else {
            switch (kind){
                case "student":
                    request.getRequestDispatcher("/student/login").forward(request,response);
                    return;
                case "teacher":
                    request.getRequestDispatcher("/teacher/login").forward(request,response);
                    return;
                case "manger":
                    request.getRequestDispatcher("/manger/login").forward(request,response);
                    return;
            }
        }
    }
    @RequestMapping("/weclome")
    public String weclome(){
        return "weclome";
    }

}

MangerController

package com.example.academicadministration.controller;

import com.example.academicadministration.pojo.Course;
import com.example.academicadministration.pojo.Student;
import com.example.academicadministration.pojo.Teacher;
import com.example.academicadministration.service.CourseService;
import com.example.academicadministration.service.MangerService;
import com.example.academicadministration.service.StudentService;
import com.example.academicadministration.service.TeacherService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.*;

@Controller
@RequestMapping("/manger")
public class MangerController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private MangerService mangerService;
    private Boolean check(Object o){
        return o==null||o.equals("");
    }
    @SneakyThrows
    private String getImg(byte[] im){
        BufferedImage img= ImageIO.read(new ByteArrayInputStream(im));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img,"png",baos);
        byte[] encodedBytes = Base64.getEncoder().encode(baos.toByteArray());
        String encodedImage = new String(encodedBytes);
        String imgSrc="data:image/png;base64,"+encodedImage;
        return imgSrc;
    }
    @SneakyThrows
    @ResponseBody
    @RequestMapping("/login")
    public String login(String id, String pwd,HttpServletRequest request){
        String res=mangerService.login(id);
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(pwd.getBytes());
        String Hpwd=new BigInteger(1,md.digest()).toString(16);
        if(res==null){
            return "用户不存在";
        }
        else if(!res.equals(Hpwd)){
            return "密码错误";
        }
        else {
            request.getSession().setAttribute("mangerid",id);
            return "登录成功";}
    }
    @RequestMapping("/function")
    public String function(Model model){
        return "/manger/mangerFunction";
    }
    @RequestMapping("/toChangePwd")
    public String toChangePwd(){
        return "/manger/changePwd";
    }
    @SneakyThrows
    @ResponseBody
    @RequestMapping("/changePwd")
    public String changePwd(String oldpwd, String newpwd1, HttpServletRequest request){
        if(check(newpwd1)){
            return "密码不能为空字符";
        }
        if(mangerService.reset((String) request.getSession().getAttribute("mangerid"),oldpwd,newpwd1)) {
            return "修改成功";
        }
        else {
            return "密码错误";
        }
    }

    @SneakyThrows
    @RequestMapping("/browseStudent")
    public String browseStudent(Model model,String condition){
       if(!check(condition)) condition="%"+condition+"%";
       else condition="%%";
       List<Student> studentList=studentService.browseStudent(condition);
       Map<String,String> stuImg=new HashMap<>();
       for(Student i:studentList){
           if(i.getStuphoto()==null||i.getStuphoto().length<1){
               stuImg.put(i.getStuid(),null);
               continue;
           }
           stuImg.put(i.getStuid(),getImg(i.getStuphoto()));
       }

       model.addAttribute("stuList",studentList);
       model.addAttribute("stuImg",stuImg);
       return "/manger/browseStudent";
    }
    @SneakyThrows
    @RequestMapping("/auditStudent")
    public void auditStudent(String stuid,HttpServletResponse response){
        if(studentService.auditStudent(stuid)){
            response.getWriter().print("<script>alert('激活成功');location.replace('/manger/browseStudent')</script>");
        }
        else {
            response.getWriter().print("<script>alert('激活失败');location.replace('/manger/browseStudent')</script>");
        }
    }
    @SneakyThrows
    @RequestMapping("/inauditStudent")
    public void inauditStudent(String stuid,HttpServletResponse response){
        if(studentService.inauditStudent(stuid)){
            response.getWriter().print("<script>alert('冻结成功');location.replace('/manger/browseStudent')</script>");
        }
        else {
            response.getWriter().print("<script>alert('冻结失败');location.replace('/manger/browseStudent')</script>");
        }
    }
    @SneakyThrows
    @RequestMapping("/auditTeacher")
    public void auditTeacher(String teaid,HttpServletResponse response){
        if(teacherService.auditTeacher(teaid)){
            response.getWriter().print("<script>alert('激活成功');location.replace('/manger/browseTeacher')</script>");
        }
        else {
            response.getWriter().print("<script>alert('激活失败');location.replace('/manger/browseTeacher')</script>");
        }
    }
    @SneakyThrows
    @RequestMapping("/inauditTeacher")
    public void inauditTeacher(String teaid,HttpServletResponse response){
        if(teacherService.inauditTeacher(teaid)){
            response.getWriter().print("<script>alert('冻结成功');location.replace('/manger/browseTeacher')</script>");
        }
        else {
            response.getWriter().print("<script>alert('冻结失败');location.replace('/manger/browseTeacher')</script>");
        }
    }

    @SneakyThrows
    @RequestMapping("/studentDetail")
    public String studentDetail(String stuid, Model model){
        List<Student> students=studentService.browseStudent("%%");
        for (Student i:students){
            if(i.getStuid().equals(stuid)){
                model.addAttribute("stu",i);
                if(i.getStuphoto()!=null&&i.getStuphoto().length>0)
                    model.addAttribute("Img",getImg(i.getStuphoto()));
                else model.addAttribute("Img",null);
                break;
            }
        }
        return "/manger/studentDetail";
    }
    @SneakyThrows
    @RequestMapping("/deleteStudent")
    public void deleteStudent(String stuid,HttpServletResponse response){
        if(studentService.deleteStudent(stuid)){
            response.getWriter().print("<script>alert('删除成功');location.replace('/manger/browseStudent')</script>");
        }
        else {
            response.getWriter().print("<script>alert('删除失败');location.replace('/manger/browseStudent')</script>");
        }
    }
    @SneakyThrows
    @RequestMapping("/resetStudent")
    public void resetStudent(String stuid,String stupwd,String newpwd,HttpServletResponse response){
        if(studentService.reset(stuid,stupwd,"123456")){
            response.getWriter().print("<script>alert('重置成功');location.replace('/manger/browseStudent')</script>");
        }
        else {
            response.getWriter().print("<script>alert('重置失败');location.replace('/manger/browseStudent')</script>");
        }
    }

    @SneakyThrows
    @RequestMapping("/browseTeacher")
    public String browseTeacher(String condition,Model model){
        if(!check(condition)) condition="%"+condition+"%";
        else condition="%%";
        List<Teacher> teaList=teacherService.browseTeacher(condition);
        Map<String,String> teaImg=new HashMap<>();
        for(Teacher i:teaList){
            if(i.getTeaphoto()==null||i.getTeaphoto().length<1){
                teaImg.put(i.getTeaid(),null);
                continue;
            }
            teaImg.put(i.getTeaid(),getImg(i.getTeaphoto()));
        }
        model.addAttribute("teaList",teaList);
        model.addAttribute("teaImg",teaImg);
        return "/manger/browseTeacher";
    }
    @RequestMapping("/teacherDetail")
    public String teacherDetail(String teaid,Model model){
        List<Teacher> teachers=teacherService.browseTeacher("%%");
        for (Teacher i:teachers){
            if(i.getTeaid().equals(teaid)){
                model.addAttribute("tea",i);
                if(i.getTeaphoto()!=null&&i.getTeaphoto().length>0)
                model.addAttribute("Img",getImg(i.getTeaphoto()));
                else model.addAttribute("Img",null);
                break;
            }
        }
        return "/manger/teacherDetail";
    }
    @SneakyThrows
    @RequestMapping("/deleteTeacher")
    public void deleteTeacher(String teaid,HttpServletResponse response){
        if(teacherService.deleteTeacher(teaid)){
            response.getWriter().print("<script>alert('删除成功');location.replace('/manger/browseTeacher')</script>");
        }
        else {
            response.getWriter().print("<script>alert('删除失败');location.replace('/manger/browseTeacher')</script>");
        }
    }
    @SneakyThrows
    @RequestMapping("/resetTeacher")
    public void resetTeacher(String teaid,String teapwd,String newpwd,HttpServletResponse response){
        if(teacherService.reset(teaid,teapwd,"123456")){
            response.getWriter().print("<script>alert('重置成功');location.replace('/manger/browseTeacher')</script>");
        }
        else {
            response.getWriter().print("<script>alert('重置失败');location.replace('/manger/browseTeacher')</script>");
        }
    }
    @SneakyThrows
    @ResponseBody
    @RequestMapping("/addCourse")
    public  String  addCourse(Course course){
        if(courseService.addCourse(course)){
           return "添加成功";
        }
        else {
            return "添加失败";
        }
    }
    @SneakyThrows
    @RequestMapping("/addTeacherCourse")
    public void addTeacherCourse(String teaid,String courseid,HttpServletResponse response){
        if(courseService.addTeacherCourse(teaid,courseid)){
            response.getWriter().print("<script>alert('添加成功');location.replace('/manger/browseCourse')</script>");
        }
        else {
            response.getWriter().print("<script>alert('添加失败');location.replace('/manger/browseCourse')</script>");
        }
    }
    @SneakyThrows
    @RequestMapping("/browseCourse")
    public String browseCourse(String condition,String teaCondition,Model model){
        Boolean flag=check(teaCondition)? false:true;
        if(!check(condition)) condition="%"+condition+"%";
        else condition="%%";
        if(!check(teaCondition)) teaCondition="%"+teaCondition+"%";
        else teaCondition="%%";
        List<Course> courses=courseService.browseCourse(condition);
        List<Teacher> teachers=teacherService.browseTeacher("%%");
        List<Course> courseList=new ArrayList<>();
            for (Course i:courses){
                for(Teacher j:teachers){
                    if(courseService.checkTeacher(String.valueOf(i.getCourseid()),j.getTeaid())){
                        i.setTeacher(j);
                    }
                }
            }
            if(flag){
                List<Teacher> teachers1=teacherService.browseTeacher(teaCondition);
                for(Course i:courses){
                    if(teachers1.contains(i.getTeacher())){
                        courseList.add(i);
                    }
                }
                model.addAttribute("courseList",courseList);
            }
            else {
                model.addAttribute("courseList",courses);
            }
            model.addAttribute("teachers",teachers);
        return "/manger/browseCourse";
    }
    @SneakyThrows
    @RequestMapping("/deleteCourse")
    public void deleteCourse(String courseid,HttpServletResponse response){
        if(courseService.deleteCourse(courseid)){
            response.getWriter().print("<script>alert('删除成功');location.replace('/manger/browseCourse')</script>");
        }
        else {
            response.getWriter().print("<script>alert('删除失败');location.replace('/manger/browseCourse')</script>");
        }
    }
}

StudentController

package com.example.academicadministration.controller;

import com.example.academicadministration.pojo.*;
import com.example.academicadministration.service.*;
import com.example.academicadministration.utils.SendMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.sql.Timestamp;
import java.util.*;

@Controller
@RequestMapping("/student")
public class StudentController {
    private int hid;
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private HomeworkService homeworkService;
    @Autowired
    private AnswerService answerService;
    private Boolean check(Object o){
        return o==null||o.equals("");
    }
    @SneakyThrows
    private String getImg(byte[] im){
        BufferedImage img= ImageIO.read(new ByteArrayInputStream(im));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img,"png",baos);
        byte[] encodedBytes = Base64.getEncoder().encode(baos.toByteArray());
        String encodedImage = new String(encodedBytes);
        String imgSrc="data:image/png;base64,"+encodedImage;
        return imgSrc;
    }
    @SneakyThrows
    @ResponseBody
    @RequestMapping("/login")
    public String login(String id, String pwd, HttpServletRequest request){
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(pwd.getBytes());
        String Hpwd=new BigInteger(1,md.digest()).toString(16);
        List<Student> students=studentService.browseStudent("%%");
        for(Student i:students){
            if(i.getStuid().equals(id)){
                if(i.getStupwd().equals(Hpwd)){
                    if (i.getStustate()){
                        i.setStupwd(pwd);
                        request.getSession().setAttribute("StudentPerson",i);
                        if(i.getStuphoto()!=null&&i.getStuphoto().length>0){
                            request.getSession().setAttribute("StudentPersonImg",getImg(i.getStuphoto()));
                        }
                        else request.getSession().setAttribute("StudentPersonImg",null);
                        return "登录成功";
                    }
                    return "账号未激活";
                }
                return "密码错误";
            }
        }
        return "没有该用户";
    }
    @RequestMapping("/toRegister")
    public String toRegister(){
        return "student/studentRegister";
    }
    @SneakyThrows
    @ResponseBody
    @RequestMapping("/register")
    public String register(Student student,MultipartFile photo){
        if(photo!=null&&!photo.isEmpty())
        student.setStuphoto(photo.getBytes());
        else student.setStuphoto(null);
        if(studentService.addStudent(student)){
           return "注册成功";
        }
        else{
            return "注册失败";
        }
    }
    @SneakyThrows
    @ResponseBody
    @RequestMapping("/getFlag")
    public String getFlag(String phone){
        String flag= SendMessage.message(phone);
        return flag;
    }
    @RequestMapping("/function")
    public String function(){
        return "/student/studentFunction";
    }
    @RequestMapping("/toChangePwd")
    public String toChangePwd(){
        return "/student/changePwd";
    }
    @SneakyThrows
    @ResponseBody
    @RequestMapping("/changePwd")
    public String changePwd(String oldpwd,String newpwd1,HttpServletRequest request){
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(oldpwd.getBytes());
        String Hpwd=new BigInteger(1,md.digest()).toString(16);
        if(check(newpwd1)){
            return "密码不能为空字符";
        }
        Student t=(Student) request.getSession().getAttribute("StudentPerson");
        if(studentService.reset(t.getStuid(),Hpwd,newpwd1)) {
            return "修改成功";
        }
        else {
            return "密码错误";
        }
    }
    @RequestMapping("/detail")
    public String detail(Model model,HttpServletRequest request){
        Student i=(Student)request.getSession().getAttribute("StudentPerson");
        model.addAttribute("stu",i);
        if(i.getStuphoto()!=null&&i.getStuphoto().length>0)
            model.addAttribute("Img",getImg(i.getStuphoto()));
        else model.addAttribute("Img",null);
        return "/student/studentDetail";
    }
    @SneakyThrows
    @RequestMapping("/browseCourse")
    public String browseCourse(String condition,String teaCondition,Model model){
        Boolean flag=check(teaCondition)? false:true;
        if(!check(condition)) condition="%"+condition+"%";
        else condition="%%";
        if(!check(teaCondition)) teaCondition="%"+teaCondition+"%";
        else teaCondition="%%";
        List<Course> courses=courseService.browseCourse(condition);
        List<Teacher> teachers=teacherService.browseTeacher("%%");
        List<Course> courseList=new ArrayList<>();
        for (Course i:courses){
            for(Teacher j:teachers){
                if(courseService.checkTeacher(String.valueOf(i.getCourseid()),j.getTeaid())){
                    i.setTeacher(j);
                }
            }
        }
        if(flag){
            List<Teacher> teachers1=teacherService.browseTeacher(teaCondition);
            for(Course i:courses){
                if(teachers1.contains(i.getTeacher())){
                    courseList.add(i);
                }
            }
            model.addAttribute("courseList",courseList);
        }
        else {
            model.addAttribute("courseList",courses);
        }
        model.addAttribute("teachers",teachers);
        return "/student/browseCourse";
    }
    @SneakyThrows
    @RequestMapping("/addStudentCourse")
    public void addStudentCourse(String courseid,HttpServletResponse response,HttpServletRequest request){
        Student i=(Student)request.getSession().getAttribute("StudentPerson");
        List<Course> courses=courseService.myCourse(i.getStuid());
        for(Course j:courses){
            if(String.valueOf(j.getCourseid()).equals(courseid)){
                response.getWriter().print("<script>alert('已有该课程');location.replace('/student/browseCourse')</script>");
                return;
            }
        }
        if(courseService.addStudentCourse(i.getStuid(),courseid)&&courseService.addNum(courseid)){
            response.getWriter().print("<script>alert('选课成功');location.replace('/student/browseCourse')</script>");
        }
        else {
            response.getWriter().print("<script>alert('选课失败');location.replace('/student/browseCourse')</script>");
        }

    }
    @RequestMapping("/myCourse")
    public String myCourse(Model model,HttpServletRequest request){
        Student i=(Student)request.getSession().getAttribute("StudentPerson");
        List<Course> courses=courseService.myCourse(i.getStuid());
        List<Teacher> teachers=teacherService.browseTeacher("%%");
       Map<Integer,String> score=new HashMap<>();
        for(Course j:courses) {
            String sc=courseService.browseScore(i.getStuid(), String.valueOf(j.getCourseid()));
            if(sc.equals("-1")){
                score.put(j.getCourseid(),null);
            }
            else score.put(j.getCourseid(), sc);
            for (Teacher k : teachers) {
                if (courseService.checkTeacher(String.valueOf(j.getCourseid()), k.getTeaid())) {
                    j.setTeacher(k);
                }
            }
        }
        model.addAttribute("courseList",courses);
        model.addAttribute("score",score);
        return "/student/myCourse";
    }
    @SneakyThrows
    @RequestMapping("/deleteStudentCourse")
    public void deleteSelection(String courseid,HttpServletResponse response,HttpServletRequest request){
        Student i=(Student)request.getSession().getAttribute("StudentPerson");
        if(courseService.deleteStudentCourse(i.getStuid(),courseid)&&courseService.deleteNum(courseid)){
            response.getWriter().print("<script>alert('退课成功');location.replace('/student/myCourse')</script>");
        }
        else {
            response.getWriter().print("<script>alert('退课失败');location.replace('/student/myCourse')</script>");
        }
    }

    @SneakyThrows
    @RequestMapping("/browseVideo")
    public String browseMaterial(String courseid,String condition,Model model,HttpServletRequest request){
        if(courseid==null) courseid= (String) request.getSession().getAttribute("courseid");
        else request.getSession().setAttribute("courseid",courseid);
        if(condition==null) condition="%%";
        else condition="%"+condition+"%";
        List<Video> videoList=videoService.browseVideo(condition);
        Map<Integer,String> videoImg=new HashMap<>();
        for(Video i:videoList){
            videoImg.put(i.getVideoid(),getImg(i.getVideocover()));
        }
        model.addAttribute("videoList",videoList);
        model.addAttribute("videoImg",videoImg);
        return "/student/browseVideo";
    }

    @SneakyThrows
    @RequestMapping("/browseHomework")
    public String browseHomework(String courseid,String kind,Model model,HttpServletRequest request){
        if(courseid==null) courseid=(String) request.getSession().getAttribute("courseid");
        else request.getSession().setAttribute("courseid",courseid);
        List<Homework> homeworkList=new ArrayList<>();
        if(check(kind)||kind.equals("0")){
            homeworkList=homeworkService.browseHomeworkAll(courseid);
        }
        else if(kind.equals("1")){
            homeworkList=homeworkService.browseHomework1(courseid);
        }
        else if(kind.equals("2")){
            homeworkList=homeworkService.browseHomework2(courseid);
        }

        model.addAttribute("homeworkList",homeworkList);
        return "/student/browseHomework";
    }
    @SneakyThrows
    @RequestMapping("/homeworkDetail")
    public String homeworkDetail(String homeworkid,String index,HttpServletRequest request,Model model){
        if(homeworkid==null) homeworkid=(String) request.getSession().getAttribute("homeworkid");
        else request.getSession().setAttribute("homeworkid",homeworkid);
        model.addAttribute("index",index);
        String courseid=(String) request.getSession().getAttribute("courseid");
        Student stu=(Student)request.getSession().getAttribute("StudentPerson");
        String stuid=stu.getStuid();
        List<Homework> homeworkList=homeworkService.browseHomeworkAll(courseid);
        for(Homework i:homeworkList){
            if(homeworkid.equals(String.valueOf(i.getHomeworkid()))){
                model.addAttribute("homework",i);
                model.addAttribute("homeworkImg",getImg(i.getHomeworkfile()));
                break;
            }
        }
       Answer answer=answerService.myAnswer(stuid,homeworkid);
        model.addAttribute("answer",answer);
        if (answer!=null)
        model.addAttribute("answerImg",getImg(answer.getAnswerfile()));

        return "/student/homeworkDetail";
    }
    @ResponseBody
    @SneakyThrows
    @RequestMapping("/addAnswer")
    public String addAnswer(Answer answer,MultipartFile answerImg,HttpServletRequest request){
        String homeworkid=(String) request.getSession().getAttribute("homeworkid");
        answer.setAnswerfile(answerImg.getBytes());
        Student stu=(Student)request.getSession().getAttribute("StudentPerson");
        String stuid=stu.getStuid();
        answer.setStuid(stuid);
        answer.setHomeworkid(Integer.valueOf(homeworkid));
        answer.setAnswerfile(answerImg.getBytes());
        answer.setScore(-1);
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        answer.setSubmittime(timestamp);
        if(answerService.addAnswer(answer)){
           return "上传成功";
        }
        else {
            return "上传失败";
        }
    }
    @ResponseBody
    @SneakyThrows
    @RequestMapping("/updateAnswer")
    public String updateAnswer(Answer answer,MultipartFile answerImg,HttpServletRequest request){
        String homeworkid=(String) request.getSession().getAttribute("homeworkid");
        answer.setAnswerfile(answerImg.getBytes());
        Student stu=(Student)request.getSession().getAttribute("StudentPerson");
        String stuid=stu.getStuid();
        answer.setStuid(stuid);
        answer.setHomeworkid(Integer.valueOf(homeworkid));
        answer.setAnswerfile(answerImg.getBytes());
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        answer.setSubmittime(timestamp);
        if(answerService.updateAnswer(answer)){
            return "上传成功";
        }
        else {
            return "上传失败";
        }
    }
}

 

TeacherController

package com.example.academicadministration.controller;

import com.example.academicadministration.pojo.*;
import com.example.academicadministration.service.*;
import com.example.academicadministration.utils.SendMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.time.LocalDate;
import java.util.*;



@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private HomeworkService homeworkService;
    @Autowired
    private AnswerService answerService;

    private Boolean check(Object o){
        return o==null||o.equals("");
    }
    @SneakyThrows
    private String getImg(byte[] im){
        BufferedImage img= ImageIO.read(new ByteArrayInputStream(im));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img,"png",baos);
        byte[] encodedBytes = Base64.getEncoder().encode(baos.toByteArray());
        String encodedImage = new String(encodedBytes);
        String imgSrc="data:image/png;base64,"+encodedImage;
        return imgSrc;
    }
    @SneakyThrows
    private String getVideo(byte[] vid,String kind){
        String filename= String.valueOf(UUID.nameUUIDFromBytes(vid));
        File file=File.createTempFile(filename,kind);
        FileOutputStream fos=new FileOutputStream((file));
        fos.write(vid);
        fos.close();
        System.out.println("文件名字:"+filename+kind);
        return filename;

    }
    @SneakyThrows
    @ResponseBody
    @RequestMapping("/login")
    public String login(String id, String pwd, HttpServletRequest request){
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(pwd.getBytes());
        String Hpwd=new BigInteger(1,md.digest()).toString(16);
        List<Teacher> teachers=teacherService.browseTeacher("%%");
        for(Teacher i:teachers){
            if(i.getTeaid().equals(id)) {
                if(i.getTeapwd().equals(Hpwd)){
                    if(i.getTeastate()){
                        i.setTeapwd(pwd);
                        request.getSession().setAttribute("TeacherPerson",i);
                        if(i.getTeaphoto()!=null&&i.getTeaphoto().length>0){
                            request.getSession().setAttribute("TeacherPersonImg",getImg(i.getTeaphoto()));
                        }
                        else request.getSession().setAttribute("TeacherPersonImg",null);
                        return "登录成功";
                    }
                    else return "账号未激活";
                }
                else return "密码错误";
            }
        }
        return "没用该用户";
    }
    @RequestMapping("/toRegister")
    public String toRegister(){
        return "/teacher/teacherRegister";
    }
    @SneakyThrows
    @ResponseBody
    @RequestMapping("/register")
    public String register(Teacher teacher,MultipartFile photo){
        if(photo!=null&&!photo.isEmpty())
        teacher.setTeaphoto(photo.getBytes());
        else teacher.setTeaphoto(null);
        if(teacherService.addTeacher(teacher)){
            return "注册成功";
        }
        else{
           return "注册失败";
        }
    }
    @ResponseBody
    @RequestMapping("/getFlag")
    public String getFlag(String phone){
        String flag= SendMessage.message(phone);
        return flag;
    }
    @RequestMapping("/function")
    public String function(){
        return "/teacher/teacherFunction";
    }
    @RequestMapping("/toChangePwd")
    public String toChangePwd(){
        return "/teacher/changePwd";
    }
    @SneakyThrows
    @ResponseBody
    @RequestMapping("/changePwd")
    public String changePwd(String oldpwd,String newpwd1,HttpServletRequest request){
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(oldpwd.getBytes());
        String Hpwd=new BigInteger(1,md.digest()).toString(16);
        if(check(newpwd1)){
            return "密码不能为空字符";
        }
        Teacher t=(Teacher) request.getSession().getAttribute("TeacherPerson");
        if(teacherService.reset(t.getTeaid(),Hpwd,newpwd1)) {
            return "修改成功";
        }
        else {
            return "密码错误";
        }
    }
    @RequestMapping("/detail")
    public String detail(Model model,HttpServletRequest request){
        Teacher i=(Teacher) request.getSession().getAttribute("TeacherPerson");
        model.addAttribute("tea",i);
        if(i.getTeaphoto()!=null&&i.getTeaphoto().length>0){
            model.addAttribute("Img",getImg(i.getTeaphoto()));
        }
        else model.addAttribute("Img",null);
        return "/teacher/teacherDetail";
    }
    @RequestMapping("/myCourse")
    public String myCourse(Model model,HttpServletRequest request){
        Teacher i=(Teacher) request.getSession().getAttribute("TeacherPerson");
        List<Course> courses=courseService.TmyCourse(i.getTeaid());
        model.addAttribute("courseList",courses);
        return "/teacher/myCourse";
    }
    @SneakyThrows
    @RequestMapping("/myStudent")
    public String myStudnet(Model model,String courseid,HttpServletRequest request){
        if(courseid==null) courseid= (String) request.getSession().getAttribute("courseid");
        else request.getSession().setAttribute("courseid",courseid);
        List<Student> studentList=studentService.myStudent(courseid);
        Map<String,String> stuImg=new HashMap<>();
        Map<String,String> score=new HashMap<>();
        for(Student i:studentList){
            String sc=courseService.browseScore(i.getStuid(),courseid);
            if(sc.equals("-1")){
                score.put(i.getStuid(),null);
            }
            else {
                score.put(i.getStuid(),sc);
            }
            if(i.getStuphoto()==null||i.getStuphoto().length<1){
                stuImg.put(i.getStuid(),null);
                continue;
            }
            stuImg.put(i.getStuid(),getImg(i.getStuphoto()));
        }

        model.addAttribute("stuList",studentList);
        model.addAttribute("stuImg",stuImg);
        model.addAttribute("score",score);
        model.addAttribute("courseid",courseid);
        return "/teacher/myStudent";
    }
    @SneakyThrows
    @RequestMapping("/updateScore")
    public  void updateGrade(String stuid,String courseid,String coursescore,HttpServletResponse response){
        response.setCharacterEncoding("GBK");
        if(courseService.updateScore(coursescore,stuid,courseid)){
            response.getWriter().print("<script>alert('登分成功');location.replace('/teacher/myStudent')</script>");
        }
        else response.getWriter().print("<script>alert('登分失败');location.replace('/teacher/myStudent')</script>");
    }
    @ResponseBody
    @SneakyThrows
    @RequestMapping("/addVideo")
    public String addVideo(Video video,MultipartFile videoImg,MultipartFile videos,HttpServletRequest request){
        int courseid=Integer.valueOf((String) request.getSession().getAttribute("courseid"));
        video.setCourseid(courseid);
        if(videoImg!=null&&!videoImg.isEmpty()){
            video.setVideocover(videoImg.getBytes());
        }
        if(videos!=null&&!videos.isEmpty()){
            String filename= String.valueOf(UUID.randomUUID())+videos.getOriginalFilename().substring(videos.getOriginalFilename().lastIndexOf("."));
            video.setVideofile(filename);
            videos.transferTo(new File("D:\\JavaProject\\AcademicAdministration\\src\\main\\resources\\static\\video\\"+filename));
        }
        video.setVideotime(LocalDate.now());
        if(videoService.addVideo(video)){
            return "添加成功";
        }
        else {
            return "添加失败";
        }
    }
    @SneakyThrows
    @RequestMapping("/browseVideo")
    public String browseMaterial(String courseid,String condition,Model model,HttpServletRequest request){
        if(courseid==null) courseid= (String) request.getSession().getAttribute("courseid");
        else request.getSession().setAttribute("courseid",courseid);
        if(condition==null) condition="%%";
        else condition="%"+condition+"%";
        List<Video> videoList=videoService.browseVideo(condition);
        Map<Integer,String> videoImg=new HashMap<>();
        for(Video i:videoList){
            videoImg.put(i.getVideoid(),getImg(i.getVideocover()));
        }
        model.addAttribute("videoList",videoList);
        model.addAttribute("videoImg",videoImg);
        return "/teacher/browseVideo";
    }
    @SneakyThrows
    @RequestMapping("/videoDetail")
    public String videoDetail(String videoid,Model model,HttpServletResponse response){
        List<Video> videoList=videoService.browseVideo("%%");
        Video video=null;
        for(Video i:videoList) {
            if (videoid.equals(String.valueOf(i.getVideoid()))) {
                video = i;
                break;
            }
        }
        response.getWriter().print("<video id='v' src='/"+video.getVideofile()+"' controls>视频</video>");
        model.addAttribute("video",video);
        return "/teacher/videoDetail";
    }
    @SneakyThrows
    @RequestMapping("/deleteVideo")
    public void deleteVideo(String videoid,HttpServletResponse response){
        response.setCharacterEncoding("GBK");
        if(videoService.deleteVideo(videoid)){
            response.getWriter().print("<script>alert('删除成功');location.replace('/teacher/browseVideo')</script>");
        }
        else {
            response.getWriter().print("<script>alert('删除失败');location.replace('/teacher/browseVideo')</script>");
        }
    }
    @SneakyThrows
    @ResponseBody
    @RequestMapping("/addHomework")
    public String addHomework(Homework homework,MultipartFile workfile, HttpServletRequest request){
        int courseid=Integer.valueOf((String) request.getSession().getAttribute("courseid"));
        homework.setCourseid(courseid);
        homework.setHomeworkfile(workfile.getBytes());
        if(homeworkService.addHomework(homework)){
            return "添加成功";
        }
        else {
            return "添加失败";
        }
    }
    @SneakyThrows
    @RequestMapping("/browseHomework")
    public String browseHomework(String courseid,String kind,Model model,HttpServletRequest request){
            if(courseid==null) courseid=(String) request.getSession().getAttribute("courseid");
            else request.getSession().setAttribute("courseid",courseid);
        List<Homework> homeworkList=new ArrayList<>();
        if(check(kind)||kind.equals("0")){
            homeworkList=homeworkService.browseHomeworkAll(courseid);
        }
        else if(kind.equals("1")){
            homeworkList=homeworkService.browseHomework1(courseid);
        }
        else if(kind.equals("2")){
            homeworkList=homeworkService.browseHomework2(courseid);
        }
            List<Student> studentList=studentService.myStudent(courseid);
            HashMap<Integer,Integer> solveNum=new HashMap<>();
            for(Homework i:homeworkList){
                int num=0;
                for(Student j:studentList){
                    if(answerService.myAnswer(j.getStuid(), String.valueOf(i.getHomeworkid()))!=null) num++;
                }
                solveNum.put(i.getHomeworkid(),num);
            }
            model.addAttribute("homeworkList",homeworkList);
            model.addAttribute("solveNum",solveNum);
            model.addAttribute("allNum",studentList.size());
            return "/teacher/browseHomework";
    }
    @SneakyThrows
    @RequestMapping("/homeworkDetail")
    public String homeworkDetail(String homeworkid,String index,Model model,HttpServletRequest request){
       String courseid=(String) request.getSession().getAttribute("courseid");
       if (check(homeworkid)) homeworkid= (String) request.getSession().getAttribute("Hid");
       else request.getSession().setAttribute("Hid",homeworkid);
       if (check(index)) index=(String) request.getSession().getAttribute("Index");
       else request.getSession().setAttribute("Index",index);
        model.addAttribute("index",index);
        List<Student> studentList=studentService.myStudent(courseid);
       List<Homework> homeworkList=homeworkService.browseHomeworkAll(courseid);
       HashMap<String,String> stuImg=new HashMap<>();
       HashMap<String,String> answerImg=new HashMap<>();
       for(Homework i:homeworkList){
           if(homeworkid.equals(String.valueOf(i.getHomeworkid()))) {
               model.addAttribute("homework", i);
               model.addAttribute("homeworkImg",getImg(i.getHomeworkfile()));
               for(Student j:studentList){
                   j.setMyAnswer(answerService.myAnswer(j.getStuid(),homeworkid));
                   if(!check(j.getStuphoto()))
                   stuImg.put(j.getStuid(),getImg(j.getStuphoto()));
                   else stuImg.put(j.getStuid(),null);
                   if(j.getMyAnswer()!=null){
                       answerImg.put(j.getStuid(),getImg(j.getMyAnswer().getAnswerfile()));
                   }
               }
               break;
           }
       }
       model.addAttribute("studentList",studentList);
       model.addAttribute("stuImg",stuImg);
       model.addAttribute("answerImg",answerImg);
       return "/teacher/homeworkDetail";
    }
    @SneakyThrows
    @RequestMapping("/deleteHomework")
    public void deleteHomework(String homeworkid,HttpServletResponse response){
        response.setCharacterEncoding("GBK");
        if(homeworkService.deleteHomework(homeworkid)){
            response.getWriter().print("<script>alert('删除成功');location.replace('/teacher/browseHomework')</script>");
        }
        else {
            response.getWriter().print("<script>alert('删除失败');location.replace('/teacher/browseHomework')</script>");
        }
    }
    @SneakyThrows
    @RequestMapping("/updateHScore")
    public void updateHScore(String homeworkid,String stuid,String score,HttpServletResponse response){
        response.setCharacterEncoding("GBK");
        if (check(score)) {
            response.getWriter().print("<script>alert('分数不能为空');location.replace('/teacher/homeworkDetail')</script>");
            return;
        }
        Answer answer=new Answer();
        answer.setScore(Integer.parseInt(score));
        answer.setStuid(stuid);
        answer.setHomeworkid(Integer.parseInt(homeworkid));
        if(answerService.updateScore(answer)){
            response.getWriter().print("<script>alert('判分成功');location.replace('/teacher/homeworkDetail')</script>");
        }
        else {
            response.getWriter().print("<script>alert('判分失败');location.replace('/teacher/homeworkDetail')</script>");
        }
    }


}

 

posted @ 2023-06-10 11:35  突破铁皮  阅读(7)  评论(0编辑  收藏  举报