2117847720qq

导航

4.23

已经花费的时间7天,还剩余的时间3天

package com.example.training.entity;

import javax.persistence.*;

@Entity
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

private String name;
private String teacher;

@Column(columnDefinition = "TEXT")
private String content;

public Course() {
}

public Course(Integer id, String name, String teacher, String content) {
    this.id = id;
    this.name = name;
    this.teacher = teacher;
    this.content = content;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getTeacher() {
    return teacher;
}

public void setTeacher(String teacher) {
    this.teacher = teacher;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

}
package com.example.training.entity;

import javax.persistence.*;
import java.time.LocalDateTime;
@Table(name = "employee_course")
@Entity
public class EmployeeCourse {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@ManyToOne
@JoinColumn(name = "employee_id", nullable = false)
private User employee;

@ManyToOne
@JoinColumn(name = "course_id", nullable = false)
private Course course;

private boolean active = true;
private LocalDateTime selectedAt = LocalDateTime.now();

// Getters and Setters
public Integer getId() { return id; }
public User getEmployee() { return employee; }
public void setEmployee(User employee) { this.employee = employee; }
public Course getCourse() { return course; }
public void setCourse(Course course) { this.course = course; }
public boolean isActive() { return active; }
public void setActive(boolean active) { this.active = active; }
public LocalDateTime getSelectedAt() { return selectedAt; }
public void setSelectedAt(LocalDateTime selectedAt) { this.selectedAt = selectedAt; }

}

// 课程管理
@GetMapping("/courses")
public String courseManage(HttpSession session, Model model) {
if (!checkAdmin(session)) return "redirect:/login";
model.addAttribute("courses", adminService.findAllCourses());
return "admin/course";
}

@PostMapping("/courses")
public String saveCourse(@ModelAttribute Course course, HttpSession session) {
    if (!checkAdmin(session)) return "redirect:/login";
    adminService.saveCourse(course);
    return "redirect:/admin/courses";
}

// 课程删除
@PostMapping("/courses/delete/{id}")
public String deleteCourse(@PathVariable Integer id,
                           HttpSession session,
                           RedirectAttributes ra) {
    if (!checkAdmin(session)) return "redirect:/login";

    try {
        adminService.deleteCourse(id);
        ra.addFlashAttribute("success", "课程删除成功");
    } catch (AdminService.ServiceException e) {
        ra.addFlashAttribute("error", e.getMessage());
    } catch (Exception e) {
        ra.addFlashAttribute("error", "系统错误:" + e.getMessage());
    }
    return "redirect:/admin/courses";
}

@GetMapping("/course")
public String course(HttpSession session, Model model) {
User employee = getCurrentEmployee(session);

    // 确保调用正确的服务方法
    List<Course> selectedCourses = employeeService.getSelectedCourses(employee.getId());
    List<Course> allCourses = employeeService.getAllCourses();
    List<ExamPaper> papers = employeeService.getAllPapers();

    // 正确设置模型属性
    model.addAttribute("selectedCourses", selectedCourses);
    model.addAttribute("courses", allCourses);
    model.addAttribute("papers", papers);

    return "employee/course";
}


@PostMapping("/select-course")
public String selectCourse(@RequestParam Integer courseId, HttpSession session) {
    User employee = getCurrentEmployee(session);
    employeeService.selectCourse(employee.getId(), courseId);
    return "redirect:/employee/course";
}

// 课程管理增强
@PostMapping("/cancel-course")
public String cancelCourse(@RequestParam Integer courseId,
HttpSession session,
RedirectAttributes ra) {
try {
// 安全获取当前用户
User employee = (User) session.getAttribute("user");
if (employee == null || !employee.getRole().equals(User.Role.EMPLOYEE)) {
ra.addFlashAttribute("error", "请先登录员工账号");
return "redirect:/login";
}

        employeeService.cancelCourse(employee.getId(), courseId);
        ra.addFlashAttribute("success", "课程退选成功");
    } catch (ServiceException e) {
        ra.addFlashAttribute("error", e.getMessage());
    } catch (Exception e) {
        ra.addFlashAttribute("error", "系统错误:" + e.getMessage());
    }
    return "redirect:/employee/course";
}
课程管理

posted on 2025-04-23 23:10  我爱玩原神(原神大王)  阅读(15)  评论(0)    收藏  举报