BandingTaskServiceImpl.java 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. package com.xjrsoft.module.banding.service.impl;
  2. import cn.dev33.satoken.secure.BCrypt;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.github.yulichang.base.MPJBaseServiceImpl;
  7. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  8. import com.xjrsoft.common.enums.ArchivesStatusEnum;
  9. import com.xjrsoft.common.enums.DeleteMark;
  10. import com.xjrsoft.common.enums.EnabledMark;
  11. import com.xjrsoft.common.enums.GenderDictionaryEnum;
  12. import com.xjrsoft.common.enums.RoleEnum;
  13. import com.xjrsoft.common.exception.MyException;
  14. import com.xjrsoft.common.utils.VoToColumnUtil;
  15. import com.xjrsoft.config.CommonPropertiesConfig;
  16. import com.xjrsoft.module.banding.dto.AutomaticBandingTaskDto;
  17. import com.xjrsoft.module.banding.dto.BandingTaskClassStudentPageDto;
  18. import com.xjrsoft.module.banding.dto.BandingTaskPageDto;
  19. import com.xjrsoft.module.banding.dto.SureBandingTaskDto;
  20. import com.xjrsoft.module.banding.entity.BandingRule;
  21. import com.xjrsoft.module.banding.entity.BandingTask;
  22. import com.xjrsoft.module.banding.entity.BandingTaskClass;
  23. import com.xjrsoft.module.banding.entity.BandingTaskClassStudent;
  24. import com.xjrsoft.module.banding.entity.BandingTaskMajorCondition;
  25. import com.xjrsoft.module.banding.entity.BandingTaskRule;
  26. import com.xjrsoft.module.banding.mapper.BandingRuleMapper;
  27. import com.xjrsoft.module.banding.mapper.BandingTaskClassMapper;
  28. import com.xjrsoft.module.banding.mapper.BandingTaskMapper;
  29. import com.xjrsoft.module.banding.mapper.BandingTaskRuleMapper;
  30. import com.xjrsoft.module.banding.service.IBandingTaskClassStudentService;
  31. import com.xjrsoft.module.banding.service.IBandingTaskMajorConditionService;
  32. import com.xjrsoft.module.banding.service.IBandingTaskService;
  33. import com.xjrsoft.module.banding.vo.BandingTaskClassSureListVo;
  34. import com.xjrsoft.module.banding.vo.BandingTaskPageVo;
  35. import com.xjrsoft.module.banding.vo.IdManyCountVo;
  36. import com.xjrsoft.module.base.entity.BaseClass;
  37. import com.xjrsoft.module.base.entity.BaseGrade;
  38. import com.xjrsoft.module.base.entity.BaseMajorSet;
  39. import com.xjrsoft.module.base.mapper.BaseMajorSetMapper;
  40. import com.xjrsoft.module.base.service.IBaseClassService;
  41. import com.xjrsoft.module.base.service.IBaseGradeService;
  42. import com.xjrsoft.module.organization.entity.User;
  43. import com.xjrsoft.module.organization.entity.UserRoleRelation;
  44. import com.xjrsoft.module.organization.service.IUserRoleRelationService;
  45. import com.xjrsoft.module.organization.service.IUserService;
  46. import com.xjrsoft.module.outint.vo.IdCountVo;
  47. import com.xjrsoft.module.student.entity.BaseClassMajorSet;
  48. import com.xjrsoft.module.student.entity.BaseNewStudent;
  49. import com.xjrsoft.module.student.entity.BaseStudent;
  50. import com.xjrsoft.module.student.entity.BaseStudentFamily;
  51. import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
  52. import com.xjrsoft.module.student.entity.EnrollmentPlan;
  53. import com.xjrsoft.module.student.mapper.BaseClassMajorSetMapper;
  54. import com.xjrsoft.module.student.service.IBaseNewStudentService;
  55. import com.xjrsoft.module.student.service.IBaseStudentFamilyService;
  56. import com.xjrsoft.module.student.service.IBaseStudentSchoolRollService;
  57. import com.xjrsoft.module.student.service.IBaseStudentService;
  58. import lombok.AllArgsConstructor;
  59. import org.springframework.stereotype.Service;
  60. import org.springframework.transaction.annotation.Transactional;
  61. import java.math.BigDecimal;
  62. import java.time.LocalDate;
  63. import java.time.LocalDateTime;
  64. import java.time.format.DateTimeFormatter;
  65. import java.util.ArrayList;
  66. import java.util.Collections;
  67. import java.util.Date;
  68. import java.util.HashMap;
  69. import java.util.List;
  70. import java.util.Map;
  71. import java.util.Objects;
  72. import java.util.Random;
  73. import java.util.stream.Collectors;
  74. /**
  75. * @title: 新生分班任务
  76. * @Author dzx
  77. * @Date: 2024-07-01
  78. * @Version 1.0
  79. */
  80. @Service
  81. @AllArgsConstructor
  82. public class BandingTaskServiceImpl extends MPJBaseServiceImpl<BandingTaskMapper, BandingTask> implements IBandingTaskService {
  83. private final BandingTaskMapper bandingTaskMapper;
  84. private final BandingRuleMapper ruleMapper;
  85. private final BandingTaskRuleMapper taskRuleMapper;
  86. private final BandingTaskClassMapper taskClassMapper;
  87. private final IBaseNewStudentService newStudentService;
  88. private final IBandingTaskClassStudentService classStudentService;
  89. private final IBandingTaskMajorConditionService conditionService;
  90. private final IBaseClassService classService;
  91. private final BaseClassMajorSetMapper classMajorSetMapper;
  92. private final CommonPropertiesConfig propertiesConfig;
  93. private final IUserService userService;
  94. private final IUserRoleRelationService roleRelationService;
  95. private final IBaseStudentSchoolRollService schoolRollService;
  96. private final IBaseStudentService studentService;
  97. private final IBaseStudentFamilyService familyService;
  98. private final IBaseGradeService gradeService;
  99. private final BaseMajorSetMapper majorSetMapper;
  100. @Override
  101. @Transactional(rollbackFor = Exception.class)
  102. public Boolean add(BandingTask bandingTask) {
  103. bandingTask.setCreateDate(new Date());
  104. bandingTaskMapper.insert(bandingTask);
  105. return true;
  106. }
  107. @Override
  108. @Transactional(rollbackFor = Exception.class)
  109. public Boolean update(BandingTask bandingTask) {
  110. bandingTask.setModifyDate(new Date());
  111. bandingTaskMapper.updateById(bandingTask);
  112. return true;
  113. }
  114. @Override
  115. @Transactional(rollbackFor = Exception.class)
  116. public Boolean delete(List<Long> ids) {
  117. bandingTaskMapper.deleteBatchIds(ids);
  118. taskRuleMapper.delete(Wrappers.lambdaQuery(BandingTaskRule.class).in(BandingTaskRule::getBandingTaskId, ids));
  119. taskClassMapper.delete(Wrappers.lambdaQuery(BandingTaskClass.class).in(BandingTaskClass::getBandingTaskId, ids));
  120. return true;
  121. }
  122. /**
  123. * 自动分班
  124. * 1、根据分班任务的信息,查询出来需要分班的学生
  125. * 2、查询该任务下的班级信息
  126. * 3、查询该任务使用的规则
  127. * 4、执行自动分班
  128. * 5、完成后,将分好班的信息存到banding_task_class_student表中
  129. */
  130. @Override
  131. public Boolean automaticBanding(AutomaticBandingTaskDto dto) {
  132. BandingTask bandingTask = this.getById(dto.getBandingTaskId());
  133. if(bandingTask == null){
  134. throw new MyException("未能查询到该任务,无法自动分班");
  135. }
  136. //1、查询需要分班的学生信息
  137. List<BandingTaskClassStudent> classStudents = classStudentService.selectJoinList(BandingTaskClassStudent.class,
  138. new MPJLambdaWrapper<BandingTaskClassStudent>()
  139. .select(BandingTaskClassStudent::getId)
  140. .select(BandingTaskClassStudent.class, x -> VoToColumnUtil.fieldsToColumns(BandingTaskClassStudent.class).contains(x.getProperty()))
  141. .leftJoin(BandingTaskClass.class, BandingTaskClass::getId, BandingTaskClassStudent::getBandingTaskClassId)
  142. .eq(BandingTaskClass::getStatus, 1)
  143. );
  144. List<Long> studentIds = classStudents.stream().map(BandingTaskClassStudent::getNewStudentId).collect(Collectors.toList());
  145. List<String> orderColumn = new ArrayList();
  146. orderColumn.add("score");
  147. List<BaseNewStudent> baseNewStudents = newStudentService.selectJoinList(BaseNewStudent.class,
  148. new MPJLambdaWrapper<BaseNewStudent>()
  149. .select(BaseNewStudent::getId)
  150. .select(BaseNewStudent.class, x -> VoToColumnUtil.fieldsToColumns(BaseNewStudent.class).contains(x.getProperty()))
  151. .leftJoin(EnrollmentPlan.class, EnrollmentPlan::getId, BaseNewStudent::getEnrollmentPlanId)
  152. .eq(EnrollmentPlan::getGradeId, bandingTask.getGradeId())
  153. .eq(EnrollmentPlan::getEnrollType, bandingTask.getEnrollType())
  154. .eq(BaseNewStudent::getStatus, 0)
  155. .notIn(!studentIds.isEmpty(), BaseNewStudent::getId, studentIds)
  156. .orderByDescStr(orderColumn)
  157. );
  158. //2、查询所有班级信息
  159. List<BandingTaskClass> classList = taskClassMapper.getListOrderByAsc(bandingTask.getId());
  160. if(!classList.isEmpty()){
  161. //清除数据
  162. List<Long> classIds = classList.stream().map(BandingTaskClass::getId).collect(Collectors.toList());
  163. classStudentService.remove(
  164. new QueryWrapper<BandingTaskClassStudent>().lambda()
  165. .in(BandingTaskClassStudent::getBandingTaskClassId, classIds)
  166. );
  167. }
  168. //3、查询所用到的规则
  169. List<BandingRule> ruleList = ruleMapper.selectJoinList(BandingRule.class,
  170. new MPJLambdaWrapper<BandingRule>()
  171. .select(BandingRule::getId)
  172. .select(BandingRule.class, x -> VoToColumnUtil.fieldsToColumns(BandingRule.class).contains(x.getProperty()))
  173. .innerJoin(BandingTaskRule.class, BandingTaskRule::getBandingRuleId, BandingRule::getId)
  174. .eq(BandingTaskRule::getBandingTaskId, bandingTask.getId())
  175. );
  176. List<String> ruleCodes = ruleList.stream().map(BandingRule::getCode).collect(Collectors.toList());
  177. //查询每个专业符合限制条件的人数
  178. List<IdManyCountVo> majorStudentCountList = newStudentService.getMajorStudentCount(bandingTask.getId());
  179. Map<Long, IdManyCountVo> majorStudentCountMap = majorStudentCountList.stream().collect(Collectors.toMap(IdManyCountVo::getId, x -> x));
  180. //查询每个专业下面有多少个班级
  181. List<IdCountVo> majorClassCountList = taskClassMapper.getMajorClassCount(bandingTask.getId());
  182. Map<Long, Integer> majorClassCount = majorClassCountList.stream().collect(Collectors.toMap(IdCountVo::getId, IdCountVo::getCount));
  183. //计算每个专业平均的男女人数
  184. Map<Long, Integer> majorMaleCountMap = new HashMap<>();
  185. Map<Long, Integer> majorFemaleCountMap = new HashMap<>();
  186. if(ruleCodes.contains("BR0001")){
  187. for (Long majorSetId : majorClassCount.keySet()) {
  188. int classCount = majorClassCount.get(majorSetId);
  189. Integer maleCount = majorStudentCountMap.get(majorSetId).getMaleCount();
  190. Integer femaleCount = majorStudentCountMap.get(majorSetId).getFemaleCount();
  191. if(classCount == 1 || classCount == 0){
  192. majorMaleCountMap.put(majorSetId, maleCount);
  193. majorFemaleCountMap.put(majorSetId, femaleCount);
  194. continue;
  195. }
  196. majorMaleCountMap.put(majorSetId, maleCount / classCount);
  197. majorFemaleCountMap.put(majorSetId, femaleCount / classCount);
  198. }
  199. }
  200. //包含下面个条件,则需要计算每个班级应该分配的人数
  201. Map<Long, Integer> classLimitMap = new HashMap<>();
  202. if(ruleCodes.contains("BR0004")){
  203. //查询每个专业下面的班级人数
  204. Map<Long, Integer> majorClassStudentCount = taskClassMapper.getMajorClassStudentCount(bandingTask.getId())
  205. .stream().collect(Collectors.toMap(IdCountVo::getId, IdCountVo::getCount));
  206. Map<Long, Integer> majorStudentCount = majorStudentCountList.stream().collect(Collectors.toMap(IdManyCountVo::getId, IdManyCountVo::getCount));
  207. Map<Long, Integer> majorLimitMap = new HashMap<>();
  208. for (Long majorSetId : majorClassStudentCount.keySet()) {
  209. int majorClassNumber = 0;
  210. if(majorClassStudentCount.get(majorSetId) != null){
  211. majorClassNumber = majorClassStudentCount.get(majorSetId);
  212. }
  213. if(majorClassCount.get(majorSetId) == 1 || majorClassCount.get(majorSetId) == 0){
  214. continue;
  215. }
  216. int majorStudentNumber = 0;
  217. if(majorStudentCount.get(majorSetId) != null){
  218. majorStudentNumber = majorStudentCount.get(majorSetId);
  219. }
  220. Integer classCount = majorClassCount.get(majorSetId);
  221. if(majorStudentNumber < majorClassNumber){//报名人数小于班级人数
  222. Integer classLimtCount = majorStudentNumber / classCount;
  223. majorLimitMap.put(majorSetId, classLimtCount);
  224. }else{
  225. Integer classLimtCount = majorClassNumber / classCount;
  226. majorLimitMap.put(majorSetId, classLimtCount);
  227. }
  228. }
  229. for (BandingTaskClass bandingTaskClass : classList) {
  230. classLimitMap.put(bandingTaskClass.getId(), majorLimitMap.get(bandingTaskClass.getMajorSetId()));
  231. }
  232. }
  233. //查询每个专业的限制条件
  234. List<BandingTaskMajorCondition> list = conditionService.list(
  235. new QueryWrapper<BandingTaskMajorCondition>().lambda()
  236. .eq(BandingTaskMajorCondition::getBandingTaskId, dto.getBandingTaskId())
  237. );
  238. Map<Long, BandingTaskMajorCondition> classConditionMap = new HashMap<>();
  239. for (BandingTaskMajorCondition conditionDto : list) {
  240. classConditionMap.put(conditionDto.getMajorSetId(), conditionDto);
  241. }
  242. Map<Long, List<BaseNewStudent>> classStudentMap = new HashMap<>();
  243. if(ruleCodes.contains("BR0002")){
  244. classStudentMap.putAll(divideStudentByScore(classConditionMap, baseNewStudents, classList));
  245. }
  246. //存班级和学生的关系
  247. Map<Long, Long> studentClassMap = new HashMap<>();
  248. Map<Long, List<String>> classNameMap = new HashMap<>();
  249. //4、开始分班
  250. for (BandingTaskClass taskClass : classList) {
  251. Integer number = taskClass.getNumber();//班级总人数
  252. if(classLimitMap.get(taskClass.getId()) != null){
  253. if(number > classLimitMap.get(taskClass.getId())){
  254. number = classLimitMap.get(taskClass.getId());
  255. }
  256. }
  257. Integer maleCount = 0, femaleCount = 0;
  258. if(ruleCodes.contains("BR0001")){
  259. IdManyCountVo countVo = majorStudentCountMap.get(taskClass.getMajorSetId());
  260. maleCount = number / 2;
  261. femaleCount = number / 2;
  262. //如果班级人数是奇数,随机分配一个名额
  263. if(number % 2 != 0){
  264. Random random = new Random();
  265. int randomIndex = random.nextInt(GenderDictionaryEnum.getCodes().length);
  266. String randomGender = GenderDictionaryEnum.getCodes()[randomIndex];
  267. if(GenderDictionaryEnum.MALE.getCode().equals(randomGender)){
  268. maleCount ++;
  269. }else if(GenderDictionaryEnum.FEMALE.getCode().equals(randomGender)){
  270. femaleCount ++ ;
  271. }
  272. }
  273. if(majorMaleCountMap.get(taskClass.getMajorSetId()) < maleCount){
  274. maleCount = countVo.getMaleCount();
  275. femaleCount = number - maleCount;
  276. }else if(majorFemaleCountMap.get(taskClass.getMajorSetId()) < femaleCount){
  277. femaleCount = countVo.getFemaleCount();
  278. maleCount = number - femaleCount;
  279. }
  280. }
  281. List<String> nameList = new ArrayList<>();
  282. List<BaseNewStudent> maleList = new ArrayList();//男生
  283. List<BaseNewStudent> femaleList = new ArrayList();//女生
  284. List<BaseNewStudent> studentList = new ArrayList<>();
  285. studentList.addAll(baseNewStudents);
  286. if(ruleCodes.contains("BR0002") && classStudentMap.get(taskClass.getMajorSetId()) != null && !classStudentMap.get(taskClass.getMajorSetId()).isEmpty()){
  287. studentList.clear();
  288. studentList.addAll(classStudentMap.get(taskClass.getMajorSetId()));
  289. }
  290. for (BaseNewStudent newStudent : studentList) {
  291. //人数已满,进行下一个班级的的循环
  292. if(nameList.size() == number){
  293. break;
  294. }
  295. //该学生已被分配
  296. if(studentClassMap.containsKey(newStudent.getId())){
  297. continue;
  298. }
  299. if(ruleCodes.contains("BR0003") && nameList.contains(newStudent.getName())){
  300. continue;
  301. }
  302. //专业不匹配,直接跳过
  303. if(!Objects.equals(taskClass.getMajorSetId(), newStudent.getFirstAmbitionId()) && !Objects.equals(taskClass.getMajorSetId(), newStudent.getSecondAmbitionId())){
  304. continue;
  305. }
  306. //判断该班性别是否已满,如果设置了排序,即使性别满了班级人数没满继续分班
  307. if(ruleCodes.contains("BR0001")){
  308. if(GenderDictionaryEnum.MALE.getCode().equals(newStudent.getGender()) && maleList.size() == maleCount){
  309. continue;
  310. }else if(GenderDictionaryEnum.FEMALE.getCode().equals(newStudent.getGender()) && femaleList.size() == femaleCount){
  311. continue;
  312. }
  313. }
  314. if(taskClass.getSortCode() == null && !Objects.equals(taskClass.getMajorSetId(), newStudent.getFirstAmbitionId())){//如果未给班级设置优先级,只匹配一志愿的学生
  315. continue;
  316. }
  317. List<Boolean> conditionList = new ArrayList<>();
  318. BandingTaskMajorCondition condition = classConditionMap.get(taskClass.getMajorSetId());
  319. if(condition != null){
  320. if(condition.getHeight() !=null && newStudent.getHeight() != null && newStudent.getHeight().compareTo(condition.getHeight()) >= 0 ){
  321. conditionList.add(true);
  322. }else if(condition.getHeight() !=null && newStudent.getHeight() != null && newStudent.getHeight().compareTo(condition.getHeight()) < 0){
  323. conditionList.add(false);
  324. }else if(condition.getHeight() !=null && newStudent.getHeight() == null){
  325. conditionList.add(false);
  326. }
  327. if(condition.getScore() !=null && newStudent.getScore() != null && newStudent.getScore().compareTo(condition.getScore()) >= 0 ){
  328. conditionList.add(true);
  329. }else if(condition.getScore() !=null && newStudent.getScore() != null && newStudent.getScore().compareTo(condition.getScore()) < 0){
  330. conditionList.add(false);
  331. }else if(condition.getScore() !=null && newStudent.getScore() == null){
  332. conditionList.add(false);
  333. }
  334. }
  335. //如果包含false,则表明不符合条件,这个学生跳过
  336. if(conditionList.contains(false)){
  337. continue;
  338. }
  339. studentClassMap.put(newStudent.getId(), taskClass.getId());
  340. if(GenderDictionaryEnum.MALE.getCode().equals(newStudent.getGender())){
  341. maleList.add(newStudent);
  342. }else if(GenderDictionaryEnum.FEMALE.getCode().equals(newStudent.getGender())){
  343. femaleList.add(newStudent);
  344. }
  345. nameList.add(newStudent.getName());
  346. }
  347. classNameMap.put(taskClass.getId(), nameList);
  348. }
  349. // 4.1、二次循环班级,对没有设置排序的班级进行分班,
  350. for (BandingTaskClass taskClass : classList) {
  351. Integer number = taskClass.getNumber();//班级总人数
  352. if(classLimitMap.get(taskClass.getId()) != null){
  353. if(number > classLimitMap.get(taskClass.getId())){
  354. number = classLimitMap.get(taskClass.getId());
  355. }
  356. }
  357. List<String> nameList = classNameMap.get(taskClass.getId());
  358. if(nameList.size() == number){
  359. continue;
  360. }
  361. for (BaseNewStudent newStudent : baseNewStudents) {
  362. //人数已满,进行下一个班级的的循环
  363. if(nameList.size() == number){
  364. break;
  365. }
  366. //该学生已被分配
  367. if(studentClassMap.containsKey(newStudent.getId())){
  368. continue;
  369. }
  370. if(ruleCodes.contains("BR0003") && nameList.contains(newStudent.getName())){
  371. continue;
  372. }
  373. //专业不匹配,直接跳过
  374. if(!Objects.equals(taskClass.getMajorSetId(), newStudent.getFirstAmbitionId()) && !Objects.equals(taskClass.getMajorSetId(), newStudent.getSecondAmbitionId())){
  375. continue;
  376. }
  377. //判断该班性别是否已满
  378. List<Boolean> conditionList = new ArrayList<>();
  379. BandingTaskMajorCondition condition = classConditionMap.get(taskClass.getMajorSetId());
  380. if(condition != null){
  381. if(condition.getHeight() !=null && newStudent.getHeight() != null && newStudent.getHeight().compareTo(condition.getHeight()) >= 0 ){
  382. conditionList.add(true);
  383. }else if(condition.getHeight() !=null && newStudent.getHeight() != null && newStudent.getHeight().compareTo(condition.getHeight()) < 0){
  384. conditionList.add(false);
  385. }else if(condition.getHeight() !=null && newStudent.getHeight() == null){
  386. conditionList.add(false);
  387. }
  388. if(condition.getScore() !=null && newStudent.getScore() != null && newStudent.getScore().compareTo(condition.getScore()) >= 0 ){
  389. conditionList.add(true);
  390. }else if(condition.getScore() !=null && newStudent.getScore() != null && newStudent.getScore().compareTo(condition.getScore()) < 0){
  391. conditionList.add(false);
  392. }else if(condition.getScore() !=null && newStudent.getScore() == null){
  393. conditionList.add(false);
  394. }
  395. }
  396. //如果包含false,则表明不符合条件,这个学生跳过
  397. if(conditionList.contains(false)){
  398. continue;
  399. }
  400. studentClassMap.put(newStudent.getId(), taskClass.getId());
  401. nameList.add(newStudent.getName());
  402. }
  403. }
  404. List<BandingTaskClassStudent> dataList = new ArrayList<>();
  405. Date createDate = new Date();
  406. for (Long studentId : studentClassMap.keySet()) {
  407. dataList.add(
  408. new BandingTaskClassStudent(){{
  409. setBandingTaskClassId(studentClassMap.get(studentId));
  410. setNewStudentId(studentId);
  411. setStatus(0);
  412. setCreateDate(createDate);
  413. }}
  414. );
  415. }
  416. if(!dataList.isEmpty()){
  417. classStudentService.saveBatch(dataList);
  418. }
  419. return true;
  420. }
  421. /**
  422. * 按照成绩均衡划分学生
  423. * @return 班级id和学生
  424. */
  425. Map<Long, List<BaseNewStudent>> divideStudentByScore(Map<Long, BandingTaskMajorCondition> classConditionMap, List<BaseNewStudent> baseNewStudents,
  426. List<BandingTaskClass> classList){
  427. Map<Long, List<BaseNewStudent>> classStudentMap = new HashMap<>();
  428. for (Long majorSetId : classConditionMap.keySet()) {
  429. //查询该专业下面有几个班级,把这部分学生按照成绩均匀分组
  430. List<List<BaseNewStudent>> result = new ArrayList<>();
  431. for (int i = 0; i < classList.size(); i++) {
  432. result.add(new ArrayList<>());
  433. }
  434. if(result.size() == 1){
  435. continue;
  436. }
  437. // 1、先把每个专业匹配的学生分组存一起,并按照分数高低排序
  438. List<BaseNewStudent> stuList = new ArrayList<>();
  439. for (BaseNewStudent newStudent : baseNewStudents) {
  440. if(!Objects.equals(majorSetId, newStudent.getFirstAmbitionId()) && !Objects.equals(majorSetId, newStudent.getSecondAmbitionId())){
  441. continue;
  442. }
  443. List<Boolean> conditionList = new ArrayList<>();
  444. BandingTaskMajorCondition condition = classConditionMap.get(majorSetId);
  445. if(condition.getHeight() !=null && newStudent.getHeight() != null && newStudent.getHeight().compareTo(condition.getHeight()) >= 0 ){
  446. conditionList.add(true);
  447. }else if(condition.getHeight() !=null && newStudent.getHeight() != null && newStudent.getHeight().compareTo(condition.getHeight()) < 0){
  448. conditionList.add(false);
  449. }else if(condition.getHeight() !=null && newStudent.getHeight() == null){
  450. conditionList.add(false);
  451. }
  452. if(condition.getScore() !=null && newStudent.getScore() != null && newStudent.getScore().compareTo(condition.getScore()) >= 0 ){
  453. conditionList.add(true);
  454. }else if(condition.getScore() !=null && newStudent.getScore() != null && newStudent.getScore().compareTo(condition.getScore()) < 0){
  455. conditionList.add(false);
  456. }else if(condition.getScore() !=null && newStudent.getScore() == null){
  457. conditionList.add(false);
  458. }
  459. //如果包含false,则表明不符合条件,这个学生跳过
  460. if(conditionList.contains(false)){
  461. continue;
  462. }
  463. stuList.add(newStudent);
  464. }
  465. if(!stuList.isEmpty()){
  466. for (BaseNewStudent student : stuList) {
  467. if(student.getScore() == null){
  468. student.setScore(BigDecimal.ZERO);
  469. }
  470. }
  471. Collections.sort(stuList, (s1, s2) -> (int) (s2.getScore().doubleValue() - s1.getScore().doubleValue())); //按照成绩降序排序
  472. }
  473. for (int i = 0; i < stuList.size(); i++) {
  474. BaseNewStudent currentStudent = stuList.get(i);
  475. int classIndex = i % result.size(); //分配班级
  476. result.get(classIndex).add(currentStudent);
  477. }
  478. for (int i = 0; i < result.size(); i ++){
  479. classStudentMap.put(classList.get(i).getId(), result.get(i));
  480. }
  481. }
  482. return classStudentMap;
  483. }
  484. @Transactional
  485. @Override
  486. public Boolean sure(SureBandingTaskDto dto) {
  487. List<BandingTaskClassStudent> classStudents = classStudentService.selectJoinList(BandingTaskClassStudent.class,
  488. new MPJLambdaWrapper<BandingTaskClassStudent>()
  489. .select(BandingTaskClassStudent::getId)
  490. .select(BandingTaskClassStudent.class, x -> VoToColumnUtil.fieldsToColumns(BandingTaskClassStudent.class).contains(x.getProperty()))
  491. .leftJoin(BandingTaskClass.class, BandingTaskClass::getId, BandingTaskClassStudent::getBandingTaskClassId)
  492. .eq(BandingTaskClass::getBandingTaskId, dto.getId())
  493. .eq(BandingTaskClassStudent::getStatus, 0)
  494. );
  495. List<Long> studentIds = classStudents.stream().map(BandingTaskClassStudent::getNewStudentId).collect(Collectors.toList());
  496. if(studentIds.isEmpty()){
  497. throw new MyException("未能查询到学生,无法确认");
  498. }
  499. List<BaseNewStudent> list = newStudentService.list(
  500. new QueryWrapper<BaseNewStudent>().lambda()
  501. .in(BaseNewStudent::getId, studentIds)
  502. );
  503. List<BaseNewStudent> updateList = new ArrayList<>();
  504. for (BaseNewStudent student : list) {
  505. student.setStatus(1);
  506. updateList.add(student);
  507. }
  508. if(!updateList.isEmpty()){
  509. newStudentService.updateBatchById(updateList);
  510. }
  511. //形成学生数据
  512. {
  513. Date createDate = new Date();
  514. BandingTask bandingTask = this.getById(dto.getId());
  515. List<BandingTaskClassSureListVo> classSure = taskClassMapper.getClassSure(new BandingTaskClassStudentPageDto() {{
  516. setBandingTaskId(dto.getId());
  517. }});
  518. Map<Long, Integer> classTotal = classSure.stream().collect(Collectors.toMap(BandingTaskClassSureListVo::getId, BandingTaskClassSureListVo::getNumber));
  519. Map<Long, Integer> classBoy = classSure.stream().collect(Collectors.toMap(BandingTaskClassSureListVo::getId, BandingTaskClassSureListVo::getMaleCount));
  520. Map<Long, Integer> classGirl = classSure.stream().collect(Collectors.toMap(BandingTaskClassSureListVo::getId, BandingTaskClassSureListVo::getFemaleCount));
  521. //生成班级数据
  522. Map<Long, Long> classMap = new HashMap<>();
  523. Map<Long, Long> taskClassMajorMap = new HashMap<>();
  524. //查询出需要新增的班级信息
  525. List<Long> classIds = classStudents.stream().map(BandingTaskClassStudent::getBandingTaskClassId).collect(Collectors.toList());
  526. List<BandingTaskClass> classList = taskClassMapper.selectList(new QueryWrapper<BandingTaskClass>().lambda().in(BandingTaskClass::getId, classIds));
  527. long maxCode = classService.count(
  528. new QueryWrapper<BaseClass>().lambda()
  529. .eq(BaseClass::getGradeId, bandingTask.getGradeId())
  530. );
  531. BaseGrade baseGrade = gradeService.getById(bandingTask.getGradeId());
  532. String gradeCode = baseGrade.getTitle().replace("年", "");
  533. Map<Long, Long> majorDeptMap = majorSetMapper.selectList(new QueryWrapper<BaseMajorSet>()).stream().collect(Collectors.toMap(BaseMajorSet::getId, BaseMajorSet::getDepartmentId));
  534. for (BandingTaskClass taskClass : classList) {
  535. long finalMaxCode = maxCode;
  536. BaseClass baseClass = new BaseClass() {{
  537. setName(taskClass.getName());
  538. setClassroomId(taskClass.getClassroomId());
  539. setTeacherId(taskClass.getTeacherId());
  540. setIsGraduate(1);
  541. setIsOrderClass(taskClass.getIsOrderClass()==null?0:taskClass.getIsOrderClass().intValue());
  542. setGradeId(bandingTask.getGradeId());
  543. setDeleteMark(DeleteMark.NODELETE.getCode());
  544. setEnrollType(bandingTask.getEnrollType());
  545. setOrgId(majorDeptMap.get(taskClass.getMajorSetId()));
  546. setCode(gradeCode + String.format("%03d", finalMaxCode));
  547. setMajorSetId(taskClass.getMajorSetId());
  548. }};
  549. classService.save(baseClass);
  550. BaseClassMajorSet majorSet = new BaseClassMajorSet() {{
  551. setCreateDate(createDate);
  552. setMajorSetId(taskClass.getMajorSetId());
  553. setClassId(baseClass.getId());
  554. setPlanTotalStudent(taskClass.getNumber());
  555. setTotalStudent(classTotal.get(taskClass.getId()));
  556. setBoyNum(classBoy.get(taskClass.getId()));
  557. setGirlNum(classGirl.get(taskClass.getId()));
  558. }};
  559. classMajorSetMapper.insert(majorSet);
  560. classMap.put(taskClass.getId(), baseClass.getId());
  561. taskClassMajorMap.put(taskClass.getId(), taskClass.getMajorSetId());
  562. maxCode ++;
  563. }
  564. /**
  565. * 新增学生数据
  566. * 1、新增用户xjr_user
  567. * 2、新增用户与角色的关系xjr_user_role_relation
  568. * 3、新增学生基本信息base_student
  569. * 4、新增学籍信息表base_student_school_roll
  570. * 5、新增家庭信息表base_student_family
  571. */
  572. LocalDateTime now = LocalDateTime.now();
  573. Map<Long, Long> studentClassRelation = classStudents.stream().collect(Collectors.toMap(BandingTaskClassStudent::getNewStudentId, BandingTaskClassStudent::getBandingTaskClassId));
  574. for (BaseNewStudent student : updateList) {
  575. LocalDate birthDate = getBirthDate(student.getCredentialNumber());
  576. User xjrUser = new User() {{
  577. setCreateDate(now);
  578. setPassword(BCrypt.hashpw(propertiesConfig.getDefaultPassword(), BCrypt.gensalt()));
  579. setName(student.getName());
  580. setUserName(student.getCredentialNumber());
  581. setCredentialNumber(student.getCredentialNumber());
  582. setCredentialType("ZZLS10007");
  583. setMobile(student.getMobile());
  584. setEnabledMark(EnabledMark.DISABLED.getCode());
  585. setGender(student.getGender());
  586. setIsChangePassword(1);
  587. setBirthDate(birthDate.atStartOfDay());
  588. }};
  589. userService.save(xjrUser);
  590. UserRoleRelation userRoleRelation = new UserRoleRelation() {{
  591. setRoleId(RoleEnum.STUDENT.getCode());
  592. setUserId(xjrUser.getId());
  593. }};
  594. roleRelationService.save(userRoleRelation);
  595. BaseStudent baseStudent = new BaseStudent() {{
  596. setUserId(xjrUser.getId());
  597. setCreateDate(now);
  598. setStudentId(student.getCredentialNumber());
  599. setHeight(student.getHeight().doubleValue());
  600. setWeight(student.getWeight().doubleValue());
  601. }};
  602. studentService.save(baseStudent);
  603. BaseStudentSchoolRoll schoolRoll = new BaseStudentSchoolRoll() {{
  604. setUserId(xjrUser.getId());
  605. if(student.getScore() != null){
  606. setGraduatedScore(student.getScore().doubleValue());
  607. }
  608. setGraduatedUniversity(student.getGraduateSchool());
  609. setClassId(classMap.get(studentClassRelation.get(student.getId())));
  610. setMajorSetId(taskClassMajorMap.get(studentClassRelation.get(student.getId())));
  611. setStduyStatus(student.getStduyStatus());
  612. setEnrollType(bandingTask.getEnrollType());
  613. setStudentSource(student.getSource());
  614. setGradeId(bandingTask.getGradeId());
  615. setArchivesStatus(ArchivesStatusEnum.FB2901.getCode());
  616. setCreateDate(now);
  617. }};
  618. schoolRollService.save(schoolRoll);
  619. BaseStudentFamily studentFamily = new BaseStudentFamily() {{
  620. setCreateDate(now);
  621. setUserId(xjrUser.getId());
  622. setTelephone(student.getFamilyMobile());
  623. setAddress(student.getFamilyAddress());
  624. }};
  625. familyService.save(studentFamily);
  626. }
  627. }
  628. BandingTask bandingTask = this.getById(dto.getId());
  629. bandingTask.setStatus(1);
  630. bandingTask.setModifyDate(new Date());
  631. Boolean isSuccess = this.update(bandingTask);
  632. return isSuccess;
  633. }
  634. @Override
  635. public Page<BandingTaskPageVo> getPage(Page<BandingTaskPageVo> page, BandingTaskPageDto dto) {
  636. return this.baseMapper.getPage(page, dto);
  637. }
  638. LocalDate getBirthDate(String idCardNumber){
  639. // 获取出生日期前6位,即yyyyMM
  640. String birthdayString = idCardNumber.substring(6, 14);
  641. // 将字符串解析为LocalDate对象
  642. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
  643. try {
  644. LocalDate parse = LocalDate.parse(birthdayString, formatter);
  645. return parse;
  646. }catch (Exception e){
  647. throw new MyException("身份证号填写错误,无法提取出生日期");
  648. }
  649. }
  650. }