BAOLH
2025-11-06 6da28ea4f40d34b26dc6984ccd4c4ebbb0c107c6
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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);
    }
}