StatisticsController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package com.xjrsoft.module.attendance.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.github.yulichang.toolkit.MPJWrappers;
  6. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  7. import com.google.gson.JsonArray;
  8. import com.google.gson.JsonElement;
  9. import com.google.gson.JsonObject;
  10. import com.google.gson.JsonParser;
  11. import com.xjrsoft.common.annotation.XjrLog;
  12. import com.xjrsoft.common.enums.DeleteMark;
  13. import com.xjrsoft.common.enums.OutInStatusEnum;
  14. import com.xjrsoft.common.enums.StudyStatusEnum;
  15. import com.xjrsoft.common.model.result.RT;
  16. import com.xjrsoft.common.utils.VoToColumnUtil;
  17. import com.xjrsoft.module.attendance.dto.AttendanceStatisticDto;
  18. import com.xjrsoft.module.attendance.dto.TeacherDetailsDto;
  19. import com.xjrsoft.module.attendance.entity.AttendanceRuleCategory;
  20. import com.xjrsoft.module.attendance.service.IAttendanceRuleCategoryService;
  21. import com.xjrsoft.module.attendance.service.ITeacherAttendanceRecordService;
  22. import com.xjrsoft.module.attendance.vo.TeacherStatisticsPageVo;
  23. import com.xjrsoft.module.attendance.vo.TeacherStatisticsVo;
  24. import com.xjrsoft.module.attendance.vo.VisitorInfoVo;
  25. import com.xjrsoft.module.base.entity.BaseClass;
  26. import com.xjrsoft.module.concat.service.IXjrUserService;
  27. import com.xjrsoft.module.organization.entity.UserDeptRelation;
  28. import com.xjrsoft.module.outint.entity.StudentOutInRecord;
  29. import com.xjrsoft.module.outint.service.IStudentOutInRecordService;
  30. import com.xjrsoft.module.personnel.service.IReservationSchoolService;
  31. import com.xjrsoft.module.student.entity.BaseStudent;
  32. import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
  33. import com.xjrsoft.module.student.service.IStudentLeaveService;
  34. import com.xjrsoft.module.teacher.dto.BaseTeacherPageDto;
  35. import com.xjrsoft.module.teacher.entity.BaseTeacher;
  36. import com.xjrsoft.module.teacher.entity.XjrUser;
  37. import com.xjrsoft.module.teacher.service.ITeacherbaseManagerService;
  38. import com.xjrsoft.module.teacher.vo.XjrUserPageVo;
  39. import io.swagger.annotations.Api;
  40. import io.swagger.annotations.ApiOperation;
  41. import lombok.AllArgsConstructor;
  42. import org.springframework.web.bind.annotation.GetMapping;
  43. import org.springframework.web.bind.annotation.RequestMapping;
  44. import org.springframework.web.bind.annotation.RestController;
  45. import javax.validation.Valid;
  46. import java.math.BigDecimal;
  47. import java.math.RoundingMode;
  48. import java.time.LocalDate;
  49. import java.time.LocalDateTime;
  50. import java.time.format.DateTimeFormatter;
  51. import java.util.ArrayList;
  52. import java.util.HashSet;
  53. import java.util.List;
  54. import java.util.Set;
  55. import java.util.stream.Collectors;
  56. /**
  57. * @title: 考勤消息设置
  58. * @Author dzx
  59. * @Date: 2024-05-21
  60. * @Version 1.0
  61. */
  62. @RestController
  63. @RequestMapping("/statistics")
  64. @Api(value = "/statistics", tags = "考勤统计")
  65. @AllArgsConstructor
  66. public class StatisticsController {
  67. private final IXjrUserService xjrUserService;
  68. private final IStudentOutInRecordService studentOutInRecordService;
  69. private final IStudentLeaveService studentLeaveService;
  70. private final IReservationSchoolService reservationSchoolService;
  71. private final ITeacherAttendanceRecordService attendanceRecordService;
  72. private final ITeacherbaseManagerService teacherbaseManagerService;
  73. private final IAttendanceRuleCategoryService ruleCategoryService;
  74. @GetMapping(value = "/teacher-statistics")
  75. @ApiOperation(value = "教职工考勤统计")
  76. @SaCheckPermission("statistics:detail")
  77. @XjrLog(value = "", saveResponseData = true)
  78. public RT<TeacherStatisticsVo> teacherStatistics(@Valid AttendanceStatisticDto dto) {
  79. TeacherStatisticsVo statisticsVo = new TeacherStatisticsVo();
  80. //查询总人数
  81. MPJLambdaWrapper<XjrUser> queryWrapper = MPJWrappers.<XjrUser>lambdaJoin()
  82. .disableSubLogicDel()
  83. .eq(ObjectUtil.isNotNull(dto.getDeptId()), UserDeptRelation::getDeptId, dto.getDeptId())
  84. .innerJoin(BaseTeacher.class, BaseTeacher::getUserId, XjrUser::getId)
  85. .innerJoin(UserDeptRelation.class, UserDeptRelation::getUserId, XjrUser::getId);
  86. long allCount = xjrUserService.count(queryWrapper);
  87. statisticsVo.setAllCount(allCount);
  88. TeacherDetailsDto detailsDto = new TeacherDetailsDto();
  89. detailsDto.setDate(dto.getDate());
  90. detailsDto.setDeptId(dto.getDeptId());
  91. detailsDto.setTimePeriod(dto.getTimePeriod());
  92. List<TeacherStatisticsPageVo> list = attendanceRecordService.getList(detailsDto);
  93. Long actualCount = 0L;
  94. Long lateCount = 0L;
  95. Long leaveCount = 0L;
  96. Long absenteeismCount = 0L;
  97. Integer dividendCount = 0;
  98. Set<Long> teacherIds = new HashSet<>();
  99. for (TeacherStatisticsPageVo recordVo : list) {
  100. teacherIds.add(recordVo.getUserId());
  101. if ("迟到".equals(recordVo.getStatus())) {
  102. lateCount++;
  103. dividendCount++;
  104. } else if ("请假".equals(recordVo.getStatus())) {
  105. leaveCount++;
  106. dividendCount++;
  107. } else if ("缺勤".equals(recordVo.getStatus())) {
  108. absenteeismCount++;
  109. dividendCount++;
  110. }
  111. if (recordVo.getRecordTime() != null) {
  112. actualCount++;
  113. dividendCount++;
  114. }
  115. }
  116. statisticsVo.setLateCount(lateCount);
  117. statisticsVo.setActualCount(actualCount);
  118. statisticsVo.setLeaveCount(leaveCount);
  119. statisticsVo.setAbsenteeismCount(absenteeismCount);
  120. //查询不需要考勤的人
  121. List<AttendanceRuleCategory> ruleCategories = ruleCategoryService.list(
  122. new QueryWrapper<AttendanceRuleCategory>().lambda()
  123. .eq(AttendanceRuleCategory::getDeleteMark, DeleteMark.NODELETE.getCode())
  124. .eq(AttendanceRuleCategory::getRoleId, 2)
  125. .le(AttendanceRuleCategory::getStartDate, LocalDate.now())
  126. .ge(AttendanceRuleCategory::getEndDate, LocalDate.now())
  127. );
  128. if(!ruleCategories.isEmpty()){
  129. BaseTeacherPageDto teacherPageDto = new BaseTeacherPageDto();
  130. List<Long> userIds = new ArrayList<>();
  131. List<Long> deptIds = new ArrayList<>();
  132. for (AttendanceRuleCategory ruleCategory : ruleCategories) {
  133. JsonArray userRelation = new JsonParser().parse(ruleCategory.getUserRelation()).getAsJsonArray();
  134. for (JsonElement jsonElement : userRelation) {
  135. JsonObject jsonObject = jsonElement.getAsJsonObject();
  136. if(jsonObject.get("deptId") != null && !jsonObject.get("deptId").isJsonNull()){
  137. deptIds.add(jsonObject.get("deptId").getAsLong());
  138. }
  139. if(jsonObject.get("userId") != null && !jsonObject.get("userId").isJsonNull()){
  140. userIds.add(jsonObject.get("userId").getAsLong());
  141. }
  142. }
  143. }
  144. if(dto.getDeptId() == null){
  145. teacherPageDto.setDeptIds(deptIds);
  146. }else{
  147. deptIds = new ArrayList<>();
  148. deptIds.add(dto.getDeptId());
  149. }
  150. List<XjrUserPageVo> attendanceTeacherList = teacherbaseManagerService.getList(teacherPageDto);
  151. teacherPageDto = new BaseTeacherPageDto();
  152. teacherPageDto.setUserIds(userIds);
  153. attendanceTeacherList.addAll(teacherbaseManagerService.getList(teacherPageDto));
  154. teacherPageDto = new BaseTeacherPageDto();
  155. List<Long> dtoDeptIds = new ArrayList<>();
  156. dtoDeptIds.add(dto.getDeptId());
  157. teacherPageDto.setDeptIds(dtoDeptIds);
  158. List<XjrUserPageVo> allTeacherList = teacherbaseManagerService.getList(teacherPageDto);
  159. statisticsVo.setNotAttendanceCount(allTeacherList.size() - attendanceTeacherList.size());
  160. }
  161. //计算出勤率
  162. BigDecimal divide = BigDecimal.ZERO;
  163. if (dividendCount != 0) {
  164. divide = BigDecimal.valueOf(statisticsVo.getActualCount()).divide(BigDecimal.valueOf(dividendCount), 4, RoundingMode.HALF_UP);
  165. }
  166. statisticsVo.setAttendanceRate(divide.doubleValue() + "");
  167. return RT.ok(statisticsVo);
  168. }
  169. @GetMapping(value = "/student-statistics")
  170. @ApiOperation(value = "学生考勤统计")
  171. @SaCheckPermission("statistics:detail")
  172. @XjrLog(value = "学生考勤统计", saveResponseData = true)
  173. public RT<TeacherStatisticsVo> studentStatistics(@Valid AttendanceStatisticDto dto) {
  174. TeacherStatisticsVo statisticsVo = new TeacherStatisticsVo();
  175. //查询总人数
  176. MPJLambdaWrapper<XjrUser> queryWrapper = MPJWrappers.<XjrUser>lambdaJoin()
  177. .disableSubLogicDel()
  178. .distinct()
  179. .eq(ObjectUtil.isNotNull(dto.getGradeId()), BaseClass::getGradeId, dto.getGradeId())
  180. .eq(BaseStudentSchoolRoll::getArchivesStatus, "FB2901")
  181. .eq(ObjectUtil.isNotNull(dto.getClassId()), BaseStudentSchoolRoll::getClassId, dto.getClassId())
  182. .innerJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, XjrUser::getId)
  183. .innerJoin(BaseClass.class, BaseClass::getId, BaseStudentSchoolRoll::getClassId)
  184. .innerJoin(BaseStudent.class, BaseStudent::getUserId, XjrUser::getId);
  185. long allCount = xjrUserService.count(queryWrapper);
  186. queryWrapper.eq(BaseStudentSchoolRoll::getStduyStatus, StudyStatusEnum.AttendDaySchool.getCode());
  187. long attendDaySchoolCount = xjrUserService.count(queryWrapper);
  188. statisticsVo.setAllCount(allCount);
  189. if (dto.getDate() != null && !"".equals(dto.getDate())) {
  190. DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
  191. LocalDate queryDate = LocalDate.parse(dto.getDate(), formatter);
  192. LocalDateTime startTime, endTime;
  193. if (dto.getTimePeriod() != null && dto.getTimePeriod() == 1) {
  194. startTime = queryDate.atTime(5, 0, 0);
  195. endTime = queryDate.atTime(12, 0, 0);
  196. } else if (dto.getTimePeriod() != null && dto.getTimePeriod() == 2) {
  197. startTime = queryDate.atTime(12, 0, 0);
  198. endTime = queryDate.atTime(18, 0, 0);
  199. } else {
  200. startTime = queryDate.atTime(0, 0, 0);
  201. endTime = queryDate.atTime(23, 59, 59);
  202. }
  203. List<StudentOutInRecord> outInRecords = studentOutInRecordService.list(
  204. new MPJLambdaWrapper<StudentOutInRecord>()
  205. .select(StudentOutInRecord.class, x -> VoToColumnUtil.fieldsToColumns(StudentOutInRecord.class).contains(x.getProperty()))
  206. .innerJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, StudentOutInRecord::getUserId)
  207. .innerJoin(BaseClass.class, BaseClass::getId, BaseStudentSchoolRoll::getClassId)
  208. .le(StudentOutInRecord::getRecordTime, endTime)
  209. .eq("DATE_FORMAT(record_time, '%Y-%m-%d')", endTime.toLocalDate())
  210. .eq(StudentOutInRecord::getStatus, OutInStatusEnum.enter.getCode())
  211. .eq(ObjectUtil.isNotNull(dto.getClassId()), BaseStudentSchoolRoll::getClassId, dto.getClassId())
  212. .eq(ObjectUtil.isNotNull(dto.getGradeId()), BaseClass::getGradeId, dto.getGradeId())
  213. );
  214. //实到人数
  215. Set<Long> collect = outInRecords.stream().map(StudentOutInRecord::getUserId).collect(Collectors.toSet());
  216. statisticsVo.setActualCount(Long.valueOf(collect.size()));
  217. //查询学生请假人数
  218. Long leaveCount = studentLeaveService.getLeaveCount(startTime, endTime, dto);
  219. if (leaveCount == null) {
  220. leaveCount = 0L;
  221. }
  222. statisticsVo.setLeaveCount(leaveCount);
  223. //通过考勤规则和实到人数信息,计算迟到的
  224. Long lateCount = 0L;
  225. Integer playTruantCount = 0;
  226. for (StudentOutInRecord outInRecord : outInRecords) {
  227. if ("迟到".equals(outInRecord.getAttendanceStatus())) {
  228. lateCount++;
  229. } else if ("旷课".equals(outInRecord.getAttendanceStatus())) {
  230. playTruantCount++;
  231. }
  232. }
  233. statisticsVo.setPlayTruantCount(playTruantCount);
  234. statisticsVo.setLateCount(lateCount);
  235. //计算出勤率
  236. if (statisticsVo.getAllCount() != null && statisticsVo.getAllCount() != 0) {
  237. //最后通过总人数-实到人数-请假人数计算出缺勤人数
  238. statisticsVo.setAbsenteeismCount(attendDaySchoolCount - statisticsVo.getLeaveCount() - statisticsVo.getActualCount());
  239. BigDecimal divide = BigDecimal.valueOf(statisticsVo.getActualCount()).divide(BigDecimal.valueOf(attendDaySchoolCount), 4, RoundingMode.HALF_UP);
  240. statisticsVo.setAttendanceRate(divide.doubleValue() + "");
  241. } else {
  242. statisticsVo.setAttendanceRate("0");
  243. statisticsVo.setAbsenteeismCount(0L);
  244. }
  245. }
  246. return RT.ok(statisticsVo);
  247. }
  248. @GetMapping(value = "/visitor-list")
  249. @ApiOperation(value = "访客列表")
  250. @SaCheckPermission("statistics:detail")
  251. @XjrLog(value = "访客列表")
  252. public RT<List<VisitorInfoVo>> visitorList(@Valid AttendanceStatisticDto dto) {
  253. List<VisitorInfoVo> visionList = reservationSchoolService.getVisionList(dto.getDate());
  254. return RT.ok(visionList);
  255. }
  256. }