BandingTaskClassStudentServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package com.xjrsoft.module.banding.service.impl;
  2. import cn.dev33.satoken.secure.BCrypt;
  3. import cn.dev33.satoken.stp.StpUtil;
  4. import cn.hutool.core.util.IdUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  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.RoleEnum;
  14. import com.xjrsoft.common.exception.MyException;
  15. import com.xjrsoft.common.utils.RedisUtil;
  16. import com.xjrsoft.config.CommonPropertiesConfig;
  17. import com.xjrsoft.module.banding.dto.BandingTaskClassStudentPageDto;
  18. import com.xjrsoft.module.banding.dto.ChangeClassDto;
  19. import com.xjrsoft.module.banding.dto.StudentDto;
  20. import com.xjrsoft.module.banding.entity.BandingTask;
  21. import com.xjrsoft.module.banding.entity.BandingTaskClass;
  22. import com.xjrsoft.module.banding.entity.BandingTaskClassStudent;
  23. import com.xjrsoft.module.banding.mapper.BandingTaskClassMapper;
  24. import com.xjrsoft.module.banding.mapper.BandingTaskClassStudentMapper;
  25. import com.xjrsoft.module.banding.mapper.BandingTaskMapper;
  26. import com.xjrsoft.module.banding.service.IBandingTaskClassStudentService;
  27. import com.xjrsoft.module.banding.vo.BandingTaskClassSureListVo;
  28. import com.xjrsoft.module.base.entity.BaseClass;
  29. import com.xjrsoft.module.base.entity.BaseMajorSet;
  30. import com.xjrsoft.module.base.mapper.BaseMajorSetMapper;
  31. import com.xjrsoft.module.base.service.IBaseClassService;
  32. import com.xjrsoft.module.organization.entity.User;
  33. import com.xjrsoft.module.organization.entity.UserRoleRelation;
  34. import com.xjrsoft.module.organization.service.IUserRoleRelationService;
  35. import com.xjrsoft.module.organization.service.IUserService;
  36. import com.xjrsoft.module.student.dto.BaseNewStudentPageDto;
  37. import com.xjrsoft.module.student.entity.BaseClassMajorSet;
  38. import com.xjrsoft.module.student.entity.BaseNewStudent;
  39. import com.xjrsoft.module.student.entity.BaseStudent;
  40. import com.xjrsoft.module.student.entity.BaseStudentFamily;
  41. import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
  42. import com.xjrsoft.module.student.mapper.BaseClassMajorSetMapper;
  43. import com.xjrsoft.module.student.service.IBaseNewStudentService;
  44. import com.xjrsoft.module.student.service.IBaseStudentFamilyService;
  45. import com.xjrsoft.module.student.service.IBaseStudentSchoolRollService;
  46. import com.xjrsoft.module.student.service.IBaseStudentService;
  47. import lombok.AllArgsConstructor;
  48. import org.springframework.stereotype.Service;
  49. import org.springframework.transaction.annotation.Transactional;
  50. import java.time.LocalDate;
  51. import java.time.LocalDateTime;
  52. import java.time.format.DateTimeFormatter;
  53. import java.util.ArrayList;
  54. import java.util.Date;
  55. import java.util.HashMap;
  56. import java.util.List;
  57. import java.util.Map;
  58. import java.util.concurrent.CompletableFuture;
  59. import java.util.stream.Collectors;
  60. /**
  61. * @title: 新生分班任务
  62. * @Author dzx
  63. * @Date: 2024-07-01
  64. * @Version 1.0
  65. */
  66. @Service
  67. @AllArgsConstructor
  68. public class BandingTaskClassStudentServiceImpl extends MPJBaseServiceImpl<BandingTaskClassStudentMapper, BandingTaskClassStudent> implements IBandingTaskClassStudentService {
  69. private final BandingTaskClassMapper taskClassMapper;
  70. private final BandingTaskMapper bandingTaskMapper;
  71. private final IBaseNewStudentService newStudentService;
  72. private final IBaseClassService classService;
  73. private final BaseClassMajorSetMapper classMajorSetMapper;
  74. private final CommonPropertiesConfig propertiesConfig;
  75. private final IUserService userService;
  76. private final IUserRoleRelationService roleRelationService;
  77. private final IBaseStudentSchoolRollService schoolRollService;
  78. private final IBaseStudentService studentService;
  79. private final IBaseStudentFamilyService familyService;
  80. private final BaseMajorSetMapper majorSetMapper;
  81. private final RedisUtil redisUtil;
  82. @Override
  83. public Boolean add(BandingTaskClassStudent bandingTaskClass) {
  84. bandingTaskClass.setCreateDate(new Date());
  85. this.baseMapper.insert(bandingTaskClass);
  86. return true;
  87. }
  88. @Override
  89. public Boolean update(BandingTaskClassStudent bandingTaskClass) {
  90. bandingTaskClass.setModifyDate(new Date());
  91. this.baseMapper.updateById(bandingTaskClass);
  92. return true;
  93. }
  94. @Override
  95. @Transactional(rollbackFor = Exception.class)
  96. public Boolean delete(List<Long> ids) {
  97. this.baseMapper.deleteBatchIds(ids);
  98. return true;
  99. }
  100. @Transactional
  101. @Override
  102. public Boolean changeClass(ChangeClassDto dto) {
  103. this.baseMapper.delete(
  104. new QueryWrapper<BandingTaskClassStudent>().lambda()
  105. .in(BandingTaskClassStudent::getNewStudentId, dto.getNewStudentIds())
  106. );
  107. List<BandingTaskClassStudent> dataList = new ArrayList<>();
  108. long createUserId = StpUtil.getLoginIdAsLong();
  109. for (Long newStudentId : dto.getNewStudentIds()) {
  110. dataList.add(
  111. new BandingTaskClassStudent(){{
  112. setCreateDate(new Date());
  113. setNewStudentId(newStudentId);
  114. setBandingTaskClassId(dto.getBandingTaskClassId());
  115. setCreateUserId(createUserId);
  116. setStatus(0);
  117. if(dto.getIsHandle() != null && dto.getIsHandle() == 1){
  118. setStatus(1);
  119. }
  120. }}
  121. );
  122. }
  123. if(!dataList.isEmpty()){
  124. this.saveBatch(dataList);
  125. }
  126. if(dto.getIsHandle() != null && dto.getIsHandle() == 1){
  127. BandingTaskClass taskClass = taskClassMapper.selectById(dto.getBandingTaskClassId());
  128. List<Long> studentIds = dataList.stream().map(BandingTaskClassStudent::getNewStudentId).collect(Collectors.toList());
  129. List<BaseNewStudent> list = newStudentService.list(
  130. new QueryWrapper<BaseNewStudent>().lambda()
  131. .in(BaseNewStudent::getId, studentIds)
  132. );
  133. List<BaseNewStudent> updateList = new ArrayList<>();
  134. for (BaseNewStudent student : list) {
  135. student.setStatus(1);
  136. updateList.add(student);
  137. }
  138. if(!updateList.isEmpty()){
  139. newStudentService.updateBatchById(updateList);
  140. }
  141. {
  142. Date createDate = new Date();
  143. BandingTask bandingTask = bandingTaskMapper.selectById(taskClass.getBandingTaskId());
  144. List<BandingTaskClassSureListVo> classSure = taskClassMapper.getClassSure(new BandingTaskClassStudentPageDto() {{
  145. setBandingTaskId(taskClass.getBandingTaskId());
  146. }});
  147. Map<Long, Integer> classTotal = classSure.stream().collect(Collectors.toMap(BandingTaskClassSureListVo::getId, BandingTaskClassSureListVo::getNumber));
  148. Map<Long, Integer> classBoy = classSure.stream().collect(Collectors.toMap(BandingTaskClassSureListVo::getId, BandingTaskClassSureListVo::getMaleCount));
  149. Map<Long, Integer> classGirl = classSure.stream().collect(Collectors.toMap(BandingTaskClassSureListVo::getId, BandingTaskClassSureListVo::getFemaleCount));
  150. //生成班级数据
  151. Map<Long, Long> classMap = new HashMap<>();
  152. Map<Long, Long> taskClassMajorMap = new HashMap<>();
  153. //查询出需要新增的班级信息
  154. Map<Long, Long> majorDeptMap = majorSetMapper.selectList(
  155. new QueryWrapper<BaseMajorSet>()
  156. ).stream().collect(Collectors.toMap(BaseMajorSet::getId, BaseMajorSet::getDepartmentId));
  157. //先查询是否已经存在这个班级,如果存在就更新,不存在就新增
  158. BaseClass baseClass = classService.getOne(
  159. new QueryWrapper<BaseClass>().lambda()
  160. .eq(BaseClass::getName, taskClass.getName())
  161. .eq(BaseClass::getGradeId, bandingTask.getGradeId())
  162. .eq(BaseClass::getEnrollType, bandingTask.getEnrollType())
  163. );
  164. if(baseClass == null){
  165. baseClass = new BaseClass() {{
  166. setName(taskClass.getName());
  167. setClassroomId(taskClass.getClassroomId());
  168. setTeacherId(taskClass.getTeacherId());
  169. setIsGraduate(1);
  170. setMajorSetId(taskClass.getMajorSetId());
  171. setOrgId(majorDeptMap.get(taskClass.getMajorSetId()));
  172. setIsOrderClass(taskClass.getIsOrderClass()==null?0:taskClass.getIsOrderClass().intValue());
  173. setGradeId(bandingTask.getGradeId());
  174. setDeleteMark(DeleteMark.NODELETE.getCode());
  175. setEnrollType(bandingTask.getEnrollType());
  176. setCreateDate(new Date());
  177. }};
  178. classService.save(baseClass);
  179. BaseClassMajorSet majorSet = new BaseClassMajorSet() {{
  180. setCreateDate(createDate);
  181. setMajorSetId(taskClass.getMajorSetId());
  182. setPlanTotalStudent(taskClass.getNumber());
  183. setTotalStudent(classTotal.get(taskClass.getId()));
  184. setBoyNum(classBoy.get(taskClass.getId()));
  185. setGirlNum(classGirl.get(taskClass.getId()));
  186. }};
  187. majorSet.setClassId(baseClass.getId());
  188. classMajorSetMapper.insert(majorSet);
  189. }
  190. classMap.put(taskClass.getId(), baseClass.getId());
  191. taskClassMajorMap.put(taskClass.getId(), taskClass.getMajorSetId());
  192. /**
  193. * 新增学生数据
  194. * 1、新增用户xjr_user,需要先查询用户信息是否已经存在
  195. * 2、新增用户与角色的关系xjr_user_role_relation
  196. * 3、新增学生基本信息base_student
  197. * 4、新增学籍信息表base_student_school_roll
  198. * 5、新增家庭信息表base_student_family
  199. */
  200. LocalDateTime now = LocalDateTime.now();
  201. List<String> idNumbers = updateList.stream().map(BaseNewStudent::getCredentialNumber).collect(Collectors.toList());
  202. List<User> students = userService.list(
  203. new MPJLambdaWrapper<User>()
  204. .disableLogicDel()
  205. .in(User::getCredentialNumber, idNumbers)
  206. .orderByAsc(User::getCreateDate)
  207. );
  208. Map<String, Long> userSet = new HashMap<>();
  209. Map<String, Integer> userDeleteMarkMap = new HashMap<>();
  210. for (User student : students) {
  211. userSet.put(student.getCredentialNumber(), student.getId());
  212. userDeleteMarkMap.put(student.getCredentialNumber(), student.getDeleteMark());
  213. }
  214. Map<Long, Long> studentClassRelation = dataList.stream().collect(Collectors.toMap(BandingTaskClassStudent::getNewStudentId, BandingTaskClassStudent::getBandingTaskClassId));
  215. for (BaseNewStudent student : updateList) {
  216. if(userSet.containsKey(student.getCredentialNumber())){
  217. schoolRollService.updateStudentClass(classMap.get(studentClassRelation.get(student.getId())), userSet.get(student.getCredentialNumber()));
  218. Integer deleteMark = userDeleteMarkMap.get(student.getCredentialNumber());
  219. if(deleteMark != null && deleteMark == 1){
  220. userService.recoveryStudentInfo(userSet.get(student.getCredentialNumber()));
  221. }
  222. continue;
  223. }
  224. LocalDate birthDate = getBirthDate(student.getCredentialNumber());
  225. User xjrUser = new User() {{
  226. setCreateDate(now);
  227. setPassword(BCrypt.hashpw(propertiesConfig.getDefaultPassword(), BCrypt.gensalt()));
  228. setName(student.getName());
  229. setUserName(student.getCredentialNumber());
  230. setCredentialNumber(student.getCredentialNumber());
  231. setCredentialType("ZZLS10007");
  232. setMobile(student.getMobile());
  233. setEnabledMark(EnabledMark.DISABLED.getCode());
  234. setGender(student.getGender());
  235. setIsChangePassword(1);
  236. setBirthDate(birthDate.atStartOfDay());
  237. }};
  238. userService.save(xjrUser);
  239. UserRoleRelation userRoleRelation = new UserRoleRelation() {{
  240. setRoleId(RoleEnum.STUDENT.getCode());
  241. setUserId(xjrUser.getId());
  242. }};
  243. roleRelationService.save(userRoleRelation);
  244. BaseStudent baseStudent = new BaseStudent() {{
  245. setUserId(xjrUser.getId());
  246. setCreateDate(now);
  247. setStudentId(student.getCredentialNumber());
  248. if(student.getHeight() != null){
  249. setHeight(student.getHeight().doubleValue());
  250. }
  251. if(student.getWeight() != null){
  252. setWeight(student.getWeight().doubleValue());
  253. }
  254. }};
  255. studentService.save(baseStudent);
  256. BaseStudentSchoolRoll schoolRoll = new BaseStudentSchoolRoll() {{
  257. setUserId(xjrUser.getId());
  258. if(student.getScore() != null){
  259. setGraduatedScore(student.getScore().doubleValue());
  260. }
  261. setGraduatedUniversity(student.getGraduateSchool());
  262. setClassId(classMap.get(studentClassRelation.get(student.getId())));
  263. setMajorSetId(taskClassMajorMap.get(studentClassRelation.get(student.getId())));
  264. setStduyStatus(student.getStduyStatus());
  265. setEnrollType(bandingTask.getEnrollType());
  266. setStudentSource(student.getSource());
  267. setGradeId(bandingTask.getGradeId());
  268. setArchivesStatus(ArchivesStatusEnum.FB2901.getCode());
  269. setCreateDate(now);
  270. }};
  271. schoolRollService.save(schoolRoll);
  272. BaseStudentFamily studentFamily = new BaseStudentFamily() {{
  273. setCreateDate(now);
  274. setUserId(xjrUser.getId());
  275. setTelephone(student.getFamilyMobile());
  276. setAddress(student.getFamilyAddress());
  277. }};
  278. familyService.save(studentFamily);
  279. }
  280. }
  281. CompletableFuture.runAsync(() -> {
  282. List<User> userList = userService.list();
  283. redisUtil.set(GlobalConstant.USER_CACHE_KEY, userList);
  284. List<UserRoleRelation> userRoleRelationList = roleRelationService.list(Wrappers.lambdaQuery(UserRoleRelation.class));
  285. redisUtil.set(GlobalConstant.USER_ROLE_RELATION_CACHE_KEY, userRoleRelationList);
  286. });
  287. }
  288. return true;
  289. }
  290. LocalDate getBirthDate(String idCardNumber){
  291. // 获取出生日期前6位,即yyyyMM
  292. String birthdayString = idCardNumber.substring(6, 14);
  293. // 将字符串解析为LocalDate对象
  294. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
  295. try {
  296. LocalDate parse = LocalDate.parse(birthdayString, formatter);
  297. return parse;
  298. }catch (Exception e){
  299. throw new MyException("身份证号填写错误,无法提取出生日期");
  300. }
  301. }
  302. @Override
  303. public Boolean removeStudent(ChangeClassDto dto) {
  304. this.baseMapper.delete(
  305. new QueryWrapper<BandingTaskClassStudent>().lambda()
  306. .in(BandingTaskClassStudent::getNewStudentId, dto.getNewStudentIds())
  307. .eq(BandingTaskClassStudent::getBandingTaskClassId, dto.getBandingTaskClassId())
  308. );
  309. return true;
  310. }
  311. @Override
  312. public List<BaseNewStudentPageDto> satisfyStudent(StudentDto dto) {
  313. BandingTaskClass taskClass = taskClassMapper.selectById(dto.getBandingTaskClassId());
  314. dto.setBandingTaskId(taskClass.getBandingTaskId());
  315. return this.baseMapper.satisfyStudent(dto);
  316. }
  317. @Override
  318. public List<BaseNewStudentPageDto> surplusStudent(StudentDto dto) {
  319. BandingTaskClass taskClass = taskClassMapper.selectById(dto.getBandingTaskClassId());
  320. dto.setMajorSetId(taskClass.getMajorSetId());
  321. dto.setBandingTaskId(taskClass.getBandingTaskId());
  322. return this.baseMapper.surplusStudent(dto);
  323. }
  324. @Override
  325. public Boolean insertStudent(ChangeClassDto dto) {
  326. List<BandingTaskClassStudent> dataList = new ArrayList<>();
  327. Date createDate = new Date();
  328. for (Long newStudentId : dto.getNewStudentIds()) {
  329. dataList.add(
  330. new BandingTaskClassStudent(){{
  331. setNewStudentId(newStudentId);
  332. setCreateDate(createDate);
  333. setStatus(0);
  334. setBandingTaskClassId(dto.getBandingTaskClassId());
  335. }}
  336. );
  337. }
  338. if(!dataList.isEmpty()){
  339. this.saveBatch(dataList);
  340. }
  341. return true;
  342. }
  343. }