ExamTeacherSalaryServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.service.impl.BaseServiceImpl;
  6. import com.keao.edu.common.tenant.TenantContextHolder;
  7. import com.keao.edu.user.api.entity.ExamRoom;
  8. import com.keao.edu.user.api.entity.ExamRoomStudentRelation;
  9. import com.keao.edu.user.dao.*;
  10. import com.keao.edu.user.dto.*;
  11. import com.keao.edu.user.entity.ExamTeacherSalary;
  12. import com.keao.edu.user.entity.Teacher;
  13. import com.keao.edu.user.enums.TeacherSettlementTypeEnum;
  14. import com.keao.edu.user.page.ExamTeacherSalaryQueryInfo;
  15. import com.keao.edu.user.service.ExamTeacherSalaryService;
  16. import com.keao.edu.util.collection.MapUtil;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import org.springframework.util.CollectionUtils;
  22. import java.math.BigDecimal;
  23. import java.util.*;
  24. import java.util.stream.Collectors;
  25. @Service
  26. public class ExamTeacherSalaryServiceImpl extends BaseServiceImpl<Long, ExamTeacherSalary> implements ExamTeacherSalaryService {
  27. @Autowired
  28. private ExamRoomDao examRoomDao;
  29. @Autowired
  30. private ExamRoomStudentRelationDao examRoomStudentRelationDao;
  31. @Autowired
  32. private ExamTeacherSalaryDao examTeacherSalaryDao;
  33. @Autowired
  34. private TeacherDao teacherDao;
  35. @Autowired
  36. private ExaminationBasicDao examinationBasicDao;
  37. @Override
  38. public BaseDAO<Long, ExamTeacherSalary> getDAO() {
  39. return examTeacherSalaryDao;
  40. }
  41. @Override
  42. @Transactional(rollbackFor = Exception.class)
  43. public void teacherSalarySettlementWithExam(Long examId) {
  44. List<ExamRoom> examRooms = examRoomDao.getWithExam(null, examId);
  45. if(CollectionUtils.isEmpty(examRooms)){
  46. examRooms=Collections.EMPTY_LIST;
  47. }
  48. Map<Integer, List<ExamRoom>> teacherExamRoomMap=new HashMap<>();
  49. for (ExamRoom examRoom : examRooms) {
  50. if(!teacherExamRoomMap.containsKey(examRoom.getMainTeacherUserId())){
  51. teacherExamRoomMap.put(examRoom.getMainTeacherUserId(), new ArrayList<>());
  52. }
  53. teacherExamRoomMap.get(examRoom.getMainTeacherUserId()).add(examRoom);
  54. if(StringUtils.isBlank(examRoom.getAssistantTeacherUserIdList())){
  55. continue;
  56. }
  57. for (String assistantTeacherId : examRoom.getAssistantTeacherUserIdList().split(",")) {
  58. if(!teacherExamRoomMap.containsKey(Integer.valueOf(assistantTeacherId))){
  59. teacherExamRoomMap.put(Integer.valueOf(assistantTeacherId), new ArrayList<>());
  60. }
  61. teacherExamRoomMap.get(Integer.valueOf(assistantTeacherId)).add(examRoom);
  62. }
  63. }
  64. List<ExamRoomStudentRelation> examRoomStudentRelations = examRoomStudentRelationDao.getExamRoomStudentRelations(examId, null, null);
  65. List<ExamTeacherSalary> examTeacherSalaries = examTeacherSalaryDao.queryWithExam(examId);
  66. for (ExamTeacherSalary examTeacherSalary : examTeacherSalaries) {
  67. List<ExamRoom> teacherExamRooms = teacherExamRoomMap.get(examTeacherSalary.getTeacherId());
  68. if(CollectionUtils.isEmpty(teacherExamRooms)){
  69. examTeacherSalary.setTotalInvigilationNum(0);
  70. examTeacherSalary.setTotalInvigilationStudentNum(0);
  71. examTeacherSalary.setTotalSettlementCost(BigDecimal.ZERO);
  72. continue;
  73. }
  74. Set<Long> examRoomIds = teacherExamRooms.stream().map(ExamRoom::getId).collect(Collectors.toSet());
  75. long studentNum = examRoomStudentRelations.stream().filter(e -> examRoomIds.contains(e.getExamRoomId())).count();
  76. examTeacherSalary.setTotalInvigilationNum(examRoomIds.size());
  77. examTeacherSalary.setTotalInvigilationStudentNum((int) studentNum);
  78. if(TeacherSettlementTypeEnum.SINGLE.equals(examTeacherSalary.getSettlementType())){
  79. examTeacherSalary.setTotalSettlementCost(examTeacherSalary.getShareProfitAmount().multiply(new BigDecimal(examRoomIds.size())));
  80. }else if(TeacherSettlementTypeEnum.PEOPLE_NUM.equals(examTeacherSalary.getSettlementType())){
  81. examTeacherSalary.setTotalSettlementCost(examTeacherSalary.getShareProfitAmount().multiply(new BigDecimal(studentNum)));
  82. }
  83. }
  84. if(!CollectionUtils.isEmpty(examTeacherSalaries)){
  85. examTeacherSalaryDao.batchUpdate(examTeacherSalaries);
  86. Set<Integer> teacherIds = examTeacherSalaries.stream().map(ExamTeacherSalary::getTeacherId).collect(Collectors.toSet());
  87. List<ExamTeacherSalary> teacherAllExamSalarys = examTeacherSalaryDao.getWithTeachers(new ArrayList<>(teacherIds));
  88. Map<Integer, List<ExamTeacherSalary>> teacherExamSalarysMap = teacherAllExamSalarys.stream().collect(Collectors.groupingBy(ExamTeacherSalary::getTeacherId));
  89. List<Teacher> updateTeachers=new ArrayList<>();
  90. for (Integer teacherId : teacherIds) {
  91. List<ExamTeacherSalary> examSalarys = teacherExamSalarysMap.get(teacherId);
  92. Integer totalInvigilationNum=0;
  93. Integer totalInvigilationStudentNum=0;
  94. BigDecimal totalSettlementCost=BigDecimal.ZERO;
  95. if(!CollectionUtils.isEmpty(examSalarys)){
  96. for (ExamTeacherSalary examSalary : examSalarys) {
  97. totalInvigilationNum += examSalary.getTotalInvigilationNum();
  98. totalInvigilationStudentNum += examSalary.getTotalInvigilationStudentNum();
  99. totalSettlementCost=totalSettlementCost.add(examSalary.getTotalSettlementCost());
  100. }
  101. }
  102. Teacher teacher=new Teacher();
  103. teacher.setUserId(teacherId);
  104. teacher.setTotalInvigilationNum(totalInvigilationNum);
  105. teacher.setTotalInvigilationStudentNum(totalInvigilationStudentNum);
  106. teacher.setTotalSettlementCost(totalSettlementCost);
  107. updateTeachers.add(teacher);
  108. }
  109. if(!CollectionUtils.isEmpty(updateTeachers)){
  110. teacherDao.batchUpdate(updateTeachers);
  111. }
  112. }
  113. }
  114. @Override
  115. public PageInfo<ExamTeacherSalaryDto> queryExamTeacherSalary(ExamTeacherSalaryQueryInfo queryInfo) {
  116. PageInfo<ExamTeacherSalaryDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
  117. Map<String, Object> params = new HashMap<String, Object>();
  118. MapUtil.populateMap(params, queryInfo);
  119. List<ExamTeacherSalaryDto> dataList = new ArrayList<>();
  120. int count = examTeacherSalaryDao.countExamTeacherSalary(params);
  121. if (count > 0) {
  122. pageInfo.setTotal(count);
  123. params.put("offset", pageInfo.getOffset());
  124. dataList = examTeacherSalaryDao.queryExamTeacherSalary(params);
  125. List<Long> examIds = dataList.stream().map(ExamTeacherSalaryDto::getExaminationBasicId).collect(Collectors.toList());
  126. List<ExaminationBasicDto> exams = examinationBasicDao.getExams(examIds);
  127. Map<Long, ExaminationBasicDto> idExamMap = exams.stream().collect(Collectors.toMap(ExaminationBasicDto::getId, e -> e));
  128. for (ExamTeacherSalaryDto examTeacherSalaryDto : dataList) {
  129. examTeacherSalaryDto.setExaminationBasic(idExamMap.get(examTeacherSalaryDto.getExaminationBasicId()));
  130. }
  131. }
  132. pageInfo.setRows(dataList);
  133. return pageInfo;
  134. }
  135. @Override
  136. public void deleteExamTeacherSalary(Long examTeacherSalaryId) {
  137. ExamTeacherSalary examTeacherSalary = examTeacherSalaryDao.get(examTeacherSalaryId);
  138. if(Objects.isNull(examTeacherSalary)){
  139. throw new BizException("教室分润设置不能存在");
  140. }
  141. if(examTeacherSalary.getTotalInvigilationNum()>0){
  142. throw new BizException("该教室已被分配到考场");
  143. }
  144. examTeacherSalaryDao.delete(examTeacherSalaryId);
  145. }
  146. @Override
  147. public void addExamTeacherSalary(Long examId, String teacherIdsStr) {
  148. if(Objects.isNull(examId)){
  149. throw new BizException("请指定考级项目");
  150. }
  151. if(StringUtils.isBlank(teacherIdsStr)){
  152. throw new BizException("请指定教师");
  153. }
  154. List<Integer> teacherIds = Arrays.stream(teacherIdsStr.split(",")).map(e -> Integer.valueOf(e)).collect(Collectors.toList());
  155. List<ExamTeacherSalary> withExamAndTeacher = examTeacherSalaryDao.getWithExamAndTeacher(examId, teacherIds);
  156. if(!CollectionUtils.isEmpty(withExamAndTeacher)){
  157. List<String> teacherNames = withExamAndTeacher.stream().map(e -> e.getTeacher().getRealName()).collect(Collectors.toList());
  158. throw new BizException("{}教师已存在", teacherNames);
  159. }
  160. List<Teacher> teachers = teacherDao.getWithTeachers(teacherIds);
  161. Map<Integer, Teacher> idTeacherMap = teachers.stream().collect(Collectors.toMap(Teacher::getUserId, t -> t));
  162. List<ExamTeacherSalary> examTeacherSalaries=new ArrayList<>();
  163. for (Integer teacherId : teacherIds) {
  164. Teacher teacher = idTeacherMap.get(teacherId);
  165. if(Objects.isNull(teacher)){
  166. throw new BizException("教师信息异常");
  167. }
  168. ExamTeacherSalary ets=new ExamTeacherSalary();
  169. ets.setExaminationBasicId(examId);
  170. ets.setTeacherId(teacherId);
  171. ets.setSettlementType(teacher.getSalarySettlementType());
  172. ets.setShareProfitAmount(teacher.getSalary());
  173. ets.setTotalSettlementCost(BigDecimal.ZERO);
  174. ets.setTotalInvigilationStudentNum(0);
  175. ets.setTotalInvigilationNum(0);
  176. ets.setTenantId(TenantContextHolder.getTenantId());
  177. examTeacherSalaries.add(ets);
  178. }
  179. examTeacherSalaryDao.batchInsert(examTeacherSalaries);
  180. }
  181. @Override
  182. public void updateExamTeacherSalary(ExamTeacherSalary examTeacherSalary) {
  183. if(Objects.isNull(examTeacherSalary.getId())){
  184. throw new BizException("参数错误");
  185. }
  186. ExamTeacherSalary exist = examTeacherSalaryDao.get(examTeacherSalary.getId());
  187. if(Objects.isNull(exist)){
  188. throw new BizException("参数错误");
  189. }
  190. examTeacherSalaryDao.update(examTeacherSalary);
  191. teacherSalarySettlementWithExam(exist.getExaminationBasicId());
  192. }
  193. @Override
  194. public List<BaseUserInfoDto> getExamTeachers(Integer examId) {
  195. if(Objects.isNull(examId)){
  196. throw new BizException("请指定考级项目");
  197. }
  198. return examTeacherSalaryDao.getTeachersWithExam(examId);
  199. }
  200. @Override
  201. public PageInfo<TeacherDto> getUnRelatedWithExamTeachers(ExamTeacherSalaryQueryInfo queryInfo) {
  202. PageInfo<TeacherDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
  203. Map<String, Object> params = new HashMap<String, Object>();
  204. MapUtil.populateMap(params, queryInfo);
  205. List<TeacherDto> dataList = new ArrayList<>();
  206. int count = examTeacherSalaryDao.countUnRelatedWithExamTeachers(params);
  207. if (count > 0) {
  208. pageInfo.setTotal(count);
  209. params.put("offset", pageInfo.getOffset());
  210. dataList = examTeacherSalaryDao.queryUnRelatedWithExamTeachers(params);
  211. List<Integer> subjectIds=new ArrayList<>();
  212. for (TeacherDto teacher : dataList) {
  213. if(StringUtils.isBlank(teacher.getSubjectIdList())){
  214. continue;
  215. }
  216. for (String subjectId : teacher.getSubjectIdList().split(",")) {
  217. if(!subjectIds.contains(Integer.valueOf(subjectId))){
  218. subjectIds.add(Integer.valueOf(subjectId));
  219. }
  220. }
  221. }
  222. if(!CollectionUtils.isEmpty(subjectIds)){
  223. Map<Integer, String> subjectIdNameMap = this.getMap("subject", "id_", "name_", subjectIds, Integer.class, String.class);
  224. for (TeacherDto teacher : dataList) {
  225. if(StringUtils.isBlank(teacher.getSubjectIdList())){
  226. continue;
  227. }
  228. List<String> subjectNames=new ArrayList<>();
  229. for (String subjectId : teacher.getSubjectIdList().split(",")) {
  230. String subjectName = subjectIdNameMap.get(Integer.valueOf(subjectId));
  231. if(StringUtils.isNotBlank(subjectName)){
  232. subjectNames.add(subjectName);
  233. }
  234. }
  235. teacher.setSubjectNames(StringUtils.join(subjectNames, ","));
  236. }
  237. }
  238. }
  239. pageInfo.setRows(dataList);
  240. return pageInfo;
  241. }
  242. @Override
  243. public ExamTeacherSalaryStaticsInfo getExamTeacherSalaryStaticsInfo(Integer examId) {
  244. if(Objects.isNull(examId)){
  245. throw new BizException("请指定考级项目");
  246. }
  247. return examTeacherSalaryDao.getExamTeacherSalaryStaticsInfo(examId);
  248. }
  249. }