TeacherQuery

Signed-off-by: Chenx221 <chenx221@yandex.com>
This commit is contained in:
Chenx221 2023-06-16 15:23:36 +08:00
parent 4a4871eccf
commit ebb95bbfa4
16 changed files with 155 additions and 59 deletions

View File

@ -14,10 +14,46 @@
<result property="CourseName" column="CourseName"/>
</association>
</resultMap>
<select id="getAllTeachers" resultMap="TeacherResultMap">
SELECT id, name, sex, birthday, classes, c.CourseID, c.CourseName
FROM teacher t
JOIN course c ON c.CourseID = t.course_id
WHERE t.removed = 0;
</select>
<select id="getQueryTeachers" parameterType="cyou.chenx221.pojo.Teacher" resultMap="TeacherResultMap">
SELECT id, name, sex, birthday, classes, c.CourseID, c.CourseName
FROM teacher t
JOIN course c ON c.CourseID = t.course_id
WHERE t.removed = 0
<trim prefix="AND" prefixOverrides="AND | OR">
<if test="id != -1">
AND id = #{id}
</if>
<if test="name != null">
AND name = #{name}
</if>
<if test="sex != null">
AND sex = #{sex}
</if>
<if test="course !=null">
<if test="course.CourseID != null">
AND c.CourseID = #{course.courseID}
</if>
<if test="course.CourseName != null">
AND c.CourseName LIKE CONCAT('%', #{course.courseName}, '%')
</if>
</if>
<if test="classes != null">
AND classes = #{classes}
</if>
<if test="birthdayBegin != null">
AND birthday &gt;= #{birthdayBegin}
</if>
<if test="birthdayEnd != null">
AND birthday &lt;= #{birthdayEnd}
</if>
</trim>
</select>
</mapper>

View File

@ -25,7 +25,7 @@
.table-container table {
width: 100%;
height: 100%;
/*height: 100%;*/
}
</style>
</head>
@ -158,7 +158,7 @@
</tr>
</thead>
<tbody>
<c:forEach var="teacher" items="${teacherList}">
<c:forEach var="teacher" items="${teachers}">
<tr>
<td>${teacher.id}</td>
<td>${teacher.name}</td>

View File

@ -133,7 +133,19 @@
style="background-image: url('${pageContext.request.contextPath}/resources/img/jason-blackeye-nyL-rzwP-Mk-unsplash.jpg'); margin-top: -58.59px;">
<div class="mask d-flex align-items-center h-100" style="background-color: hsla(0, 0%, 100%, 0.5);">
<div class="container d-flex justify-content-center">
<button type="button" class="btn btn-primary" onclick="location.href='../dashboard'">返回</button>
<div>
<div class="row">
<div class="col-12">
<button type="button" class="btn btn-primary" onclick="location.href='../dashboard'">返回
</button>
</div>
</div>
<div class="row">
<div class="col-12">
<button type="button" class="btn btn-primary" onclick="exportCSV()">导出</button>
</div>
</div>
</div>
<div class="table-container rounded-4 shadow-3-strong"
style="background-color: rgba(255,255,255,0.9); overflow-y: auto; max-height: 400px">
<table class="table table-striped table-hover border-primary align-middle">
@ -642,6 +654,17 @@
$('#exampleModal3').modal('hide');
}
}
function exportCSV() {
// 构造请求 URL根据需要导出的类型传递参数
var exportUrl = '/output/export-csv?type=teacher'; // 示例:导出学生数据
var link = document.createElement('a');
link.href = exportUrl;
link.style.display = 'none';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
</script>
</body>

View File

@ -3,9 +3,11 @@ package cyou.chenx221.controller;
import cyou.chenx221.pojo.Course;
import cyou.chenx221.pojo.Score;
import cyou.chenx221.pojo.Student;
import cyou.chenx221.pojo.Teacher;
import cyou.chenx221.service.CourseService;
import cyou.chenx221.service.ScoreService;
import cyou.chenx221.service.StudentService;
import cyou.chenx221.service.TeacherService;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.springframework.beans.factory.annotation.Autowired;
@ -34,6 +36,9 @@ public class OutputController {
@Autowired
private ScoreService scoreService;
@Autowired
private TeacherService teacherService;
@GetMapping("/export-csv")
public void exportCSV(HttpServletResponse response, @RequestParam("type") String type) throws IOException {
response.setHeader("Content-Disposition", "attachment; filename=export.csv");
@ -59,6 +64,11 @@ public class OutputController {
List<Score> scores = scoreService.getAllScores(); // 根据实际情况获取成绩数据
data = transformScoresToData(scores);
}
case "teacher" -> {
headers = Arrays.asList("教师ID", "姓名", "性别","出生日期","授课课程ID","授课课程名","授课班级");
List<Teacher> teachers = teacherService.getAllTeachers(); // 根据实际情况获取教师数据
data = transformTeachersToData(teachers);
}
default -> throw new IllegalArgumentException("Invalid export type: " + type);
}
@ -122,6 +132,23 @@ public class OutputController {
return data;
}
private List<List<String>> transformTeachersToData(List<Teacher> teachers) {
List<List<String>> data = new ArrayList<>();
for (Teacher teacher : teachers) {
List<String> rowData = Arrays.asList(
String.valueOf(teacher.getId()),
teacher.getName(),
teacher.getSex(),
teacher.getBirthday().toString(),
String.valueOf(teacher.getCourse().getCourseID()),
teacher.getCourse().getCourseName(),
teacher.getClasses()
);
data.add(rowData);
}
return data;
}
@GetMapping("/get-csv")
public void getCSV(HttpServletResponse response, @RequestParam("filename") String filename) throws IOException {
String filePath = "file" + File.separator + filename;

View File

@ -33,11 +33,15 @@ public class TeacherController {
}
@GetMapping("/manage")
public String manageTeacherInfo(Model model) {
public String manageTeacherInfo(@RequestParam(value = "successMessage", required = false, defaultValue = "null") String successMessage,
@RequestParam(value = "errorMessage", required = false, defaultValue = "null") String errorMessage,
Model model) {
String username = new UsernameHelper().getCurrentUsername();
if (username != null) {
model.addAttribute("username", username);
}
model.addAttribute("successMessage", successMessage);
model.addAttribute("errorMessage", errorMessage);
List<Teacher> teacherList = teacherService.getAllTeachers();
model.addAttribute("teacherList", teacherList);
return "teachermanage";
@ -52,7 +56,7 @@ public class TeacherController {
@RequestParam(value = "course_id", defaultValue = "-1", required = false) int course_id,//课程ID
@RequestParam(value = "course_name", defaultValue = "null", required = false) String course_name,//课程名
@RequestParam(value = "classes", defaultValue = "null", required = false) String classes,//授课班级
Model model) {
Model model) throws IOException {
//username helper start
String username = new UsernameHelper().getCurrentUsername();
@ -102,11 +106,8 @@ public class TeacherController {
}
Teacher teacher = new Teacher(id, name, sex, (course_id == -1 && course_name == null ? null : (new Course(course_id, course_name))), classes, birthdayBegin, birthdayEnd);
List<Teacher> teachers = teacherService.getQueryTeachers(teacher);
// scores = scoreService.getQueryScores(score);
// String download = new CsvHelper().generateScoreCSVFile(scores);
// model.addAttribute("download", download);
// model.addAttribute("scores", scores);
return "scoreQueryResult";
model.addAttribute("teachers", teachers);
model.addAttribute("download", new CsvHelper().generateTeacherCSVFile(teachers));
return "teacherQueryResult";
}
}

View File

@ -3,6 +3,7 @@ package cyou.chenx221.helper;
import cyou.chenx221.pojo.Course;
import cyou.chenx221.pojo.Score;
import cyou.chenx221.pojo.Student;
import cyou.chenx221.pojo.Teacher;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
@ -31,6 +32,10 @@ public class CsvHelper {
);
data.add(rowData);
}
return getString(headers, data);
}
private String getString(List<String> headers, List<List<String>> data) throws IOException {
String directoryPath = "file";
File directory = new File(directoryPath);
@ -70,28 +75,7 @@ public class CsvHelper {
);
data.add(rowData);
}
String directoryPath = "file";
File directory = new File(directoryPath);
if (!directory.exists()) {
boolean created = directory.mkdir(); //Be careful, path: tomcat's installation path (D:\softs\res\apache-tomcat-9.0.75-windows-x64\apache-tomcat-9.0.75\bin)
if (created) {
System.out.println("目录已创建");
} else {
System.out.println("无法创建目录");
}
}
File csvFile = new File("file/export-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".csv");
try (PrintWriter writer = new PrintWriter(csvFile);
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) {
csvPrinter.printRecord(headers);
for (List<String> row : data) {
csvPrinter.printRecord(row);
}
csvPrinter.flush();
}
return csvFile.getName();
return getString(headers, data);
}
public String generateCourseCSVFile(List<Course> courses) throws IOException {
@ -106,28 +90,25 @@ public class CsvHelper {
);
data.add(rowData);
}
String directoryPath = "file";
File directory = new File(directoryPath);
if (!directory.exists()) {
boolean created = directory.mkdir(); //Be careful, path: tomcat's installation path (D:\softs\res\apache-tomcat-9.0.75-windows-x64\apache-tomcat-9.0.75\bin)
if (created) {
System.out.println("目录已创建");
} else {
System.out.println("无法创建目录");
}
}
File csvFile = new File("file/export-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".csv");
try (PrintWriter writer = new PrintWriter(csvFile);
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) {
csvPrinter.printRecord(headers);
for (List<String> row : data) {
csvPrinter.printRecord(row);
}
csvPrinter.flush();
}
return csvFile.getName();
return getString(headers, data);
}
public String generateTeacherCSVFile(List<Teacher> teachers) throws IOException {
List<String> headers;
headers = Arrays.asList("教师ID", "姓名", "性别","出生日期","授课课程ID","授课课程名","授课班级");
List<List<String>> data = new ArrayList<>();
for (Teacher teacher : teachers) {
List<String> rowData = Arrays.asList(
String.valueOf(teacher.getId()),
teacher.getName(),
teacher.getSex(),
teacher.getBirthday().toString(),
String.valueOf(teacher.getCourse().getCourseID()),
teacher.getCourse().getCourseName(),
teacher.getClasses()
);
data.add(rowData);
}
return getString(headers, data);
}
}

View File

@ -48,7 +48,12 @@
<if test="classes != null">
AND classes = #{classes}
</if>
# 待添加
<if test="birthdayBegin != null">
AND birthday &gt;= #{birthdayBegin}
</if>
<if test="birthdayEnd != null">
AND birthday &lt;= #{birthdayEnd}
</if>
</trim>
</select>
</mapper>

View File

@ -25,7 +25,7 @@
.table-container table {
width: 100%;
height: 100%;
/*height: 100%;*/
}
</style>
</head>
@ -158,7 +158,7 @@
</tr>
</thead>
<tbody>
<c:forEach var="teacher" items="${teacherList}">
<c:forEach var="teacher" items="${teachers}">
<tr>
<td>${teacher.id}</td>
<td>${teacher.name}</td>

View File

@ -133,7 +133,19 @@
style="background-image: url('${pageContext.request.contextPath}/resources/img/jason-blackeye-nyL-rzwP-Mk-unsplash.jpg'); margin-top: -58.59px;">
<div class="mask d-flex align-items-center h-100" style="background-color: hsla(0, 0%, 100%, 0.5);">
<div class="container d-flex justify-content-center">
<button type="button" class="btn btn-primary" onclick="location.href='../dashboard'">返回</button>
<div>
<div class="row">
<div class="col-12">
<button type="button" class="btn btn-primary" onclick="location.href='../dashboard'">返回
</button>
</div>
</div>
<div class="row">
<div class="col-12">
<button type="button" class="btn btn-primary" onclick="exportCSV()">导出</button>
</div>
</div>
</div>
<div class="table-container rounded-4 shadow-3-strong"
style="background-color: rgba(255,255,255,0.9); overflow-y: auto; max-height: 400px">
<table class="table table-striped table-hover border-primary align-middle">
@ -642,6 +654,17 @@
$('#exampleModal3').modal('hide');
}
}
function exportCSV() {
// 构造请求 URL根据需要导出的类型传递参数
var exportUrl = '/output/export-csv?type=teacher'; // 示例:导出学生数据
var link = document.createElement('a');
link.href = exportUrl;
link.style.display = 'none';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
</script>
</body>