WelcomeBedController.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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.WelcomeBedControllerAPI;
  6. import com.template.common.utils.ExcelUtils;
  7. import com.template.common.utils.paramUtils;
  8. import com.template.model.enumModel.eFileType;
  9. import com.template.model.pojo.*;
  10. import com.template.model.request.InsertWelcomeBedRequest;
  11. import com.template.model.request.UpdateWelcomeBedRequest;
  12. import com.template.model.result.CommonResult;
  13. import com.template.model.result.PageUtils;
  14. import com.template.model.vo.BedDetailsVo;
  15. import com.template.model.vo.ListVo;
  16. import com.template.model.vo.StudentPageVo;
  17. import com.template.services.*;
  18. import org.apache.poi.hssf.usermodel.HSSFRow;
  19. import org.apache.poi.hssf.usermodel.HSSFSheet;
  20. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  21. import org.apache.poi.ss.usermodel.DataFormatter;
  22. import org.apache.poi.ss.usermodel.Row;
  23. import org.apache.poi.ss.usermodel.Sheet;
  24. import org.apache.poi.ss.usermodel.Workbook;
  25. import org.apache.poi.xssf.usermodel.XSSFRow;
  26. import org.apache.poi.xssf.usermodel.XSSFSheet;
  27. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.util.StringUtils;
  30. import org.springframework.validation.BindingResult;
  31. import org.springframework.web.bind.annotation.RequestMapping;
  32. import org.springframework.web.bind.annotation.RestController;
  33. import org.springframework.web.multipart.MultipartFile;
  34. import javax.servlet.http.HttpServletResponse;
  35. import java.io.IOException;
  36. import java.io.InputStream;
  37. import java.text.ParseException;
  38. import java.util.ArrayList;
  39. import java.util.List;
  40. import java.util.Optional;
  41. import java.util.stream.Collectors;
  42. /**
  43. * <p>
  44. * 前端控制器
  45. * </p>
  46. *
  47. * @author ceshi
  48. * @since 2025-06-13
  49. */
  50. @RestController
  51. public class WelcomeBedController implements WelcomeBedControllerAPI {
  52. @Autowired
  53. private WelcomeOrgService welcomeOrgService;
  54. @Autowired
  55. private WelcomeBedService welcomeBedService;
  56. @Autowired
  57. private WelcomeBuildService welcomeBuildService;
  58. @Autowired
  59. private WelcomeStudentService welcomeStudentService;
  60. @Autowired
  61. private WelcomeDormitoryService welcomeDormitoryService;
  62. @Override
  63. public CommonResult insertBedInfo(InsertWelcomeBedRequest iwbr, BindingResult bindingResult) {
  64. if (bindingResult.hasErrors()) {
  65. String st = paramUtils.getParamError(bindingResult);
  66. return CommonResult.fail(st);
  67. }
  68. WelcomeBed existData = welcomeBedService.queryBedData(iwbr.getSchool(),iwbr.getBuildId(),iwbr.getDormitoryId(),iwbr.getNumber());
  69. if(existData != null){
  70. return CommonResult.fail("该床位号已存在,请勿重复插入!");
  71. }
  72. WelcomeBed wb = new WelcomeBed();
  73. if(StringUtils.hasText(iwbr.getCardNum())){
  74. WelcomeStudent student =welcomeStudentService.queryStudentInfo(iwbr.getCollegeId(),iwbr.getMajorId(),iwbr.getClassstrId(),iwbr.getCardNum());
  75. if(student == null){
  76. return CommonResult.fail("当前学生信息在系统中不存在!");
  77. }
  78. wb.setStudentCard(student.getCardId());
  79. wb.setCollegeId(iwbr.getCollegeId());
  80. wb.setCollege(iwbr.getCollege());
  81. wb.setMajorId(iwbr.getMajorId());
  82. wb.setMajor(iwbr.getMajor());
  83. wb.setClassstrId(iwbr.getClassstrId());
  84. wb.setClassstr(iwbr.getClassstr());
  85. wb.setCardNum(iwbr.getCardNum());
  86. wb.setName(iwbr.getName());
  87. }
  88. wb.setNumber(iwbr.getNumber());
  89. wb.setSchool(iwbr.getSchool());
  90. wb.setSchoolId(iwbr.getSchoolId());
  91. wb.setBuildId(iwbr.getBuildId());
  92. wb.setBuild(iwbr.getBuild());
  93. wb.setDormitoryId(iwbr.getDormitoryId());
  94. wb.setDormitory(iwbr.getDormitory());
  95. wb.setSex(iwbr.getSex());
  96. wb.setIsCheck(StringUtils.hasText(iwbr.getCardNum()) ? 1 : 0);
  97. wb.setRemark(iwbr.getRemark());
  98. int result = welcomeBedService.insertWelcomeBed(wb);
  99. return result > 0 ? CommonResult.ok("添加成功!") : CommonResult.fail("添加失败");
  100. }
  101. @Override
  102. public CommonResult updateBedInfo(UpdateWelcomeBedRequest uwbr, BindingResult bindingResult) throws Exception {
  103. if (bindingResult.hasErrors()) {
  104. String st = paramUtils.getParamError(bindingResult);
  105. return CommonResult.fail(st);
  106. }
  107. WelcomeBed oldData = welcomeBedService.getBedById(uwbr.getId());
  108. if(oldData == null){
  109. return CommonResult.fail("床位信息已失效,编辑失败");
  110. }
  111. WelcomeBed existData = welcomeBedService.queryBedData(uwbr.getSchool(),uwbr.getBuildId(),uwbr.getDormitoryId(),uwbr.getNumber());
  112. if(existData != null && existData.getId().intValue() != uwbr.getId().intValue()){
  113. return CommonResult.fail("该床位号已存在,请勿重复插入!");
  114. }
  115. if(StringUtils.hasText(uwbr.getCardNum())){
  116. WelcomeStudent student =welcomeStudentService.queryStudentInfo(uwbr.getCollegeId(),uwbr.getMajorId(),uwbr.getClassstrId(),uwbr.getCardNum());
  117. if(student == null){
  118. return CommonResult.fail("当前学生信息在系统中不存在!");
  119. }
  120. oldData.setStudentCard(student.getCardId());
  121. oldData.setCollegeId(uwbr.getCollegeId());
  122. oldData.setCollege(uwbr.getCollege());
  123. oldData.setMajorId(uwbr.getMajorId());
  124. oldData.setMajor(uwbr.getMajor());
  125. oldData.setClassstrId(uwbr.getClassstrId());
  126. oldData.setClassstr(uwbr.getClassstr());
  127. oldData.setCardNum(uwbr.getCardNum());
  128. oldData.setName(uwbr.getName());
  129. }
  130. oldData.setNumber(uwbr.getNumber());
  131. oldData.setSchool(uwbr.getSchool());
  132. oldData.setSchoolId(uwbr.getSchoolId());
  133. oldData.setBuildId(uwbr.getBuildId());
  134. oldData.setBuild(uwbr.getBuild());
  135. oldData.setDormitoryId(uwbr.getDormitoryId());
  136. oldData.setDormitory(uwbr.getDormitory());
  137. oldData.setSex(uwbr.getSex());
  138. oldData.setCollegeId(uwbr.getCollegeId());
  139. oldData.setCollege(uwbr.getCollege());
  140. oldData.setMajorId(uwbr.getMajorId());
  141. oldData.setMajor(uwbr.getMajor());
  142. oldData.setClassstrId(uwbr.getClassstrId());
  143. oldData.setClassstr(uwbr.getClassstr());
  144. oldData.setIsCheck(uwbr.getIsCheck());
  145. oldData.setCardNum(uwbr.getCardNum());
  146. oldData.setName(uwbr.getName());
  147. oldData.setRemark(uwbr.getRemark());
  148. int result = welcomeBedService.updateWelcomeBed(oldData);
  149. return result > 0 ? CommonResult.ok("编辑成功!") : CommonResult.fail("编辑失败");
  150. }
  151. @Override
  152. public CommonResult queryPageBeds(int currentPage, int pageCount, Integer schoolId, Integer buildId, Integer dormitoryId, String sex, Integer isCheck, Integer collegeId, Integer majorId, Integer classstrId) {
  153. PageUtils<WelcomeBed> result = welcomeBedService.queryPageWelcomeBeds(currentPage, pageCount, schoolId, buildId, dormitoryId, sex, isCheck, collegeId, majorId, classstrId);
  154. return CommonResult.ok(result);
  155. }
  156. @Override
  157. public CommonResult deleteBedInfo(int id) throws Exception {
  158. int result = welcomeBedService.deleteWelcomeBedById(id);
  159. return result > 0 ? CommonResult.ok() : CommonResult.fail();
  160. }
  161. @Override
  162. public CommonResult importBedExcel(MultipartFile file) throws IOException, ParseException {
  163. System.out.println("导入床位信息");
  164. if (file.isEmpty() || file.getSize() == 0) {
  165. return CommonResult.fail("导入文件不能为空");
  166. }
  167. String ContentType = file.getContentType();
  168. InputStream inputStream = file.getInputStream();
  169. List<WelcomeBed> result = new ArrayList<>();
  170. //xls格式文件
  171. if (ContentType.equals(eFileType.Xls.getValue())) {
  172. CommonResult<List<WelcomeBed>> resultData = readXls(inputStream);
  173. if (!resultData.isSuccess()) {
  174. return resultData;
  175. }
  176. result = resultData.getData();
  177. } else if (ContentType.equals(eFileType.Xlsx.getValue())) {
  178. CommonResult<List<WelcomeBed>> resultData = readXlsx(inputStream);
  179. if (!resultData.isSuccess()) {
  180. return resultData;
  181. }
  182. result = resultData.getData();
  183. } else {
  184. return CommonResult.fail("床位信息数据导入只支持Xls或Xlsx格式文件");
  185. }
  186. List<String> admissNums = result.stream().map(WelcomeBed::getCardNum).distinct().collect(Collectors.toList());
  187. if(admissNums != null && admissNums.size() > 0){
  188. List<WelcomeStudent> students = welcomeStudentService.getDataByAdmissNum(admissNums);
  189. if(students != null && students.size() > 0){
  190. for (WelcomeBed r:result) {
  191. Optional<WelcomeStudent> os = students.stream().filter(e -> e.getAdmissNum().equals(r.getCardNum())).findFirst();
  192. if(os != null && os.isPresent()){
  193. r.setStudentCard(os.get().getCardId());
  194. }
  195. }
  196. }
  197. }
  198. boolean resultBool = welcomeBedService.saveOrUpdateBatch(result);
  199. System.out.println("导入楼栋1");
  200. return resultBool ? CommonResult.ok("导入成功") : CommonResult.fail("导入失败");
  201. }
  202. /**
  203. * xls文件读取方法
  204. *
  205. * @param inputStream
  206. * @return
  207. * @throws IOException
  208. * @throws ParseException
  209. */
  210. private CommonResult<List<WelcomeBed>> readXls(InputStream inputStream) throws IOException, ParseException {
  211. List<WelcomeBed> result = new ArrayList<>();
  212. HSSFWorkbook sheets = new HSSFWorkbook(inputStream);
  213. List<WelcomeBed> wb = welcomeBedService.list(null);
  214. List<WelcomeOrg> wos = welcomeOrgService.list(null);
  215. List<WelcomeBuild> wbils = welcomeBuildService.list(null);
  216. List<WelcomeDormitory> wds = welcomeDormitoryService.list(null);
  217. //读取第一张sheet
  218. HSSFSheet sheetAt = sheets.getSheetAt(0);
  219. DataFormatter dataFormatter = new DataFormatter();
  220. try {
  221. //rowNum = 3 从第三行开始获取值
  222. //sheetAt.getLastRowNum():从0开始统计数量 所以得+1
  223. for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
  224. HSSFRow row = sheetAt.getRow(rowNum);
  225. if (row != null) {
  226. //使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
  227. //所以先使用setCellType()方法先将该单元格的类型设置为STRING
  228. //然后poi会根据字符串读取它
  229. if (rowNum == 0) {
  230. String num = dataFormatter.formatCellValue(row.getCell(0));//序号
  231. if (!num.equals("序号")) {
  232. return CommonResult.fail("导入数据第一列为序号");
  233. }
  234. String school = dataFormatter.formatCellValue(row.getCell(1));//序号
  235. if (!school.equals("校区名称")) {
  236. return CommonResult.fail("导入数据第一列为校区名称");
  237. }
  238. String build = dataFormatter.formatCellValue(row.getCell(2));//校区名称
  239. if (!build.equals("楼栋名称")) {
  240. return CommonResult.fail("导入数据第二列为楼栋名称");
  241. }
  242. String dormitory = dataFormatter.formatCellValue(row.getCell(3));//楼栋名称
  243. if (!dormitory.equals("寝室号")) {
  244. return CommonResult.fail("导入数据第三列为寝室号");
  245. }
  246. String number = dataFormatter.formatCellValue(row.getCell(4));//楼栋性别
  247. if (!number.equals("床位号")) {
  248. return CommonResult.fail("导入数据第四列为床位号");
  249. }
  250. String sex = dataFormatter.formatCellValue(row.getCell(5));//楼栋层数
  251. if (!sex.equals("床位性别")) {
  252. return CommonResult.fail("导入数据第五列为床位性别");
  253. }
  254. String college = dataFormatter.formatCellValue(row.getCell(6));//起始层数
  255. if (!college.equals("所属学院")) {
  256. return CommonResult.fail("导入数据第六列为所属学院");
  257. }
  258. String major = dataFormatter.formatCellValue(row.getCell(7));//备注
  259. if (!major.equals("所属专业")) {
  260. return CommonResult.fail("导入数据第七列为所属专业");
  261. }
  262. String classstr = dataFormatter.formatCellValue(row.getCell(8));//备注
  263. if (!classstr.equals("所属班级")) {
  264. return CommonResult.fail("导入数据第七列为所属班级");
  265. }
  266. String isCheck = dataFormatter.formatCellValue(row.getCell(9));//备注
  267. if (!isCheck.equals("是否入住")) {
  268. return CommonResult.fail("导入数据第七列为是否入住");
  269. }
  270. String admissNum = dataFormatter.formatCellValue(row.getCell(10));//备注
  271. if (!admissNum.equals("录取号")) {
  272. return CommonResult.fail("导入数据第七列为录取号");
  273. }
  274. String name = dataFormatter.formatCellValue(row.getCell(11));//备注
  275. if (!name.equals("姓名")) {
  276. return CommonResult.fail("导入数据第七列为姓名");
  277. }
  278. String remark = dataFormatter.formatCellValue(row.getCell(12));//备注
  279. if (!remark.equals("备注")) {
  280. return CommonResult.fail("导入数据第七列为备注");
  281. }
  282. } else {
  283. WelcomeBed bedData = new WelcomeBed();
  284. String school = dataFormatter.formatCellValue(row.getCell(1));//校区
  285. String build = dataFormatter.formatCellValue(row.getCell(2));//楼栋
  286. String dormitory = dataFormatter.formatCellValue(row.getCell(3));//寝室号
  287. String number = dataFormatter.formatCellValue(row.getCell(4));//床位号
  288. Optional<WelcomeBed> ob = wb.stream().filter(e -> e.getSchool().equals(school) && e.getBuild().equals(build)
  289. && e.getDormitory().equals(dormitory) && e.getNumber().intValue() == Integer.valueOf(number).intValue()).findFirst();
  290. if(ob != null && ob.isPresent()){
  291. bedData.setId(ob.get().getId());
  292. }
  293. String sex = dataFormatter.formatCellValue(row.getCell(5));//楼栋层数
  294. String college = dataFormatter.formatCellValue(row.getCell(6));//起始层数
  295. String major = dataFormatter.formatCellValue(row.getCell(7));//备注
  296. String classstr = dataFormatter.formatCellValue(row.getCell(8));//备注
  297. String isCheck = dataFormatter.formatCellValue(row.getCell(9));//备注
  298. String admissNum = dataFormatter.formatCellValue(row.getCell(10));//备注
  299. String name = dataFormatter.formatCellValue(row.getCell(11));//备注
  300. String remark = dataFormatter.formatCellValue(row.getCell(12));//备注
  301. bedData.setSchool(school);
  302. //楼栋
  303. if(wbils != null && wbils.size() > 0){
  304. Optional<WelcomeBuild> oBuild = wbils.stream().filter(e -> e.getBuild().equals(build)).findFirst();
  305. if(oBuild != null && oBuild.isPresent()){
  306. bedData.setBuild(build);
  307. bedData.setBuildId(oBuild.get().getId());
  308. }
  309. }
  310. //寝室
  311. if(wds != null && wds.size() > 0){
  312. Optional<WelcomeDormitory> oDormitory = wds.stream().filter(e -> e.getDormitory().equals(dormitory)).findFirst();
  313. if(oDormitory != null && oDormitory.isPresent()){
  314. bedData.setDormitory(dormitory);
  315. bedData.setDormitoryId(oDormitory.get().getId());
  316. }
  317. }
  318. bedData.setNumber(Integer.valueOf(number));
  319. bedData.setSex(sex);
  320. //region 院系专业
  321. Optional<WelcomeOrg> owo = wos.stream().filter(e -> e.getName().equals(college)).findFirst();
  322. if (owo != null && owo.isPresent()) {
  323. bedData.setCollege(college);
  324. bedData.setCollegeId(owo.get().getId());
  325. }
  326. Optional<WelcomeOrg> omwo = wos.stream().filter(e -> e.getName().equals(major)).findFirst();
  327. if (omwo != null && omwo.isPresent()) {
  328. bedData.setMajor(major);
  329. bedData.setMajorId(omwo.get().getId());
  330. }
  331. Optional<WelcomeOrg> ocwo = wos.stream().filter(e -> e.getName().equals(classstr)).findFirst();
  332. if (ocwo != null && ocwo.isPresent()) {
  333. bedData.setClassstr(classstr);
  334. bedData.setClassstrId(ocwo.get().getId());
  335. }
  336. //endregion
  337. bedData.setIsCheck(Integer.valueOf(isCheck));
  338. bedData.setCardNum(admissNum);
  339. bedData.setName(name);
  340. bedData.setRemark(remark);
  341. result.add(bedData);
  342. }
  343. }
  344. }
  345. } catch (Exception e) {
  346. return CommonResult.fail("请按模板格式导入数据");
  347. }
  348. return CommonResult.ok(result);
  349. }
  350. /**
  351. * xls文件读取方法
  352. *
  353. * @param inputStream
  354. * @return
  355. * @throws IOException
  356. * @throws ParseException
  357. */
  358. private CommonResult<List<WelcomeBed>> readXlsx(InputStream inputStream) throws IOException, ParseException {
  359. List<WelcomeBed> result = new ArrayList<>();
  360. XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
  361. List<WelcomeBed> wb = welcomeBedService.list(null);
  362. List<WelcomeOrg> wos = welcomeOrgService.list(null);
  363. List<WelcomeBuild> wbils = welcomeBuildService.list(null);
  364. List<WelcomeDormitory> wds = welcomeDormitoryService.list(null);
  365. //读取第一张sheet
  366. XSSFSheet sheetAt = sheets.getSheetAt(0);
  367. DataFormatter dataFormatter = new DataFormatter();
  368. try {
  369. //rowNum = 3 从第三行开始获取值
  370. //sheetAt.getLastRowNum():从0开始统计数量 所以得+1
  371. for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
  372. XSSFRow row = sheetAt.getRow(rowNum);
  373. if (row != null) {
  374. //使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
  375. //所以先使用setCellType()方法先将该单元格的类型设置为STRING
  376. //然后poi会根据字符串读取它
  377. if (rowNum == 0) {
  378. String num = dataFormatter.formatCellValue(row.getCell(0));//序号
  379. if (!num.equals("序号")) {
  380. return CommonResult.fail("导入数据第一列为序号");
  381. }
  382. String school = dataFormatter.formatCellValue(row.getCell(1));//序号
  383. if (!school.equals("校区名称")) {
  384. return CommonResult.fail("导入数据第一列为校区名称");
  385. }
  386. String build = dataFormatter.formatCellValue(row.getCell(2));//校区名称
  387. if (!build.equals("楼栋名称")) {
  388. return CommonResult.fail("导入数据第二列为楼栋名称");
  389. }
  390. String dormitory = dataFormatter.formatCellValue(row.getCell(3));//楼栋名称
  391. if (!dormitory.equals("寝室号")) {
  392. return CommonResult.fail("导入数据第三列为寝室号");
  393. }
  394. String number = dataFormatter.formatCellValue(row.getCell(4));//楼栋性别
  395. if (!number.equals("床位号")) {
  396. return CommonResult.fail("导入数据第四列为床位号");
  397. }
  398. String sex = dataFormatter.formatCellValue(row.getCell(5));//楼栋层数
  399. if (!sex.equals("床位性别")) {
  400. return CommonResult.fail("导入数据第五列为床位性别");
  401. }
  402. String college = dataFormatter.formatCellValue(row.getCell(6));//起始层数
  403. if (!college.equals("所属学院")) {
  404. return CommonResult.fail("导入数据第六列为所属学院");
  405. }
  406. String major = dataFormatter.formatCellValue(row.getCell(7));//备注
  407. if (!major.equals("所属专业")) {
  408. return CommonResult.fail("导入数据第七列为所属专业");
  409. }
  410. String classstr = dataFormatter.formatCellValue(row.getCell(8));//备注
  411. if (!classstr.equals("所属班级")) {
  412. return CommonResult.fail("导入数据第七列为所属班级");
  413. }
  414. String isCheck = dataFormatter.formatCellValue(row.getCell(9));//备注
  415. if (!isCheck.equals("是否入住")) {
  416. return CommonResult.fail("导入数据第七列为是否入住");
  417. }
  418. String admissNum = dataFormatter.formatCellValue(row.getCell(10));//备注
  419. if (!admissNum.equals("录取号")) {
  420. return CommonResult.fail("导入数据第七列为录取号");
  421. }
  422. String name = dataFormatter.formatCellValue(row.getCell(11));//备注
  423. if (!name.equals("姓名")) {
  424. return CommonResult.fail("导入数据第七列为姓名");
  425. }
  426. String remark = dataFormatter.formatCellValue(row.getCell(12));//备注
  427. if (!remark.equals("备注")) {
  428. return CommonResult.fail("导入数据第七列为备注");
  429. }
  430. } else {
  431. WelcomeBed bedData = new WelcomeBed();
  432. String school = dataFormatter.formatCellValue(row.getCell(1));//校区
  433. String build = dataFormatter.formatCellValue(row.getCell(2));//楼栋
  434. String dormitory = dataFormatter.formatCellValue(row.getCell(3));//寝室号
  435. String number = dataFormatter.formatCellValue(row.getCell(4));//床位号
  436. Optional<WelcomeBed> ob = wb.stream().filter(e -> e.getSchool().equals(school) && e.getBuild().equals(build)
  437. && e.getDormitory().equals(dormitory) && e.getNumber().intValue() == Integer.valueOf(number).intValue()).findFirst();
  438. if(ob != null && ob.isPresent()){
  439. bedData.setId(ob.get().getId());
  440. }
  441. String sex = dataFormatter.formatCellValue(row.getCell(5));//楼栋层数
  442. String college = dataFormatter.formatCellValue(row.getCell(6));//起始层数
  443. String major = dataFormatter.formatCellValue(row.getCell(7));//备注
  444. String classstr = dataFormatter.formatCellValue(row.getCell(8));//备注
  445. String isCheck = dataFormatter.formatCellValue(row.getCell(9));//备注
  446. String admissNum = dataFormatter.formatCellValue(row.getCell(10));//备注
  447. String name = dataFormatter.formatCellValue(row.getCell(11));//备注
  448. String remark = dataFormatter.formatCellValue(row.getCell(12));//备注
  449. bedData.setSchool(school);
  450. //楼栋
  451. if(wbils != null && wbils.size() > 0){
  452. Optional<WelcomeBuild> oBuild = wbils.stream().filter(e -> e.getBuild().equals(build)).findFirst();
  453. if(oBuild != null && oBuild.isPresent()){
  454. bedData.setBuild(build);
  455. bedData.setBuildId(oBuild.get().getId());
  456. }
  457. }
  458. //寝室
  459. if(wds != null && wds.size() > 0){
  460. Optional<WelcomeDormitory> oDormitory = wds.stream().filter(e -> e.getDormitory().equals(dormitory)).findFirst();
  461. if(oDormitory != null && oDormitory.isPresent()){
  462. bedData.setDormitory(dormitory);
  463. bedData.setDormitoryId(oDormitory.get().getId());
  464. }
  465. }
  466. bedData.setNumber(Integer.valueOf(number));
  467. bedData.setSex(sex);
  468. //region 院系专业
  469. Optional<WelcomeOrg> owo = wos.stream().filter(e -> e.getName().equals(college)).findFirst();
  470. if (owo != null && owo.isPresent()) {
  471. bedData.setCollege(college);
  472. bedData.setCollegeId(owo.get().getId());
  473. }
  474. Optional<WelcomeOrg> omwo = wos.stream().filter(e -> e.getName().equals(major)).findFirst();
  475. if (omwo != null && omwo.isPresent()) {
  476. bedData.setMajor(major);
  477. bedData.setMajorId(omwo.get().getId());
  478. }
  479. Optional<WelcomeOrg> ocwo = wos.stream().filter(e -> e.getName().equals(classstr)).findFirst();
  480. if (ocwo != null && ocwo.isPresent()) {
  481. bedData.setClassstr(classstr);
  482. bedData.setClassstrId(ocwo.get().getId());
  483. }
  484. //endregion
  485. bedData.setIsCheck(Integer.valueOf(isCheck));
  486. bedData.setCardNum(admissNum);
  487. bedData.setName(name);
  488. bedData.setRemark(remark);
  489. result.add(bedData);
  490. }
  491. }
  492. }
  493. } catch (Exception e) {
  494. return CommonResult.fail("请按模板格式导入数据");
  495. }
  496. return CommonResult.ok(result);
  497. }
  498. @Override
  499. public CommonResult downloadBedExcel() {
  500. return CommonResult.ok("200", "操作成功", "https://chtech.ncjti.edu.cn/alumnus/homeimage/床位信息管理.xlsx");
  501. }
  502. @Override
  503. public void welcomeBedExport(HttpServletResponse response, Integer schoolId, Integer buildId, Integer dormitoryId, String sex, Integer isCheck, Integer collegeId, Integer majorId, Integer classstrId) {
  504. List<WelcomeBed> result = welcomeBedService.queryPageWelcomeBeds(schoolId, buildId, dormitoryId, sex, isCheck, collegeId, majorId, classstrId);
  505. //导出
  506. Workbook workbook = new XSSFWorkbook();
  507. Sheet sheet = workbook.createSheet("床位信息");
  508. Row headerRow = sheet.createRow(0);
  509. headerRow.createCell(0).setCellValue("序号");
  510. headerRow.createCell(1).setCellValue("校区名称");
  511. headerRow.createCell(2).setCellValue("楼栋名称");
  512. headerRow.createCell(3).setCellValue("寝室号");
  513. headerRow.createCell(4).setCellValue("床位号");
  514. headerRow.createCell(5).setCellValue("床位性别");
  515. headerRow.createCell(6).setCellValue("所属学院");
  516. headerRow.createCell(7).setCellValue("所属专业");
  517. headerRow.createCell(8).setCellValue("所属班级");
  518. headerRow.createCell(9).setCellValue("是否入住");
  519. headerRow.createCell(10).setCellValue("录取号");
  520. headerRow.createCell(11).setCellValue("姓名");
  521. headerRow.createCell(12).setCellValue("备注");
  522. for (int i = 0; i < result.size(); i++) {
  523. WelcomeBed bed = result.get(i);
  524. Row dataRow = sheet.createRow(i + 1);
  525. dataRow.createCell(0).setCellValue(i + 1);
  526. dataRow.createCell(1).setCellValue(bed.getSchool());
  527. dataRow.createCell(2).setCellValue(bed.getBuild());
  528. dataRow.createCell(3).setCellValue(bed.getDormitory());
  529. dataRow.createCell(4).setCellValue(bed.getNumber());
  530. dataRow.createCell(5).setCellValue(bed.getSex());
  531. dataRow.createCell(6).setCellValue(bed.getCollege());
  532. dataRow.createCell(7).setCellValue(bed.getMajor());
  533. dataRow.createCell(8).setCellValue(bed.getClassstr());
  534. dataRow.createCell(9).setCellValue(bed.getIsCheck());
  535. dataRow.createCell(10).setCellValue(bed.getCardNum());
  536. dataRow.createCell(11).setCellValue(bed.getName());
  537. dataRow.createCell(12).setCellValue(bed.getRemark());
  538. }
  539. // 将工作簿写入文件
  540. ExcelUtils.excelDownload(workbook, "床位信息.xlsx", response);
  541. }
  542. @Override
  543. public CommonResult submit(WelcomeBed welcomeBed) {
  544. String studentCard = welcomeBed.getStudentCard();
  545. // 先判断是否已经提交
  546. LambdaQueryWrapper<WelcomeBed> wrapper=new LambdaQueryWrapper<>();
  547. wrapper.eq(WelcomeBed::getStudentCard,studentCard);
  548. WelcomeBed one = welcomeBedService.getOne(wrapper);
  549. if (ObjectUtils.isNotEmpty(one)) {
  550. return CommonResult.fail("已提交,请勿重复提交");
  551. }
  552. welcomeBedService.updateById(welcomeBed);
  553. return CommonResult.ok();
  554. }
  555. @Override
  556. public CommonResult bedDetails(Integer schoolId, Integer buildId, Integer dormitoryId, String studentCard) {
  557. // 获取未入住的床位
  558. List<BedDetailsVo> beds = welcomeBedService.getBedList(schoolId, buildId, dormitoryId, studentCard);
  559. return CommonResult.ok(beds);
  560. }
  561. @Override
  562. public CommonResult queryBedDatas(int dormitoryId) {
  563. List<WelcomeBed> beds = welcomeBedService.queryDormitorys(dormitoryId);
  564. List<ListVo> datas = new ArrayList<>();
  565. for (WelcomeBed bed:beds) {
  566. ListVo data = new ListVo();
  567. data.setId(bed.getId());
  568. data.setName(String.valueOf( bed.getNumber()));
  569. datas.add(data);
  570. }
  571. return CommonResult.ok(datas);
  572. }
  573. }