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.CjCollege; import com.ruoyi.system.mapper.CjCollegeMapper; import com.ruoyi.system.mapper.CjMajorMapper; import com.ruoyi.system.service.ICjCollegeService; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.core.page.TableDataInfo; /** * 学院Controller * * @author ruoyi * @date 2026-09-03 */ @RestController @RequestMapping("/system/college") public class CjCollegeController extends BaseController { @Autowired private ICjCollegeService cjCollegeService; @Autowired private CjCollegeMapper cjCollegeMapper; @Autowired private CjMajorMapper cjMajorMapper; /** * 查询学院列表 */ @PreAuthorize("@ss.hasPermi('system:college:list')") @GetMapping("/list") public TableDataInfo list(CjCollege cjCollege) { startPage(); List list = cjCollegeService.selectCjCollegeList(cjCollege); return getDataTable(list); } /** * 导出学院列表 */ @PreAuthorize("@ss.hasPermi('system:college:export')") @Log(title = "学院", businessType = BusinessType.EXPORT) @PostMapping("/export") public void export(HttpServletResponse response, CjCollege cjCollege) { List list = cjCollegeService.selectCjCollegeList(cjCollege); ExcelUtil util = new ExcelUtil(CjCollege.class); util.exportExcel(response, list, "学院数据"); } /** * 获取学院详细信息 */ @PreAuthorize("@ss.hasPermi('system:college:query')") @GetMapping(value = "/{collegeId}") public AjaxResult getInfo(@PathVariable("collegeId") Long collegeId) { return success(cjCollegeService.selectCjCollegeByCollegeId(collegeId)); } /** * 新增学院 */ @PreAuthorize("@ss.hasPermi('system:college:add')") @Log(title = "学院", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody CjCollege cjCollege) { return toAjax(cjCollegeService.insertCjCollege(cjCollege)); } /** * 修改学院 */ @PreAuthorize("@ss.hasPermi('system:college:edit')") @Log(title = "学院", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody CjCollege cjCollege) { return toAjax(cjCollegeService.updateCjCollege(cjCollege)); } /** * 删除学院(引用保护:其下存在专业时禁止删除) */ @PreAuthorize("@ss.hasPermi('system:college:remove')") @Log(title = "学院", businessType = BusinessType.DELETE) @DeleteMapping("/{collegeIds}") public AjaxResult remove(@PathVariable Long[] collegeIds) { if (collegeIds != null && collegeIds.length > 0 && cjMajorMapper.countByCollegeIds(collegeIds) > 0) { return error("该学院下存在专业,无法删除;请先删除其下专业,或改用“停用”"); } return toAjax(cjCollegeService.deleteCjCollegeByCollegeIds(collegeIds)); } /** * 学院停用/启用(0正常 1停用) */ @PreAuthorize("@ss.hasPermi('system:college:status')") @Log(title = "学院", businessType = BusinessType.UPDATE) @PutMapping("/changeStatus") public AjaxResult changeStatus(@RequestBody CjCollege cjCollege) { CjCollege update = new CjCollege(); update.setCollegeId(cjCollege.getCollegeId()); update.setStatus(cjCollege.getStatus()); update.setUpdateBy(getUsername()); return toAjax(cjCollegeService.updateCjCollege(update)); } /** * 导入学院(冲突处理:匹配学院名称,覆盖或跳过) */ @PreAuthorize("@ss.hasPermi('system:college:import')") @Log(title = "学院", businessType = BusinessType.IMPORT) @PostMapping("/importData") public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception { ExcelUtil util = new ExcelUtil(CjCollege.class); List collegeList = 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 < collegeList.size(); i++) { CjCollege col = collegeList.get(i); int rowNo = i + 1; try { if (col.getCollegeName() == null || col.getCollegeName().trim().isEmpty()) { failCount++; failMsg.append("第" + rowNo + "条: 学院名称不能为空; "); continue; } col.setCollegeName(col.getCollegeName().trim()); List exists = cjCollegeMapper.selectByCollegeName(col.getCollegeName()); if (exists.isEmpty()) { col.setCreateBy(operName); cjCollegeService.insertCjCollege(col); insertCount++; } else if (exists.size() > 1) { failCount++; failMsg.append("第" + rowNo + "条: 学院名称“" + col.getCollegeName() + "”在系统中重复,无法确定更新对象; "); } else if (updateSupport) { CjCollege old = exists.get(0); col.setCollegeId(old.getCollegeId()); col.setUpdateBy(operName); cjCollegeService.updateCjCollege(col); updateCount++; } else { failCount++; failMsg.append("第" + rowNo + "条: 学院“" + col.getCollegeName() + "”已存在; "); } } 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:college:import')") @PostMapping("/importTemplate") public void importTemplate(HttpServletResponse response) throws Exception { ExcelUtil util = new ExcelUtil(CjCollege.class); util.importTemplateExcel(response, "学院导入模板"); } }