RoomBedServiceImpl.java 31 KB

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