Commit b69f10e6 by zhangshaowu

排班信息

1 parent d3148508
...@@ -62,7 +62,7 @@ public class RosteringController { ...@@ -62,7 +62,7 @@ public class RosteringController {
@PostMapping(value = "gen") @PostMapping(value = "gen")
public Result genRostering(@RequestParam("month") int month, public Result genRostering(@RequestParam("date") Date date,
@RequestParam("shiftRuleModel") String shiftRuleModelStr, @RequestParam("shiftRuleModel") String shiftRuleModelStr,
@RequestParam("staffRuleModel") String staffRuleModelStr, @RequestParam("staffRuleModel") String staffRuleModelStr,
@RequestParam("dateRuleModel") String dateRuleModelStr) { @RequestParam("dateRuleModel") String dateRuleModelStr) {
...@@ -70,7 +70,7 @@ public class RosteringController { ...@@ -70,7 +70,7 @@ public class RosteringController {
List<ShiftRuleModel> shiftRuleModels = JSONObject.parseArray(shiftRuleModelStr, ShiftRuleModel.class); List<ShiftRuleModel> shiftRuleModels = JSONObject.parseArray(shiftRuleModelStr, ShiftRuleModel.class);
List<StaffRuleModel> staffRuleModels = JSONObject.parseArray(staffRuleModelStr, StaffRuleModel.class); List<StaffRuleModel> staffRuleModels = JSONObject.parseArray(staffRuleModelStr, StaffRuleModel.class);
List<DateRuleModel> dateRuleModels = JSONObject.parseArray(dateRuleModelStr, DateRuleModel.class); List<DateRuleModel> dateRuleModels = JSONObject.parseArray(dateRuleModelStr, DateRuleModel.class);
return ResultGenerator.genSuccessResult(rosteringService.gen(month, shiftRuleModels, staffRuleModels, dateRuleModels)); return ResultGenerator.genSuccessResult(rosteringService.gen(date, shiftRuleModels, staffRuleModels, dateRuleModels));
} }
......
...@@ -8,6 +8,7 @@ import com.pipihelper.project.rostering.model.RosteringModel; ...@@ -8,6 +8,7 @@ import com.pipihelper.project.rostering.model.RosteringModel;
import com.pipihelper.project.rostering.model.ShiftRuleModel; import com.pipihelper.project.rostering.model.ShiftRuleModel;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
...@@ -31,13 +32,13 @@ public interface RosteringService { ...@@ -31,13 +32,13 @@ public interface RosteringService {
/** /**
* 生成班次 * 生成班次
* *
* @param month * @param date
* @param shiftRuleModels 班次规则 * @param shiftRuleModels 班次规则
* @param staffRuleModels 原因规则 * @param staffRuleModels 原因规则
* @param dateRuleModels 时间规则 * @param dateRuleModels 时间规则
* @return * @return
*/ */
List<RosteringModel> gen(int month, List<RosteringModel> gen(Date date,
List<ShiftRuleModel> shiftRuleModels, List<ShiftRuleModel> shiftRuleModels,
List<StaffRuleModel> staffRuleModels, List<StaffRuleModel> staffRuleModels,
List<DateRuleModel> dateRuleModels); List<DateRuleModel> dateRuleModels);
......
...@@ -24,6 +24,7 @@ import org.springframework.stereotype.Service; ...@@ -24,6 +24,7 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -41,9 +42,6 @@ import java.util.stream.Collectors; ...@@ -41,9 +42,6 @@ import java.util.stream.Collectors;
public class RosteringServiceImpl implements RosteringService { public class RosteringServiceImpl implements RosteringService {
@Autowired @Autowired
private RosteringStaffDao rosteringStaffDao; private RosteringStaffDao rosteringStaffDao;
...@@ -58,7 +56,6 @@ public class RosteringServiceImpl implements RosteringService { ...@@ -58,7 +56,6 @@ public class RosteringServiceImpl implements RosteringService {
Map<String, List<RosteringExportModel>> dataMap = rosteringModels.stream().collect(Collectors.groupingBy(RosteringExportModel::getName)); Map<String, List<RosteringExportModel>> dataMap = rosteringModels.stream().collect(Collectors.groupingBy(RosteringExportModel::getName));
List<ExcelExportEntity> colList = new ArrayList<>(); List<ExcelExportEntity> colList = new ArrayList<>();
ExcelExportEntity departmentCol = new ExcelExportEntity(); ExcelExportEntity departmentCol = new ExcelExportEntity();
departmentCol.setName("部门"); departmentCol.setName("部门");
...@@ -141,52 +138,57 @@ public class RosteringServiceImpl implements RosteringService { ...@@ -141,52 +138,57 @@ public class RosteringServiceImpl implements RosteringService {
} }
@Override @Override
public List<RosteringModel> gen(int month, public List<RosteringModel> gen(Date date,
List<ShiftRuleModel> shiftRuleModels, List<ShiftRuleModel> shiftRuleModels,
List<StaffRuleModel> staffRuleModels, List<StaffRuleModel> staffRuleModels,
List<DateRuleModel> dateRuleModels) { List<DateRuleModel> dateRuleModels) {
int daysOfMonth = getDaysOfNextMonth(month); int daysOfMonth = getDaysOfNextMonth(date);
Map<String, StaffRuleModel> staffRuleModelMap = staffRuleModels.stream().collect(Collectors.toMap(StaffRuleModel::getName, Function.identity())); Map<String, StaffRuleModel> staffRuleModelMap = staffRuleModels.stream().collect(Collectors.toMap(StaffRuleModel::getName, Function.identity()));
//记录用户当次排版已经排了多少次 //记录用户当次排版已经排了多少次
Map<String, List<ShiftRuleModel>> staffMap = new HashMap<>(); Map<String, List<ShiftRuleModel>> staffMap = new HashMap<>();
for (int i = 0; i < daysOfMonth; i++) { for (int i = 0; i < daysOfMonth; i++) {
int currentDay = i + 1; int currentDay = i + 1;
staffRuleModels.forEach(e -> { int year = DateUtil.year(date);
List<ShiftRuleModel> currentShifts = Optional.ofNullable(staffMap.get(e.getName())).orElse(Lists.newArrayList()); int month = DateUtil.month(date);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, currentDay);
Date time = calendar.getTime();
//指定日期班次 DateRuleModel dateRuleModel = dateRuleModels.stream().filter(e -> DateUtil.isSameDay(e.getDate(), time)).findAny().orElse(null);
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) { List<ShiftRuleModel> dateShifts = new ArrayList<>();
shiftRuleModel = getByShiftRule(shiftRuleModels, currentShifts, e);
}
//判断是否已经达到了最大次数 staffRuleModels.forEach(e -> {
StaffRuleModel.Threshold maxTimes = e.getMaxTimes(); List<ShiftRuleModel> currentShifts = Optional.ofNullable(staffMap.get(e.getName())).orElse(Lists.newArrayList());
if (maxTimes != null && maxTimes.getTimes() != null && StringUtils.isNotBlank(maxTimes.getShift())) {
String shift = maxTimes.getShift(); ShiftRuleModel shiftRuleModel = gen(e, shiftRuleModels, currentDay, currentShifts);
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中 //添加到map中
currentShifts.add(shiftRuleModel); currentShifts.add(shiftRuleModel);
staffMap.putIfAbsent(e.getName(), currentShifts); staffMap.putIfAbsent(e.getName(), currentShifts);
dateShifts.add(shiftRuleModel);
}); });
//日期规则配置
if (dateRuleModel != null) {
Integer times = dateRuleModel.getTimes();
String shift = dateRuleModel.getShift();
int size = (int) dateShifts.stream().filter(e -> ObjectUtil.equal(e.getName(), shift)).count();
//判断是否满足每天指定班次的人数
if (size < times) {
}
}
} }
List<RosteringModel> result = new ArrayList<>(); List<RosteringModel> result = new ArrayList<>();
...@@ -217,23 +219,45 @@ public class RosteringServiceImpl implements RosteringService { ...@@ -217,23 +219,45 @@ public class RosteringServiceImpl implements RosteringService {
} }
private ShiftRuleModel gen(StaffRuleModel staffRuleModel, List<ShiftRuleModel> shiftRuleModels, Integer currentDay, List<ShiftRuleModel> currentShifts) {
//指定日期班次
StaffRuleModel.Threshold fixedShift = staffRuleModel.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, staffRuleModel);
}
//判断是否已经达到了最大次数
StaffRuleModel.Threshold maxTimes = staffRuleModel.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()));
}
}
return shiftRuleModel;
}
/** /**
* 获取某个月的天数 * 获取某个月的天数
* *
* @return * @return
*/ */
private int getDaysOfNextMonth(int month) { private int getDaysOfNextMonth(Date date) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
//获得当前日期往后推1个月 amount 为设置的月份值 +为往后推 +号可以省略 -为往前推 calendar.setTime(date);
calendar.add(Calendar.MONTH, +1); calendar.set(Calendar.DATE, 1);
//获得下一个月是多少年 calendar.roll(Calendar.DATE, -1);
int year = calendar.get(Calendar.YEAR); return calendar.get(Calendar.DATE);
calendar.set(year, month, 0);
//获得下一个月有多少天
return calendar.get(Calendar.DAY_OF_MONTH);
} }
/** /**
* 根据班次规则获取 * 根据班次规则获取
* *
......
spring: spring:
profiles: profiles:
active: prod active: dev
# 日志 # 日志
logging: logging:
file: file:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!