RoomBedServiceImpl.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. package com.xjrsoft.module.room.service.impl;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import cn.hutool.core.bean.BeanUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.github.yulichang.base.MPJBaseServiceImpl;
  10. import com.github.yulichang.toolkit.MPJWrappers;
  11. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  12. import com.xjrsoft.common.enums.DeleteMark;
  13. import com.xjrsoft.common.enums.GenderDictionaryEnum;
  14. import com.xjrsoft.common.exception.MyException;
  15. import com.xjrsoft.common.utils.VoToColumnUtil;
  16. import com.xjrsoft.module.base.entity.BaseClass;
  17. import com.xjrsoft.module.base.mapper.BaseClassMapper;
  18. import com.xjrsoft.module.room.dto.AdjustBedPageDto;
  19. import com.xjrsoft.module.room.dto.AdjustClassPageDto;
  20. import com.xjrsoft.module.room.dto.AdjustStudentBedDto;
  21. import com.xjrsoft.module.room.dto.DistributeClassPageDto;
  22. import com.xjrsoft.module.room.dto.DistributeRoomBedDto;
  23. import com.xjrsoft.module.room.dto.DistributeRoomBedPageDto;
  24. import com.xjrsoft.module.room.dto.RoomBedPageDto;
  25. import com.xjrsoft.module.room.entity.Room;
  26. import com.xjrsoft.module.room.entity.RoomBed;
  27. import com.xjrsoft.module.room.entity.RoomBedRecord;
  28. import com.xjrsoft.module.room.mapper.RoomBedMapper;
  29. import com.xjrsoft.module.room.mapper.RoomBedRecordMapper;
  30. import com.xjrsoft.module.room.mapper.RoomMapper;
  31. import com.xjrsoft.module.room.service.IRoomBedService;
  32. import com.xjrsoft.module.room.vo.AdjustBedClassPageVo;
  33. import com.xjrsoft.module.room.vo.AdjustBedClassStudentPageVo;
  34. import com.xjrsoft.module.room.vo.AdjustBedStudentPageVo;
  35. import com.xjrsoft.module.room.vo.ClassStudentCountVo;
  36. import com.xjrsoft.module.room.vo.DistributeClassPageVo;
  37. import com.xjrsoft.module.room.vo.DistributeResultClassVo;
  38. import com.xjrsoft.module.room.vo.DistributeResultListVo;
  39. import com.xjrsoft.module.room.vo.DistributeRoomBedPageVo;
  40. import com.xjrsoft.module.room.vo.NoBedStudentPageVo;
  41. import com.xjrsoft.module.room.vo.RoomBedExcelVo;
  42. import com.xjrsoft.module.room.vo.RoomBedPageVo;
  43. import com.xjrsoft.module.room.vo.RoomClassCountVo;
  44. import com.xjrsoft.module.room.vo.RoomClassExistentVo;
  45. import com.xjrsoft.module.room.vo.StudentPayStatusVo;
  46. import com.xjrsoft.module.student.entity.BaseStudent;
  47. import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
  48. import com.xjrsoft.module.student.mapper.BaseStudentMapper;
  49. import com.xjrsoft.module.teacher.entity.XjrUser;
  50. import com.xjrsoft.module.teacher.mapper.XjrUserMapper;
  51. import lombok.AllArgsConstructor;
  52. import org.springframework.stereotype.Service;
  53. import java.util.ArrayList;
  54. import java.util.Date;
  55. import java.util.HashMap;
  56. import java.util.HashSet;
  57. import java.util.List;
  58. import java.util.Map;
  59. import java.util.Set;
  60. /**
  61. * @title: 寝室床位
  62. * @Author dzx
  63. * @Date: 2023-12-27
  64. * @Version 1.0
  65. */
  66. @Service
  67. @AllArgsConstructor
  68. public class RoomBedServiceImpl extends MPJBaseServiceImpl<RoomBedMapper, RoomBed> implements IRoomBedService {
  69. private final RoomMapper roomMapper;
  70. private final RoomBedMapper roomBedMapper;
  71. private final BaseStudentMapper baseStudentMapper;
  72. private final BaseClassMapper baseClassMapper;
  73. private final XjrUserMapper xjrUserMapper;
  74. private final RoomBedRecordMapper roomBedRecordMapper;
  75. @Override
  76. public Page<RoomBedPageVo> getPage(Page<RoomBedPageVo> page, RoomBedPageDto dto) {
  77. Page<RoomBedPageVo> result = roomBedMapper.getPage(page, dto);
  78. return result;
  79. }
  80. @Override
  81. public List<RoomBedExcelVo> getList(RoomBedPageDto dto) {
  82. return roomBedMapper.getList(dto);
  83. }
  84. @Override
  85. public Boolean clearStudentInfo(List<Long> ids) {
  86. for (Long id : ids) {
  87. RoomBed roomBed = roomBedMapper.selectById(id);
  88. roomBed.setStudentUserId(null);
  89. // RoomBed roomBedData = BeanUtil.toBean(roomBed, RoomBed.class);
  90. // roomBedMapper.updateById(new RoomBed(){{
  91. // setId(id);
  92. // setStudentUserId(null);
  93. // }});
  94. UpdateWrapper<RoomBed> updateWrapper = new UpdateWrapper<>();
  95. updateWrapper.eq("id", id);
  96. updateWrapper.setSql("student_user_id = null");
  97. updateWrapper.setSql("is_check_in = 0");
  98. roomBedMapper.update(roomBed, updateWrapper);
  99. }
  100. return true;
  101. }
  102. @Override
  103. public Boolean clearStudentInfoByRoomId(Long id) {
  104. List<RoomBed> bedList = roomBedMapper.selectList(
  105. MPJWrappers.<RoomBed>lambdaJoin().eq(RoomBed::getRoomId, id)
  106. );
  107. for (RoomBed roomBed : bedList) {
  108. UpdateWrapper<RoomBed> updateWrapper = new UpdateWrapper<>();
  109. updateWrapper.eq("id", roomBed.getId());
  110. updateWrapper.setSql("student_user_id = null");
  111. updateWrapper.setSql("is_check_in = 0");
  112. roomBedMapper.update(roomBed, updateWrapper);
  113. }
  114. Room room = roomMapper.selectById(id);
  115. room.setIsMax(0);
  116. roomMapper.updateById(room);
  117. return true;
  118. }
  119. @Override
  120. public Boolean clearStudentInfoByBedId(Long id) {
  121. UpdateWrapper<RoomBed> updateWrapper = new UpdateWrapper<>();
  122. updateWrapper.eq("id", id);
  123. updateWrapper.setSql("student_user_id = null");
  124. updateWrapper.setSql("is_check_in = 0");
  125. RoomBed roomBed = roomBedMapper.selectById(id);
  126. roomBedMapper.update(roomBed, updateWrapper);
  127. //如果寝室的人只剩下一个班级,将混合寝室改为否
  128. List<Long> roomIds = new ArrayList<>();
  129. roomIds.add(roomBed.getRoomId());
  130. List<RoomClassCountVo> classCountVoList = roomMapper.getRoomClassCount(roomIds);
  131. for (RoomClassCountVo roomClassCountVo : classCountVoList) {
  132. if(roomClassCountVo.getClassCount() == 1){
  133. Room updRoom = roomMapper.selectById(roomClassCountVo.getId());
  134. updRoom.setIsMax(0);
  135. roomMapper.updateById(updRoom);
  136. }
  137. }
  138. return true;
  139. }
  140. @Override
  141. public Page<DistributeClassPageVo> getDistributeClassInfo(Page<DistributeClassPageDto> page, DistributeClassPageDto dto) {
  142. Page<DistributeClassPageVo> classInfo = roomBedMapper.getDistributeClassInfo(page, dto);
  143. List<DistributeClassPageVo> records = classInfo.getRecords();
  144. //查询所有班级需要安排住宿的人数,并组装成map备用
  145. List<ClassStudentCountVo> allStayCount = roomBedMapper.getAllStayCount();
  146. Map<String, Integer> allStayCountMap = new HashMap<>();
  147. for (ClassStudentCountVo classStudentCountVo : allStayCount) {
  148. allStayCountMap.put(classStudentCountVo.getClassId(), classStudentCountVo.getStudentCount());
  149. }
  150. //查询所有班级需要安排住宿的男生人数,并组装成map备用
  151. List<ClassStudentCountVo> allStayMaleCount = roomBedMapper.getAllStayMaleCount();
  152. Map<String, Integer> allStayMaleCountMap = new HashMap<>();
  153. for (ClassStudentCountVo classStudentCountVo : allStayMaleCount) {
  154. allStayMaleCountMap.put(classStudentCountVo.getClassId(), classStudentCountVo.getStudentCount());
  155. }
  156. //查询所有班级需要安排住宿的女生人数,并组装成map备用
  157. List<ClassStudentCountVo> allStayFemaleCount = roomBedMapper.getAllStayFemaleCount();
  158. Map<String, Integer> allStayFemaleCountMap = new HashMap<>();
  159. for (ClassStudentCountVo classStudentCountVo : allStayFemaleCount) {
  160. allStayFemaleCountMap.put(classStudentCountVo.getClassId(), classStudentCountVo.getStudentCount());
  161. }
  162. //查询所有班级已经安排住宿的总人数,并组装成map备用
  163. List<ClassStudentCountVo> allArrangedCount = roomBedMapper.getAllArrangedCount();
  164. Map<String, Integer> allArrangedCountMap = new HashMap<>();
  165. for (ClassStudentCountVo classStudentCountVo : allArrangedCount) {
  166. allArrangedCountMap.put(classStudentCountVo.getClassId(), classStudentCountVo.getStudentCount());
  167. }
  168. //查询所有班级已经安排住宿的男生人数,并组装成map备用
  169. List<ClassStudentCountVo> allArrangedMaleCount = roomBedMapper.getAllArrangedMaleCount();
  170. Map<String, Integer> allArrangedMaleCountMap = new HashMap<>();
  171. for (ClassStudentCountVo classStudentCountVo : allArrangedMaleCount) {
  172. allArrangedMaleCountMap.put(classStudentCountVo.getClassId(), classStudentCountVo.getStudentCount());
  173. }
  174. //查询所有班级已经安排住宿的女生人数,并组装成map备用
  175. List<ClassStudentCountVo> allArrangedFemaleCount = roomBedMapper.getAllArrangedFemaleCount();
  176. Map<String, Integer> allArrangedFemaleCountMap = new HashMap<>();
  177. for (ClassStudentCountVo classStudentCountVo : allArrangedFemaleCount) {
  178. allArrangedFemaleCountMap.put(classStudentCountVo.getClassId(), classStudentCountVo.getStudentCount());
  179. }
  180. for (DistributeClassPageVo record : records) {
  181. Integer needCout = 0;
  182. Integer distributeCount = 0;
  183. Integer maleCount = 0, femaleCount = 0;
  184. record.setMaleCount(maleCount);
  185. record.setFemaleCount(femaleCount);
  186. if("SB10001".equals(dto.getGender()) && allStayMaleCountMap.get(record.getId()) != null){
  187. if(allStayMaleCountMap.get(record.getId()) != null){
  188. maleCount = allStayMaleCountMap.get(record.getId()) - (allArrangedMaleCountMap.get(record.getId()) == null ? 0 : allArrangedMaleCountMap.get(record.getId()));
  189. }
  190. if(maleCount < 0){
  191. maleCount = 0;
  192. }
  193. record.setMaleCount(maleCount);
  194. needCout = allStayMaleCountMap.get(record.getId());
  195. if(allArrangedMaleCountMap.get(record.getId()) != null){
  196. distributeCount = (allArrangedMaleCountMap.get(record.getId()) == null ? 0 : allArrangedMaleCountMap.get(record.getId()));
  197. }
  198. }else if("SB10002".equals(dto.getGender())){
  199. if(allStayFemaleCountMap.get(record.getId()) != null){
  200. femaleCount = allStayFemaleCountMap.get(record.getId()) - (allArrangedFemaleCountMap.get(record.getId()) == null ? 0 : allArrangedFemaleCountMap.get(record.getId()));
  201. }
  202. if(femaleCount < 0){
  203. femaleCount = 0;
  204. }
  205. record.setFemaleCount(femaleCount);
  206. needCout = femaleCount;
  207. if(allArrangedFemaleCountMap.get(record.getId()) != null){
  208. distributeCount = (allArrangedFemaleCountMap.get(record.getId()) == null ? 0 : allArrangedFemaleCountMap.get(record.getId()));
  209. }
  210. }else{
  211. needCout = allStayCountMap.get(record.getId());
  212. if(allArrangedCountMap.get(record.getId()) != null){
  213. distributeCount = (allArrangedCountMap.get(record.getId()) == null ? 0 : allArrangedCountMap.get(record.getId()));
  214. }
  215. if(allStayMaleCountMap.get(record.getId()) != null){
  216. maleCount = allStayMaleCountMap.get(record.getId()) - (allArrangedMaleCountMap.get(record.getId()) == null ? 0 : allArrangedMaleCountMap.get(record.getId()));
  217. }
  218. if(maleCount < 0){
  219. maleCount = 0;
  220. }
  221. record.setMaleCount(maleCount);
  222. if(allStayFemaleCountMap.get(record.getId()) != null){
  223. femaleCount = allStayFemaleCountMap.get(record.getId()) - (allArrangedFemaleCountMap.get(record.getId()) == null ? 0 : allArrangedFemaleCountMap.get(record.getId()));
  224. }
  225. if(femaleCount < 0){
  226. femaleCount = 0;
  227. }
  228. record.setFemaleCount(femaleCount);
  229. }
  230. if(needCout == null){
  231. needCout = 0;
  232. }
  233. if(distributeCount == null){
  234. distributeCount = 0;
  235. }
  236. record.setNeedCount(needCout);
  237. record.setDistributeCount(distributeCount);
  238. }
  239. return classInfo;
  240. }
  241. @Override
  242. public Page<DistributeRoomBedPageVo> getDistributeRoomBedInfo(Page<DistributeRoomBedPageDto> page, DistributeRoomBedPageDto dto) {
  243. return roomBedMapper.getDistributeRoomBedInfo(page, dto);
  244. }
  245. /**
  246. * 分配床位
  247. * @param dto
  248. * @return
  249. */
  250. @Override
  251. public Boolean distributeRoomBed(DistributeRoomBedDto dto) {
  252. List<Room> roomList = roomMapper.selectList(new QueryWrapper<Room>().lambda().in(Room::getId, dto.getRoomIds()));
  253. Set<String> genderSet = new HashSet<>();
  254. //存入每个寝室已经入住的班级id
  255. Map<Long, Long> roomClassMaps = new HashMap<>();
  256. for (Room room : roomList) {
  257. genderSet.add(room.getGender());
  258. roomClassMaps.put(room.getId(), null);
  259. }
  260. //
  261. List<RoomClassExistentVo> roomClassExistent = roomMapper.getRoomClassExistent(dto.getRoomIds());
  262. Map<Long, String> roomClassExistentMaps = new HashMap<>();
  263. for (RoomClassExistentVo roomClassExistentVo : roomClassExistent) {
  264. roomClassExistentMaps.put(roomClassExistentVo.getId(), roomClassExistentVo.getClassIds());
  265. }
  266. Date modifyDate = new Date();
  267. Long modifyUserId = StpUtil.getLoginIdAsLong();
  268. //查询每个班的学生,修改床位信息
  269. Map<Long, Integer> classDistributeBedNumber = new HashMap<>();
  270. Map<Long, Integer> classStudent = new HashMap<>();
  271. //查询学生住宿费缴费状态
  272. List<StudentPayStatusVo> studentPayStatus = roomBedMapper.getStudentPayStatus(dto);
  273. Map<Long, Integer> payStatusMap = new HashMap<>();
  274. for (StudentPayStatusVo payStatus : studentPayStatus) {
  275. payStatusMap.put(payStatus.getId(), (payStatus.getPayStatus() == null?0:payStatus.getPayStatus()));
  276. }
  277. //按照性别分组分配
  278. for (String genderNumber : genderSet) {
  279. int i = 0;
  280. //根据性别查询出所有空床位信息
  281. List<RoomBed> bedInfoList = roomBedMapper.selectList(
  282. new MPJLambdaWrapper<RoomBed>()
  283. .select(RoomBed::getId)
  284. .select(RoomBed.class, x -> VoToColumnUtil.fieldsToColumns(RoomBed.class).contains(x.getProperty()))
  285. .innerJoin(Room.class, Room::getId, RoomBed::getRoomId)
  286. .eq(Room::getGender, genderNumber)
  287. .isNull(RoomBed::getStudentUserId)
  288. .eq(RoomBed::getIsCheckIn, 0)
  289. .in(RoomBed::getRoomId, dto.getRoomIds())
  290. );
  291. //按照班级分配
  292. for (Long classId : dto.getClassIds()) {
  293. //先查询出班上的在读住校生
  294. List<BaseStudent> studentList = baseStudentMapper.selectList(
  295. MPJWrappers.<BaseStudent>lambdaJoin()
  296. .select(BaseStudent::getUserId)
  297. .eq(BaseClass::getId, classId)
  298. .eq(XjrUser::getGender, genderNumber)
  299. .eq(BaseStudentSchoolRoll::getStduyStatus, "FB3002")
  300. .eq(BaseStudentSchoolRoll::getArchivesStatus, "FB2901")//只查询在读学生
  301. .eq(BaseStudent::getDeleteMark, DeleteMark.NODELETE.getCode())
  302. .eq(XjrUser::getDeleteMark, DeleteMark.NODELETE.getCode())
  303. .innerJoin(XjrUser.class, XjrUser::getId, BaseStudent::getUserId)
  304. .leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, BaseStudent::getUserId)
  305. .leftJoin(BaseClass.class, BaseClass::getId, BaseStudentSchoolRoll::getClassId)
  306. );
  307. //已分配床位数
  308. int distributeBedNumber = 0;
  309. //先分配已缴费的,再分配未交费的,把已缴费的学生排序到一起
  310. List<BaseStudent> studentOrderList = new ArrayList<>();
  311. for (BaseStudent baseStudent : studentList) {
  312. if(payStatusMap.get(baseStudent.getUserId()) != null && payStatusMap.get(baseStudent.getUserId()) == 1){
  313. studentOrderList.add(baseStudent);
  314. }
  315. }
  316. for (BaseStudent baseStudent : studentList) {
  317. if(payStatusMap.get(baseStudent.getUserId()) == null || payStatusMap.get(baseStudent.getUserId()) == 0){
  318. studentOrderList.add(baseStudent);
  319. }
  320. }
  321. //判断生成策略,是否需要混合寝室
  322. if(dto.getIsNeedMaxRoom() == 0 && i <= bedInfoList.size()){
  323. //预先查看下一个床位所属寝室是否已经有其他班级入住,有就直接跳过
  324. for (int j = i; j < bedInfoList.size(); j ++) {
  325. Long roomId = bedInfoList.get(j).getRoomId();
  326. if((roomClassMaps.get(roomId) != null
  327. && !classId.equals(roomClassMaps.get(roomId)))
  328. && (roomClassExistentMaps.get(roomId) != null && roomClassExistentMaps.get(roomId).contains(classId.toString()))){
  329. i ++;
  330. }
  331. }
  332. }
  333. //执行床位分配
  334. for (BaseStudent studentInfoVo : studentOrderList) {
  335. if(i >= bedInfoList.size()){
  336. continue;
  337. }
  338. RoomBed roomBed = bedInfoList.get(i);
  339. roomBed.setStudentUserId(studentInfoVo.getUserId());
  340. roomBed.setModifyDate(modifyDate);
  341. roomBed.setModifyUserId(modifyUserId);
  342. roomBedMapper.updateById(roomBed);
  343. i ++;
  344. distributeBedNumber ++;
  345. //分配好一个学生之后,将这个寝室已入住的班级存入
  346. roomClassMaps.putIfAbsent(roomBed.getRoomId(), classId);
  347. }
  348. classDistributeBedNumber.put(classId, distributeBedNumber);
  349. classStudent.put(classId, studentList.size());
  350. }
  351. }
  352. Map<Long, Long> classGradeMap = new HashMap<>();
  353. baseClassMapper.selectList(
  354. MPJWrappers.<BaseClass>lambdaJoin().in(BaseClass::getId, dto.getClassIds())
  355. ).forEach((baseClass)->{
  356. classGradeMap.put(baseClass.getId(), baseClass.getGradeId());
  357. });
  358. //插入记录表 room_bed_record
  359. Integer maxSortCode = roomBedRecordMapper.getMaxSortCode();
  360. for (Long classId : classGradeMap.keySet()) {
  361. maxSortCode ++;
  362. // Integer studentCount = classStudent.get(classId);
  363. RoomBedRecord record = new RoomBedRecord();
  364. record.setClassId(classId);
  365. record.setGradeId(classGradeMap.get(classId));
  366. record.setCreateDate(modifyDate);
  367. record.setSortCode(maxSortCode);
  368. record.setNeedBedNumber(classStudent.get(classId));
  369. record.setDistributeBedNumber(classDistributeBedNumber.get(classId));
  370. record.setDeleteMark(DeleteMark.NODELETE.getCode());
  371. roomBedRecordMapper.insert(record);
  372. }
  373. if(dto.getIsNeedMaxRoom() == 1){
  374. roomMapper.setIsmaxTrue();
  375. }
  376. //查询每个寝室住入的班级数量,大于2的设置为混合寝室
  377. // List<RoomClassCountVo> classCountVoList = roomMapper.getRoomClassCount(dto.getRoomIds());
  378. // for (RoomClassCountVo roomClassCountVo : classCountVoList) {
  379. // if(roomClassCountVo.getClassCount() > 1){
  380. // Room updRoom = roomMapper.selectById(roomClassCountVo.getId());
  381. // updRoom.setIsMax(1);
  382. // roomMapper.updateById(updRoom);
  383. // }
  384. // }
  385. return true;
  386. }
  387. @Override
  388. public List<DistributeResultClassVo> getDistributeResult(DistributeRoomBedDto dto) {
  389. List<DistributeResultListVo> list = roomBedMapper.getDistributeResult(dto);
  390. List<BaseClass> classList = baseClassMapper.selectList(
  391. Wrappers.lambdaQuery(BaseClass.class).in(BaseClass::getId, dto.getClassIds())
  392. .select(BaseClass.class, x -> VoToColumnUtil.fieldsToColumns(BaseClass.class).contains(x.getProperty()))
  393. );
  394. List<DistributeResultClassVo> result = new ArrayList<>();
  395. for (BaseClass baseClass : classList) {
  396. DistributeResultClassVo classVo = new DistributeResultClassVo();
  397. classVo.setClassName(baseClass.getName());
  398. List<DistributeResultListVo> studentList = new ArrayList<>();
  399. for (DistributeResultListVo listVo : list) {
  400. if(listVo.getClassId() == null || !listVo.getClassId().equals(baseClass.getId().toString())){
  401. continue;
  402. }
  403. studentList.add(listVo);
  404. }
  405. classVo.setStudentList(studentList);
  406. result.add(classVo);
  407. }
  408. return result;
  409. }
  410. @Override
  411. public List<AdjustBedClassPageVo> getClassStudetBed(AdjustClassPageDto dto) {
  412. List<AdjustBedClassPageVo> classList = roomBedMapper.getClassTeacherInfo(dto);
  413. List<AdjustBedClassPageVo> result = new ArrayList<>();
  414. List<AdjustBedClassStudentPageVo> allStudent = roomBedMapper.getClassStudetBed(dto);
  415. //查询缴费状态
  416. DistributeRoomBedDto distributeRoomBedDto = new DistributeRoomBedDto();
  417. List<Long> classIds = new ArrayList<>();
  418. for (AdjustBedClassPageVo baseClass : classList) {
  419. classIds.add(Long.parseLong(baseClass.getId()));
  420. }
  421. if(!classIds.isEmpty()){
  422. distributeRoomBedDto.setClassIds(classIds);
  423. }
  424. List<StudentPayStatusVo> studentPayStatusVoList = roomBedMapper.getStudentPayStatus(distributeRoomBedDto);
  425. Map<Long, Integer> payStatusMap = new HashMap<>();
  426. for (StudentPayStatusVo statusVo : studentPayStatusVoList) {
  427. Integer payStatus = 0;
  428. if(statusVo.getPayStatus() != null){
  429. payStatus = statusVo.getPayStatus();
  430. }
  431. payStatusMap.put(statusVo.getId(), payStatus);
  432. }
  433. for (AdjustBedClassPageVo classOne : classList) {
  434. List<AdjustBedClassStudentPageVo> studentList = new ArrayList<>();
  435. for (AdjustBedClassStudentPageVo adjustBedClassStudentPageVo : allStudent) {
  436. if(!classOne.getId().toString().equals(adjustBedClassStudentPageVo.getClassId())){
  437. continue;
  438. }
  439. Integer payStatus = payStatusMap.get(Long.valueOf(adjustBedClassStudentPageVo.getUserId()));
  440. if(payStatus == null){
  441. payStatus = 0;
  442. }
  443. adjustBedClassStudentPageVo.setPayStatus(payStatus);
  444. studentList.add(adjustBedClassStudentPageVo);
  445. }
  446. if(studentList.isEmpty()){
  447. continue;
  448. }
  449. AdjustBedClassPageVo adjustBedClassPageVo = BeanUtil.toBean(classOne, AdjustBedClassPageVo.class);
  450. adjustBedClassPageVo.setStudentList(studentList);
  451. result.add(adjustBedClassPageVo);
  452. }
  453. return result;
  454. }
  455. @Override
  456. public List<AdjustBedStudentPageVo> getBedStudetInfo(AdjustBedPageDto dto) {
  457. return roomBedMapper.getBedStudentInfo(dto);
  458. }
  459. @Override
  460. public Page<NoBedStudentPageVo> getNoBedStudent(Page<AdjustBedPageDto> page, AdjustBedPageDto dto) {
  461. return roomBedMapper.getNoBedStudent(page, dto);
  462. }
  463. @Override
  464. public Boolean adjustBed(AdjustStudentBedDto dto) {
  465. //查询学生的性别
  466. XjrUser xjrUser = xjrUserMapper.selectById(dto.getStudentUserId());
  467. String studentGender = xjrUser.getGender();
  468. // if(1 == xjrUser.getGender()){
  469. // studentGender = "SB10001";
  470. // }else if(2 == xjrUser.getGender()){
  471. // studentGender = "SB10002";
  472. // }
  473. RoomBed roomBedInfo = roomBedMapper.selectById(dto.getBedId());
  474. Room room = roomMapper.selectById(roomBedInfo.getRoomId());
  475. if(room == null){
  476. throw new MyException("为查询到该床位");
  477. }
  478. if(!room.getGender().equals(studentGender)){
  479. String genderStr = "";
  480. if("SB10001".equals(room.getGender())){
  481. genderStr = "男";
  482. }else if("SB10002".equals(room.getGender())){
  483. genderStr = "女";
  484. }
  485. throw new MyException("该学生无法入住" + genderStr + "寝室");
  486. }
  487. //先清空学生原来的床位
  488. UpdateWrapper<RoomBed> updateWrapper = new UpdateWrapper<>();
  489. updateWrapper.eq("student_user_id", dto.getStudentUserId());
  490. updateWrapper.setSql("student_user_id = null");
  491. updateWrapper.setSql("is_check_in = 0");
  492. List<Long> roomIds = new ArrayList<>();
  493. List<RoomBed> roomBedList = roomBedMapper.selectList(new QueryWrapper<RoomBed>().lambda().eq(RoomBed::getStudentUserId, dto.getStudentUserId()));
  494. for (RoomBed roomBed : roomBedList) {
  495. roomBedMapper.update(roomBed, updateWrapper);
  496. roomIds.add(roomBed.getRoomId());
  497. }
  498. //再把学生保存到新的床位
  499. roomBedMapper.updateById(new RoomBed(){{
  500. setId(dto.getBedId());
  501. setStudentUserId(dto.getStudentUserId());
  502. }});
  503. if(roomIds.isEmpty()){
  504. roomIds = null;
  505. }
  506. //如果寝室的人是多个班级,将混合寝室改为是
  507. List<RoomClassCountVo> classCountVoList = roomMapper.getRoomClassCount(roomIds);
  508. for (RoomClassCountVo roomClassCountVo : classCountVoList) {
  509. if(roomClassCountVo.getClassCount() > 1){
  510. Room updRoom = roomMapper.selectById(roomClassCountVo.getId());
  511. updRoom.setIsMax(1);
  512. roomMapper.updateById(updRoom);
  513. }
  514. }
  515. return true;
  516. }
  517. /**
  518. * 1、先做性别校验,如果学生性别和入住寝室的性别不一致,不进行数据保存并返回学生的姓名到前端
  519. * 2、参数中可能存在学生id不为空,床位id为空的,根据学生id清空学生相应的床位信息
  520. * 3、如果一个学生被安排了多个床位,需要新增提示
  521. * @param dtoList
  522. * @return
  523. */
  524. @Override
  525. public Boolean adjustBedBatch(List<AdjustStudentBedDto> dtoList) {
  526. //存分配到女生寝室的男生名字
  527. Set<String> boyStudents = new HashSet<>();
  528. //存分配到男生寝室的女生名字
  529. Set<String> girlStudents = new HashSet<>();
  530. //需要清空床位的学生
  531. List<AdjustStudentBedDto> todoStudents = new ArrayList<>();
  532. Map<Long, Long> bedStudentMap = new HashMap<>();
  533. Map<Long, String> studentNameMap = new HashMap<>();
  534. for (AdjustStudentBedDto dto : dtoList) {
  535. if(dto.getStudentUserId() != null && dto.getBedId() != null){
  536. bedStudentMap.put(dto.getBedId(), dto.getStudentUserId());
  537. //查询学生的性别
  538. XjrUser xjrUser = xjrUserMapper.selectById(dto.getStudentUserId());
  539. studentNameMap.put(dto.getStudentUserId(), xjrUser.getName());
  540. String studentGender = xjrUser.getGender();
  541. RoomBed roomBedInfo = roomBedMapper.selectById(dto.getBedId());
  542. Room room = roomMapper.selectById(roomBedInfo.getRoomId());
  543. if(!room.getGender().equals(studentGender)){
  544. if(GenderDictionaryEnum.MALE.getCode().equals(room.getGender())){//男生
  545. boyStudents.add(xjrUser.getName());
  546. }else if(GenderDictionaryEnum.FEMALE.getCode().equals(room.getGender())){//女生
  547. girlStudents.add(xjrUser.getName());
  548. }
  549. continue;
  550. }
  551. //校验通过
  552. todoStudents.add(dto);
  553. }
  554. }
  555. //存被安排了多个床位的学生
  556. List<String> moreBedStudents = new ArrayList<>();
  557. for (Map.Entry<Long, Long> entry : bedStudentMap.entrySet()) {
  558. Long value = entry.getValue();
  559. if (bedStudentMap.entrySet().stream().filter(e -> e.getValue().equals(value)).count() > 1) {
  560. moreBedStudents.add(studentNameMap.get(value));
  561. }
  562. }
  563. //存这次处理过的寝室,查看是否需要设置为混合寝室
  564. List<Long> roomIds = new ArrayList<>();
  565. if(boyStudents.isEmpty() && girlStudents.isEmpty() && moreBedStudents.isEmpty()){
  566. for (AdjustStudentBedDto todoStudent : todoStudents) {
  567. //先清空原来的床位
  568. UpdateWrapper<RoomBed> updateWrapper = new UpdateWrapper<>();
  569. updateWrapper.eq("student_user_id", todoStudent.getStudentUserId());
  570. updateWrapper.setSql("student_user_id = null");
  571. updateWrapper.setSql("is_check_in = 0");
  572. List<RoomBed> roomBedList = roomBedMapper.selectList(new QueryWrapper<RoomBed>().lambda().eq(RoomBed::getStudentUserId, todoStudent.getStudentUserId()));
  573. for (RoomBed roomBed : roomBedList) {
  574. roomBedMapper.update(roomBed, updateWrapper);
  575. }
  576. //再把学生保存到新的床位
  577. roomBedMapper.updateById(new RoomBed(){{
  578. setId(todoStudent.getBedId());
  579. setStudentUserId(todoStudent.getStudentUserId());
  580. }});
  581. }
  582. //将有多个班级的寝室设置为混合寝室
  583. roomMapper.setIsmaxTrue();
  584. //将没有多个班级的混合寝室清除掉
  585. roomMapper.setIsmaxFalse();
  586. }else{
  587. String boyMsg = "学生";
  588. int i = 0;
  589. for (String boyStudent : boyStudents) {
  590. if(i > 0){
  591. boyMsg += "、";
  592. }
  593. boyMsg += boyStudent;
  594. i ++;
  595. }
  596. boyMsg += "被分配到女生寝室";
  597. String girlMsg = "学生";
  598. i = 0;
  599. for (String girlStudent : girlStudents) {
  600. if(i > 0){
  601. girlMsg += "、";
  602. }
  603. girlMsg += girlStudent;
  604. i ++;
  605. }
  606. girlMsg += "被分配到男生寝室";
  607. String moreBedMsg = "学生";
  608. for (int j = 0; j < moreBedStudents.size(); j ++){
  609. if(j > 0){
  610. moreBedMsg += "、";
  611. }
  612. moreBedMsg += moreBedStudents.get(j);
  613. }
  614. moreBedMsg += "被分配多个床位";
  615. String msg = "";
  616. if(!boyStudents.isEmpty()){
  617. msg += boyMsg + ";";
  618. }
  619. if(!girlStudents.isEmpty()){
  620. msg += girlMsg + ";";
  621. }
  622. if(!moreBedStudents.isEmpty()){
  623. msg += moreBedMsg + ";";
  624. }
  625. throw new MyException(msg);
  626. }
  627. return true;
  628. }
  629. }