RosteringServiceImpl.java
8.75 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
package com.pipihelper.project.rostering.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.google.common.collect.Lists;
import com.pipihelper.project.rostering.model.DateRuleModel;
import com.pipihelper.project.rostering.model.RosteringModel;
import com.pipihelper.project.rostering.model.ShiftRuleModel;
import com.pipihelper.project.rostering.model.StaffRuleModel;
import com.pipihelper.project.rostering.service.RosteringService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @description:
* @author: zsw
* @create: 2022-10-14 16:51
**/
@Service
public class RosteringServiceImpl implements RosteringService {
@Override
public List<RosteringModel> gen(int month,
List<ShiftRuleModel> shiftRuleModels,
List<StaffRuleModel> staffRuleModels,
List<DateRuleModel> dateRuleModels) {
int daysOfMonth = getDaysOfNextMonth(month);
Map<String, StaffRuleModel> staffRuleModelMap = staffRuleModels.stream().collect(Collectors.toMap(StaffRuleModel::getName, Function.identity()));
//记录用户当次排版已经排了多少次
Map<String, List<ShiftRuleModel>> staffMap = new HashMap<>();
for (int i = 0; i < daysOfMonth; i++) {
int currentDay = i + 1;
staffRuleModels.forEach(e -> {
List<ShiftRuleModel> currentShifts = Optional.ofNullable(staffMap.get(e.getName())).orElse(Lists.newArrayList());
//指定日期班次
StaffRuleModel.Threshold fixedShift = e.getFixedShift();
boolean isFixed = Optional.ofNullable(fixedShift).map(StaffRuleModel.Threshold::getDate).filter(fixedDate -> DateUtil.dayOfMonth(fixedDate) == currentDay).isPresent();
ShiftRuleModel shiftRuleModel = null;
if (isFixed) {
shiftRuleModel = shiftRuleModels.stream().filter(shift -> ObjectUtil.equal(shift.getName(), fixedShift.getShift())).findFirst().orElse(null);
}
if (shiftRuleModel == null) {
shiftRuleModel = getByShiftRule(shiftRuleModels, currentShifts, e);
}
//判断是否已经达到了最大次数
StaffRuleModel.Threshold maxTimes = e.getMaxTimes();
if (maxTimes != null && maxTimes.getTimes() != null && StringUtils.isNotBlank(maxTimes.getShift())) {
String shift = maxTimes.getShift();
int count = (int) currentShifts.stream().filter(result -> ObjectUtil.equal(result.getName(), shift)).count();
count = ObjectUtil.equal(shiftRuleModel.getName(), shift) ? count + 1 : count;
if (count > maxTimes.getTimes()) {
shiftRuleModel = getRandom(shiftRuleModels.stream().filter(ignore -> ObjectUtil.notEqual(ignore.getName(), shift)).collect(Collectors.toList()));
}
}
//添加到map中
currentShifts.add(shiftRuleModel);
staffMap.putIfAbsent(e.getName(), currentShifts);
});
}
List<RosteringModel> result = new ArrayList<>();
staffRuleModels.forEach(e -> {
List<String> shifts = Optional.ofNullable(staffMap.get(e.getName())).orElse(Lists.newArrayList())
.stream().map(ShiftRuleModel::getName).collect(Collectors.toList());
StaffRuleModel.Threshold minTimes = e.getMinTimes();
if (minTimes != null && minTimes.getTimes() != null && StringUtils.isNotBlank(minTimes.getShift())) {
int count = (int) shifts.stream().filter(shift -> ObjectUtil.equal(shift, minTimes.getShift())).count();
int diff = minTimes.getTimes() - count;
if (diff > 0) {
int index = 0;
for (int i = 0; i < diff; i++) {
index = getRandomInt(shifts.size() - 1, index);
shifts.set(index, minTimes.getShift());
}
}
}
RosteringModel rosteringModel = new RosteringModel();
rosteringModel.setName(e.getName());
rosteringModel.setShift(shifts);
result.add(rosteringModel);
});
return result;
}
/**
* 获取某个月的天数
*
* @return
*/
private int getDaysOfNextMonth(int month) {
Calendar calendar = Calendar.getInstance();
//获得当前日期往后推1个月 amount 为设置的月份值 +为往后推 +号可以省略 -为往前推
calendar.add(Calendar.MONTH, +1);
//获得下一个月是多少年
int year = calendar.get(Calendar.YEAR);
calendar.set(year, month, 0);
//获得下一个月有多少天
return calendar.get(Calendar.DAY_OF_MONTH);
}
/**
* 根据班次规则获取
*
* @param shiftRuleModels
* @param currentShifts
* @return
*/
private ShiftRuleModel getByShiftRule(List<ShiftRuleModel> shiftRuleModels, List<ShiftRuleModel> currentShifts, StaffRuleModel staffRuleModel) {
//随机获取,当前排班为null 则代表是第一次排班
if (CollectionUtil.isEmpty(currentShifts)) {
return getRandom(shiftRuleModels.stream().filter(e -> BooleanUtil.isFalse(e.isRest())).collect(Collectors.toList()));
}
//获取最后一次分配的班次
int size = currentShifts.size();
ShiftRuleModel lastShift = currentShifts.get(size - 1);
//获取最后一次分配的班次的连续次数
int currentShiftTimes = 0;
for (int i = size - 1; i >= 0; i--) {
ShiftRuleModel currentShift = currentShifts.get(i);
if (ObjectUtil.equal(lastShift.getName(), currentShift.getName())) {
currentShiftTimes++;
} else {
break;
}
}
//根据次数获取
ShiftRuleModel result = getByTimes(shiftRuleModels, lastShift, currentShiftTimes);
if (result == null) {
//证明没有设置连续班,首先判断允许班次是否为空,不为空则从允许班次中随机获取一个班次
List<String> backAllow = lastShift.getBackAllow();
if (CollectionUtil.isNotEmpty(backAllow)) {
result = getRandom(shiftRuleModels.stream().filter(e -> backAllow.contains(e.getName())).collect(Collectors.toList()));
}
//证明没有设置连续班,首先判断不允许允许班次是否为空,不为空则排除不允许班次随机获取班次
List<String> backDenied = lastShift.getBackDenied();
if (CollectionUtil.isNotEmpty(backDenied)) {
result = getRandom(shiftRuleModels.stream().filter(e -> !backDenied.contains(e.getName())).collect(Collectors.toList()));
}
}
if (result == null) {
result = getRandom(shiftRuleModels);
}
//是否有休息并且
if (BooleanUtil.isFalse(result.isRest()) && BooleanUtil.isFalse(lastShift.isRest())) {
result = shiftRuleModels.stream()
.filter(e -> ObjectUtil.equal(e.getName(), lastShift.getName()))
.findFirst().orElse(null);
}
return result;
}
private ShiftRuleModel getByTimes(List<ShiftRuleModel> shiftRuleModels, ShiftRuleModel lastShift, Integer currentShiftTimes) {
//当前班次的次数小于最大连续次数,则返回同样班次
Integer maxContinuity = lastShift.getMaxContinuity();
if (maxContinuity == null) {
return null;
}
if (currentShiftTimes < maxContinuity) {
return lastShift;
}
List<String> maxContinuityFollows = lastShift.getMaxContinuityFollow();
return shiftRuleModels.stream().filter(e -> maxContinuityFollows.contains(e.getName())).findFirst().orElse(null);
}
private ShiftRuleModel getRandom(List<ShiftRuleModel> shiftRuleModels) {
return shiftRuleModels.get(RandomUtil.randomInt(0, shiftRuleModels.size()));
}
private int getRandomInt(int size, int index) {
int i = RandomUtil.randomInt(0, size);
if (i == index) {
return getRandomInt(size, index);
}
return i;
}
}