WelcomePaySettingController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package com.template.controller;
  2. import com.template.api.WelcomePaySettingControllerAPI;
  3. import com.template.common.utils.CardUtils;
  4. import com.template.common.utils.ExcelUtils;
  5. import com.template.common.utils.paramUtils;
  6. import com.template.model.enumModel.eFileType;
  7. import com.template.model.enumModel.eSchool;
  8. import com.template.model.pojo.*;
  9. import com.template.model.request.insertPaySettingRequest;
  10. import com.template.model.request.updatePaySettingRequest;
  11. import com.template.model.result.CommonResult;
  12. import com.template.model.result.PageUtils;
  13. import com.template.model.vo.StudentPageVo;
  14. import com.template.services.WelcomeOrgService;
  15. import com.template.services.WelcomePaySettingService;
  16. import org.apache.poi.hssf.usermodel.HSSFRow;
  17. import org.apache.poi.hssf.usermodel.HSSFSheet;
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  19. import org.apache.poi.ss.usermodel.DataFormatter;
  20. import org.apache.poi.ss.usermodel.Row;
  21. import org.apache.poi.ss.usermodel.Sheet;
  22. import org.apache.poi.ss.usermodel.Workbook;
  23. import org.apache.poi.xssf.usermodel.XSSFRow;
  24. import org.apache.poi.xssf.usermodel.XSSFSheet;
  25. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.util.StringUtils;
  28. import org.springframework.validation.BindingResult;
  29. import org.springframework.web.bind.annotation.RestController;
  30. import org.springframework.web.multipart.MultipartFile;
  31. import javax.servlet.http.HttpServletResponse;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.math.BigDecimal;
  35. import java.text.ParseException;
  36. import java.util.ArrayList;
  37. import java.util.Arrays;
  38. import java.util.List;
  39. import java.util.Optional;
  40. /**
  41. * <p>
  42. * 前端控制器
  43. * </p>
  44. *
  45. * @author ceshi
  46. * @since 2025-06-13
  47. */
  48. @RestController
  49. public class WelcomePaySettingController implements WelcomePaySettingControllerAPI {
  50. @Autowired
  51. private WelcomeOrgService welcomeOrgService;
  52. @Autowired
  53. private WelcomePaySettingService welcomePaySettingService;
  54. @Override
  55. public CommonResult getPageDatas(int currentPage, int pageCount, Integer collegeId, Integer majorId, String eduLevel) {
  56. PageUtils<WelcomePaySetting> pages = welcomePaySettingService.queryPageWelcomePaySettings(currentPage, pageCount, collegeId, majorId, eduLevel);
  57. return CommonResult.ok(pages);
  58. }
  59. @Override
  60. public CommonResult insertPaySetting(insertPaySettingRequest psr, BindingResult bindingResult) {
  61. if (bindingResult.hasErrors()) {
  62. String st = paramUtils.getParamError(bindingResult);
  63. return CommonResult.fail(st);
  64. }
  65. WelcomePaySetting exist = welcomePaySettingService.queryPaySettingById(psr.getCollegeId(), psr.getMajorId(), psr.getEduLevel());
  66. if (exist != null) {
  67. return CommonResult.fail("当前院系专业已设置!");
  68. }
  69. WelcomePaySetting wps = new WelcomePaySetting();
  70. wps.setCollegeId(psr.getCollegeId());
  71. wps.setCollegeName(psr.getCollegeName());
  72. wps.setMajorId(psr.getMajorId());
  73. wps.setMajorName(psr.getMajorName());
  74. wps.setEduLevel(psr.getEduLevel());
  75. wps.setEduSystem(psr.getEduSystem());
  76. wps.setPayAmount(psr.getPayAmount());
  77. int result = welcomePaySettingService.insertWelcomePaySetting(wps);
  78. return result > 0 ? CommonResult.ok("添加成功!") : CommonResult.fail("添加失败!");
  79. }
  80. @Override
  81. public CommonResult updatePaySetting(updatePaySettingRequest psr, BindingResult bindingResult) {
  82. if (bindingResult.hasErrors()) {
  83. String st = paramUtils.getParamError(bindingResult);
  84. return CommonResult.fail(st);
  85. }
  86. WelcomePaySetting oldData = welcomePaySettingService.getManageById(psr.getId());
  87. if (oldData == null) {
  88. return CommonResult.fail("当前数据已失效,编辑失败!");
  89. }
  90. WelcomePaySetting exist = welcomePaySettingService.queryPaySettingById(psr.getCollegeId(), psr.getMajorId(), psr.getEduLevel());
  91. if (exist != null && psr.getId().intValue() != exist.getId().intValue()) {
  92. return CommonResult.fail("当前院系专业已设置!");
  93. }
  94. oldData.setCollegeId(psr.getCollegeId());
  95. oldData.setCollegeName(psr.getCollegeName());
  96. oldData.setMajorId(psr.getMajorId());
  97. oldData.setMajorName(psr.getMajorName());
  98. oldData.setEduLevel(psr.getEduLevel());
  99. oldData.setEduSystem(psr.getEduSystem());
  100. oldData.setPayAmount(psr.getPayAmount());
  101. int result = welcomePaySettingService.updateWelcomePaySetting(oldData);
  102. return result > 0 ? CommonResult.ok("编辑成功!") : CommonResult.fail("编辑失败!");
  103. }
  104. @Override
  105. public CommonResult deleteDormitory(Integer id) {
  106. int result = welcomePaySettingService.deleteWelcomePaySettingById(id);
  107. return result < 0 ? CommonResult.fail("删除失败") : CommonResult.ok("删除成功");
  108. }
  109. @Override
  110. public CommonResult importPaySettingExcel(MultipartFile file) throws IOException, ParseException {
  111. if (file.isEmpty() || file.getSize() == 0) {
  112. return CommonResult.fail("导入文件不能为空");
  113. }
  114. String ContentType = file.getContentType();
  115. InputStream inputStream = file.getInputStream();
  116. List<WelcomePaySetting> result = new ArrayList<>();
  117. //xls格式文件
  118. if (ContentType.equals(eFileType.Xls.getValue())) {
  119. CommonResult<List<WelcomePaySetting>> resultData = readXls(inputStream);
  120. if (!resultData.isSuccess()) {
  121. return resultData;
  122. }
  123. result = resultData.getData();
  124. } else if (ContentType.equals(eFileType.Xlsx.getValue())) {
  125. CommonResult<List<WelcomePaySetting>> resultData = readXlsx(inputStream);
  126. if (!resultData.isSuccess()) {
  127. return resultData;
  128. }
  129. result = resultData.getData();
  130. } else {
  131. return CommonResult.fail("缴费设置数据导入只支持Xls或Xlsx格式文件");
  132. }
  133. List<WelcomePaySetting> existPays = welcomePaySettingService.queryPaySettings();
  134. if (existPays != null && existPays.size() > 0) {
  135. for (WelcomePaySetting wps : result) {
  136. Optional<WelcomePaySetting> ows = existPays.stream().filter(e -> e.getCollegeId().intValue() == wps.getCollegeId().intValue()
  137. && e.getMajorId().intValue() == wps.getMajorId().intValue() && e.getEduLevel().equals(wps.getEduLevel())).findFirst();
  138. if (ows != null && ows.isPresent()) {
  139. wps.setId(ows.get().getId());
  140. }
  141. }
  142. }
  143. boolean resultBool = welcomePaySettingService.saveOrUpdateBatch(result);
  144. System.out.println("导入缴费设置1");
  145. return resultBool ? CommonResult.ok("导入成功") : CommonResult.fail("导入失败");
  146. }
  147. /**
  148. * xls文件读取方法
  149. *
  150. * @param inputStream
  151. * @return
  152. * @throws IOException
  153. * @throws ParseException
  154. */
  155. private CommonResult<List<WelcomePaySetting>> readXls(InputStream inputStream) throws IOException, ParseException {
  156. List<WelcomePaySetting> result = new ArrayList<>();
  157. HSSFWorkbook sheets = new HSSFWorkbook(inputStream);
  158. List<WelcomeOrg> ws = welcomeOrgService.list(null);
  159. //读取第一张sheet
  160. HSSFSheet sheetAt = sheets.getSheetAt(0);
  161. DataFormatter dataFormatter = new DataFormatter();
  162. try {
  163. //rowNum = 3 从第三行开始获取值
  164. //sheetAt.getLastRowNum():从0开始统计数量 所以得+1
  165. for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
  166. HSSFRow row = sheetAt.getRow(rowNum);
  167. if (row != null) {
  168. //使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
  169. //所以先使用setCellType()方法先将该单元格的类型设置为STRING
  170. //然后poi会根据字符串读取它
  171. //第一行数据获取月份
  172. if (rowNum == 0) {
  173. String number = dataFormatter.formatCellValue(row.getCell(0));//序号
  174. if (!number.equals("序号")) {
  175. return CommonResult.fail("导入数据第一列为序号");
  176. }
  177. String college = dataFormatter.formatCellValue(row.getCell(1));//学院
  178. if (!college.equals("学院")) {
  179. return CommonResult.fail("导入数据第二列为学院");
  180. }
  181. String major = dataFormatter.formatCellValue(row.getCell(2));//专业名称
  182. if (!major.equals("专业名称")) {
  183. return CommonResult.fail("导入数据第三列为专业名称");
  184. }
  185. String eduLevel = dataFormatter.formatCellValue(row.getCell(3));//层次
  186. if (!eduLevel.equals("层次")) {
  187. return CommonResult.fail("导入数据第四列为层次");
  188. }
  189. String eduSystem = dataFormatter.formatCellValue(row.getCell(4));//学制
  190. if (!eduSystem.equals("学制")) {
  191. return CommonResult.fail("导入数据第五列为学制");
  192. }
  193. String payMoney = dataFormatter.formatCellValue(row.getCell(5));//金额
  194. if (!payMoney.equals("金额")) {
  195. return CommonResult.fail("导入数据第六列为金额");
  196. }
  197. } else {
  198. WelcomePaySetting paySetting = new WelcomePaySetting();
  199. String college = dataFormatter.formatCellValue(row.getCell(1));//学院
  200. if (!StringUtils.hasText(college)) {
  201. break;
  202. }
  203. //region 院系专业
  204. Optional<WelcomeOrg> owo = ws.stream().filter(e -> e.getName().equals(college)).findFirst();
  205. if (owo != null && owo.isPresent()) {
  206. paySetting.setCollegeName(college);
  207. paySetting.setCollegeId(owo.get().getId());
  208. }
  209. String major = dataFormatter.formatCellValue(row.getCell(2));//专业
  210. Optional<WelcomeOrg> omwo = ws.stream().filter(e -> owo != null && owo.isPresent() && e.getParentId().intValue() == owo.get().getId().intValue() && e.getName().equals(major)).findFirst();
  211. if (omwo != null && omwo.isPresent()) {
  212. paySetting.setMajorName(major);
  213. paySetting.setMajorId(omwo.get().getId());
  214. }
  215. String eduLevel = dataFormatter.formatCellValue(row.getCell(3));//层次
  216. paySetting.setEduLevel(eduLevel);
  217. String eduSystem = dataFormatter.formatCellValue(row.getCell(4));//学制
  218. paySetting.setEduSystem(eduSystem);
  219. String payMoney = dataFormatter.formatCellValue(row.getCell(5));//金额
  220. paySetting.setPayAmount(new BigDecimal(payMoney));
  221. result.add(paySetting);
  222. }
  223. }
  224. }
  225. } catch (Exception e) {
  226. return CommonResult.fail("请按模板格式导入数据");
  227. }
  228. return CommonResult.ok(result);
  229. }
  230. /**
  231. * xlsx文件读取方法
  232. *
  233. * @param inputStream
  234. * @return
  235. * @throws IOException
  236. * @throws ParseException
  237. */
  238. private CommonResult<List<WelcomePaySetting>> readXlsx(InputStream inputStream) throws IOException, ParseException {
  239. List<WelcomePaySetting> result = new ArrayList<>();
  240. XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
  241. List<WelcomeOrg> ws = welcomeOrgService.list(null);
  242. //读取第一张sheet
  243. XSSFSheet sheetAt = sheets.getSheetAt(0);
  244. DataFormatter dataFormatter = new DataFormatter();
  245. try {
  246. //rowNum = 3 从第三行开始获取值
  247. //sheetAt.getLastRowNum():从0开始统计数量 所以得+1
  248. for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
  249. XSSFRow row = sheetAt.getRow(rowNum);
  250. if (row != null) {
  251. //使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
  252. //所以先使用setCellType()方法先将该单元格的类型设置为STRING
  253. //然后poi会根据字符串读取它
  254. //第一行数据获取月份
  255. if (rowNum == 0) {
  256. String number = dataFormatter.formatCellValue(row.getCell(0));//序号
  257. if (!number.equals("序号")) {
  258. return CommonResult.fail("导入数据第一列为序号");
  259. }
  260. String college = dataFormatter.formatCellValue(row.getCell(1));//学院
  261. if (!college.equals("学院")) {
  262. return CommonResult.fail("导入数据第二列为学院");
  263. }
  264. String major = dataFormatter.formatCellValue(row.getCell(2));//专业名称
  265. if (!major.equals("专业名称")) {
  266. return CommonResult.fail("导入数据第三列为专业名称");
  267. }
  268. String eduLevel = dataFormatter.formatCellValue(row.getCell(3));//层次
  269. if (!eduLevel.equals("层次")) {
  270. return CommonResult.fail("导入数据第四列为层次");
  271. }
  272. String eduSystem = dataFormatter.formatCellValue(row.getCell(4));//学制
  273. if (!eduSystem.equals("学制")) {
  274. return CommonResult.fail("导入数据第五列为学制");
  275. }
  276. String payMoney = dataFormatter.formatCellValue(row.getCell(5));//金额
  277. if (!payMoney.equals("金额")) {
  278. return CommonResult.fail("导入数据第六列为金额");
  279. }
  280. } else {
  281. WelcomePaySetting paySetting = new WelcomePaySetting();
  282. String college = dataFormatter.formatCellValue(row.getCell(1));//学院
  283. if (!StringUtils.hasText(college)) {
  284. break;
  285. }
  286. //region 院系专业
  287. Optional<WelcomeOrg> owo = ws.stream().filter(e -> e.getName().equals(college)).findFirst();
  288. if (owo != null && owo.isPresent()) {
  289. paySetting.setCollegeName(college);
  290. paySetting.setCollegeId(owo.get().getId());
  291. }
  292. String major = dataFormatter.formatCellValue(row.getCell(2));//专业
  293. Optional<WelcomeOrg> omwo = ws.stream().filter(e -> owo != null && owo.isPresent() && e.getParentId().intValue() == owo.get().getId().intValue() && e.getName().equals(major)).findFirst();
  294. if (omwo != null && omwo.isPresent()) {
  295. paySetting.setMajorName(major);
  296. paySetting.setMajorId(omwo.get().getId());
  297. }
  298. String eduLevel = dataFormatter.formatCellValue(row.getCell(3));//层次
  299. paySetting.setEduLevel(eduLevel);
  300. String eduSystem = dataFormatter.formatCellValue(row.getCell(4));//学制
  301. paySetting.setEduSystem(eduSystem);
  302. String payMoney = dataFormatter.formatCellValue(row.getCell(5));//金额
  303. paySetting.setPayAmount(new BigDecimal(payMoney));
  304. result.add(paySetting);
  305. }
  306. }
  307. }
  308. } catch (Exception e) {
  309. return CommonResult.fail("请按模板格式导入数据");
  310. }
  311. return CommonResult.ok(result);
  312. }
  313. @Override
  314. public CommonResult downloadPaySettingExcel() {
  315. return CommonResult.ok("200", "操作成功", "https://chtech.ncjti.edu.cn/welcome/homeimage/缴费设置管理.xlsx");
  316. }
  317. @Override
  318. public void paySettingExport(String userId, HttpServletResponse response, Integer collegeId, Integer majorId, String eduLevel) {
  319. Workbook workbook = new XSSFWorkbook();
  320. Sheet sheet = workbook.createSheet("缴费设置");
  321. List<WelcomePaySetting> result = welcomePaySettingService.queryPaySettingsById(collegeId, majorId, eduLevel);
  322. //导出
  323. Row headerRow = sheet.createRow(0);
  324. headerRow.createCell(0).setCellValue("序号");
  325. headerRow.createCell(1).setCellValue("学院");
  326. headerRow.createCell(2).setCellValue("专业名称");
  327. headerRow.createCell(3).setCellValue("层次");
  328. headerRow.createCell(4).setCellValue("学制");
  329. headerRow.createCell(5).setCellValue("金额");
  330. for (int i = 0; i < result.size(); i++) {
  331. WelcomePaySetting studentData = result.get(i);
  332. Row dataRow = sheet.createRow(i + 1);
  333. dataRow.createCell(0).setCellValue(i + 1);
  334. dataRow.createCell(1).setCellValue(studentData.getCollegeName());
  335. dataRow.createCell(2).setCellValue(studentData.getMajorName());
  336. dataRow.createCell(3).setCellValue(studentData.getEduLevel());
  337. dataRow.createCell(4).setCellValue(studentData.getEduSystem());
  338. dataRow.createCell(5).setCellValue(String.format("%02d", studentData.getPayAmount()));
  339. }
  340. // 将工作簿写入文件
  341. ExcelUtils.excelDownload(workbook, "缴费设置.xlsx", response);
  342. }
  343. }