package com.template.controller; import com.template.api.WelcomePaySettingControllerAPI; import com.template.common.utils.CardUtils; import com.template.common.utils.ExcelUtils; import com.template.common.utils.paramUtils; import com.template.model.enumModel.eFileType; import com.template.model.enumModel.eSchool; import com.template.model.pojo.*; import com.template.model.request.insertPaySettingRequest; import com.template.model.request.updatePaySettingRequest; import com.template.model.result.CommonResult; import com.template.model.result.PageUtils; import com.template.model.vo.StudentPageVo; import com.template.services.WelcomeOrgService; import com.template.services.WelcomePaySettingService; 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.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; /** *

* 前端控制器 *

* * @author ceshi * @since 2025-06-13 */ @RestController public class WelcomePaySettingController implements WelcomePaySettingControllerAPI { @Autowired private WelcomeOrgService welcomeOrgService; @Autowired private WelcomePaySettingService welcomePaySettingService; @Override public CommonResult getPageDatas(int currentPage, int pageCount, Integer collegeId, Integer majorId, String eduLevel) { PageUtils pages = welcomePaySettingService.queryPageWelcomePaySettings(currentPage, pageCount, collegeId, majorId, eduLevel); return CommonResult.ok(pages); } @Override public CommonResult insertPaySetting(insertPaySettingRequest psr, BindingResult bindingResult) { if (bindingResult.hasErrors()) { String st = paramUtils.getParamError(bindingResult); return CommonResult.fail(st); } WelcomePaySetting exist = welcomePaySettingService.queryPaySettingById(psr.getCollegeId(), psr.getMajorId(), psr.getEduLevel()); if (exist != null) { return CommonResult.fail("当前院系专业已设置!"); } WelcomePaySetting wps = new WelcomePaySetting(); wps.setCollegeId(psr.getCollegeId()); wps.setCollegeName(psr.getCollegeName()); wps.setMajorId(psr.getMajorId()); wps.setMajorName(psr.getMajorName()); wps.setEduLevel(psr.getEduLevel()); wps.setEduSystem(psr.getEduSystem()); wps.setPayAmount(psr.getPayAmount()); int result = welcomePaySettingService.insertWelcomePaySetting(wps); return result > 0 ? CommonResult.ok("添加成功!") : CommonResult.fail("添加失败!"); } @Override public CommonResult updatePaySetting(updatePaySettingRequest psr, BindingResult bindingResult) { if (bindingResult.hasErrors()) { String st = paramUtils.getParamError(bindingResult); return CommonResult.fail(st); } WelcomePaySetting oldData = welcomePaySettingService.getManageById(psr.getId()); if (oldData == null) { return CommonResult.fail("当前数据已失效,编辑失败!"); } WelcomePaySetting exist = welcomePaySettingService.queryPaySettingById(psr.getCollegeId(), psr.getMajorId(), psr.getEduLevel()); if (exist != null && psr.getId().intValue() != exist.getId().intValue()) { return CommonResult.fail("当前院系专业已设置!"); } oldData.setCollegeId(psr.getCollegeId()); oldData.setCollegeName(psr.getCollegeName()); oldData.setMajorId(psr.getMajorId()); oldData.setMajorName(psr.getMajorName()); oldData.setEduLevel(psr.getEduLevel()); oldData.setEduSystem(psr.getEduSystem()); oldData.setPayAmount(psr.getPayAmount()); int result = welcomePaySettingService.updateWelcomePaySetting(oldData); return result > 0 ? CommonResult.ok("编辑成功!") : CommonResult.fail("编辑失败!"); } @Override public CommonResult deleteDormitory(Integer id) { int result = welcomePaySettingService.deleteWelcomePaySettingById(id); return result < 0 ? CommonResult.fail("删除失败") : CommonResult.ok("删除成功"); } @Override public CommonResult importPaySettingExcel(MultipartFile file) throws IOException, ParseException { 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 existPays = welcomePaySettingService.queryPaySettings(); if (existPays != null && existPays.size() > 0) { for (WelcomePaySetting wps : result) { Optional ows = existPays.stream().filter(e -> e.getCollegeId().intValue() == wps.getCollegeId().intValue() && e.getMajorId().intValue() == wps.getMajorId().intValue() && e.getEduLevel().equals(wps.getEduLevel())).findFirst(); if (ows != null && ows.isPresent()) { wps.setId(ows.get().getId()); } } } boolean resultBool = welcomePaySettingService.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 ws = welcomeOrgService.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 number = dataFormatter.formatCellValue(row.getCell(0));//序号 if (!number.equals("序号")) { return CommonResult.fail("导入数据第一列为序号"); } String college = dataFormatter.formatCellValue(row.getCell(1));//学院 if (!college.equals("学院")) { return CommonResult.fail("导入数据第二列为学院"); } String major = dataFormatter.formatCellValue(row.getCell(2));//专业名称 if (!major.equals("专业名称")) { return CommonResult.fail("导入数据第三列为专业名称"); } String eduLevel = dataFormatter.formatCellValue(row.getCell(3));//层次 if (!eduLevel.equals("层次")) { return CommonResult.fail("导入数据第四列为层次"); } String eduSystem = dataFormatter.formatCellValue(row.getCell(4));//学制 if (!eduSystem.equals("学制")) { return CommonResult.fail("导入数据第五列为学制"); } String payMoney = dataFormatter.formatCellValue(row.getCell(5));//金额 if (!payMoney.equals("金额")) { return CommonResult.fail("导入数据第六列为金额"); } } else { WelcomePaySetting paySetting = new WelcomePaySetting(); String college = dataFormatter.formatCellValue(row.getCell(1));//学院 if (!StringUtils.hasText(college)) { break; } //region 院系专业 Optional owo = ws.stream().filter(e -> e.getName().equals(college)).findFirst(); if (owo != null && owo.isPresent()) { paySetting.setCollegeName(college); paySetting.setCollegeId(owo.get().getId()); } String major = dataFormatter.formatCellValue(row.getCell(2));//专业 Optional omwo = ws.stream().filter(e -> owo != null && owo.isPresent() && e.getParentId().intValue() == owo.get().getId().intValue() && e.getName().equals(major)).findFirst(); if (omwo != null && omwo.isPresent()) { paySetting.setMajorName(major); paySetting.setMajorId(omwo.get().getId()); } String eduLevel = dataFormatter.formatCellValue(row.getCell(3));//层次 paySetting.setEduLevel(eduLevel); String eduSystem = dataFormatter.formatCellValue(row.getCell(4));//学制 paySetting.setEduSystem(eduSystem); String payMoney = dataFormatter.formatCellValue(row.getCell(5));//金额 paySetting.setPayAmount(new BigDecimal(payMoney)); result.add(paySetting); } } } } catch (Exception e) { return CommonResult.fail("请按模板格式导入数据"); } return CommonResult.ok(result); } /** * xlsx文件读取方法 * * @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 ws = welcomeOrgService.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 number = dataFormatter.formatCellValue(row.getCell(0));//序号 if (!number.equals("序号")) { return CommonResult.fail("导入数据第一列为序号"); } String college = dataFormatter.formatCellValue(row.getCell(1));//学院 if (!college.equals("学院")) { return CommonResult.fail("导入数据第二列为学院"); } String major = dataFormatter.formatCellValue(row.getCell(2));//专业名称 if (!major.equals("专业名称")) { return CommonResult.fail("导入数据第三列为专业名称"); } String eduLevel = dataFormatter.formatCellValue(row.getCell(3));//层次 if (!eduLevel.equals("层次")) { return CommonResult.fail("导入数据第四列为层次"); } String eduSystem = dataFormatter.formatCellValue(row.getCell(4));//学制 if (!eduSystem.equals("学制")) { return CommonResult.fail("导入数据第五列为学制"); } String payMoney = dataFormatter.formatCellValue(row.getCell(5));//金额 if (!payMoney.equals("金额")) { return CommonResult.fail("导入数据第六列为金额"); } } else { WelcomePaySetting paySetting = new WelcomePaySetting(); String college = dataFormatter.formatCellValue(row.getCell(1));//学院 if (!StringUtils.hasText(college)) { break; } //region 院系专业 Optional owo = ws.stream().filter(e -> e.getName().equals(college)).findFirst(); if (owo != null && owo.isPresent()) { paySetting.setCollegeName(college); paySetting.setCollegeId(owo.get().getId()); } String major = dataFormatter.formatCellValue(row.getCell(2));//专业 Optional omwo = ws.stream().filter(e -> owo != null && owo.isPresent() && e.getParentId().intValue() == owo.get().getId().intValue() && e.getName().equals(major)).findFirst(); if (omwo != null && omwo.isPresent()) { paySetting.setMajorName(major); paySetting.setMajorId(omwo.get().getId()); } String eduLevel = dataFormatter.formatCellValue(row.getCell(3));//层次 paySetting.setEduLevel(eduLevel); String eduSystem = dataFormatter.formatCellValue(row.getCell(4));//学制 paySetting.setEduSystem(eduSystem); String payMoney = dataFormatter.formatCellValue(row.getCell(5));//金额 paySetting.setPayAmount(new BigDecimal(payMoney)); result.add(paySetting); } } } } catch (Exception e) { return CommonResult.fail("请按模板格式导入数据"); } return CommonResult.ok(result); } @Override public CommonResult downloadPaySettingExcel() { return CommonResult.ok("200", "操作成功", "https://chtech.ncjti.edu.cn/welcome/homeimage/缴费设置管理.xlsx"); } @Override public void paySettingExport(String userId, HttpServletResponse response, Integer collegeId, Integer majorId, String eduLevel) { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("缴费设置"); List result = welcomePaySettingService.queryPaySettingsById(collegeId, majorId, eduLevel); //导出 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("金额"); for (int i = 0; i < result.size(); i++) { WelcomePaySetting studentData = result.get(i); Row dataRow = sheet.createRow(i + 1); dataRow.createCell(0).setCellValue(i + 1); dataRow.createCell(1).setCellValue(studentData.getCollegeName()); dataRow.createCell(2).setCellValue(studentData.getMajorName()); dataRow.createCell(3).setCellValue(studentData.getEduLevel()); dataRow.createCell(4).setCellValue(studentData.getEduSystem()); dataRow.createCell(5).setCellValue(String.format("%02d", studentData.getPayAmount())); } // 将工作簿写入文件 ExcelUtils.excelDownload(workbook, "缴费设置.xlsx", response); } }