SchedulingService.java
10.1 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package com.ruoyi.service;
import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.cache.NowSchedulingCache;
import com.ruoyi.driver.mapper.DriverSchedulingMapper;
import com.ruoyi.in.domain.SignIn;
import com.ruoyi.in.mapper.SignInMapper;
import com.ruoyi.pojo.DriverSignRecommendation;
import com.ruoyi.pojo.GlobalIndex;
import com.ruoyi.pojo.entity.DriverScheduling;
import com.ruoyi.pojo.request.ReportViewRequestVo;
import com.ruoyi.pojo.response.ReportViewResponseVo;
import com.ruoyi.utils.ConstDateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.temporal.ChronoUnit;
import java.util.*;
import static com.ruoyi.common.ConstDriverProperties.BC_TYPE_IN;
import static com.ruoyi.common.ConstDriverProperties.BC_TYPE_OUT;
import static com.ruoyi.common.ConstSignInConstSignInProperties.*;
import static com.ruoyi.common.ErrorTypeProperties.SIGN_IN_TIMEOUT;
import static com.ruoyi.common.ErrorTypeProperties.SIGN_OUT_TIMEOUT;
import static com.ruoyi.common.ReportProperties.NOW;
/**
* @author 20412
*/
@Service
public class SchedulingService {
@Resource
private NowSchedulingCache nowSchedulingCache;
@Autowired
private DriverSchedulingMapper schedulingMapper;
@Autowired
private SignInMapper signInMapper;
/**
* 获取排班信息
*
* @param jobCode
* @param now
* @return
*/
public List<DriverScheduling> queryScheduling(String jobCode, Long now) {
List<DriverScheduling> dto = null;
for (int i = 0; i > -2; i--) {
dto = nowSchedulingCache.getCacheSchedulingMapValueByHKey(ConstDateUtil.formatDate(ConstDateUtil.getTheSpecifiedNumberOfDaysOfTime(i)),jobCode);
if (!CollectionUtil.isEmpty(dto)) {
dto.sort(Comparator.comparing(DriverScheduling::getZdsjT));
if (i == -1) {
LocalDateTime zdsjT = ConstDateUtil.getLocalDateTimeByLongTime(dto.get(dto.size() - 1).getZdsjT());
LocalDateTime nowTime = ConstDateUtil.getLocalDateTimeByLongTime(now);
long range = ChronoUnit.MINUTES.between(zdsjT, nowTime);
// 判定存在排班 前天的最后一次打卡时间超过2小时,排班为null
if (range > 120L) {
dto = null;
}
}
break;
}
}
return dto;
}
/**
* 计算签到逻辑
*
* @param dto
* @param now
* @return
*/
public DriverSignRecommendation computedTheCurrentClosestTimestamp(List<DriverScheduling> dto, Long now, Integer result) {
Map<Integer, DriverSignRecommendation> timeMap = new HashMap<>();
for (int i = 0; i < dto.size(); i++) {
timeMap.put(i, new DriverSignRecommendation(dto.get(i), i));
}
long minDiff = Long.MAX_VALUE;
Integer index = 0;
// 迭代比较每个时间戳与当前时间戳的差值
for (Integer i : timeMap.keySet()) {
long diff = Math.abs(now - timeMap.get(i).getTimestamps());
if (diff < minDiff) {
minDiff = diff;
index = i;
}
}
if (index + result >= 0 && index + result < timeMap.size()) {
index = index + result;
}
return timeMap.get(index);
}
/**
* 更具最新的签到记录判断是否需要更新考勤。
*
* @param dto
* @param signIn
* @param globalIndex
*/
public void computedSignInBySignIn(List<DriverScheduling> dto, SignIn signIn, GlobalIndex globalIndex) {
// 无排班不记录不在考勤表不更新
if (CollectionUtil.isEmpty(dto)) {
return;
}
// 更新最新的签到记录判断是否需要更新考勤
// 记录为空直接插入记录
if (Objects.isNull(dto.get(globalIndex.getIndex()).getSignInId())) {
schedulingMapper.updateRoster(dto.get(globalIndex.getIndex()), signIn.getId(), signIn.getExType(), signIn.getCreateTime(), signIn.getRemark(), signIn.getType(), signIn.getAlcoholFlag(), signIn.getAlcoholIntake());
// 更新缓存
nowSchedulingCache.updateCacheByJobCode(ConstDateUtil.formatDate(dto.get(0).getScheduleDate()), globalIndex.getIndex(), signIn);
}
// 之前的无效
else if (!dto.get(globalIndex.getIndex()).getExType().equals(SIGN_NO_EX_NUM)) {
schedulingMapper.updateRoster(dto.get(globalIndex.getIndex()), signIn.getId(), signIn.getExType(), signIn.getCreateTime(), signIn.getRemark(), signIn.getType(), signIn.getAlcoholFlag(), signIn.getAlcoholIntake());
nowSchedulingCache.updateCacheByJobCode(ConstDateUtil.formatDate(dto.get(0).getScheduleDate()), globalIndex.getIndex(), signIn);
}
// 之前的有效
else {
handleRecord(dto, signIn, globalIndex);
}
}
private void handleRecord(List<DriverScheduling> dto, SignIn signIn, GlobalIndex globalIndex) {
if (globalIndex.getIndex() == dto.size() - 1) {
return;
}
Integer index = globalIndex.getIndex() + 1;
// 之前是签到 | 目前有效记录 -》进行修改 变成无效
if (signIn.getExType().equals(SIGN_NO_EX_NUM)) {
signIn.setStatus(SIGN_IN_FAIL);
signIn.setExType(SIGN_TIME_OUT_EX_NUM);
signIn.setRemark(dto.get(index).getBcType().equals(BC_TYPE_IN) ? SIGN_OUT_TIMEOUT : SIGN_IN_TIMEOUT);
signIn.setRemark(signIn.getRemark().replaceFirst(",$", "。"));
signInMapper.updateSignIn(signIn);
}
// 之前是签到 | 目前无效 -》往后更新
schedulingMapper.updateRoster(dto.get(globalIndex.getIndex() + 1), signIn.getId(), signIn.getExType(), signIn.getCreateTime(), signIn.getRemark(), signIn.getType(), signIn.getAlcoholFlag(), signIn.getAlcoholIntake());
nowSchedulingCache.updateCacheByJobCode(ConstDateUtil.formatDate(dto.get(0).getScheduleDate()), globalIndex.getIndex() + 1, signIn);
}
public List<ReportViewResponseVo> queryReportTableResponseVo(ReportViewRequestVo requestVo, HttpServletResponse response) {
// 处理天
if (requestVo.getExportFlag().equals(NOW)) {
return getDayReportTableResponseVo(requestVo, response);
}
// // 处理月
// else if (requestVo.getExportFlag().equals(MONTH)) {
// return getMonthReportTableResponseVo(requestVo, response);
// }
return null;
}
private List<ReportViewResponseVo> getMonthReportTableResponseVo(ReportViewRequestVo requestVo, HttpServletResponse response) {
// TODO
// 获取当月到目前为止的所有数据
List<String> dayList = getAllDaysOfTheMonth();
// for (String date : dayList) {
// getDayReportTableResponseVo(date, response);
// }
return null;
}
private List<String> getAllDaysOfTheMonth() {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 获取当前月份
YearMonth currentMonth = YearMonth.from(currentDate);
int nowDay = Integer.parseInt(ConstDateUtil.formatDate("dd"));
// 构建日期集合
List<String> datesInMonth = new ArrayList<>();
for (int day = 1; day <= nowDay; day++) {
String date = currentMonth.atDay(day).toString();
datesInMonth.add(date);
}
return datesInMonth;
}
private List<ReportViewResponseVo> getDayReportTableResponseVo(ReportViewRequestVo vo, HttpServletResponse response) {
// 签到数据
List<DriverScheduling> toDay = schedulingMapper.queryToDay(vo.getDate(), vo.getName(), vo.getJobCode(), vo.getLineName());
// 转换日期 + jobCode为key
Map<String, List<DriverScheduling>> orangeMap = new HashMap<>(1200);
for (DriverScheduling scheduling : toDay) {
String key = vo.getDate() + scheduling.getJobCode();
if (Objects.isNull(orangeMap.get(key))) {
orangeMap.put(key, new ArrayList<>(Arrays.asList(scheduling)));
} else {
orangeMap.get(key).add(scheduling);
}
}
Map<String, ReportViewResponseVo> resultMap = new HashMap<>(1200);
for (String key : orangeMap.keySet()) {
List<DriverScheduling> list = orangeMap.get(key);
ReportViewResponseVo vo1 = new ReportViewResponseVo();
handleScheduling(list, vo1);
resultMap.put(key, vo1);
}
return new ArrayList<>(resultMap.values());
}
private static void handleScheduling(List<DriverScheduling> list, ReportViewResponseVo vo) {
int planSignInCount = 0;
int actualSignInCount = 0;
int planSignOutCount = 0;
int actualSignOutCount = 0;
BeanUtils.copyProperties(list.get(0), vo);
String exString = NO_EX;
for (DriverScheduling scheduling : list) {
// 获取计划签到|签退次数
if (scheduling.getBcType().equals(BC_TYPE_OUT)) {
planSignInCount = planSignInCount + 1;
if (!Objects.isNull(scheduling.getSignInId())) {
actualSignInCount = actualSignInCount + 1;
}
} else {
planSignOutCount = planSignOutCount + 1;
if (!Objects.isNull(scheduling.getSignInId())) {
actualSignOutCount = actualSignOutCount + 1;
}
}
// 判断是否异常
if (!SIGN_NO_EX_NUM.equals(scheduling.getExType())) {
exString = HAVE_EX;
}
}
vo.setExString(exString);
vo.setPlanSignInCount(planSignInCount);
vo.setPlanSignOutCount(planSignOutCount);
vo.setActualSignInCount(actualSignInCount);
vo.setActualSignOutCount(actualSignOutCount);
}
}