ExamSubjectSongServiceImpl.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package com.keao.edu.user.service.impl;
  2. import com.keao.edu.common.dal.BaseDAO;
  3. import com.keao.edu.common.exception.BizException;
  4. import com.keao.edu.common.page.PageInfo;
  5. import com.keao.edu.common.page.QueryInfo;
  6. import com.keao.edu.common.service.impl.BaseServiceImpl;
  7. import com.keao.edu.common.tenant.TenantContextHolder;
  8. import com.keao.edu.user.dao.ExamSongDao;
  9. import com.keao.edu.user.dao.ExamSubjectDao;
  10. import com.keao.edu.user.dao.ExamSubjectSongDao;
  11. import com.keao.edu.user.dao.ExaminationBasicDao;
  12. import com.keao.edu.user.dto.ExamSubjectSongDto;
  13. import com.keao.edu.user.entity.ExamSong;
  14. import com.keao.edu.user.entity.ExamSubject;
  15. import com.keao.edu.user.entity.ExamSubjectSong;
  16. import com.keao.edu.user.entity.ExaminationBasic;
  17. import com.keao.edu.user.enums.ExamStatusEnum;
  18. import com.keao.edu.user.page.ExamSubjectSongQueryInfo;
  19. import com.keao.edu.user.service.ExamSubjectSongService;
  20. import com.keao.edu.util.collection.MapUtil;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.apache.poi.ss.formula.functions.T;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Isolation;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import org.springframework.util.CollectionUtils;
  28. import java.util.*;
  29. import java.util.stream.Collectors;
  30. @Service
  31. public class ExamSubjectSongServiceImpl extends BaseServiceImpl<Long, ExamSubjectSong> implements ExamSubjectSongService {
  32. @Autowired
  33. private ExaminationBasicDao examinationBasicDao;
  34. @Autowired
  35. private ExamSubjectSongDao examSubjectSongDao;
  36. @Autowired
  37. private ExamSubjectDao examSubjectDao;
  38. @Autowired
  39. private ExamSongDao examSongDao;
  40. @Override
  41. public BaseDAO<Long, ExamSubjectSong> getDAO() {
  42. return examSubjectSongDao;
  43. }
  44. @Override
  45. @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
  46. public void addExamSubjects(List<ExamSubjectSong> examSubjectSongs) {
  47. if(CollectionUtils.isEmpty(examSubjectSongs)){
  48. return;
  49. }
  50. if(Objects.isNull(examSubjectSongs.get(0).getExaminationBasicId())){
  51. throw new BizException("请指定考级项目");
  52. }
  53. ExaminationBasic examinationBasic = examinationBasicDao.get(examSubjectSongs.get(0).getExaminationBasicId().longValue());
  54. if(Objects.isNull(examinationBasic)){
  55. throw new BizException("考级项目不存在");
  56. }
  57. if(!ExamStatusEnum.SETTING.equals(examinationBasic.getStatus())
  58. &&!ExamStatusEnum.NOT_START.equals(examinationBasic.getStatus())){
  59. throw new BizException("此状态考级项目不支持添加考级内容");
  60. }
  61. List<ExamSubjectSong> existExamSubjectSongs=examSubjectSongDao.findByExam(examSubjectSongs.get(0).getExaminationBasicId());
  62. Map<Long, List<Integer>> subjectLevelMap = new HashMap<>();
  63. Set<Long> existSubjectIds = new HashSet<>();
  64. if(!CollectionUtils.isEmpty(existExamSubjectSongs)){
  65. subjectLevelMap = existExamSubjectSongs.stream().collect(Collectors.groupingBy(ExamSubjectSong::getExamSubjectId, Collectors.mapping(ExamSubjectSong::getLevel, Collectors.toList())));
  66. existSubjectIds = existExamSubjectSongs.stream().map(ExamSubjectSong::getExamSubjectId).collect(Collectors.toSet());
  67. }
  68. List<ExamSubject> newExamSubjects = new ArrayList<>();
  69. for (ExamSubjectSong examSubjectSong : examSubjectSongs) {
  70. if(Objects.isNull(examSubjectSong.getExaminationBasicId())){
  71. throw new BizException("请指定考级项目");
  72. }
  73. if(Objects.isNull(examSubjectSong.getExamSubjectId())){
  74. throw new BizException("请选择专业");
  75. }
  76. if(Objects.isNull(examSubjectSong.getLevel())){
  77. throw new BizException("请指定报考等级");
  78. }
  79. List<Integer> levels = subjectLevelMap.get(examSubjectSong.getExamSubjectId());
  80. if(!CollectionUtils.isEmpty(levels)&&levels.contains(examSubjectSong.getLevel())){
  81. throw new BizException("考级内容等级重复");
  82. }
  83. if(Objects.isNull(examSubjectSong.getRegistrationFee())){
  84. throw new BizException("请指定报名费用");
  85. }
  86. boolean praNull = Objects.isNull(examSubjectSong.getPracticeNum());
  87. boolean perNull = Objects.isNull(examSubjectSong.getPerformNum());
  88. if(praNull&&perNull){
  89. throw new BizException("参数错误");
  90. }
  91. if(!praNull&&perNull){
  92. examSubjectSong.setPerformNum(0);
  93. }else if(!perNull&&praNull){
  94. examSubjectSong.setPracticeNum(0);
  95. }
  96. examSubjectSong.setTenantId(TenantContextHolder.getTenantId());
  97. if(existSubjectIds.contains(examSubjectSong.getExamSubjectId().longValue())){
  98. continue;
  99. }
  100. ExamSubject newExamSubject=new ExamSubject();
  101. newExamSubject.setExaminationBasicId(examSubjectSong.getExaminationBasicId());
  102. newExamSubject.setTenantId(examSubjectSong.getTenantId());
  103. newExamSubject.setSubjectId(examSubjectSong.getExamSubjectId().intValue());
  104. newExamSubject.setTenantId(TenantContextHolder.getTenantId());
  105. newExamSubjects.add(newExamSubject);
  106. existSubjectIds.add(newExamSubject.getSubjectId().longValue());
  107. }
  108. examSubjectSongDao.batchInsert(examSubjectSongs);
  109. if(!CollectionUtils.isEmpty(newExamSubjects)){
  110. examSubjectDao.batchInsert(newExamSubjects);
  111. }
  112. }
  113. @Override
  114. public PageInfo<ExamSubjectSongDto> queryExamSubjectSongs(ExamSubjectSongQueryInfo queryInfo) {
  115. PageInfo<ExamSubjectSongDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
  116. Map<String, Object> params = new HashMap<String, Object>();
  117. MapUtil.populateMap(params, queryInfo);
  118. List<ExamSubjectSongDto> dataList = new ArrayList<>();
  119. int count = examSubjectSongDao.countExamSubjectSongs(params);
  120. if (count > 0) {
  121. pageInfo.setTotal(count);
  122. params.put("offset", pageInfo.getOffset());
  123. dataList = examSubjectSongDao.queryExamSubjectSongs(params);
  124. List<Integer> examSongIds=new ArrayList<>();
  125. if(!CollectionUtils.isEmpty(dataList)){
  126. for (ExamSubjectSongDto examSubjectSongDto : dataList) {
  127. if(StringUtils.isNotBlank(examSubjectSongDto.getPracticeSongIdList())){
  128. for (String s : examSubjectSongDto.getPracticeSongIdList().split(",")) {
  129. if(!examSongIds.contains(Integer.valueOf(s))){
  130. examSongIds.add(Integer.valueOf(s));
  131. }
  132. }
  133. }
  134. if(StringUtils.isNotBlank(examSubjectSongDto.getPerformSongIdList())){
  135. for (String s : examSubjectSongDto.getPerformSongIdList().split(",")) {
  136. if(!examSongIds.contains(Integer.valueOf(s))){
  137. examSongIds.add(Integer.valueOf(s));
  138. }
  139. }
  140. }
  141. }
  142. List<ExamSong> examSongs = new ArrayList<>();
  143. if(!CollectionUtils.isEmpty(examSongIds)){
  144. examSongs = examSongDao.getWithIds(examSongIds);
  145. }
  146. Map<Integer, ExamSong> idExamSongMap = examSongs.stream().collect(Collectors.toMap(ExamSong::getId, e -> e));
  147. for (ExamSubjectSongDto examSubjectSongDto : dataList) {
  148. if(StringUtils.isNotBlank(examSubjectSongDto.getPracticeSongIdList())){
  149. List<String> songNames=new ArrayList<>();
  150. for (String s : examSubjectSongDto.getPracticeSongIdList().split(",")) {
  151. if(idExamSongMap.containsKey(Integer.valueOf(s))){
  152. songNames.add(idExamSongMap.get(Integer.valueOf(s)).getSongName());
  153. }
  154. }
  155. examSubjectSongDto.setPracticeSongNames(StringUtils.join(songNames, ","));
  156. }
  157. if(StringUtils.isNotBlank(examSubjectSongDto.getPerformSongIdList())){
  158. List<String> songNames=new ArrayList<>();
  159. for (String s : examSubjectSongDto.getPerformSongIdList().split(",")) {
  160. if(examSongIds.contains(Integer.valueOf(s))){
  161. ExamSong examSong = idExamSongMap.get(Integer.valueOf(s));
  162. if(Objects.nonNull(examSong)){
  163. songNames.add(examSong.getSongName());
  164. }
  165. }
  166. }
  167. examSubjectSongDto.setPerformSongNames(StringUtils.join(songNames, ","));
  168. }
  169. }
  170. }
  171. }
  172. pageInfo.setRows(dataList);
  173. return pageInfo;
  174. }
  175. @Override
  176. public void deleteExamSubjectSong(Long id) {
  177. if(Objects.isNull(id)){
  178. throw new BizException("参数错误");
  179. }
  180. ExamSubjectSong examSubjectSong = examSubjectSongDao.get(id);
  181. if(Objects.isNull(examSubjectSong)){
  182. throw new BizException("数据错误");
  183. }
  184. ExaminationBasic examinationBasic = examinationBasicDao.get(examSubjectSong.getExaminationBasicId().longValue());
  185. if(Objects.isNull(examinationBasic)){
  186. throw new BizException("考级项目不存在");
  187. }
  188. if(!ExamStatusEnum.SETTING.equals(examinationBasic.getStatus())
  189. &&!ExamStatusEnum.NOT_START.equals(examinationBasic.getStatus())){
  190. throw new BizException("此状态无法删除");
  191. }
  192. int i = examSubjectSongDao.countExamSongsWithSubject(examSubjectSong.getExaminationBasicId().longValue(),examSubjectSong.getExamSubjectId().intValue());
  193. examSubjectSongDao.delete(id);
  194. if(i<=1){
  195. examSubjectDao.deleteWithExamSubject(examSubjectSong.getExaminationBasicId().longValue(), examSubjectSong.getExamSubjectId().intValue());
  196. }
  197. }
  198. @Override
  199. public List<ExamSong> getExamSubjectSong(Integer examinationBasicId,Long examSubjectId, Integer level) {
  200. List<ExamSong> examSongs = new ArrayList<>();
  201. ExamSubjectSong examSubjectSong = examSubjectSongDao.getExamSubjectSong(examinationBasicId,examSubjectId, level);
  202. if(examSubjectSong == null){
  203. return examSongs;
  204. }
  205. List<ExamSong> practiceSong = examSongDao.getExamSongs(examSubjectSong.getPracticeSongIdList());
  206. if(practiceSong != null){
  207. examSongs.addAll(practiceSong);
  208. }
  209. List<ExamSong> performSong = examSongDao.getExamSongs(examSubjectSong.getPerformSongIdList());
  210. if(performSong != null){
  211. examSongs.addAll(performSong);
  212. }
  213. return examSongs;
  214. }
  215. @Override
  216. public List<ExamSubjectSong> getExamSubjectLevels(Integer examinationBasicId, Long examSubjectId) {
  217. return examSubjectSongDao.getExamSubjectLevels(examinationBasicId,examSubjectId);
  218. }
  219. }