yaozc
2024-05-30 86b0362eaf4c320dc6a54fc12ce80b0bf7890a85
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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();
    }
}