|
|
@@ -2,9 +2,12 @@ package se.cloud.analysis.service;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import se.cloud.analysis.dao.TaskDao;
|
|
|
import se.cloud.analysis.domain.Event;
|
|
|
import se.cloud.analysis.domain.Task;
|
|
|
+import se.cloud.analysis.exception.GlobalException;
|
|
|
+import se.cloud.analysis.result.CodeMsg;
|
|
|
import se.cloud.analysis.vo.TaskDetailVO;
|
|
|
|
|
|
import java.util.Date;
|
|
|
@@ -19,7 +22,16 @@ public class TaskService {
|
|
|
@Autowired
|
|
|
private EventService eventService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
public List<Task> getTaskList(String email) {
|
|
|
+ if (StringUtils.isEmpty(email)) {
|
|
|
+ throw new GlobalException(CodeMsg.EMAIL_EMPTY);
|
|
|
+ }
|
|
|
+ if (userService.getUserByEmail(email) == null) {
|
|
|
+ throw new GlobalException(CodeMsg.USER_NOT_EXISTS);
|
|
|
+ }
|
|
|
return taskDao.getTaskByEmail(email);
|
|
|
}
|
|
|
|
|
|
@@ -28,6 +40,14 @@ public class TaskService {
|
|
|
}
|
|
|
|
|
|
public boolean addTask(Task task) {
|
|
|
+ Task find = getTaskByEmailAndName(task.getUserEmail(), task.getTaskName());
|
|
|
+ if (find != null) {
|
|
|
+ throw new GlobalException(CodeMsg.TASK_ALREADY_EXISTS);
|
|
|
+ }
|
|
|
+ if (userService.getUserByEmail(task.getUserEmail()) == null) {
|
|
|
+ throw new GlobalException(CodeMsg.USER_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ task.setIsDone(false);
|
|
|
// TODO 事务实现
|
|
|
Date start = task.getStart();
|
|
|
Date end = task.getEnd();
|
|
|
@@ -43,26 +63,59 @@ public class TaskService {
|
|
|
endEvent.setType("task");
|
|
|
endEvent.setTime(end);
|
|
|
endEvent.setTaskName(task.getTaskName());
|
|
|
- int startEventRes = eventService.addEvent(startEvent);
|
|
|
- int endEventRes = eventService.addEvent(endEvent);
|
|
|
- int taskRes = taskDao.insert(task);
|
|
|
- return startEventRes + endEventRes + taskRes == 3;
|
|
|
+ boolean startEventRes = eventService.addEvent(startEvent);
|
|
|
+ boolean endEventRes = eventService.addEvent(endEvent);
|
|
|
+ boolean taskRes = taskDao.insert(task) == 1;
|
|
|
+ return startEventRes && endEventRes && taskRes;
|
|
|
}
|
|
|
|
|
|
- public int updateTask(Task task) {
|
|
|
- return taskDao.update(task);
|
|
|
+ public boolean updateTask(Task task) {
|
|
|
+ if (task.getId() == null || task.getId() < 0) {
|
|
|
+ throw new GlobalException(CodeMsg.REQUEST_ILLEGAL);
|
|
|
+ }
|
|
|
+ if (userService.getUserByEmail(task.getUserEmail()) == null) {
|
|
|
+ throw new GlobalException(CodeMsg.EMAIL_NOT_EXIST);
|
|
|
+ }
|
|
|
+ if (getTaskByEmailAndName(task.getUserEmail(), task.getTaskName()) != null) {
|
|
|
+ throw new GlobalException(CodeMsg.TASK_ALREADY_EXISTS);
|
|
|
+ }
|
|
|
+ if (taskDao.update(task) == 1) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ throw new GlobalException(CodeMsg.SERVER_ERROR);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public int deleteTask(int taskId) {
|
|
|
- return taskDao.deleteById(taskId);
|
|
|
+ public boolean deleteTask(int taskId) {
|
|
|
+ Task task = getTaskById(taskId);
|
|
|
+ if (task == null) {
|
|
|
+ throw new GlobalException(CodeMsg.TASK_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ if (taskDao.deleteById(taskId) == 1) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ throw new GlobalException(CodeMsg.SERVER_ERROR);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public Task getTaskByEmailAndName(String email, String name) {
|
|
|
+ if (StringUtils.isEmpty(email) || StringUtils.isEmpty(name)) {
|
|
|
+ throw new GlobalException(CodeMsg.REQUEST_ILLEGAL);
|
|
|
+ }
|
|
|
return taskDao.getTaskByEmailAndName(email, name);
|
|
|
}
|
|
|
|
|
|
public TaskDetailVO getTaskDetail(String email, int id) {
|
|
|
+ if (StringUtils.isEmpty(email) || id < 0) {
|
|
|
+ throw new GlobalException(CodeMsg.REQUEST_ILLEGAL);
|
|
|
+ }
|
|
|
+ if (userService.getUserByEmail(email) == null) {
|
|
|
+ throw new GlobalException(CodeMsg.EMAIL_NOT_EXIST);
|
|
|
+ }
|
|
|
Task task = getTaskById(id);
|
|
|
+ if (task == null) {
|
|
|
+ throw new GlobalException(CodeMsg.TASK_NOT_EXISTS);
|
|
|
+ }
|
|
|
List<Event> events = eventService.getTaskEventList(email, task.getTaskName());
|
|
|
TaskDetailVO taskDetailVO = new TaskDetailVO();
|
|
|
taskDetailVO.setEvents(events);
|