BandingTaskServiceImpl.java 35 KB

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