yaozc2021
2024-11-22 0799f543beca64dd1c6007e18cb8d8388b2fc391
eladmin-master/eladmin-system/src/test/java/me/zhengjie/AttendanceTest.java
@@ -1,116 +1,227 @@
package me.zhengjie;
import me.zhengjie.modules.attendance.domain.AttendanceGoOut;
import me.zhengjie.modules.attendance.domain.AttendanceOvertime;
import me.zhengjie.modules.attendance.domain.Calculate;
import me.zhengjie.modules.attendance.service.AttendanceGoOutService;
import me.zhengjie.modules.constant.Constant;
import me.zhengjie.modules.quartz.task.AttendanceMonthlyMethod;
import me.zhengjie.modules.system.domain.SysRostering;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.SysRosteringRepository;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.SysConfigHolidayService;
import me.zhengjie.modules.system.service.dto.SysConfigHolidayDto;
import me.zhengjie.utils.SecurityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.stream.Stream;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AttendanceTest {
    @Autowired
    private AttendanceGoOutService attendanceGoOutService;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private SysConfigHolidayService sysConfigHolidayService;
    @Autowired
    private SysRosteringRepository sysRosteringRepository;
    @Test
    public void test() {
        AttendanceGoOut resources = new AttendanceGoOut();
        resources.setUserId(128L);
        resources.setOutStartTime(Timestamp.valueOf(LocalDateTime.of(2024, 3, 25, 9, 0, 0)));
        resources.setOutEndTime(Timestamp.valueOf(LocalDateTime.of(2024, 3, 25, 18, 0, 0)));
// 获取当前登陆人信息,设置为请假发起人
        User user = userRepository.getOne(resources.getUserId());
        LocalDate startDate = resources.getOutStartTime().toLocalDateTime().toLocalDate();
        LocalDate endDate = resources.getOutEndTime().toLocalDateTime().toLocalDate();
        LocalTime startTime = resources.getOutStartTime().toLocalDateTime().toLocalTime();
        LocalTime endTime = resources.getOutEndTime().toLocalDateTime().toLocalTime();
        long distance = ChronoUnit.DAYS.between(startDate, endDate);
        Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(localDate -> {
            //resUserId为null说明是添加,反之
            if (resources.getUserId() == null) {
                resources.setUserId(user.getId());
            }
            resources.setOutStartTime(Timestamp.valueOf(localDate.atTime(startTime)));
            resources.setOutEndTime(Timestamp.valueOf(localDate.atTime(endTime)));
            Calculate calculate = new Calculate();
            calculate.setStart(resources.getOutStartTime().toLocalDateTime());
            calculate.setEnd(resources.getOutEndTime().toLocalDateTime());
            SysRostering sysRostering = sysRosteringRepository.findByRosId(2L);
            LocalDate startDate1 = calculate.getStart().toLocalDate();
            LocalDate endDate2 = calculate.getEnd().toLocalDate();
            LocalTime startTime2 = calculate.getStart().toLocalTime();
            LocalTime endTime2 = calculate.getEnd().toLocalTime();
            long distance2 = ChronoUnit.DAYS.between(startDate1, endDate2);
            long timeSeconds =  Stream.iterate(startDate1, d -> d.plusDays(1)).limit(distance2 + 1).map(localDate2 -> {
                SysConfigHolidayDto sysConfigHolidayDto = sysConfigHolidayService.queryByHolidayDate(localDate2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                Boolean isWeekend = localDate2.getDayOfWeek() == DayOfWeek.SATURDAY || localDate2.getDayOfWeek() == DayOfWeek.SUNDAY;
                if ((sysConfigHolidayDto == null && !isWeekend) ||
                        sysConfigHolidayDto != null && Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHolidayDto.getDateType())) {
                    LocalDateTime goOutStartTime = localDate2.atTime(startTime2);
                    LocalDateTime goOutEndTime = localDate2.atTime(endTime2);
                    LocalDateTime lunchStartTime = localDate2.atTime(sysRostering.getLunchStartTime());
                    LocalDateTime lunchEndTime = localDate2.atTime(sysRostering.getLunchEndTime());
                    if (goOutEndTime.isBefore(lunchStartTime) || goOutStartTime.isAfter(lunchEndTime)) {
                        return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime);
                    } else if (!goOutEndTime.isBefore(lunchStartTime) && !goOutEndTime.isAfter(lunchEndTime)) {
                        return ChronoUnit.SECONDS.between(goOutStartTime, lunchStartTime);
                    } else if (!goOutStartTime.isBefore(lunchStartTime) && !goOutStartTime.isAfter(lunchEndTime)) {
                        return ChronoUnit.SECONDS.between(lunchEndTime, goOutEndTime);
                    } else {
                        return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime) - 3600;
                    }
                }
                return 0L;
            }).reduce(0L, (result, current) -> result + current);
            Double result = Double.valueOf(new BigDecimal((float) timeSeconds / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            System.out.println("result : " + result);
        });
    }
    @Autowired
    private AttendanceMonthlyMethod attendanceMonthlyMethod;
    @Test
    public void exec100() throws ParseException {
        attendanceMonthlyMethod.getMonthlyData();
    }
}
package me.zhengjie;
import me.zhengjie.modules.attendance.domain.Attendance;
import me.zhengjie.modules.attendance.domain.AttendanceGoOut;
import me.zhengjie.modules.attendance.domain.AttendanceOvertime;
import me.zhengjie.modules.attendance.domain.Calculate;
import me.zhengjie.modules.attendance.repository.AttendanceGoOutRepository;
import me.zhengjie.modules.attendance.service.AttendanceGoOutService;
import me.zhengjie.modules.attendance.service.AttendanceService;
import me.zhengjie.modules.attendance.service.dto.AttendanceGoOutDto;
import me.zhengjie.modules.attendance.service.dto.AttendanceGoOutQueryCriteria;
import me.zhengjie.modules.attendance.service.mapstruct.AttendanceGoOutMapper;
import me.zhengjie.modules.constant.Constant;
import me.zhengjie.modules.quartz.task.AttendanceMonthlyMethod;
import me.zhengjie.modules.system.domain.SysRostering;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.SysRosteringRepository;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.SysConfigHolidayService;
import me.zhengjie.modules.system.service.dto.SysConfigHolidayDto;
import me.zhengjie.utils.SecurityUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Stream;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@ExtendWith(SpringExtension.class)
//@DataJpaTest
public class AttendanceTest {
    @Autowired
    private AttendanceGoOutService attendanceGoOutService;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private SysConfigHolidayService sysConfigHolidayService;
    @Autowired
    private SysRosteringRepository sysRosteringRepository;
    @Test
    public void test() {
        AttendanceGoOut resources = new AttendanceGoOut();
        resources.setUserId(128L);
        resources.setOutStartTime(Timestamp.valueOf(LocalDateTime.of(2024, 3, 25, 9, 0, 0)));
        resources.setOutEndTime(Timestamp.valueOf(LocalDateTime.of(2024, 3, 25, 18, 0, 0)));
// 获取当前登陆人信息,设置为请假发起人
        User user = userRepository.getOne(resources.getUserId());
        LocalDate startDate = resources.getOutStartTime().toLocalDateTime().toLocalDate();
        LocalDate endDate = resources.getOutEndTime().toLocalDateTime().toLocalDate();
        LocalTime startTime = resources.getOutStartTime().toLocalDateTime().toLocalTime();
        LocalTime endTime = resources.getOutEndTime().toLocalDateTime().toLocalTime();
        long distance = ChronoUnit.DAYS.between(startDate, endDate);
        Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(localDate -> {
            //resUserId为null说明是添加,反之
            if (resources.getUserId() == null) {
                resources.setUserId(user.getId());
            }
            resources.setOutStartTime(Timestamp.valueOf(localDate.atTime(startTime)));
            resources.setOutEndTime(Timestamp.valueOf(localDate.atTime(endTime)));
            Calculate calculate = new Calculate();
            calculate.setStart(resources.getOutStartTime().toLocalDateTime());
            calculate.setEnd(resources.getOutEndTime().toLocalDateTime());
            SysRostering sysRostering = sysRosteringRepository.findByRosId(2L);
            LocalDate startDate1 = calculate.getStart().toLocalDate();
            LocalDate endDate2 = calculate.getEnd().toLocalDate();
            LocalTime startTime2 = calculate.getStart().toLocalTime();
            LocalTime endTime2 = calculate.getEnd().toLocalTime();
            long distance2 = ChronoUnit.DAYS.between(startDate1, endDate2);
            long timeSeconds =  Stream.iterate(startDate1, d -> d.plusDays(1)).limit(distance2 + 1).map(localDate2 -> {
                SysConfigHolidayDto sysConfigHolidayDto = sysConfigHolidayService.queryByHolidayDate(localDate2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                Boolean isWeekend = localDate2.getDayOfWeek() == DayOfWeek.SATURDAY || localDate2.getDayOfWeek() == DayOfWeek.SUNDAY;
                if ((sysConfigHolidayDto == null && !isWeekend) ||
                        sysConfigHolidayDto != null && Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHolidayDto.getDateType())) {
                    LocalDateTime goOutStartTime = localDate2.atTime(startTime2);
                    LocalDateTime goOutEndTime = localDate2.atTime(endTime2);
                    LocalDateTime lunchStartTime = localDate2.atTime(sysRostering.getLunchStartTime());
                    LocalDateTime lunchEndTime = localDate2.atTime(sysRostering.getLunchEndTime());
                    if (goOutEndTime.isBefore(lunchStartTime) || goOutStartTime.isAfter(lunchEndTime)) {
                        return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime);
                    } else if (!goOutEndTime.isBefore(lunchStartTime) && !goOutEndTime.isAfter(lunchEndTime)) {
                        return ChronoUnit.SECONDS.between(goOutStartTime, lunchStartTime);
                    } else if (!goOutStartTime.isBefore(lunchStartTime) && !goOutStartTime.isAfter(lunchEndTime)) {
                        return ChronoUnit.SECONDS.between(lunchEndTime, goOutEndTime);
                    } else {
                        return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime) - 3600;
                    }
                }
                return 0L;
            }).reduce(0L, (result, current) -> result + current);
            Double result = Double.valueOf(new BigDecimal((float) timeSeconds / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            System.out.println("result : " + result);
        });
    }
    @Autowired
    private AttendanceMonthlyMethod attendanceMonthlyMethod;
    @Test
    public void exec100() throws ParseException {
        attendanceMonthlyMethod.getMonthlyData();
    }
    @Autowired
    private AttendanceService attendanceService;
    @Test
    public void addAttendanceInfo() {
        LocalDate startDate = LocalDate.of(2024, 6, 7);
        LocalDate endDate = LocalDate.of(2024, 6, 7);
        long distance = ChronoUnit.DAYS.between(startDate, endDate);
        Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(localDate -> {
            Attendance attendance = new Attendance();
            attendance.setUserId(44L);
            attendance.setAttendanceDate(Timestamp.valueOf(localDate.atStartOfDay()));
            try {
                attendanceService.addAttendanceInfo(attendance);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }
    @Autowired
    private AttendanceGoOutMapper attendanceGoOutMapper;
    @Autowired
    private AuthenticationManagerBuilder authenticationManagerBuilder;
    @Autowired
    private AttendanceGoOutRepository attendanceGoOutRepository;
    @Test
    @Transactional
    @Rollback(false)
    public void AttendanceGoOutServiceTest() {
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken("yaozc", "0350111443");
        Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        AttendanceGoOutQueryCriteria criteria = new AttendanceGoOutQueryCriteria();
        criteria.setOutOfOfficeType("go_out_type_personnel");
        criteria.setOutStartTimeLike("2024-10-%");
        List<AttendanceGoOutDto> attendanceGoOutDtos = attendanceGoOutService.queryAll(criteria);
        attendanceGoOutDtos.forEach(attendanceGoOut -> {
            Calculate calculate = new Calculate();
            calculate.setStart(attendanceGoOut.getOutStartTime().toLocalDateTime());
            calculate.setEnd(attendanceGoOut.getOutEndTime().toLocalDateTime());
            long timeSeconds = calculateTimes(calculate, attendanceGoOut.getUserId());
            Double result = Double.valueOf(new BigDecimal((float) timeSeconds / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            if (result.compareTo(attendanceGoOut.getOutDuration()) != 0) {
                System.out.println(attendanceGoOut.getUserName() + " : " + result + " : " + attendanceGoOut.getOutDuration() + " : " + attendanceGoOut.getOutStartTime() + " : " + attendanceGoOut.getOutEndTime());
                attendanceGoOut.setOutDuration(result);
                attendanceGoOutRepository.save(attendanceGoOutMapper.toEntity(attendanceGoOut));
            }
        });
    }
    private Long calculateTimes(Calculate calculate, Long userId) {
        User userInfo = userRepository.getOne(userId);
        SysRostering sysRostering = userInfo.getSysRostering();
        LocalDate startDate = calculate.getStart().toLocalDate();
        LocalDate endDate = calculate.getEnd().toLocalDate();
        LocalTime startTime = calculate.getStart().toLocalTime();
        LocalTime endTime = calculate.getEnd().toLocalTime();
        long distance = ChronoUnit.DAYS.between(startDate, endDate);
        return Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).map(localDate -> {
            SysConfigHolidayDto sysConfigHolidayDto = sysConfigHolidayService.queryByHolidayDate(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            Boolean isWeekend = localDate.getDayOfWeek() == DayOfWeek.SATURDAY || localDate.getDayOfWeek() == DayOfWeek.SUNDAY;
            if ((sysConfigHolidayDto == null && !isWeekend) ||
                    sysConfigHolidayDto != null && Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHolidayDto.getDateType())) {
                LocalDateTime goOutStartTime = localDate.atTime(startTime);
                LocalDateTime goOutEndTime = localDate.atTime(endTime);
                LocalDateTime lunchStartTime = localDate.atTime(sysRostering.getLunchStartTime());
                LocalDateTime lunchEndTime = localDate.atTime(sysRostering.getLunchEndTime());
                if (goOutEndTime.isBefore(lunchStartTime) || goOutStartTime.isAfter(lunchEndTime)) {
                    return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime);
                } else if (!goOutEndTime.isBefore(lunchStartTime) && !goOutEndTime.isAfter(lunchEndTime)) {
                    return ChronoUnit.SECONDS.between(goOutStartTime, lunchStartTime);
                } else if (!goOutStartTime.isBefore(lunchStartTime) && !goOutStartTime.isAfter(lunchEndTime)) {
                    return ChronoUnit.SECONDS.between(lunchEndTime, goOutEndTime);
                } else {
                    return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime) - 3600;
                }
            } else {
                LocalDateTime goOutStartTime = localDate.atTime(startTime);
                LocalDateTime goOutEndTime = localDate.atTime(endTime);
                return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime);
            }
        }).reduce(0L, (result, current) -> result + current);
    }
}