排课系统

我要开发同款
BIRDMAN09082022年04月13日
156阅读

作品详情

主要使用了springboot技术
thymeleaf模板引擎
mybatis,mysql数据库技术
js, css, sementicui, ajax等技术
回溯法实现了课程自动排课算法
package com.zj.demo.controller;

import com.zj.demo.entity.*;
import com.zj.demo.mapper.*;
import com.zj.demo.utils.IDGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.annotation.Resource;
import java.util.*;

@Controller
public class MajorController {

@Resource
private MajorMapper majorMapper;

@Resource
private MajorSubjectMapper majorSubjectMapper;

@Resource
private SubjectMapper subjectMapper;

@Resource
private StudentMapper studentMapper;

@Resource
private StudentMajorMapper studentMajorMapper;

@Resource
private TeacherSubjectMapper teacherSubjectMapper;

@Resource
private TeacherTimeMapper teacherTimeMapper;

@Resource
private TeacherMapper teacherMapper;

@Resource
private ClassroomTimeMapper classroomTimeMapper;

@Resource
private ClassroomMapper classroomMapper;

@Resource
private PaikeMapper paikeMapper;

@RequestMapping("/major_index")
public String major_index(@ModelAttribute("msg")String msg, Model model) {
List majorList = majorMapper.findAllMajors();
majorList.sort(((o1, o2) -> o2.getName().compareTo(o1.getName())));
model.addAttribute("majorList", majorList);
model.addAttribute("msg",msg);
return "major_index";
}

@RequestMapping("/to_major_add")
public String to_major_add(Model model) {
List subjects = subjectMapper.findAllSubjects();
model.addAttribute("subjects", subjects);
return "major_add";
}

@RequestMapping("/major_add")
@Transactional
public String major_add(Major major, String[] subject) {
major.setId("MJ" + IDGenerator.getUniqueID());
major.setState("0");
System.out.println(Arrays.toString(subject));
for (String suid :
subject) {
majorSubjectMapper.addSubjectsToMajor(new MajorSubject(
"MS" + IDGenerator.getUniqueID(), major.getId(), suid
));
}
majorMapper.addMajor(major);
return "redirect:major_index";
}

@RequestMapping("/to_major_update")
public String to_major_update(@RequestParam("id") String id, Model model) {
Major major = majorMapper.findMajorById(id);
model.addAttribute("major", major);
return "major_update";
}

@RequestMapping("/major_update")
@Transactional
public String major_update(Major major,RedirectAttributes attributes) {
majorMapper.updateMajorById(major);
attributes.addFlashAttribute("msg","更新成功");
return "redirect:major_index";
}

@RequestMapping("/to_major_delete")
@Transactional
public String to_major_delete(@RequestParam("id") String id,RedirectAttributes attributes) {
deleteMajorAndSubjectByMajorId(id);
attributes.addFlashAttribute("msg","删除成功");
return "redirect:major_index";
}

@RequestMapping("/major_search")
public String major_search(@ModelAttribute(value = "keyword", binding = false) String keyword, Model model) {
List majorList = majorMapper.findMajorsByKeyword(keyword);
model.addAttribute("majorList", majorList);
model.addAttribute("keyword", keyword);
model.addAttribute("num", majorList.size());
return "major_search";
}

@RequestMapping("/to_major_update_search")
public String to_major_update_search(@RequestParam("id") String id,
@RequestParam("keyword") String keyword, Model model) {
Major major = majorMapper.findMajorById(id);
model.addAttribute("major", major);
model.addAttribute("keyword", keyword);
return "major_update_search";
}

@RequestMapping("/major_update_search")
public String major_update_search(Major major, String keyword, RedirectAttributes attributes) {
majorMapper.updateMajorById(major);
attributes.addFlashAttribute("keyword", keyword);
attributes.addFlashAttribute("msg","更新成功");
return "redirect:major_search";
}

@RequestMapping("/to_major_delete_search")
@Transactional
public String to_major_delete_search(@RequestParam("id") String id, String keyword, RedirectAttributes attributes) {
deleteMajorAndSubjectByMajorId(id);
attributes.addFlashAttribute("keyword", keyword);
attributes.addFlashAttribute("msg","删除成功");
return "redirect:major_search";
}

/**
* 根据班级id删除班级和课程以及学生之间的关联
* @param id
*/
private void deleteMajorAndSubjectByMajorId(@RequestParam("id") String id) {
majorSubjectMapper.deleteMajorAndSubjectByMajorId(id);
studentMajorMapper.deleteMajorAndStudentByMajorId(id);
majorMapper.deleteMajorById(id);
findPaikes(id);
}

/**
* 根据班级id查找对应的课程和学生
* @param id
* @param model
* @return
*/
@RequestMapping("/to_major_detail")
public String to_major_detail(String id, Model model) {
Major major = majorMapper.findMajorById(id);
List subjects = majorSubjectMapper.findSubjectsByMajorID(id);
List subjectList = new ArrayList<>();
int sum =0;// 总课时
for (String sid :
subjects) {
subjectList.add(subjectMapper.findSubjectById(sid));
sum+=Integer.parseInt(subjectMapper.findSubjectById(sid).getNumOfWeek());
}
// 获取班级的学生对象
List students = studentMajorMapper.findStudentsByMajorID(id);
List studentList = new ArrayList<>();
for (String tid :
students) {
studentList.add(studentMapper.findStudentById(tid));
}
model.addAttribute("subjectList", subjectList);
model.addAttribute("numOfStu",studentList.size());
model.addAttribute("totalTime",sum);
model.addAttribute("numOfSub",subjectList.size());
model.addAttribute("studentList", studentList);
model.addAttribute("major", major);
return "major_detail";
}

/**
* 给每个班级排课
* 1.找出班级所有的课程
* 2.遍历每个课程进行排课
* 2.1.找出当前课程的授课老师的可用空间
* 2.2.根据选择的时间找空闲的教室
* 2.3.给当前课程排课,继续直到班级的课程都排好
* 3.结束
* @param id
* @param attributes
* @return
*/
@RequestMapping("/to_paike")
public String to_paike(@ModelAttribute("id") String id,RedirectAttributes attributes) {
/*
根据专业id找专业对应的课程
*/
List subjects = majorSubjectMapper.findSubjectsByMajorID(id);
/*
存储当前班级所有已安排课程的时间段,通过比较防止后面的课程时间与前面的课程时间重复
*/
List timeList = new ArrayList<>();

/*
遍历该班级的所有课程进行排课
条件:课程分为必修和选修课程,必修课程优先安排在白天,选修课程优先安排在晚上
*/
for (String sid :
subjects) {
// 获取课程一周的课时量
String num = subjectMapper.findSubjectById(sid).getNumOfWeek();
// 获取课程的类别:必修,选修
String type = subjectMapper.findSubjectById(sid).getType();
// 按照每个课程一周的课程量排课
for (int i = 0; i < Integer.parseInt(num); i++) {
// 定义变量: 每个课程排好后都有教师,教室,时间
Teacher teacher = null;
Classroom classroom = null;
String timeNum = null;

// 存储待选的授课老师
List teachers = new ArrayList<>();
// 要求:同一课程同一老师
if (i >= 1) {// 如果这门课之前排过,则找到之前的这门课老师
List teacherId = paikeMapper.findTeacherIdByMajorIdAndSubjectId(id, sid);
if (teacherId.size()!=0)teachers.add(teacherId.get(0));
} else {// 如果第一次排这门课,则根据课程id获取所有的授课老师
teachers = teacherSubjectMapper.findTeacherIdBySubjectId(sid);
}
// 洗牌
Collections.shuffle(teachers);
// 循环待选老师找出满足条件的老师
for (String tid :
teachers) {
// 找到老师的有空时间
List teacherTimes = teacherTimeMapper.findTeacherTimesByTeacherId(tid);
// 预处理找出空闲时间(可选)
Collections.shuffle(teacherTimes);
boolean fa = false;
for (TeacherTime tt :
teacherTimes) {
// 判断必修,如果是必修,但当前老师空闲时间是晚上则继续选老师的下一个时间段
if (type.equals("1") & Integer.parseInt(tt.getTimeNum()) % 5 == 0) {
continue;
}
if (type.equals("0") & Integer.parseInt(tt.getTimeNum()) % 5 != 0) {
continue;
}
// 判断该老师空闲时间上是否与之前的课程重复了
boolean fb = false;
for (String tl :
timeList) {
if (tl.equals(tt.getTimeNum())) {
fb = true;
break;
}
}
// 如果当前老师空闲时间与之前课程时间冲突则继续
if (fb) {
continue;
}
// 时间不重复继续,判断是否空闲
if (tt.getState().equals("0")) {
// 将该时间存储到timeList中
timeList.add(tt.getTimeNum());
// 根据老师时间找教室
List
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论