WelcomeBuildController.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. package com.template.controller;
  2. import com.baomidou.mybatisplus.core.conditions.Wrapper;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  5. import com.template.api.WelcomeBuildControllerAPI;
  6. import com.template.common.utils.ExcelUtils;
  7. import com.template.model.enumModel.eFileType;
  8. import com.template.model.pojo.*;
  9. import com.template.model.result.CommonResult;
  10. import com.template.model.result.PageUtils;
  11. import com.template.model.vo.StudentPageVo;
  12. import com.template.model.vo.WelcomeBuildVo;
  13. import com.template.services.WelcomeBedService;
  14. import com.template.services.WelcomeBuildService;
  15. import com.template.services.WelcomeDormitoryService;
  16. import com.template.services.WelcomeSchoolService;
  17. import org.apache.poi.hssf.usermodel.HSSFRow;
  18. import org.apache.poi.hssf.usermodel.HSSFSheet;
  19. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  20. import org.apache.poi.ss.usermodel.*;
  21. import org.apache.poi.xssf.usermodel.XSSFRow;
  22. import org.apache.poi.xssf.usermodel.XSSFSheet;
  23. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.web.bind.annotation.RequestMapping;
  26. import org.springframework.web.bind.annotation.RestController;
  27. import org.springframework.web.multipart.MultipartFile;
  28. import javax.servlet.http.HttpServletResponse;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.text.ParseException;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import java.util.Optional;
  35. import java.util.stream.Collectors;
  36. /**
  37. * <p>
  38. * 前端控制器
  39. * </p>
  40. *
  41. * @author ceshi
  42. * @since 2025-06-13
  43. */
  44. @RestController
  45. public class WelcomeBuildController implements WelcomeBuildControllerAPI {
  46. @Autowired
  47. private WelcomeBuildService welcomeBuildService;
  48. @Autowired
  49. WelcomeSchoolService welcomeSchoolService;
  50. @Autowired
  51. private WelcomeDormitoryService welcomeDormitoryService;
  52. @Autowired
  53. private WelcomeBedService welcomeBedService;
  54. @Override
  55. public CommonResult saveBuild(WelcomeBuild welcomeBuild) {
  56. String build = welcomeBuild.getBuild();
  57. LambdaQueryWrapper<WelcomeBuild> wrapper = new LambdaQueryWrapper<>();
  58. wrapper.eq(WelcomeBuild::getBuild, build)
  59. .eq(WelcomeBuild::getSchool, welcomeBuild.getSchool());
  60. WelcomeBuild wb = welcomeBuildService.getOne(wrapper);
  61. if (ObjectUtils.isNotEmpty(wb)) {
  62. return CommonResult.fail("已存在该楼栋");
  63. }
  64. welcomeBuildService.save(welcomeBuild);
  65. return CommonResult.ok();
  66. }
  67. @Override
  68. public CommonResult deleteBuild(Integer buildId) {
  69. // 需同步删除寝室和床位
  70. WelcomeBuild byId = welcomeBuildService.getById(buildId);
  71. String schoolId = byId.getSchoolId();
  72. LambdaQueryWrapper<WelcomeBed> wrapperBed = new LambdaQueryWrapper<>();
  73. wrapperBed.eq(WelcomeBed::getSchoolId, schoolId)
  74. .eq(WelcomeBed::getBuildId, buildId);
  75. welcomeBedService.remove(wrapperBed);
  76. LambdaQueryWrapper<WelcomeDormitory> wrapperDormitory=new LambdaQueryWrapper<>();
  77. wrapperDormitory.eq(WelcomeDormitory::getSchoolId,schoolId)
  78. .eq(WelcomeDormitory::getBuildId,buildId);
  79. welcomeDormitoryService.remove(wrapperDormitory);
  80. boolean result = welcomeBuildService.removeById(buildId);
  81. return result ? CommonResult.ok() : CommonResult.fail();
  82. }
  83. @Override
  84. public CommonResult updateBuild(WelcomeBuild welcomeBuild) {
  85. //先查询是否有当前楼栋信息
  86. LambdaQueryWrapper<WelcomeBuild> wrapper = new LambdaQueryWrapper<>();
  87. wrapper.eq(WelcomeBuild::getBuild, welcomeBuild.getBuild())
  88. .eq(WelcomeBuild::getSchool, welcomeBuild.getSchool());
  89. WelcomeBuild one = welcomeBuildService.getOne(wrapper);
  90. if (ObjectUtils.isNotEmpty(one)) {
  91. Integer id = one.getId();
  92. if (!id.equals(welcomeBuild.getId())) {
  93. return CommonResult.fail("修改的楼栋已存在");
  94. }
  95. }
  96. // 判断楼栋名称是否修改, 是的话需要修改宿舍喝床位表
  97. WelcomeBuild byId = welcomeBuildService.getById(welcomeBuild.getId());
  98. if (ObjectUtils.isNotEmpty(byId)) {
  99. if (!byId.getBuild().equals(welcomeBuild.getBuild())) {
  100. welcomeDormitoryService.updateBuild(welcomeBuild.getSchoolId(), byId.getId(), welcomeBuild.getBuild());
  101. welcomeBedService.updateBuild(welcomeBuild.getSchoolId(), byId.getId(), welcomeBuild.getBuild());
  102. }
  103. }
  104. welcomeBuildService.updateById(welcomeBuild);
  105. return CommonResult.ok();
  106. }
  107. @Override
  108. public CommonResult listBuild(int currentPage, int pageCount, Integer schoolId, String buildId, String sex) {
  109. PageUtils<WelcomeBuildVo> pageUtils = welcomeBuildService.listBuild(currentPage, pageCount, schoolId, buildId, sex);
  110. return CommonResult.ok(pageUtils);
  111. }
  112. @Override
  113. public CommonResult schoolGroup() {
  114. List<WelcomeSchool> schoolGroup = welcomeSchoolService.schoolGroup();
  115. return CommonResult.ok(schoolGroup);
  116. }
  117. @Override
  118. public CommonResult buildGroup(Integer schoolId) {
  119. List<WelcomeBuild> schoolGroup = welcomeBuildService.buildGroup(schoolId);
  120. return CommonResult.ok(schoolGroup);
  121. }
  122. @Override
  123. public CommonResult importBuildExcel(MultipartFile file) throws IOException, ParseException {
  124. System.out.println("导入楼栋信息");
  125. if (file.isEmpty() || file.getSize() == 0) {
  126. return CommonResult.fail("导入文件不能为空");
  127. }
  128. String ContentType = file.getContentType();
  129. InputStream inputStream = file.getInputStream();
  130. List<WelcomeBuild> result = new ArrayList<>();
  131. //xls格式文件
  132. if (ContentType.equals(eFileType.Xls.getValue())) {
  133. CommonResult<List<WelcomeBuild>> resultData = readXls(inputStream);
  134. if (!resultData.isSuccess()) {
  135. return resultData;
  136. }
  137. result = resultData.getData();
  138. } else if (ContentType.equals(eFileType.Xlsx.getValue())) {
  139. CommonResult<List<WelcomeBuild>> resultData = readXlsx(inputStream);
  140. if (!resultData.isSuccess()) {
  141. return resultData;
  142. }
  143. result = resultData.getData();
  144. } else {
  145. return CommonResult.fail("楼栋信息数据导入只支持Xls或Xlsx格式文件");
  146. }
  147. // 去掉导入表的重复数据
  148. ArrayList<WelcomeBuild> welcomeBuilds = new ArrayList<>();
  149. for (int i = 0; i < result.size(); i++) {
  150. WelcomeBuild welcomeBuild = result.get(i);
  151. if (!welcomeBuilds.contains(welcomeBuild)) {
  152. welcomeBuilds.add(welcomeBuild);
  153. }
  154. }
  155. boolean resultBool = welcomeBuildService.saveOrUpdateBatch(welcomeBuilds);
  156. System.out.println("导入楼栋1");
  157. return resultBool ? CommonResult.ok("导入成功") : CommonResult.fail("导入失败");
  158. }
  159. /**
  160. * xls文件读取方法
  161. *
  162. * @param inputStream
  163. * @return
  164. * @throws IOException
  165. * @throws ParseException
  166. */
  167. private CommonResult<List<WelcomeBuild>> readXls(InputStream inputStream) throws IOException, ParseException {
  168. List<WelcomeBuild> result = new ArrayList<>();
  169. HSSFWorkbook sheets = new HSSFWorkbook(inputStream);
  170. List<WelcomeSchool> ws = welcomeSchoolService.list(null);
  171. List<WelcomeBuild> wb = welcomeBuildService.list(null);
  172. //读取第一张sheet
  173. HSSFSheet sheetAt = sheets.getSheetAt(0);
  174. DataFormatter dataFormatter = new DataFormatter();
  175. try {
  176. //rowNum = 3 从第三行开始获取值
  177. //sheetAt.getLastRowNum():从0开始统计数量 所以得+1
  178. for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
  179. HSSFRow row = sheetAt.getRow(rowNum);
  180. if (row != null) {
  181. //使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
  182. //所以先使用setCellType()方法先将该单元格的类型设置为STRING
  183. //然后poi会根据字符串读取它
  184. //第一行数据获取月份
  185. if (rowNum == 0) {
  186. String number = dataFormatter.formatCellValue(row.getCell(0));//序号
  187. if (!number.equals("序号")) {
  188. return CommonResult.fail("导入数据第一列为序号");
  189. }
  190. String school = dataFormatter.formatCellValue(row.getCell(1));//校区名称
  191. if (!school.equals("校区名称")) {
  192. return CommonResult.fail("导入数据第二列为校区名称");
  193. }
  194. String build = dataFormatter.formatCellValue(row.getCell(2));//楼栋名称
  195. if (!build.equals("楼栋名称")) {
  196. return CommonResult.fail("导入数据第三列为楼栋名称");
  197. }
  198. String sex = dataFormatter.formatCellValue(row.getCell(3));//楼栋性别
  199. if (!sex.equals("楼栋性别")) {
  200. return CommonResult.fail("导入数据第四列为楼栋性别");
  201. }
  202. String floors = dataFormatter.formatCellValue(row.getCell(4));//楼栋层数
  203. if (!floors.equals("楼栋层数")) {
  204. return CommonResult.fail("导入数据第五列为楼栋层数");
  205. }
  206. String startFloor = dataFormatter.formatCellValue(row.getCell(5));//起始层数
  207. if (!startFloor.equals("起始层数")) {
  208. return CommonResult.fail("导入数据第六列为起始层数");
  209. }
  210. String remark = dataFormatter.formatCellValue(row.getCell(6));//备注
  211. if (!remark.equals("备注")) {
  212. return CommonResult.fail("导入数据第七列为备注");
  213. }
  214. } else {
  215. WelcomeBuild buildData = new WelcomeBuild();
  216. String school = dataFormatter.formatCellValue(row.getCell(1));//校区名称
  217. if (ObjectUtils.isEmpty(school)) {
  218. continue;
  219. }
  220. Optional<WelcomeSchool> wos = ws.stream().filter(e -> e.getSchool().equals(school)).findFirst();
  221. if (wos != null && wos.isPresent()) {
  222. buildData.setSchoolId(wos.get().getId() + "");
  223. } else {
  224. int h = rowNum + 1;
  225. return CommonResult.fail("第" + h + "行,不存在该校区");
  226. }
  227. String build = dataFormatter.formatCellValue(row.getCell(2));//楼栋名称
  228. Optional<WelcomeBuild> ob = wb.stream().filter(e -> e.getSchool().equals(school) && e.getBuild().equals(build)).findFirst();
  229. if (ob != null && ob.isPresent()) {
  230. buildData.setId(ob.get().getId());
  231. }
  232. buildData.setSchool(school);
  233. buildData.setBuild(build);
  234. String sex = dataFormatter.formatCellValue(row.getCell(3));//楼栋性别
  235. buildData.setSex(sex);
  236. String floors = dataFormatter.formatCellValue(row.getCell(4));//楼栋层数
  237. buildData.setFloors(floors);
  238. String startFloor = dataFormatter.formatCellValue(row.getCell(5));//起始层数
  239. buildData.setStartFloor(Integer.valueOf(startFloor));
  240. String remark = dataFormatter.formatCellValue(row.getCell(6));//备注
  241. buildData.setRemark(remark);
  242. result.add(buildData);
  243. }
  244. }
  245. }
  246. } catch (Exception e) {
  247. return CommonResult.fail("请按模板格式导入数据");
  248. }
  249. return CommonResult.ok(result);
  250. }
  251. /**
  252. * xls文件读取方法
  253. *
  254. * @param inputStream
  255. * @return
  256. * @throws IOException
  257. * @throws ParseException
  258. */
  259. private CommonResult<List<WelcomeBuild>> readXlsx(InputStream inputStream) throws IOException, ParseException {
  260. List<WelcomeBuild> result = new ArrayList<>();
  261. XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
  262. List<WelcomeSchool> ws = welcomeSchoolService.list(null);
  263. List<WelcomeBuild> wb = welcomeBuildService.list(null);
  264. //读取第一张sheet
  265. XSSFSheet sheetAt = sheets.getSheetAt(0);
  266. DataFormatter dataFormatter = new DataFormatter();
  267. try {
  268. //rowNum = 3 从第三行开始获取值
  269. //sheetAt.getLastRowNum():从0开始统计数量 所以得+1
  270. for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
  271. XSSFRow row = sheetAt.getRow(rowNum);
  272. if (row != null) {
  273. //使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
  274. //所以先使用setCellType()方法先将该单元格的类型设置为STRING
  275. //然后poi会根据字符串读取它
  276. //第一行数据获取月份
  277. if (rowNum == 0) {
  278. String number = dataFormatter.formatCellValue(row.getCell(0));//序号
  279. if (!number.equals("序号")) {
  280. return CommonResult.fail("导入数据第一列为序号");
  281. }
  282. String school = dataFormatter.formatCellValue(row.getCell(1));//校区名称
  283. if (!school.equals("校区名称")) {
  284. return CommonResult.fail("导入数据第二列为校区名称");
  285. }
  286. String build = dataFormatter.formatCellValue(row.getCell(2));//楼栋名称
  287. if (!build.equals("楼栋名称")) {
  288. return CommonResult.fail("导入数据第三列为楼栋名称");
  289. }
  290. String sex = dataFormatter.formatCellValue(row.getCell(3));//楼栋性别
  291. if (!sex.equals("楼栋性别")) {
  292. return CommonResult.fail("导入数据第四列为楼栋性别");
  293. }
  294. String floors = dataFormatter.formatCellValue(row.getCell(4));//楼栋层数
  295. if (!floors.equals("楼栋层数")) {
  296. return CommonResult.fail("导入数据第五列为楼栋层数");
  297. }
  298. String startFloor = dataFormatter.formatCellValue(row.getCell(5));//起始层数
  299. if (!startFloor.equals("起始层数")) {
  300. return CommonResult.fail("导入数据第六列为起始层数");
  301. }
  302. String remark = dataFormatter.formatCellValue(row.getCell(6));//备注
  303. if (!remark.equals("备注")) {
  304. return CommonResult.fail("导入数据第七列为备注");
  305. }
  306. } else {
  307. WelcomeBuild buildData = new WelcomeBuild();
  308. String school = dataFormatter.formatCellValue(row.getCell(1));//校区名称
  309. if (ObjectUtils.isEmpty(school)) {
  310. continue;
  311. }
  312. Optional<WelcomeSchool> wos = ws.stream().filter(e -> e.getSchool().equals(school)).findFirst();
  313. if (wos != null && wos.isPresent()) {
  314. buildData.setSchoolId(wos.get().getId() + "");
  315. } else {
  316. int h = rowNum + 1;
  317. return CommonResult.fail("第" + h + "行,不存在该校区");
  318. }
  319. String build = dataFormatter.formatCellValue(row.getCell(2));//楼栋名称
  320. Optional<WelcomeBuild> ob = wb.stream().filter(e -> e.getSchool().equals(school) && e.getBuild().equals(build)).findFirst();
  321. if (ob != null && ob.isPresent()) {
  322. buildData.setId(ob.get().getId());
  323. }
  324. buildData.setSchool(school);
  325. buildData.setBuild(build);
  326. String sex = dataFormatter.formatCellValue(row.getCell(3));//楼栋性别
  327. buildData.setSex(sex);
  328. String floors = dataFormatter.formatCellValue(row.getCell(4));//楼栋层数
  329. buildData.setFloors(floors);
  330. String startFloor = dataFormatter.formatCellValue(row.getCell(5));//起始层数
  331. buildData.setStartFloor(Integer.valueOf(startFloor));
  332. String remark = dataFormatter.formatCellValue(row.getCell(6));//备注
  333. buildData.setRemark(remark);
  334. result.add(buildData);
  335. }
  336. }
  337. }
  338. } catch (Exception e) {
  339. return CommonResult.fail("请按模板格式导入数据");
  340. }
  341. return CommonResult.ok(result);
  342. }
  343. @Override
  344. public CommonResult downloadBuildExcel() {
  345. return CommonResult.ok("200", "操作成功", "https://chtech.ncjti.edu.cn/welcome/homeimage/楼栋信息管理.xlsx");
  346. }
  347. @Override
  348. public void welcomeBuildExport(HttpServletResponse response, String schoolName, String buildName, String sex) {
  349. List<WelcomeBuild> result = welcomeBuildService.listBuild(schoolName, buildName, sex);
  350. //导出
  351. Workbook workbook = new XSSFWorkbook();
  352. Sheet sheet = workbook.createSheet("楼栋信息");
  353. // 创建样式对象
  354. CellStyle style = workbook.createCellStyle();
  355. // 设置边框样式
  356. style.setBorderBottom(BorderStyle.THIN);
  357. style.setBorderTop(BorderStyle.THIN);
  358. style.setBorderRight(BorderStyle.THIN);
  359. style.setBorderLeft(BorderStyle.THIN);
  360. style.setAlignment(HorizontalAlignment.CENTER);
  361. style.setVerticalAlignment(VerticalAlignment.CENTER);
  362. Row headerRow = sheet.createRow(0);
  363. Cell no = headerRow.createCell(0);
  364. no.setCellStyle(style);
  365. no.setCellValue("序号");
  366. Cell school = headerRow.createCell(1);
  367. school.setCellStyle(style);
  368. school.setCellValue("校区名称");
  369. Cell buildStr = headerRow.createCell(2);
  370. buildStr.setCellStyle(style);
  371. buildStr.setCellValue("楼栋名称");
  372. Cell sexStr = headerRow.createCell(3);
  373. sexStr.setCellStyle(style);
  374. sexStr.setCellValue("楼栋性别");
  375. Cell floors = headerRow.createCell(4);
  376. floors.setCellStyle(style);
  377. floors.setCellValue("楼栋层数");
  378. Cell startFloor = headerRow.createCell(5);
  379. startFloor.setCellStyle(style);
  380. startFloor.setCellValue("起始层数");
  381. Cell remark = headerRow.createCell(6);
  382. remark.setCellStyle(style);
  383. remark.setCellValue("备注");
  384. for (int i = 0; i < result.size(); i++) {
  385. WelcomeBuild build = result.get(i);
  386. Row dataRow = sheet.createRow(i + 1);
  387. Cell nod = dataRow.createCell(0);
  388. nod.setCellStyle(style);
  389. nod.setCellValue(i + 1);
  390. Cell schoold = dataRow.createCell(1);
  391. schoold.setCellStyle(style);
  392. schoold.setCellValue(build.getSchool());
  393. Cell buildStrd = dataRow.createCell(2);
  394. buildStrd.setCellStyle(style);
  395. buildStrd.setCellValue(build.getBuild());
  396. Cell sexd = dataRow.createCell(3);
  397. sexd.setCellStyle(style);
  398. sexd.setCellValue(build.getSex());
  399. Cell floord = dataRow.createCell(4);
  400. floord.setCellStyle(style);
  401. floord.setCellValue(build.getFloors());
  402. Cell startFloord = dataRow.createCell(5);
  403. startFloord.setCellStyle(style);
  404. startFloord.setCellValue(build.getStartFloor());
  405. Cell remarkd = dataRow.createCell(6);
  406. remarkd.setCellStyle(style);
  407. remarkd.setCellValue(build.getRemark());
  408. }
  409. // 将工作簿写入文件
  410. ExcelUtils.excelDownload(workbook, "楼栋信息.xlsx", response);
  411. }
  412. }