|
@@ -12,7 +12,11 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
+import java.util.Comparator;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
/**
|
|
* <p>
|
|
* <p>
|
|
@@ -44,6 +48,13 @@ public class TaskDailyAllocateController {
|
|
HttpRespMsg msg = new HttpRespMsg();
|
|
HttpRespMsg msg = new HttpRespMsg();
|
|
if (StringUtils.isNotEmpty(jsonStr)){
|
|
if (StringUtils.isNotEmpty(jsonStr)){
|
|
List<TaskDailyAllocate> taskDailyAllocates = JSONArray.parseArray(jsonStr, TaskDailyAllocate.class);
|
|
List<TaskDailyAllocate> taskDailyAllocates = JSONArray.parseArray(jsonStr, TaskDailyAllocate.class);
|
|
|
|
+
|
|
|
|
+ String taskTimeNonOverlapping = isTaskTimeNonOverlapping(taskDailyAllocates);
|
|
|
|
+ if (StringUtils.isNotEmpty(taskTimeNonOverlapping)){
|
|
|
|
+ msg.setError(taskTimeNonOverlapping);
|
|
|
|
+ return msg;
|
|
|
|
+ }
|
|
|
|
+
|
|
for (TaskDailyAllocate taskDailyAllocate : taskDailyAllocates) {
|
|
for (TaskDailyAllocate taskDailyAllocate : taskDailyAllocates) {
|
|
int count=taskDailyAllocateService.getConflict(taskDailyAllocate, request);
|
|
int count=taskDailyAllocateService.getConflict(taskDailyAllocate, request);
|
|
if(count>0){
|
|
if(count>0){
|
|
@@ -56,5 +67,55 @@ public class TaskDailyAllocateController {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 检查同一用户在同一天的任务时间是否重叠
|
|
|
|
+ * @param taskDailyAllocates 任务分配列表
|
|
|
|
+ * @return true=无重叠,false=有重叠
|
|
|
|
+ */
|
|
|
|
+ public static String isTaskTimeNonOverlapping(List<TaskDailyAllocate> taskDailyAllocates) {
|
|
|
|
+ if (taskDailyAllocates == null || taskDailyAllocates.isEmpty()) {
|
|
|
|
+ return "列表为空";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 1. 按 userId 和 allocateDate 分组
|
|
|
|
+ Map<String, Map<LocalDate, List<TaskDailyAllocate>>> userDateTasksMap = taskDailyAllocates.stream()
|
|
|
|
+ .collect(Collectors.groupingBy(
|
|
|
|
+ TaskDailyAllocate::getUserId,
|
|
|
|
+ Collectors.groupingBy(TaskDailyAllocate::getAllocateDate)
|
|
|
|
+ ));
|
|
|
|
+
|
|
|
|
+ // 2. 遍历每个用户每天的分配情况
|
|
|
|
+ for (Map.Entry<String, Map<LocalDate, List<TaskDailyAllocate>>> userEntry : userDateTasksMap.entrySet()) {
|
|
|
|
+ String userId = userEntry.getKey();
|
|
|
|
+ Map<LocalDate, List<TaskDailyAllocate>> dateTasksMap = userEntry.getValue();
|
|
|
|
+
|
|
|
|
+ for (Map.Entry<LocalDate, List<TaskDailyAllocate>> dateEntry : dateTasksMap.entrySet()) {
|
|
|
|
+ LocalDate date = dateEntry.getKey();
|
|
|
|
+ List<TaskDailyAllocate> tasks = dateEntry.getValue();
|
|
|
|
+
|
|
|
|
+ // 3. 按 startTime 排序
|
|
|
|
+ tasks.sort(Comparator.comparing(TaskDailyAllocate::getStartTime));
|
|
|
|
+
|
|
|
|
+ // 4. 检查相邻任务是否重叠
|
|
|
|
+ for (int i = 0; i < tasks.size() - 1; i++) {
|
|
|
|
+ TaskDailyAllocate current = tasks.get(i);
|
|
|
|
+ TaskDailyAllocate next = tasks.get(i + 1);
|
|
|
|
+
|
|
|
|
+ if (current.getEndTime().isAfter(next.getStartTime())) {
|
|
|
|
+ System.err.printf(
|
|
|
|
+ "时间冲突:用户 %s 在 %s 的任务 %s-%s 与 %s-%s 重叠%n",
|
|
|
|
+ userId, date,
|
|
|
|
+ current.getStartTime(), current.getEndTime(),
|
|
|
|
+ next.getStartTime(), next.getEndTime()
|
|
|
|
+ );
|
|
|
|
+ return "时间冲突:在 "+date+" 的任务 "+current.getStartTime()+"-"+current.getEndTime()+" 与 "+next.getStartTime()+"-"+next.getEndTime()+" 有冲突";
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|