Skip to content

Instantly share code, notes, and snippets.

@suxiaogang
Created September 12, 2017 02:21
Show Gist options
  • Save suxiaogang/0e318580e9679f64f64a9e9f6d0fc05d to your computer and use it in GitHub Desktop.
Save suxiaogang/0e318580e9679f64f64a9e9f6d0fc05d to your computer and use it in GitHub Desktop.
TaskController.java
/*
* Copyright (c) 2016, All Rights Reserved.
*/
package com.mojinshi.rest.controller.task;
import com.mojinshi.commons.annotation.Permit;
import com.mojinshi.commons.constant.Permission;
import com.mojinshi.commons.domain.comment.Comment;
import com.mojinshi.commons.dto.common.PagingList;
import com.mojinshi.commons.dto.paging.Paging;
import com.mojinshi.commons.dto.workflow.TaskInfo;
import com.mojinshi.commons.rest.exception.GeneralException;
import com.mojinshi.commons.service.AuthService;
import com.mojinshi.commons.service.workflow.BpmTaskCommand;
import com.mojinshi.commons.service.workflow.WorkFlowService;
import com.mojinshi.domain.project.Keeper;
import com.mojinshi.dto.repair.RepairInfo;
import com.mojinshi.service.point.PointService;
import com.mojinshi.service.project.KeeperService;
import com.mojinshi.service.repair.RepairService;
import com.mojinshi.service.task.TaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* Created by ellazhao on 2016-03-31
*/
@RestController
@RequestMapping(value = {"/task"})
@Permit(value = Permission.AUTH_COMMON)
public class TaskController {
@Autowired
WorkFlowService workflow;
@Autowired
private AuthService authService;
@Autowired
private TaskService taskService;
@Autowired
private RepairService repairService;
@Autowired
private KeeperService keeperService;
@Autowired
private PointService pointService;
@Autowired
HttpServletRequest request;
protected ResponseEntity convertUnPagedList(Paging paging, List<TaskInfo<?>> unPagedList) {
if(paging == null) {
return new ResponseEntity(unPagedList, HttpStatus.OK);
} else {
PagingList<TaskInfo<?>> pageList = new PagingList<TaskInfo<?>>();
pageList.setTotalNumbers(unPagedList.size());
int end = 0;
if (paging.getStart() + paging.getNumber() < unPagedList.size()) {
end = paging.getStart() + paging.getNumber();
} else {
end = unPagedList.size();
}
pageList.setData(unPagedList.subList(paging.getStart(), end));
return new ResponseEntity(pageList, HttpStatus.OK);
}
}
@RequestMapping(value = "/todotasks", method = RequestMethod.GET)
@ResponseBody
ResponseEntity getToDoTasks() throws Exception {
Paging paging = (Paging) request.getAttribute("paging");
String userId = authService.getUserId();
List<TaskInfo<?>> toDoTasks = taskService.getToDoTasks(userId);
return convertUnPagedList(paging, toDoTasks);
}
@RequestMapping(value = "/inprogresstasks", method = RequestMethod.GET)
@ResponseBody
ResponseEntity getInProgressTasks() throws Exception {
Paging paging = (Paging) request.getAttribute("paging");
String userId = authService.getUserId();
List<TaskInfo<?>> tasks = taskService.getCompletedTasks(userId, false);
//filter out the duplicate tasks which belongs to same process instance
Set<String> filters = new HashSet<String>();
List<TaskInfo<?>> todoTasks = taskService.getToDoTasks(userId);
for (TaskInfo<?> item : todoTasks) {
filters.add(item.getId());
}
List<TaskInfo<?>> completedTasks = new LinkedList<TaskInfo<?>>();
for (TaskInfo<?> item : tasks) {
if (!filters.contains(item.getId())) {
filters.add(item.getId());
completedTasks.add(item);
}
}
return convertUnPagedList(paging, completedTasks);
}
@RequestMapping(value = "/completedtasks", method = RequestMethod.GET)
@ResponseBody
ResponseEntity getCompletedTasks() throws Exception {
Paging paging = (Paging) request.getAttribute("paging");
String userId = authService.getUserId();
List<TaskInfo<?>> tasks = taskService.getCompletedTasks(userId, true);
//filter out the duplicate tasks which belongs to same process instance
List<TaskInfo<?>> completedTasks = new LinkedList<TaskInfo<?>>();
Set<String> tmp = new HashSet<String>();
for (TaskInfo<?> item : tasks) {
if (!tmp.contains(item.getId())) {
tmp.add(item.getId());
completedTasks.add(item);
}
}
return convertUnPagedList(paging, completedTasks);
}
@RequestMapping(value = "/{hostObjId}/comment", method = RequestMethod.PUT)
public ResponseEntity<Comment> addComment(@PathVariable("hostObjId") String hostObjId, @RequestBody Comment comment) {
String userId = authService.getUserId();
taskService.addTaskComment(userId, hostObjId, comment);
return new ResponseEntity<Comment>(comment, HttpStatus.OK);
}
@RequestMapping(value = "/{hostObjId}/assign", method = RequestMethod.PUT)
@ResponseBody
public void assignTask(@PathVariable("hostObjId") String hostObjId, @RequestBody BpmTaskCommand command) throws GeneralException {
if (StringUtils.isEmpty(hostObjId)) {
throw new GeneralException("common.400.error");
}
String userId = authService.getUserId();
//如果是报修,那么报修被受理后获得积分. fix by suxiaogang 2016-09-14
RepairInfo repairInfo = repairService.getOne(hostObjId);
if (null != repairInfo) {
String keeperId = repairInfo.getKeeperId();
Keeper keeper = keeperService.findOne(keeperId);
if (null != keeper.getOpenId()) {
pointService.repaireAcceptPoint(keeper.getOpenId());
}
}
taskService.assignTask(userId, hostObjId, command);
}
@RequestMapping(value = "/{hostObjId}/complete", method = RequestMethod.PUT)
@ResponseBody
void complete(@PathVariable("hostObjId") String hostObjId, @RequestBody BpmTaskCommand command) {
String userId = authService.getUserId();
taskService.completeTask(userId, hostObjId, command);
}
//TODO: should be removed
@RequestMapping(value = "/{hostObjId}", method = RequestMethod.GET)
public ResponseEntity<TaskInfo<?>> getTaskDetail(@PathVariable String hostObjId) {
String userId = authService.getUserId();
TaskInfo<?> taskInfo = taskService.getTaskDetail(userId, hostObjId);
return new ResponseEntity<TaskInfo<?>>(taskInfo, HttpStatus.OK);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment