RosteringServiceImpl.java
12.2 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package com.pipihelper.project.rostering.service.impl;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
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.feishu.dao.RosteringStaffDao;
import com.pipihelper.project.feishu.entity.RosteringStaff;
import com.pipihelper.project.rostering.model.DateRuleModel;
import com.pipihelper.project.rostering.model.RosteringExportModel;
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.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
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 {
@Autowired
private RosteringStaffDao rosteringStaffDao;
@Override
public Workbook export(List<RosteringExportModel> rosteringModels) {
Set<String> names = rosteringModels.stream().map(RosteringExportModel::getName).collect(Collectors.toSet());
Map<String, RosteringStaff> rosteringStaffMap = Optional.ofNullable(rosteringStaffDao.findByNames(names))
.orElse(Lists.newArrayList())
.stream()
.collect(Collectors.toMap(RosteringStaff::getName, Function.identity()));
Map<String, List<RosteringExportModel>> dataMap = rosteringModels.stream().collect(Collectors.groupingBy(RosteringExportModel::getName));
List<ExcelExportEntity> colList = new ArrayList<>();
ExcelExportEntity departmentCol = new ExcelExportEntity();
departmentCol.setName("部门");
departmentCol.setKey("department");
departmentCol.setOrderNum(1);
colList.add(departmentCol);
ExcelExportEntity dingIdCol = new ExcelExportEntity();
dingIdCol.setName("用户ID");
dingIdCol.setKey("dingId");
dingIdCol.setOrderNum(2);
colList.add(dingIdCol);
ExcelExportEntity jobNoCol = new ExcelExportEntity();
jobNoCol.setName("工号");
jobNoCol.setKey("jobNo");
jobNoCol.setOrderNum(3);
colList.add(jobNoCol);
ExcelExportEntity nameCol = new ExcelExportEntity();
nameCol.setName("姓名");
nameCol.setKey("name");
nameCol.setOrderNum(4);
colList.add(nameCol);
List<String> dates = rosteringModels.stream().map(e -> {
String dateStr = DateUtil.format(e.getDate(), "yyyy/MM/dd");
return dateStr + e.getWeek();
}).distinct().collect(Collectors.toList());
for (int i = 0; i < dates.size(); i++) {
int orderNum = i + 5;
String name = dates.get(i);
ExcelExportEntity excelExportEntity = new ExcelExportEntity();
excelExportEntity.setName(name);
excelExportEntity.setKey(name);
excelExportEntity.setOrderNum(orderNum);
colList.add(excelExportEntity);
}
List<Map> datas = new ArrayList<>();
dataMap.forEach((k, v) -> {
Map<String, Object> map = new HashMap<>();
map.put("name", k);
RosteringStaff rosteringStaff = rosteringStaffMap.get(k);
map.put("department", rosteringStaff.getDepartment());
map.put("dingId", rosteringStaff.getDingId());
map.put("jobNo", rosteringStaff.getJobNo());
v.forEach(e -> {
String dateStr = DateUtil.format(e.getDate(), "yyyy/MM/dd");
String date = dateStr + e.getWeek();
map.put(date, e.getShift());
});
datas.add(map);
});
return ExcelExportUtil.exportExcel(new ExportParams(), colList, datas);
}
@Override
public List<RosteringStaff> findAllStaff() {
return rosteringStaffDao.findAll();
}
@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);
}
//获取最后一次分配的班次
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 = Optional.ofNullable(lastShift.getMaxContinuityFollow()).orElse(Lists.newArrayList());
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;
}
}