Browse Source

教师考勤查询逻辑调整

dzx 1 month ago
parent
commit
06974cdb84

+ 211 - 3
src/main/java/com/xjrsoft/module/attendance/controller/TeacherStatisticsController.java

@@ -1,13 +1,42 @@
 package com.xjrsoft.module.attendance.controller;
 
 import cn.dev33.satoken.annotation.SaCheckPermission;
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.github.yulichang.wrapper.MPJLambdaWrapper;
+import com.xjrsoft.common.enums.OutInStatusEnum;
 import com.xjrsoft.common.model.result.RT;
 import com.xjrsoft.common.page.ConventPage;
 import com.xjrsoft.common.page.PageOutput;
+import com.xjrsoft.common.utils.VoToColumnUtil;
 import com.xjrsoft.module.attendance.dto.TeacherDetailsDto;
+import com.xjrsoft.module.attendance.entity.AttendanceRuleDetails;
+import com.xjrsoft.module.attendance.entity.AttendanceUserRelation;
+import com.xjrsoft.module.attendance.service.IAttendanceRuleCategoryService;
 import com.xjrsoft.module.attendance.service.ITeacherAttendanceRecordService;
+import com.xjrsoft.module.attendance.vo.AttendanceRuleDetailsUserVo;
 import com.xjrsoft.module.attendance.vo.TeacherStatisticsPageVo;
+import com.xjrsoft.module.holiday.entity.HolidayDate;
+import com.xjrsoft.module.holiday.service.IHolidayDateService;
+import com.xjrsoft.module.organization.entity.Department;
+import com.xjrsoft.module.organization.entity.User;
+import com.xjrsoft.module.organization.entity.UserDeptRelation;
+import com.xjrsoft.module.organization.service.IUserService;
+import com.xjrsoft.module.outint.dto.TeacherOutInRecordDto;
+import com.xjrsoft.module.outint.entity.CarOutInRecord;
+import com.xjrsoft.module.outint.entity.TeacherOutInRecord;
+import com.xjrsoft.module.outint.service.ICarOutInRecordService;
+import com.xjrsoft.module.outint.service.ITeacherOutInRecordService;
+import com.xjrsoft.module.personnel.entity.CarMessageApply;
+import com.xjrsoft.module.teacher.entity.BaseTeacher;
+import com.xjrsoft.module.teacher.entity.WfHeadTeacherLeave;
+import com.xjrsoft.module.teacher.entity.WfTeacherleave;
+import com.xjrsoft.module.teacher.entity.XjrUser;
+import com.xjrsoft.module.teacher.service.IWfHeadTeacherLeaveService;
+import com.xjrsoft.module.teacher.service.IWfTeacherleaveService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
@@ -16,6 +45,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.validation.Valid;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
 * @title: 教师考勤
