package com.template.controller; import com.template.api.WelcomeBedControllerAPI; import com.template.common.utils.ExcelUtils; import com.template.common.utils.paramUtils; import com.template.model.enumModel.eFileType; import com.template.model.pojo.*; import com.template.model.request.InsertWelcomeBedRequest; import com.template.model.request.UpdateWelcomeBedRequest; import com.template.model.result.CommonResult; import com.template.model.result.PageUtils; import com.template.model.vo.ListVo; import com.template.model.vo.StudentPageVo; import com.template.services.*; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; /** *

* 前端控制器 *

* * @author ceshi * @since 2025-06-13 */ @RestController public class WelcomeBedController implements WelcomeBedControllerAPI { @Autowired private WelcomeOrgService welcomeOrgService; @Autowired private WelcomeBedService welcomeBedService; @Autowired private WelcomeBuildService welcomeBuildService; @Autowired private WelcomeStudentService welcomeStudentService; @Autowired private WelcomeDormitoryService welcomeDormitoryService; @Override public CommonResult insertBedInfo(InsertWelcomeBedRequest iwbr, BindingResult bindingResult) { if (bindingResult.hasErrors()) { String st = paramUtils.getParamError(bindingResult); return CommonResult.fail(st); } WelcomeBed existData = welcomeBedService.queryBedData(iwbr.getSchool(),iwbr.getBuildId(),iwbr.getDormitoryId(),iwbr.getNumber()); if(existData != null){ return CommonResult.fail("该床位号已存在,请勿重复插入!"); } WelcomeBed wb = new WelcomeBed(); if(StringUtils.hasText(iwbr.getCardNum())){ WelcomeStudent student =welcomeStudentService.queryStudentInfo(iwbr.getCollegeId(),iwbr.getMajorId(),iwbr.getClassstrId(),iwbr.getCardNum()); if(student == null){ return CommonResult.fail("当前学生信息在系统中不存在!"); } wb.setStudentCard(student.getCardId()); wb.setCollegeId(iwbr.getCollegeId()); wb.setCollege(iwbr.getCollege()); wb.setMajorId(iwbr.getMajorId()); wb.setMajor(iwbr.getMajor()); wb.setClassstrId(iwbr.getClassstrId()); wb.setClassstr(iwbr.getClassstr()); wb.setCardNum(iwbr.getCardNum()); wb.setName(iwbr.getName()); } wb.setNumber(iwbr.getNumber()); wb.setSchool(iwbr.getSchool()); wb.setSchoolId(iwbr.getSchoolId()); wb.setBuildId(iwbr.getBuildId()); wb.setBuild(iwbr.getBuild()); wb.setDormitoryId(iwbr.getDormitoryId()); wb.setDormitory(iwbr.getDormitory()); wb.setSex(iwbr.getSex()); wb.setIsCheck(StringUtils.hasText(iwbr.getCardNum()) ? 1 : 0); wb.setRemark(iwbr.getRemark()); int result = welcomeBedService.insertWelcomeBed(wb); return result > 0 ? CommonResult.ok("添加成功!") : CommonResult.fail("添加失败"); } @Override public CommonResult updateBedInfo(UpdateWelcomeBedRequest uwbr, BindingResult bindingResult) throws Exception { if (bindingResult.hasErrors()) { String st = paramUtils.getParamError(bindingResult); return CommonResult.fail(st); } WelcomeBed oldData = welcomeBedService.getBedById(uwbr.getId()); if(oldData == null){ return CommonResult.fail("床位信息已失效,编辑失败"); } WelcomeBed existData = welcomeBedService.queryBedData(uwbr.getSchool(),uwbr.getBuildId(),uwbr.getDormitoryId(),uwbr.getNumber()); if(existData != null && existData.getId().intValue() != uwbr.getId().intValue()){ return CommonResult.fail("该床位号已存在,请勿重复插入!"); } if(StringUtils.hasText(uwbr.getCardNum())){ WelcomeStudent student =welcomeStudentService.queryStudentInfo(uwbr.getCollegeId(),uwbr.getMajorId(),uwbr.getClassstrId(),uwbr.getCardNum()); if(student == null){ return CommonResult.fail("当前学生信息在系统中不存在!"); } oldData.setStudentCard(student.getCardId()); oldData.setCollegeId(uwbr.getCollegeId()); oldData.setCollege(uwbr.getCollege()); oldData.setMajorId(uwbr.getMajorId()); oldData.setMajor(uwbr.getMajor()); oldData.setClassstrId(uwbr.getClassstrId()); oldData.setClassstr(uwbr.getClassstr()); oldData.setCardNum(uwbr.getCardNum()); oldData.setName(uwbr.getName()); } oldData.setNumber(uwbr.getNumber()); oldData.setSchool(uwbr.getSchool()); oldData.setSchoolId(uwbr.getSchoolId()); oldData.setBuildId(uwbr.getBuildId()); oldData.setBuild(uwbr.getBuild()); oldData.setDormitoryId(uwbr.getDormitoryId()); oldData.setDormitory(uwbr.getDormitory()); oldData.setSex(uwbr.getSex()); oldData.setCollegeId(uwbr.getCollegeId()); oldData.setCollege(uwbr.getCollege()); oldData.setMajorId(uwbr.getMajorId()); oldData.setMajor(uwbr.getMajor()); oldData.setClassstrId(uwbr.getClassstrId()); oldData.setClassstr(uwbr.getClassstr()); oldData.setIsCheck(uwbr.getIsCheck()); oldData.setCardNum(uwbr.getCardNum()); oldData.setName(uwbr.getName()); oldData.setRemark(uwbr.getRemark()); int result = welcomeBedService.updateWelcomeBed(oldData); return result > 0 ? CommonResult.ok("编辑成功!") : CommonResult.fail("编辑失败"); } @Override public CommonResult queryPageBeds(int currentPage, int pageCount, Integer schoolId, Integer buildId, Integer dormitoryId, String sex, Integer isCheck, Integer collegeId, Integer majorId, Integer classstrId) { PageUtils result = welcomeBedService.queryPageWelcomeBeds(currentPage, pageCount, schoolId, buildId, dormitoryId, sex, isCheck, collegeId, majorId, classstrId); return CommonResult.ok(result); } @Override public CommonResult deleteBedInfo(int id) throws Exception { int result = welcomeBedService.deleteWelcomeBedById(id); return result > 0 ? CommonResult.ok() : CommonResult.fail(); } @Override public CommonResult importBedExcel(MultipartFile file) throws IOException, ParseException { System.out.println("导入床位信息"); if (file.isEmpty() || file.getSize() == 0) { return CommonResult.fail("导入文件不能为空"); } String ContentType = file.getContentType(); InputStream inputStream = file.getInputStream(); List result = new ArrayList<>(); //xls格式文件 if (ContentType.equals(eFileType.Xls.getValue())) { CommonResult> resultData = readXls(inputStream); if (!resultData.isSuccess()) { return resultData; } result = resultData.getData(); } else if (ContentType.equals(eFileType.Xlsx.getValue())) { CommonResult> resultData = readXlsx(inputStream); if (!resultData.isSuccess()) { return resultData; } result = resultData.getData(); } else { return CommonResult.fail("床位信息数据导入只支持Xls或Xlsx格式文件"); } List admissNums = result.stream().map(WelcomeBed::getCardNum).distinct().collect(Collectors.toList()); if(admissNums != null && admissNums.size() > 0){ List students = welcomeStudentService.getDataByAdmissNum(admissNums); if(students != null && students.size() > 0){ for (WelcomeBed r:result) { Optional os = students.stream().filter(e -> e.getAdmissNum().equals(r.getCardNum())).findFirst(); if(os != null && os.isPresent()){ r.setStudentCard(os.get().getCardId()); } } } } boolean resultBool = welcomeBedService.saveOrUpdateBatch(result); System.out.println("导入楼栋1"); return resultBool ? CommonResult.ok("导入成功") : CommonResult.fail("导入失败"); } /** * xls文件读取方法 * * @param inputStream * @return * @throws IOException * @throws ParseException */ private CommonResult> readXls(InputStream inputStream) throws IOException, ParseException { List result = new ArrayList<>(); HSSFWorkbook sheets = new HSSFWorkbook(inputStream); List wb = welcomeBedService.list(null); List wos = welcomeOrgService.list(null); List wbils = welcomeBuildService.list(null); List wds = welcomeDormitoryService.list(null); //读取第一张sheet HSSFSheet sheetAt = sheets.getSheetAt(0); DataFormatter dataFormatter = new DataFormatter(); try { //rowNum = 3 从第三行开始获取值 //sheetAt.getLastRowNum():从0开始统计数量 所以得+1 for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) { HSSFRow row = sheetAt.getRow(rowNum); if (row != null) { //使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。 //所以先使用setCellType()方法先将该单元格的类型设置为STRING //然后poi会根据字符串读取它 if (rowNum == 0) { String num = dataFormatter.formatCellValue(row.getCell(0));//序号 if (!num.equals("序号")) { return CommonResult.fail("导入数据第一列为序号"); } String school = dataFormatter.formatCellValue(row.getCell(1));//序号 if (!school.equals("校区名称")) { return CommonResult.fail("导入数据第一列为校区名称"); } String build = dataFormatter.formatCellValue(row.getCell(2));//校区名称 if (!build.equals("楼栋名称")) { return CommonResult.fail("导入数据第二列为楼栋名称"); } String dormitory = dataFormatter.formatCellValue(row.getCell(3));//楼栋名称 if (!dormitory.equals("寝室号")) { return CommonResult.fail("导入数据第三列为寝室号"); } String number = dataFormatter.formatCellValue(row.getCell(4));//楼栋性别 if (!number.equals("床位号")) { return CommonResult.fail("导入数据第四列为床位号"); } String sex = dataFormatter.formatCellValue(row.getCell(5));//楼栋层数 if (!sex.equals("床位性别")) { return CommonResult.fail("导入数据第五列为床位性别"); } String college = dataFormatter.formatCellValue(row.getCell(6));//起始层数 if (!college.equals("所属学院")) { return CommonResult.fail("导入数据第六列为所属学院"); } String major = dataFormatter.formatCellValue(row.getCell(7));//备注 if (!major.equals("所属专业")) { return CommonResult.fail("导入数据第七列为所属专业"); } String classstr = dataFormatter.formatCellValue(row.getCell(8));//备注 if (!classstr.equals("所属班级")) { return CommonResult.fail("导入数据第七列为所属班级"); } String isCheck = dataFormatter.formatCellValue(row.getCell(9));//备注 if (!isCheck.equals("是否入住")) { return CommonResult.fail("导入数据第七列为是否入住"); } String admissNum = dataFormatter.formatCellValue(row.getCell(10));//备注 if (!admissNum.equals("录取号")) { return CommonResult.fail("导入数据第七列为录取号"); } String name = dataFormatter.formatCellValue(row.getCell(11));//备注 if (!name.equals("姓名")) { return CommonResult.fail("导入数据第七列为姓名"); } String remark = dataFormatter.formatCellValue(row.getCell(12));//备注 if (!remark.equals("备注")) { return CommonResult.fail("导入数据第七列为备注"); } } else { WelcomeBed bedData = new WelcomeBed(); String school = dataFormatter.formatCellValue(row.getCell(1));//校区 String build = dataFormatter.formatCellValue(row.getCell(2));//楼栋 String dormitory = dataFormatter.formatCellValue(row.getCell(3));//寝室号 String number = dataFormatter.formatCellValue(row.getCell(4));//床位号 Optional ob = wb.stream().filter(e -> e.getSchool().equals(school) && e.getBuild().equals(build) && e.getDormitory().equals(dormitory) && e.getNumber().intValue() == Integer.valueOf(number).intValue()).findFirst(); if(ob != null && ob.isPresent()){ bedData.setId(ob.get().getId()); } String sex = dataFormatter.formatCellValue(row.getCell(5));//楼栋层数 String college = dataFormatter.formatCellValue(row.getCell(6));//起始层数 String major = dataFormatter.formatCellValue(row.getCell(7));//备注 String classstr = dataFormatter.formatCellValue(row.getCell(8));//备注 String isCheck = dataFormatter.formatCellValue(row.getCell(9));//备注 String admissNum = dataFormatter.formatCellValue(row.getCell(10));//备注 String name = dataFormatter.formatCellValue(row.getCell(11));//备注 String remark = dataFormatter.formatCellValue(row.getCell(12));//备注 bedData.setSchool(school); //楼栋 if(wbils != null && wbils.size() > 0){ Optional oBuild = wbils.stream().filter(e -> e.getBuild().equals(build)).findFirst(); if(oBuild != null && oBuild.isPresent()){ bedData.setBuild(build); bedData.setBuildId(oBuild.get().getId()); } } //寝室 if(wds != null && wds.size() > 0){ Optional oDormitory = wds.stream().filter(e -> e.getDormitory().equals(dormitory)).findFirst(); if(oDormitory != null && oDormitory.isPresent()){ bedData.setDormitory(dormitory); bedData.setDormitoryId(oDormitory.get().getId()); } } bedData.setNumber(Integer.valueOf(number)); bedData.setSex(sex); //region 院系专业 Optional owo = wos.stream().filter(e -> e.getName().equals(college)).findFirst(); if (owo != null && owo.isPresent()) { bedData.setCollege(college); bedData.setCollegeId(owo.get().getId()); } Optional omwo = wos.stream().filter(e -> e.getName().equals(major)).findFirst(); if (omwo != null && omwo.isPresent()) { bedData.setMajor(major); bedData.setMajorId(omwo.get().getId()); } Optional ocwo = wos.stream().filter(e -> e.getName().equals(classstr)).findFirst(); if (ocwo != null && ocwo.isPresent()) { bedData.setClassstr(classstr); bedData.setClassstrId(ocwo.get().getId()); } //endregion bedData.setIsCheck(Integer.valueOf(isCheck)); bedData.setCardNum(admissNum); bedData.setName(name); bedData.setRemark(remark); result.add(bedData); } } } } catch (Exception e) { return CommonResult.fail("请按模板格式导入数据"); } return CommonResult.ok(result); } /** * xls文件读取方法 * * @param inputStream * @return * @throws IOException * @throws ParseException */ private CommonResult> readXlsx(InputStream inputStream) throws IOException, ParseException { List result = new ArrayList<>(); XSSFWorkbook sheets = new XSSFWorkbook(inputStream); List wb = welcomeBedService.list(null); List wos = welcomeOrgService.list(null); List wbils = welcomeBuildService.list(null); List wds = welcomeDormitoryService.list(null); //读取第一张sheet XSSFSheet sheetAt = sheets.getSheetAt(0); DataFormatter dataFormatter = new DataFormatter(); try { //rowNum = 3 从第三行开始获取值 //sheetAt.getLastRowNum():从0开始统计数量 所以得+1 for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) { XSSFRow row = sheetAt.getRow(rowNum); if (row != null) { //使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。 //所以先使用setCellType()方法先将该单元格的类型设置为STRING //然后poi会根据字符串读取它 if (rowNum == 0) { String num = dataFormatter.formatCellValue(row.getCell(0));//序号 if (!num.equals("序号")) { return CommonResult.fail("导入数据第一列为序号"); } String school = dataFormatter.formatCellValue(row.getCell(1));//序号 if (!school.equals("校区名称")) { return CommonResult.fail("导入数据第一列为校区名称"); } String build = dataFormatter.formatCellValue(row.getCell(2));//校区名称 if (!build.equals("楼栋名称")) { return CommonResult.fail("导入数据第二列为楼栋名称"); } String dormitory = dataFormatter.formatCellValue(row.getCell(3));//楼栋名称 if (!dormitory.equals("寝室号")) { return CommonResult.fail("导入数据第三列为寝室号"); } String number = dataFormatter.formatCellValue(row.getCell(4));//楼栋性别 if (!number.equals("床位号")) { return CommonResult.fail("导入数据第四列为床位号"); } String sex = dataFormatter.formatCellValue(row.getCell(5));//楼栋层数 if (!sex.equals("床位性别")) { return CommonResult.fail("导入数据第五列为床位性别"); } String college = dataFormatter.formatCellValue(row.getCell(6));//起始层数 if (!college.equals("所属学院")) { return CommonResult.fail("导入数据第六列为所属学院"); } String major = dataFormatter.formatCellValue(row.getCell(7));//备注 if (!major.equals("所属专业")) { return CommonResult.fail("导入数据第七列为所属专业"); } String classstr = dataFormatter.formatCellValue(row.getCell(8));//备注 if (!classstr.equals("所属班级")) { return CommonResult.fail("导入数据第七列为所属班级"); } String isCheck = dataFormatter.formatCellValue(row.getCell(9));//备注 if (!isCheck.equals("是否入住")) { return CommonResult.fail("导入数据第七列为是否入住"); } String admissNum = dataFormatter.formatCellValue(row.getCell(10));//备注 if (!admissNum.equals("录取号")) { return CommonResult.fail("导入数据第七列为录取号"); } String name = dataFormatter.formatCellValue(row.getCell(11));//备注 if (!name.equals("姓名")) { return CommonResult.fail("导入数据第七列为姓名"); } String remark = dataFormatter.formatCellValue(row.getCell(12));//备注 if (!remark.equals("备注")) { return CommonResult.fail("导入数据第七列为备注"); } } else { WelcomeBed bedData = new WelcomeBed(); String school = dataFormatter.formatCellValue(row.getCell(1));//校区 String build = dataFormatter.formatCellValue(row.getCell(2));//楼栋 String dormitory = dataFormatter.formatCellValue(row.getCell(3));//寝室号 String number = dataFormatter.formatCellValue(row.getCell(4));//床位号 Optional ob = wb.stream().filter(e -> e.getSchool().equals(school) && e.getBuild().equals(build) && e.getDormitory().equals(dormitory) && e.getNumber().intValue() == Integer.valueOf(number).intValue()).findFirst(); if(ob != null && ob.isPresent()){ bedData.setId(ob.get().getId()); } String sex = dataFormatter.formatCellValue(row.getCell(5));//楼栋层数 String college = dataFormatter.formatCellValue(row.getCell(6));//起始层数 String major = dataFormatter.formatCellValue(row.getCell(7));//备注 String classstr = dataFormatter.formatCellValue(row.getCell(8));//备注 String isCheck = dataFormatter.formatCellValue(row.getCell(9));//备注 String admissNum = dataFormatter.formatCellValue(row.getCell(10));//备注 String name = dataFormatter.formatCellValue(row.getCell(11));//备注 String remark = dataFormatter.formatCellValue(row.getCell(12));//备注 bedData.setSchool(school); //楼栋 if(wbils != null && wbils.size() > 0){ Optional oBuild = wbils.stream().filter(e -> e.getBuild().equals(build)).findFirst(); if(oBuild != null && oBuild.isPresent()){ bedData.setBuild(build); bedData.setBuildId(oBuild.get().getId()); } } //寝室 if(wds != null && wds.size() > 0){ Optional oDormitory = wds.stream().filter(e -> e.getDormitory().equals(dormitory)).findFirst(); if(oDormitory != null && oDormitory.isPresent()){ bedData.setDormitory(dormitory); bedData.setDormitoryId(oDormitory.get().getId()); } } bedData.setNumber(Integer.valueOf(number)); bedData.setSex(sex); //region 院系专业 Optional owo = wos.stream().filter(e -> e.getName().equals(college)).findFirst(); if (owo != null && owo.isPresent()) { bedData.setCollege(college); bedData.setCollegeId(owo.get().getId()); } Optional omwo = wos.stream().filter(e -> e.getName().equals(major)).findFirst(); if (omwo != null && omwo.isPresent()) { bedData.setMajor(major); bedData.setMajorId(omwo.get().getId()); } Optional ocwo = wos.stream().filter(e -> e.getName().equals(classstr)).findFirst(); if (ocwo != null && ocwo.isPresent()) { bedData.setClassstr(classstr); bedData.setClassstrId(ocwo.get().getId()); } //endregion bedData.setIsCheck(Integer.valueOf(isCheck)); bedData.setCardNum(admissNum); bedData.setName(name); bedData.setRemark(remark); result.add(bedData); } } } } catch (Exception e) { return CommonResult.fail("请按模板格式导入数据"); } return CommonResult.ok(result); } @Override public CommonResult downloadBedExcel() { return CommonResult.ok("200", "操作成功", "https://chtech.ncjti.edu.cn/alumnus/homeimage/床位信息管理.xlsx"); } @Override public void welcomeBedExport(HttpServletResponse response, Integer schoolId, Integer buildId, Integer dormitoryId, String sex, Integer isCheck, Integer collegeId, Integer majorId, Integer classstrId) { List result = welcomeBedService.queryPageWelcomeBeds(schoolId, buildId, dormitoryId, sex, isCheck, collegeId, majorId, classstrId); //导出 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("床位信息"); Row headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue("序号"); headerRow.createCell(1).setCellValue("校区名称"); headerRow.createCell(2).setCellValue("楼栋名称"); headerRow.createCell(3).setCellValue("寝室号"); headerRow.createCell(4).setCellValue("床位号"); headerRow.createCell(5).setCellValue("床位性别"); headerRow.createCell(6).setCellValue("所属学院"); headerRow.createCell(7).setCellValue("所属专业"); headerRow.createCell(8).setCellValue("所属班级"); headerRow.createCell(9).setCellValue("是否入住"); headerRow.createCell(10).setCellValue("录取号"); headerRow.createCell(11).setCellValue("姓名"); headerRow.createCell(12).setCellValue("备注"); for (int i = 0; i < result.size(); i++) { WelcomeBed bed = result.get(i); Row dataRow = sheet.createRow(i + 1); dataRow.createCell(0).setCellValue(i + 1); dataRow.createCell(1).setCellValue(bed.getSchool()); dataRow.createCell(2).setCellValue(bed.getBuild()); dataRow.createCell(3).setCellValue(bed.getDormitory()); dataRow.createCell(4).setCellValue(bed.getNumber()); dataRow.createCell(5).setCellValue(bed.getSex()); dataRow.createCell(6).setCellValue(bed.getCollege()); dataRow.createCell(7).setCellValue(bed.getMajor()); dataRow.createCell(8).setCellValue(bed.getClassstr()); dataRow.createCell(9).setCellValue(bed.getIsCheck()); dataRow.createCell(10).setCellValue(bed.getCardNum()); dataRow.createCell(11).setCellValue(bed.getName()); dataRow.createCell(12).setCellValue(bed.getRemark()); } // 将工作簿写入文件 ExcelUtils.excelDownload(workbook, "床位信息.xlsx", response); } @Override public CommonResult submit(WelcomeBed welcomeBed) { welcomeBedService.updateById(welcomeBed); return CommonResult.ok(); } @Override public CommonResult bedDetails(Integer schoolId, Integer buildId, Integer dormitoryId, String studentCard) { // 获取未入住的床位 List beds = welcomeBedService.getBedList(schoolId, buildId, dormitoryId, studentCard); return CommonResult.ok(beds); } @Override public CommonResult queryBedDatas(int dormitoryId) { List beds = welcomeBedService.queryDormitorys(dormitoryId); List datas = new ArrayList<>(); for (WelcomeBed bed:beds) { ListVo data = new ListVo(); data.setId(bed.getId()); data.setName(bed.getName()); datas.add(data); } return CommonResult.ok(datas); } }