BandingTaskServiceImpl.java 41 KB

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