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.domain.CjMajor;
|
import com.ruoyi.system.mapper.CjCollegeMapper;
|
import com.ruoyi.system.mapper.CjClassMapper;
|
import com.ruoyi.system.mapper.CjMajorMapper;
|
import com.ruoyi.system.service.ICjMajorService;
|
import com.ruoyi.common.utils.poi.ExcelUtil;
|
import com.ruoyi.common.core.page.TableDataInfo;
|
|
/**
|
* 专业Controller
|
*
|
* @author ruoyi
|
* @date 2026-09-03
|
*/
|
@RestController
|
@RequestMapping("/system/major")
|
public class CjMajorController extends BaseController
|
{
|
@Autowired
|
private ICjMajorService cjMajorService;
|
@Autowired
|
private CjCollegeMapper cjCollegeMapper;
|
@Autowired
|
private CjClassMapper cjClassMapper;
|
@Autowired
|
private CjMajorMapper cjMajorMapper;
|
|
/**
|
* 回填所属学院名称(transient,供列表/导出展示)
|
*/
|
private void fillCollegeName(List<CjMajor> majors)
|
{
|
if (majors == null || majors.isEmpty())
|
{
|
return;
|
}
|
Map<Long, String> map = new HashMap<>();
|
List<CjCollege> colleges = cjCollegeMapper.selectCjCollegeList(new CjCollege());
|
if (colleges != null)
|
{
|
for (CjCollege c : colleges)
|
{
|
map.put(c.getCollegeId(), c.getCollegeName());
|
}
|
}
|
for (CjMajor m : majors)
|
{
|
if (m.getCollegeId() != null)
|
{
|
m.setCollegeName(map.get(m.getCollegeId()));
|
}
|
}
|
}
|
|
/**
|
* 查询专业列表
|
*/
|
@PreAuthorize("@ss.hasPermi('system:major:list')")
|
@GetMapping("/list")
|
public TableDataInfo list(CjMajor cjMajor)
|
{
|
startPage();
|
List<CjMajor> list = cjMajorService.selectCjMajorList(cjMajor);
|
fillCollegeName(list);
|
return getDataTable(list);
|
}
|
|
/**
|
* 导出专业列表
|
*/
|
@PreAuthorize("@ss.hasPermi('system:major:export')")
|
@Log(title = "专业", businessType = BusinessType.EXPORT)
|
@PostMapping("/export")
|
public void export(HttpServletResponse response, CjMajor cjMajor)
|
{
|
List<CjMajor> list = cjMajorService.selectCjMajorList(cjMajor);
|
fillCollegeName(list);
|
ExcelUtil<CjMajor> util = new ExcelUtil<CjMajor>(CjMajor.class);
|
util.exportExcel(response, list, "专业数据");
|
}
|
|
/**
|
* 获取专业详细信息
|
*/
|
@PreAuthorize("@ss.hasPermi('system:major:query')")
|
@GetMapping(value = "/{majorId}")
|
public AjaxResult getInfo(@PathVariable("majorId") Long majorId)
|
{
|
return success(cjMajorService.selectCjMajorByMajorId(majorId));
|
}
|
|
/**
|
* 新增专业
|
*/
|
@PreAuthorize("@ss.hasPermi('system:major:add')")
|
@Log(title = "专业", businessType = BusinessType.INSERT)
|
@PostMapping
|
public AjaxResult add(@RequestBody CjMajor cjMajor)
|
{
|
return toAjax(cjMajorService.insertCjMajor(cjMajor));
|
}
|
|
/**
|
* 修改专业
|
*/
|
@PreAuthorize("@ss.hasPermi('system:major:edit')")
|
@Log(title = "专业", businessType = BusinessType.UPDATE)
|
@PutMapping
|
public AjaxResult edit(@RequestBody CjMajor cjMajor)
|
{
|
return toAjax(cjMajorService.updateCjMajor(cjMajor));
|
}
|
|
/**
|
* 删除专业(引用保护:其下存在班级时禁止删除)
|
*/
|
@PreAuthorize("@ss.hasPermi('system:major:remove')")
|
@Log(title = "专业", businessType = BusinessType.DELETE)
|
@DeleteMapping("/{majorIds}")
|
public AjaxResult remove(@PathVariable Long[] majorIds)
|
{
|
if (majorIds != null && majorIds.length > 0
|
&& cjClassMapper.countByMajorIds(majorIds) > 0)
|
{
|
return error("所选专业下存在班级,无法删除;请先删除其下班级,或改用“停用”");
|
}
|
return toAjax(cjMajorService.deleteCjMajorByMajorIds(majorIds));
|
}
|
|
/**
|
* 专业停用/启用(0正常 1停用)
|
*/
|
@PreAuthorize("@ss.hasPermi('system:major:status')")
|
@Log(title = "专业", businessType = BusinessType.UPDATE)
|
@PutMapping("/changeStatus")
|
public AjaxResult changeStatus(@RequestBody CjMajor cjMajor)
|
{
|
CjMajor update = new CjMajor();
|
update.setMajorId(cjMajor.getMajorId());
|
update.setStatus(cjMajor.getStatus());
|
update.setUpdateBy(getUsername());
|
return toAjax(cjMajorService.updateCjMajor(update));
|
}
|
|
/**
|
* 导入专业(模板含“所属学院”名称列,判重键=所属学院+专业名称)
|
*/
|
@PreAuthorize("@ss.hasPermi('system:major:import')")
|
@Log(title = "专业", businessType = BusinessType.IMPORT)
|
@PostMapping("/importData")
|
public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
|
{
|
ExcelUtil<CjMajor> util = new ExcelUtil<CjMajor>(CjMajor.class);
|
List<CjMajor> majorList = 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 < majorList.size(); i++)
|
{
|
CjMajor mj = majorList.get(i);
|
int rowNo = i + 1;
|
try
|
{
|
if (mj.getCollegeName() == null || mj.getCollegeName().trim().isEmpty())
|
{
|
failCount++;
|
failMsg.append("第" + rowNo + "条: 所属学院不能为空; ");
|
continue;
|
}
|
if (mj.getMajorName() == null || mj.getMajorName().trim().isEmpty())
|
{
|
failCount++;
|
failMsg.append("第" + rowNo + "条: 专业名称不能为空; ");
|
continue;
|
}
|
String collegeName = mj.getCollegeName().trim();
|
mj.setCollegeName(collegeName);
|
mj.setMajorName(mj.getMajorName().trim());
|
// 父级必须为启用中学院
|
List<CjCollege> 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<CjMajor> exists = cjMajorMapper.selectByCollegeIdAndName(collegeId, mj.getMajorName());
|
if (exists.isEmpty())
|
{
|
mj.setCollegeId(collegeId);
|
mj.setCreateBy(operName);
|
cjMajorService.insertCjMajor(mj);
|
insertCount++;
|
}
|
else if (exists.size() > 1)
|
{
|
failCount++;
|
failMsg.append("第" + rowNo + "条: 专业“" + mj.getMajorName() + "”在该学院下重复,无法确定更新对象; ");
|
}
|
else if (updateSupport)
|
{
|
CjMajor old = exists.get(0);
|
mj.setMajorId(old.getMajorId());
|
mj.setCollegeId(collegeId);
|
mj.setUpdateBy(operName);
|
cjMajorService.updateCjMajor(mj);
|
updateCount++;
|
}
|
else
|
{
|
failCount++;
|
failMsg.append("第" + rowNo + "条: 专业“" + mj.getMajorName() + "”在学院“" + collegeName + "”下已存在; ");
|
}
|
}
|
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<String, Object> 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:major:import')")
|
@PostMapping("/importTemplate")
|
public void importTemplate(HttpServletResponse response) throws Exception
|
{
|
ExcelUtil<CjMajor> util = new ExcelUtil<CjMajor>(CjMajor.class);
|
util.importTemplateExcel(response, "专业导入模板");
|
}
|
}
|