@@ -31,13 +67,185 @@ public class TeacherStatisticsController {
 
     private final ITeacherAttendanceRecordService attendanceRecordService;
 
+    private final IUserService xjrUserService;
+    private final ITeacherOutInRecordService teacherOutInRecordService;
+    private final IWfTeacherleaveService wfTeacherleaveService;
+    private final IHolidayDateService holidayDateService;
+    private final IWfHeadTeacherLeaveService headTeacherLeaveService;
+    private final IAttendanceRuleCategoryService attendanceRuleCategoryService;
+    private final ICarOutInRecordService carOutInRecordService;
+
     @GetMapping(value = "/teacher-details")
     @ApiOperation(value="教师考勤")
     @SaCheckPermission("statistics:detail")
     public RT<PageOutput<TeacherStatisticsPageVo>> teacherDetails(@Valid TeacherDetailsDto dto){
-        Page<TeacherStatisticsPageVo> voIPage = attendanceRecordService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
-        PageOutput<TeacherStatisticsPageVo> pageOutput = ConventPage.getPageOutput(voIPage, TeacherStatisticsPageVo.class);
-        return RT.ok(pageOutput);
+        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+        String today = LocalDate.now().format(dtf);
+        if(!today.equals(dto.getDate())){
+            Page<TeacherStatisticsPageVo> voIPage = attendanceRecordService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
+            PageOutput<TeacherStatisticsPageVo> pageOutput = ConventPage.getPageOutput(voIPage, TeacherStatisticsPageVo.class);
+            return RT.ok(pageOutput);
+        }else{
+            MPJLambdaWrapper<User> queryUser = new MPJLambdaWrapper<>();
+            queryUser.disableSubLogicDel().distinct()
+                    .eq(ObjectUtil.isNotNull(dto.getDeptId()), UserDeptRelation::getDeptId, dto.getDeptId())
+                    .eq(StrUtil.isNotEmpty(dto.getAttendanceMode()), AttendanceUserRelation::getAttendanceMode, dto.getAttendanceMode())
+                    .like(StrUtil.isNotEmpty(dto.getCarNumber()), AttendanceUserRelation::getCarNumber, dto.getCarNumber())
+                    .like(StrUtil.isNotEmpty(dto.getName()), User::getName, dto.getName())
+                    .selectAs(User::getName, TeacherStatisticsPageVo::getTeacherName)
+                    .selectAs(User::getId, TeacherStatisticsPageVo::getUserId)
+                    .selectAs(User::getMobile, TeacherStatisticsPageVo::getMobile)
+                    .selectAs(Department::getName, TeacherStatisticsPageVo::getDeptName)
+                    .innerJoin(BaseTeacher.class, BaseTeacher::getUserId, User::getId)
+                    .innerJoin(UserDeptRelation.class, UserDeptRelation::getUserId, User::getId)
+                    .leftJoin(AttendanceUserRelation.class, AttendanceUserRelation::getUserId, User::getId)
+                    .leftJoin(Department.class, Department::getId, UserDeptRelation::getDeptId);
+            IPage<TeacherStatisticsPageVo> voIPage = xjrUserService.selectJoinListPage(ConventPage.getPage(dto), TeacherStatisticsPageVo.class, queryUser);
+
+            if(dto.getDate() != null && !"".equals(dto.getDate())){
+                DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
+                LocalDate queryDate = LocalDate.parse(dto.getDate(), formatter);
+
+                HolidayDate holidayDate = holidayDateService.getOne(
+                        new QueryWrapper<HolidayDate>().lambda()
+                                .eq(HolidayDate::getDate, queryDate)
+                );
+                if(holidayDate != null && holidayDate.getWay() != null && holidayDate.getWay() != 0){
+                    return RT.ok(ConventPage.getPageOutput(voIPage, TeacherStatisticsPageVo.class));
+                }
+
+                List<Long> userIds = voIPage.getRecords().stream().map(TeacherStatisticsPageVo::getUserId).collect(Collectors.toList());
+                Map<Long, AttendanceRuleDetailsUserVo> teacherTodyRuleByUserId = attendanceRuleCategoryService.getTeacherTodyRuleByUserId(userIds);
+                List<String> weekList = Arrays.asList("SUNDAY", "SATURDAY");
+
+                List<HolidayDate> workHolidayDates = holidayDateService.list(
+                        new QueryWrapper<HolidayDate>().lambda()
+                                .eq(HolidayDate::getStatus, 2)
+                );
+                List<String> workHolidayDateListg = workHolidayDates.stream().map(HolidayDate::getDate).collect(Collectors.toList());
+
+                List<Integer> holidayTypes = Arrays.asList(1, 3);
+                List<HolidayDate> holidayDates = holidayDateService.list(
+                        new QueryWrapper<HolidayDate>().lambda()
+                                .in(HolidayDate::getStatus, holidayTypes)
+                );
+                List<String> holidayDateList = holidayDates.stream().map(HolidayDate::getDate).collect(Collectors.toList());
+
+                for (TeacherStatisticsPageVo record : voIPage.getRecords()) {
+                    LocalDateTime startTime, endTime, amEndTime = null;
+                    AttendanceRuleDetailsUserVo detailsUserVo = teacherTodyRuleByUserId.get(record.getUserId());
+                    if(detailsUserVo == null){
+                        record.setStatus("不考勤");
+                        continue;
+                    }
+                    AttendanceRuleDetails holidayRule = attendanceRuleCategoryService.getHolidayRuleByParam(record.getUserId());
+                    if(((!workHolidayDateListg.contains(queryDate.format(formatter)) && weekList.contains(queryDate.getDayOfWeek().name()))
+                            || holidayDateList.contains(queryDate.format(formatter))) && (holidayRule.getIsAttendance() == null || holidayRule.getIsAttendance() == 0)){
+                        record.setStatus("不考勤");
+                        continue;
+                    }
+                    if(dto.getTimePeriod() != null && dto.getTimePeriod() == 1){
+                        startTime = queryDate.atTime(5, 0, 0);
+                        if(detailsUserVo != null && detailsUserVo.getAmStartTime() != null){
+                            startTime = queryDate.atTime(detailsUserVo.getAmStartTime());
+                        }
+                        endTime = queryDate.atTime(12, 0, 0);
+                        if(detailsUserVo != null && detailsUserVo.getAmEndTime() != null){
+                            endTime = queryDate.atTime(detailsUserVo.getAmEndTime());
+                        }
+                    }else if(dto.getTimePeriod() != null && dto.getTimePeriod() == 2){
+                        startTime = queryDate.atTime(12, 0, 0);
+                        if(detailsUserVo != null && detailsUserVo.getPmStartTime() != null){
+                            startTime = queryDate.atTime(detailsUserVo.getPmStartTime());
+                        }
+                        endTime = queryDate.atTime(18, 0, 0);
+                        if(detailsUserVo != null && detailsUserVo.getPmEndTime() != null){
+                            endTime = queryDate.atTime(detailsUserVo.getPmEndTime());
+                        }
+                        amEndTime  = queryDate.atTime(12, 0, 0);
+                        if(detailsUserVo != null && detailsUserVo.getAmEndTime() != null){
+                            amEndTime = queryDate.atTime(detailsUserVo.getAmEndTime());
+                        }
+                    }else{
+                        startTime = queryDate.atTime(0, 0, 0);
+                        if(detailsUserVo != null && detailsUserVo.getEveningStartTime() != null){
+                            startTime = queryDate.atTime(detailsUserVo.getEveningStartTime());
+                        }
+                        endTime = queryDate.atTime(23, 59, 59);
+                        if(detailsUserVo != null && detailsUserVo.getEveningEndTime() != null){
+                            endTime = queryDate.atTime(detailsUserVo.getEveningEndTime());
+                        }
+                    }
+
+                    WfTeacherleave studentLeave = wfTeacherleaveService.getLeaveByUserId(startTime, endTime, record.getUserId());
+                    if(studentLeave != null){
+                        record.setStatus(studentLeave.getLeaveType());
+                        continue;
+                    }
+
+                    WfHeadTeacherLeave teacherLeave = headTeacherLeaveService.selectJoinOne(WfHeadTeacherLeave.class,
+                            new MPJLambdaWrapper<WfHeadTeacherLeave>()
+                                    .select(WfHeadTeacherLeave.class, x -> VoToColumnUtil.fieldsToColumns(WfHeadTeacherLeave.class).contains(x.getProperty()))
+                                    .innerJoin(XjrUser.class, XjrUser::getId, WfHeadTeacherLeave::getApplicantUserId)
+                                    .eq(WfHeadTeacherLeave::getStatus, 1)
+                                    .eq(WfHeadTeacherLeave::getApplicantUserId, record.getUserId())
+                                    .between(WfHeadTeacherLeave::getStartTime, startTime, endTime)
+                    );
+                    if(teacherLeave != null){
+                        record.setStatus(teacherLeave.getLeaveReason());
+                        continue;
+                    }
+                    TeacherOutInRecordDto outInDto = new TeacherOutInRecordDto();
+                    outInDto.setQueryDate(startTime.toLocalDate());
+                    outInDto.setRecordTime(endTime);
+                    outInDto.setStatus(OutInStatusEnum.enter.getCode());
+                    outInDto.setUserId(record.getUserId());
+                    List<TeacherOutInRecord> outInRecords = teacherOutInRecordService.getListByParam(outInDto);
+                    if(!outInRecords.isEmpty()){
+                        TeacherOutInRecord outInRecord = outInRecords.get(0);
+                        if(outInRecord != null){
+                            record.setRecordTime(outInRecord.getRecordTime());
+                            record.setStatus(outInRecord.getAttendanceStatus());
+                            record.setAttendanceMode("人脸");
+                        }
+                    }else{
+                        //查询该教师是否通过车辆进入
+                        List<CarOutInRecord> list = carOutInRecordService.list(
+                                new MPJLambdaWrapper<CarOutInRecord>()
+                                        .select(CarOutInRecord.class, x -> VoToColumnUtil.fieldsToColumns(CarOutInRecord.class).contains(x.getProperty()))
+                                        .leftJoin(CarMessageApply.class, CarMessageApply::getId, CarOutInRecord::getCarMessageApplyId)
+                                        .le(CarOutInRecord::getRecordTime, endTime)
+                                        .eq("DATE_FORMAT(record_time, '%Y-%m-%d')", endTime.toLocalDate())
+                                        .eq(CarMessageApply::getUserId, record.getUserId())
+                                        .ge(dto.getTimePeriod() != null && dto.getTimePeriod() == 2 && amEndTime != null, CarOutInRecord::getRecordTime, amEndTime)
+                                        .eq(CarOutInRecord::getStatus, OutInStatusEnum.enter.getCode())
+                                        .orderByAsc(CarOutInRecord::getRecordTime)
+                        );
+                        if(!list.isEmpty()){
+                            CarOutInRecord outInRecord = list.get(0);
+                            if(outInRecord != null && outInRecord.getRecordTime().isBefore(startTime)){
+                                record.setRecordTime(outInRecord.getRecordTime());
+                                record.setStatus("到校");
+                                record.setAttendanceMode("车辆");
+                                record.setCarNumber(outInRecord.getPlanNo());
+                            }else if(outInRecord != null && outInRecord.getRecordTime().isAfter(startTime)){
+                                record.setRecordTime(outInRecord.getRecordTime());
+                                record.setStatus("迟到");
+                                record.setAttendanceMode("车辆");
+                                record.setCarNumber(outInRecord.getPlanNo());
+                            }
+                        }
+                    }
+
+                    if(record.getStatus() == null){
+                        record.setStatus("缺勤");
+                    }
+                }
+            }
+            PageOutput<TeacherStatisticsPageVo> pageOutput = ConventPage.getPageOutput(voIPage, TeacherStatisticsPageVo.class);
+            return RT.ok(pageOutput);
+        }
+
     }
 
 }

