179 lines
7.4 KiB
Java
179 lines
7.4 KiB
Java
package com.yunhai.bschclassmanage.controller.api;
|
|
|
|
|
|
import com.yunhai.bschclassmanage.config.Config;
|
|
import com.yunhai.bschclassmanage.controller.BaseController;
|
|
import com.yunhai.bschclassmanage.entity.SchClassTeach;
|
|
import com.yunhai.bschclassmanage.model.vo.*;
|
|
import com.yunhai.bschclassmanage.service.*;
|
|
import com.yunhai.bschclassmanage.utils.UserContext;
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.validation.Valid;
|
|
import java.io.IOException;
|
|
import java.net.URLEncoder;
|
|
import java.util.*;
|
|
|
|
|
|
@RestController
|
|
@RequestMapping(Config.API_PATH+"/teachs/")
|
|
@CrossOrigin(originPatterns = {"*"}, allowCredentials = "true")
|
|
public class SchTeachController extends BaseController {
|
|
|
|
@Autowired
|
|
private ISchClassTeachService schClassTeachService; //使用接口
|
|
|
|
@Autowired
|
|
private ISchDicSubjectService schDicSubjectService; //使用接口
|
|
|
|
@Autowired
|
|
private ISchClassService schClassService; //使用接口
|
|
|
|
@Autowired
|
|
private ISchTeacherService schTeacherService; //使用接口
|
|
|
|
@GetMapping("search")
|
|
public SchTeachMapVO getSchTeachMapVO(@RequestParam(required = true) Integer schsystemId,
|
|
@RequestParam(required = false) Integer gradeId) {
|
|
return schClassTeachService.getSchTeachMapVO(UserContext.getSchId(),schsystemId,gradeId);
|
|
}
|
|
|
|
@GetMapping("getSchClassTeach")
|
|
public List<SchTeachMapIdVO> getSchClassTeachMapVO(@RequestParam Integer gradeId,@RequestParam Integer classId) {
|
|
return schClassTeachService.getSchClassTeachMapVO(UserContext.getSchId(),gradeId,classId);
|
|
}
|
|
|
|
@PostMapping("save")
|
|
public void saveTeach(@Valid @RequestBody SchClassTeach model) {
|
|
schClassTeachService.saveTeach(UserContext.getCurrentUser(),model);
|
|
}
|
|
|
|
@PostMapping("delete/{id}")
|
|
public void delTeachById(@PathVariable Integer id) {
|
|
schClassTeachService.delTeachById(UserContext.getCurrentUser(),id);
|
|
}
|
|
|
|
@PostMapping("delClassTeach")
|
|
public void delClassTeach(@RequestParam Integer classId) {
|
|
schClassTeachService.delClassTeach(UserContext.getCurrentUser(),classId);
|
|
}
|
|
|
|
@PostMapping("delBatch")
|
|
public void delBatch(@RequestParam Integer schsystemId, @RequestParam(required = false) Integer gradeId) {
|
|
schClassTeachService.delBatch(UserContext.getCurrentUser(),schsystemId,gradeId);
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("downloadTemplate")
|
|
public void downloadTemplate(HttpServletResponse response,@RequestParam Integer schId,@RequestParam Integer schsystemId, @RequestParam(required = false) Integer gradeId) throws IOException {
|
|
|
|
// 2. 获取科目和班级
|
|
List<SelectStrVO> subList = schDicSubjectService.select(schId, schsystemId);
|
|
if (subList == null) subList = Collections.emptyList();
|
|
List<BaseSchClassVO> schClassList = schClassService.searcBaseSchClass(schId, schsystemId, gradeId,null);
|
|
if (schClassList == null) schClassList = Collections.emptyList();
|
|
|
|
// 3. 创建工作簿
|
|
Workbook workbook = new HSSFWorkbook(); // 或 XSSFWorkbook
|
|
Sheet sheet = workbook.createSheet("教师任教导入");
|
|
|
|
// 4. 构建表头
|
|
Row headerRow = sheet.createRow(0);
|
|
int colIdx = 0;
|
|
headerRow.createCell(colIdx++).setCellValue("年级");
|
|
headerRow.createCell(colIdx++).setCellValue("班级");
|
|
for (SelectStrVO sub : subList) {
|
|
headerRow.createCell(colIdx++).setCellValue(sub.getName());
|
|
}
|
|
|
|
// 5. 填充数据行
|
|
int rowIdx = 1;
|
|
for (BaseSchClassVO clzz : schClassList) {
|
|
Row row = sheet.createRow(rowIdx++);
|
|
colIdx = 0;
|
|
row.createCell(colIdx++).setCellValue(clzz.getGradeName());
|
|
row.createCell(colIdx++).setCellValue(clzz.getName());
|
|
for (int i = 0; i < subList.size(); i++) {
|
|
row.createCell(colIdx++).setCellValue("");
|
|
}
|
|
}
|
|
|
|
// 6. 自动调整列宽(可选)
|
|
for (int i = 0; i < 2 + subList.size(); i++) {
|
|
sheet.autoSizeColumn(i);
|
|
}
|
|
|
|
// 7. 下载
|
|
String fileName = "教师任教导入模板.xls";
|
|
response.setContentType("application/vnd.ms-excel");
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
workbook.write(response.getOutputStream());
|
|
workbook.close();
|
|
}
|
|
|
|
@GetMapping("export")
|
|
public void export(HttpServletResponse response,@RequestParam Integer schId,@RequestParam Integer schsystemId, @RequestParam(required = false) Integer gradeId) throws IOException {
|
|
|
|
// 2. 获取科目和班级
|
|
List<SelectStrVO> subList = schDicSubjectService.select(schId, schsystemId);
|
|
if (subList == null) subList = Collections.emptyList();
|
|
List<BaseSchClassVO> schClassList = schClassService.searcBaseSchClass(schId, schsystemId, gradeId,null);
|
|
if (schClassList == null) schClassList = Collections.emptyList();
|
|
Map<Integer,String> schTeacherMap=schTeacherService.toIdAndNameMap(schId);
|
|
List<SchClassTeach> schClassTeachList=schClassTeachService.getSchClassTeachCacheList(schId,gradeId,null);
|
|
|
|
// 3. 创建工作簿
|
|
Workbook workbook = new HSSFWorkbook(); // 或 XSSFWorkbook
|
|
Sheet sheet = workbook.createSheet("教师任教导出");
|
|
|
|
// 4. 构建表头
|
|
Row headerRow = sheet.createRow(0);
|
|
int colIdx = 0;
|
|
headerRow.createCell(colIdx++).setCellValue("年级");
|
|
headerRow.createCell(colIdx++).setCellValue("班级");
|
|
for (SelectStrVO sub : subList) {
|
|
headerRow.createCell(colIdx++).setCellValue(sub.getName());
|
|
}
|
|
|
|
// 5. 填充数据行
|
|
int rowIdx = 1;
|
|
for (BaseSchClassVO clzz : schClassList) {
|
|
Row row = sheet.createRow(rowIdx++);
|
|
colIdx = 0;
|
|
row.createCell(colIdx++).setCellValue(clzz.getGradeName());
|
|
row.createCell(colIdx++).setCellValue(clzz.getName());
|
|
for (SelectStrVO sub:subList) {
|
|
Optional<SchClassTeach> classTeachOpt= schClassTeachList.stream().filter(s->s.getClassId().equals(clzz.getClassId())&&s.getSubId().equals(sub.getId())).findFirst();
|
|
row.createCell(colIdx++).setCellValue(classTeachOpt.isPresent()?schTeacherMap.get(classTeachOpt.get().getTeaId()):"");
|
|
}
|
|
}
|
|
|
|
// 6. 自动调整列宽(可选)
|
|
for (int i = 0; i < 2 + subList.size(); i++) {
|
|
sheet.autoSizeColumn(i);
|
|
}
|
|
|
|
// 7. 下载
|
|
String fileName = "教师任教导出.xls";
|
|
response.setContentType("application/vnd.ms-excel");
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
workbook.write(response.getOutputStream());
|
|
workbook.close();
|
|
}
|
|
|
|
@PostMapping("importTeach")
|
|
public void importTeach(@RequestPart("file") MultipartFile file,@RequestParam Integer schsystemId, @RequestParam(required = false) Integer gradeId) {
|
|
schClassTeachService.importTeach(UserContext.getCurrentUser(),file,schsystemId,gradeId);
|
|
}
|
|
|
|
}
|