package com.ruoyi.system.controller; import java.util.List; import java.util.HashMap; import java.util.Map; import jakarta.servlet.http.HttpServletResponse; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.system.domain.CjClass; import com.ruoyi.system.domain.CjCollege; import com.ruoyi.system.domain.CjMajor; import com.ruoyi.system.mapper.CjClassMapper; import com.ruoyi.system.mapper.CjCollegeMapper; import com.ruoyi.system.mapper.CjMajorMapper; import com.ruoyi.system.mapper.CjStudentMapper; import com.ruoyi.system.service.ICjClassService; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.core.page.TableDataInfo; /** * 班级Controller * * @author ruoyi * @date 2026-09-03 */ @RestController @RequestMapping("/system/class") public class CjClassController extends BaseController { @Autowired private ICjClassService cjClassService; @Autowired private CjCollegeMapper cjCollegeMapper; @Autowired private CjMajorMapper cjMajorMapper; @Autowired private CjStudentMapper cjStudentMapper; @Autowired private CjClassMapper cjClassMapper; /** * 回填所属学院/专业名称(transient,供列表/导出展示) */ private void fillNames(List classes) { if (classes == null || classes.isEmpty()) { return; } Map collegeMap = new HashMap<>(); List colleges = cjCollegeMapper.selectCjCollegeList(new CjCollege()); if (colleges != null) { for (CjCollege c : colleges) { collegeMap.put(c.getCollegeId(), c.getCollegeName()); } } Map majorMap = new HashMap<>(); List majors = cjMajorMapper.selectCjMajorList(new CjMajor()); if (majors != null) { for (CjMajor m : majors) { majorMap.put(m.getMajorId(), m); } } for (CjClass c : classes) { if (c.getMajorId() != null) { CjMajor m = majorMap.get(c.getMajorId()); if (m != null) { c.setMajorName(m.getMajorName()); if (m.getCollegeId() != null) { c.setCollegeName(collegeMap.get(m.getCollegeId())); } } } } } /** * 查询班级列表 */ @PreAuthorize("@ss.hasPermi('system:class:list')") @GetMapping("/list") public TableDataInfo list(CjClass cjClass) { startPage(); List list = cjClassService.selectCjClassList(cjClass); fillNames(list); return getDataTable(list); } /** * 导出班级列表 */ @PreAuthorize("@ss.hasPermi('system:class:export')") @Log(title = "班级", businessType = BusinessType.EXPORT) @PostMapping("/export") public void export(HttpServletResponse response, CjClass cjClass) { List list = cjClassService.selectCjClassList(cjClass); fillNames(list); ExcelUtil util = new ExcelUtil(CjClass.class); util.exportExcel(response, list, "班级数据"); } /** * 获取班级详细信息 */ @PreAuthorize("@ss.hasPermi('system:class:query')") @GetMapping(value = "/{classId}") public AjaxResult getInfo(@PathVariable("classId") Long classId) { return success(cjClassService.selectCjClassByClassId(classId)); } /** * 新增班级 */ @PreAuthorize("@ss.hasPermi('system:class:add')") @Log(title = "班级", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody CjClass cjClass) { return toAjax(cjClassService.insertCjClass(cjClass)); } /** * 修改班级 */ @PreAuthorize("@ss.hasPermi('system:class:edit')") @Log(title = "班级", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody CjClass cjClass) { return toAjax(cjClassService.updateCjClass(cjClass)); } /** * 重置班级注册码(二维码外泄/换班期结束时使用,旧码立即失效) */ @PreAuthorize("@ss.hasPermi('system:class:edit')") @Log(title = "班级", businessType = BusinessType.UPDATE) @PutMapping("/resetBindCode/{classId}") public AjaxResult resetBindCode(@PathVariable("classId") Long classId) { CjClass c = cjClassService.selectCjClassByClassId(classId); if (c == null) { return error("班级不存在"); } CjClass upd = new CjClass(); upd.setClassId(classId); upd.setBindCode(java.util.UUID.randomUUID().toString().replace("-", "")); upd.setUpdateBy(getUsername()); cjClassService.updateCjClass(upd); return success("注册码已重置,旧二维码立即失效,请重新下载打印"); } /** * 删除班级(引用保护:其下存在学生时禁止删除) */ @PreAuthorize("@ss.hasPermi('system:class:remove')") @Log(title = "班级", businessType = BusinessType.DELETE) @DeleteMapping("/{classIds}") public AjaxResult remove(@PathVariable Long[] classIds) { if (classIds != null && classIds.length > 0 && cjStudentMapper.countByClassIds(classIds) > 0) { return error("所选班级下存在学生,无法删除;请先将学生转出或停用班级"); } return toAjax(cjClassService.deleteCjClassByClassIds(classIds)); } /** * 班级停用/启用(0正常 1停用) */ @PreAuthorize("@ss.hasPermi('system:class:status')") @Log(title = "班级", businessType = BusinessType.UPDATE) @PutMapping("/changeStatus") public AjaxResult changeStatus(@RequestBody CjClass cjClass) { CjClass update = new CjClass(); update.setClassId(cjClass.getClassId()); update.setStatus(cjClass.getStatus()); update.setUpdateBy(getUsername()); return toAjax(cjClassService.updateCjClass(update)); } /** * 导入班级(模板含“所属学院/所属专业”名称列,判重键=学院+专业+班级名称) */ @PreAuthorize("@ss.hasPermi('system:class:import')") @Log(title = "班级", businessType = BusinessType.IMPORT) @PostMapping("/importData") public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception { ExcelUtil util = new ExcelUtil(CjClass.class); List classList = util.importExcel(file.getInputStream()); String message; int insertCount = 0, updateCount = 0, failCount = 0; StringBuilder failMsg = new StringBuilder(); String operName = getUsername(); for (int i = 0; i < classList.size(); i++) { CjClass cl = classList.get(i); int rowNo = i + 1; try { if (cl.getCollegeName() == null || cl.getCollegeName().trim().isEmpty()) { failCount++; failMsg.append("第" + rowNo + "条: 所属学院不能为空; "); continue; } if (cl.getMajorName() == null || cl.getMajorName().trim().isEmpty()) { failCount++; failMsg.append("第" + rowNo + "条: 所属专业不能为空; "); continue; } if (cl.getClassName() == null || cl.getClassName().trim().isEmpty()) { failCount++; failMsg.append("第" + rowNo + "条: 班级名称不能为空; "); continue; } String collegeName = cl.getCollegeName().trim(); String majorName = cl.getMajorName().trim(); cl.setCollegeName(collegeName); cl.setMajorName(majorName); cl.setClassName(cl.getClassName().trim()); // 父级两级必须为启用中 List colleges = cjCollegeMapper.selectEnabledByCollegeName(collegeName); if (colleges.isEmpty()) { failCount++; failMsg.append("第" + rowNo + "条: 所属学院“" + collegeName + "”不存在或已停用; "); continue; } if (colleges.size() > 1) { failCount++; failMsg.append("第" + rowNo + "条: 所属学院“" + collegeName + "”在系统中重复,请先清理; "); continue; } Long collegeId = colleges.get(0).getCollegeId(); List majors = cjMajorMapper.selectEnabledByCollegeIdAndName(collegeId, majorName); if (majors.isEmpty()) { failCount++; failMsg.append("第" + rowNo + "条: 所属专业“" + majorName + "”在学院“" + collegeName + "”下不存在或已停用; "); continue; } if (majors.size() > 1) { failCount++; failMsg.append("第" + rowNo + "条: 专业“" + majorName + "”在该学院下重复,请先清理; "); continue; } Long majorId = majors.get(0).getMajorId(); List exists = cjClassMapper.selectByMajorIdAndName(majorId, cl.getClassName()); if (exists.isEmpty()) { cl.setMajorId(majorId); cl.setCreateBy(operName); cjClassService.insertCjClass(cl); insertCount++; } else if (exists.size() > 1) { failCount++; failMsg.append("第" + rowNo + "条: 班级“" + cl.getClassName() + "”在该专业下重复,无法确定更新对象; "); } else if (updateSupport) { CjClass old = exists.get(0); cl.setClassId(old.getClassId()); cl.setMajorId(majorId); cl.setUpdateBy(operName); cjClassService.updateCjClass(cl); updateCount++; } else { failCount++; failMsg.append("第" + rowNo + "条: 班级“" + cl.getClassName() + "”在专业“" + majorName + "”下已存在; "); } } catch (Exception e) { failCount++; failMsg.append("第" + rowNo + "条: 导入失败(" + e.getMessage() + "); "); } } if (updateSupport) message = String.format("导入完成!新增 %d 条,更新 %d 条", insertCount, updateCount); else message = String.format("导入完成!新增 %d 条,由于数据重复导致导入失败 %d 条", insertCount, failCount); Map data = new HashMap<>(); data.put("message", message); data.put("insertCount", insertCount); data.put("updateCount", updateCount); data.put("failCount", failCount); data.put("failMsg", failMsg.toString()); return AjaxResult.success(data); } /** * 下载班级导入模板 */ @PreAuthorize("@ss.hasPermi('system:class:import')") @PostMapping("/importTemplate") public void importTemplate(HttpServletResponse response) throws Exception { ExcelUtil util = new ExcelUtil(CjClass.class); util.importTemplateExcel(response, "班级导入模板"); } }