+ 5 - 2
src/main/java/com/xjrsoft/module/hikvision/util/OutInRecordUtil.java

@@ -397,8 +397,11 @@ public class OutInRecordUtil {
 
             JsonElement responseElement = new Gson().fromJson(response, JsonElement.class);
             JsonElement responseDoorElement = new Gson().fromJson(responseDoor, JsonElement.class);
-
-            JsonArray dataList = responseElement.getAsJsonObject().getAsJsonObject("data").getAsJsonArray("rows");
+            JsonObject data = responseElement.getAsJsonObject().getAsJsonObject("data");
+            if(data.isJsonNull()){
+                continue;
+            }
+            JsonArray dataList = data.getAsJsonArray("rows");
             JsonArray dataDoorList = responseDoorElement.getAsJsonObject().getAsJsonObject("data").getAsJsonArray("list");
 
             for (JsonElement item : dataList) {

+ 2 - 2
src/test/java/com/xjrsoft/module/job/AttendanceRecordTaskTest.java

@@ -75,8 +75,8 @@ class AttendanceRecordTaskTest {
     @Test
     public void execute() {
 
-        LocalDateTime now = LocalDateTime.of(2024, 10, 12, 0, 0, 0);
-        for (int i = 0; i < 1; i ++){
+        LocalDateTime now = LocalDateTime.of(2024, 9, 1, 0, 0, 0);
+        for (int i = 0; i < 30; i ++){
             LocalDateTime startDateTime = now.plusDays(i).withHour(0).withMinute(0).withSecond(0).withNano(0);
             LocalDateTime endDateTime = startDateTime.plusDays(i).withHour(23).withMinute(59).withSecond(59);