BandingTaskServiceImpl.java 42 KB

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