package com.example.demo.controller;
import com.example.demo.mapper.*;
import com.example.demo.model.*;
import com.example.demo.service.CourseService;
import com.example.demo.service.GradeService;
import lombok.extern.flogger.Flogger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.awt.image.FilteredImageSource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Controller
@RequestMapping("/home")
public class HomeController {
@Resource
public MethodMapper methodMapper;
@Resource
public CoursesMapper coursesMapper;
@Resource
public StudentsMapper StudentsMapper;
@Resource
public CourseService courseService;
@Resource
public StudentsMapper studentsMapper;
@Resource
public TeachersMapper teachersMapper;
@Resource
public TeachcourseMapper teachcourseMapper;
@Resource
public GradeService gradeService;
@GetMapping("")
public String Home(HttpSession session, Model model) {
Account account = (Account) session.getAttribute("account");
if (account == null) {
return "redirect:/";
}
String type = account.getType();
int typeCode = Integer.parseInt(type);
model.addAttribute("typeCode", typeCode);
//学生
String studentId = account.getAccountid();
List<Stucourse> stucourseList = methodMapper.SelectByStudentId(studentId);
List<Grade> gradeList = new ArrayList<>();
Grade grade;
for (Stucourse s : stucourseList) {
if (s.getGrade().equals("0")) {
continue;
}
grade = new Grade();
Courses courses = coursesMapper.selectByPrimaryKey(s.getCourseid());
grade.setCourseId(courses.getCourseid());
grade.setCourseName(courses.getCoursename());
Teachcourse teachcourse = methodMapper.selectTeachCourse(s.getCourseid());
Teachers teachers = teachersMapper.selectByPrimaryKey(teachcourse.getTeacherid());
grade.setTeacherName(teachers.getName());
grade.setGrade(s.getGrade());
gradeList.add(grade);
}
model.addAttribute("gradeList", gradeList);
//教师
String teacherId = account.getAccountid();
List<Teachcourse> teachcourseList = methodMapper.selectTeachCourseList(teacherId);
Teachers teachers = teachersMapper.selectByPrimaryKey(teacherId);
List<TeacherCourseList> teacherCourseListArrayList = new ArrayList<>();
for (Teachcourse teachcourse : teachcourseList) {
TeacherCourseList teacherCourseList = new TeacherCourseList();
Courses courses = coursesMapper.selectByPrimaryKey(teachcourse.getCourseid());
teacherCourseList.setCourseid(teachcourse.getCourseid());
teacherCourseList.setCourseName(courses.getCoursename());
teacherCourseList.setTeacherid(teachcourse.getTeacherid());
teacherCourseList.setTeacherName(teachers.getName());
teacherCourseList.setCourseroom(teachcourse.getCourseroom());
teacherCourseList.setCoursedate(teachcourse.getCoursedate());
List<Stucourse> stucourseList1 = methodMapper.selectStuCourseById(teachcourse.getCourseid());
teacherCourseList.setSum(String.valueOf(stucourseList1.size()));
teacherCourseList.setTerm(teachcourse.getTerm());
teacherCourseListArrayList.add(teacherCourseList);
}
model.addAttribute("teacherCourseListArrayList", teacherCourseListArrayList);
return "home";
}
//学生
@GetMapping("/studentInfo")
public String studentInfo(HttpSession session, Model model) {
Account account = (Account) session.getAttribute("account");
if (account == null) {
return "redirect:/";
}
Students students = StudentsMapper.selectByPrimaryKey(account.getAccountid());
model.addAttribute(students);
return "studentInfo";
}
@PostMapping("/studentInfo")
public String studentInfo(HttpSession session, Model model, String password, String political) {
Account account = (Account) session.getAttribute("account");
String studentId = account.getAccountid();
Students students = StudentsMapper.selectByPrimaryKey(studentId);
if (password.equals("") && political.equals("")) {
methodMapper.updateStudentInfo(studentId, students.getPassword(), students.getPolitical());
}
if (political.equals("") && !password.equals("")) {
methodMapper.updateAccountPassword(studentId, password);
methodMapper.updateStudentInfo(studentId, password, students.getPolitical());
}
if (!political.equals("") && password.equals("")) {
methodMapper.updateStudentInfo(studentId, students.getPassword(), political);
}
if (!political.equals("") && !password.equals("")) {
methodMapper.updateAccountPassword(studentId, password);
methodMapper.updateStudentInfo(studentId, password, political);
}
model.addAttribute(students);
return "redirect:/home/studentInfo";
}
//教师
@GetMapping("/couresManagement")
public String couresManagement(HttpSession session, Model model) {
Account account = (Account) session.getAttribute("account");
String teacherId = account.getAccountid();
List<Teachcourse> teachcourseList = methodMapper.selectTeachCourseList(teacherId);
Teachers teachers = teachersMapper.selectByPrimaryKey(teacherId);
List<TeacherCourseList> teacherCourseListArrayList = new ArrayList<>();
for (Teachcourse teachcourse : teachcourseList) {
TeacherCourseList teacherCourseList = new TeacherCourseList();
Courses courses = coursesMapper.selectByPrimaryKey(teachcourse.getCourseid());
teacherCourseList.setCourseid(teachcourse.getCourseid());
teacherCourseList.setCourseName(courses.getCoursename());
teacherCourseList.setTeacherid(teachcourse.getTeacherid());
teacherCourseList.setTeacherName(teachers.getName());
teacherCourseList.setCourseroom(teachcourse.getCourseroom());
teacherCourseList.setCoursedate(teachcourse.getCoursedate());
List<Stucourse> stucourseList1 = methodMapper.selectStuCourseById(teachcourse.getCourseid());
teacherCourseList.setSum(String.valueOf(stucourseList1.size()));
teacherCourseList.setTerm(teachcourse.getTerm());
teacherCourseListArrayList.add(teacherCourseList);
}
model.addAttribute("teacherCourseListArrayList", teacherCourseListArrayList);
return "couresManagement";
}
@PostMapping("/couresManagement")
public String couresManagement(HttpSession session, String term, Model model) {
Account account = (Account) session.getAttribute("account");
String teacherId = account.getAccountid();
List<Teachcourse> teachcourseList = methodMapper.selectTeachCourseList(teacherId);
Teachers teachers = teachersMapper.selectByPrimaryKey(teacherId);
List<TeacherCourseList> teacherCourseListArrayList = new ArrayList<>();
for (Teachcourse teachcourse : teachcourseList) {
if (teachcourse.getTerm().equals(term)) {
TeacherCourseList teacherCourseList = new TeacherCourseList();
Courses courses = coursesMapper.selectByPrimaryKey(teachcourse.getCourseid());
teacherCourseList.setCourseid(teachcourse.getCourseid());
teacherCourseList.setCourseName(courses.getCour