WelcomeBedController.java 33 KB

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