mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-07-23 15:41:17 +08:00
新增待办管理端异常处理能力
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package com.jsowell.web.controller.system;
|
||||
|
||||
import com.jsowell.common.annotation.Log;
|
||||
import com.jsowell.common.core.controller.BaseController;
|
||||
import com.jsowell.common.core.domain.AjaxResult;
|
||||
import com.jsowell.common.core.page.TableDataInfo;
|
||||
import com.jsowell.common.enums.BusinessType;
|
||||
import com.jsowell.system.domain.SysTodoTask;
|
||||
import com.jsowell.system.domain.dto.TodoTaskAdminCreateRequest;
|
||||
import com.jsowell.system.domain.dto.TodoTaskAdminQuery;
|
||||
import com.jsowell.system.domain.dto.TodoTaskAssignRequest;
|
||||
import com.jsowell.system.domain.dto.TodoTaskCancelRequest;
|
||||
import com.jsowell.system.service.TodoTaskAdminService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 待办管理端接口。
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/system/todo/admin")
|
||||
public class SysTodoTaskAdminController extends BaseController {
|
||||
@Autowired
|
||||
private TodoTaskAdminService todoTaskAdminService;
|
||||
|
||||
@PreAuthorize("@ss.hasPermi('system:todo:admin:list')")
|
||||
@GetMapping("/list")
|
||||
public TableDataInfo list(TodoTaskAdminQuery query) {
|
||||
startPage();
|
||||
List<SysTodoTask> list = todoTaskAdminService.list(query);
|
||||
return getDataTable(list);
|
||||
}
|
||||
|
||||
@PreAuthorize("@ss.hasPermi('system:todo:admin:create')")
|
||||
@Log(title = "待办管理", businessType = BusinessType.INSERT)
|
||||
@PostMapping("/create")
|
||||
public AjaxResult create(@Validated @RequestBody TodoTaskAdminCreateRequest request) {
|
||||
return AjaxResult.success(todoTaskAdminService.create(request, getUsername()));
|
||||
}
|
||||
|
||||
@PreAuthorize("@ss.hasPermi('system:todo:admin:assign')")
|
||||
@Log(title = "待办管理", businessType = BusinessType.UPDATE)
|
||||
@PutMapping("/{todoId}/assign")
|
||||
public AjaxResult assign(@PathVariable Long todoId,
|
||||
@Validated @RequestBody TodoTaskAssignRequest request) {
|
||||
return toAjax(todoTaskAdminService.assign(todoId, request, getUsername()));
|
||||
}
|
||||
|
||||
@PreAuthorize("@ss.hasPermi('system:todo:admin:cancel')")
|
||||
@Log(title = "待办管理", businessType = BusinessType.UPDATE)
|
||||
@PutMapping("/{todoId}/cancel")
|
||||
public AjaxResult cancel(@PathVariable Long todoId,
|
||||
@Validated @RequestBody TodoTaskCancelRequest request) {
|
||||
return toAjax(todoTaskAdminService.cancel(todoId, getUserId(), getUsername(), request.getReason()));
|
||||
}
|
||||
|
||||
@PreAuthorize("@ss.hasPermi('system:todo:admin:remove')")
|
||||
@Log(title = "待办管理", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{todoId}")
|
||||
public AjaxResult hide(@PathVariable Long todoId) {
|
||||
return toAjax(todoTaskAdminService.hide(todoId, getUsername()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package com.jsowell.system.service;
|
||||
|
||||
import com.jsowell.common.constant.Constants;
|
||||
import com.jsowell.common.core.domain.entity.SysUser;
|
||||
import com.jsowell.common.exception.ServiceException;
|
||||
import com.jsowell.system.constant.TodoTaskConstants;
|
||||
import com.jsowell.system.domain.SysTodoTask;
|
||||
import com.jsowell.system.domain.dto.TodoTaskAdminCreateRequest;
|
||||
import com.jsowell.system.domain.dto.TodoTaskAdminQuery;
|
||||
import com.jsowell.system.domain.dto.TodoTaskAssignRequest;
|
||||
import com.jsowell.system.domain.dto.TodoTaskCreateCommand;
|
||||
import com.jsowell.system.mapper.SysTodoTaskMapper;
|
||||
import com.jsowell.system.mapper.SysUserMapper;
|
||||
import com.jsowell.system.service.impl.TodoTaskAdminServiceImpl;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class TodoTaskAdminServiceImplTest {
|
||||
@Test
|
||||
void list_shouldUseAdminQueryWithoutForcingAssignee() {
|
||||
Dependencies dependencies = new Dependencies();
|
||||
TodoTaskAdminServiceImpl service = dependencies.createService();
|
||||
TodoTaskAdminQuery query = new TodoTaskAdminQuery();
|
||||
query.setTaskStatus(TodoTaskConstants.STATUS_ACTIVE);
|
||||
when(dependencies.todoTaskMapper.selectAdminTodoList(query))
|
||||
.thenReturn(Collections.singletonList(new SysTodoTask()));
|
||||
|
||||
assertEquals(1, service.list(query).size());
|
||||
verify(dependencies.todoTaskMapper).selectAdminTodoList(query);
|
||||
}
|
||||
|
||||
@Test
|
||||
void create_shouldValidateUserAndGenerateIdempotentKey() {
|
||||
Dependencies dependencies = new Dependencies();
|
||||
TodoTaskAdminServiceImpl service = dependencies.createService();
|
||||
when(dependencies.userMapper.selectUserById(8L)).thenReturn(activeUser(8L));
|
||||
when(dependencies.todoTaskService.createTask(any(TodoTaskCreateCommand.class)))
|
||||
.thenReturn(new SysTodoTask());
|
||||
TodoTaskAdminCreateRequest request = createRequest();
|
||||
|
||||
service.create(request, "admin");
|
||||
|
||||
ArgumentCaptor<TodoTaskCreateCommand> captor = ArgumentCaptor.forClass(TodoTaskCreateCommand.class);
|
||||
verify(dependencies.todoTaskService).createTask(captor.capture());
|
||||
assertEquals("MANUAL_TASK:MANUAL_BUSINESS:B100:8", captor.getValue().getIdempotentKey());
|
||||
assertEquals("admin", captor.getValue().getCreateBy());
|
||||
}
|
||||
|
||||
@Test
|
||||
void assign_shouldUpdateActiveTaskAndResetAssignmentKey() {
|
||||
Dependencies dependencies = new Dependencies();
|
||||
TodoTaskAdminServiceImpl service = dependencies.createService();
|
||||
when(dependencies.userMapper.selectUserById(9L)).thenReturn(activeUser(9L));
|
||||
SysTodoTask task = task(TodoTaskConstants.STATUS_PENDING);
|
||||
when(dependencies.todoTaskMapper.selectTodoById(20L)).thenReturn(task);
|
||||
when(dependencies.todoTaskMapper.assignTodo(20L, 9L, "100",
|
||||
"MANUAL_TASK:MANUAL_BUSINESS:B100:9", "admin")).thenReturn(1);
|
||||
TodoTaskAssignRequest request = new TodoTaskAssignRequest();
|
||||
request.setAssigneeUserId(9L);
|
||||
request.setAssigneeMerchantId("100");
|
||||
|
||||
assertTrue(service.assign(20L, request, "admin"));
|
||||
|
||||
verify(dependencies.todoTaskMapper).assignTodo(20L, 9L, "100",
|
||||
"MANUAL_TASK:MANUAL_BUSINESS:B100:9", "admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void hide_shouldOnlyAllowTerminalTask() {
|
||||
Dependencies dependencies = new Dependencies();
|
||||
TodoTaskAdminServiceImpl service = dependencies.createService();
|
||||
when(dependencies.todoTaskMapper.selectTodoById(20L))
|
||||
.thenReturn(task(TodoTaskConstants.STATUS_PENDING));
|
||||
|
||||
assertThrows(ServiceException.class, () -> service.hide(20L, "admin"));
|
||||
|
||||
when(dependencies.todoTaskMapper.selectTodoById(21L))
|
||||
.thenReturn(task(TodoTaskConstants.STATUS_CANCELLED));
|
||||
when(dependencies.todoTaskMapper.hideTodo(21L, "admin")).thenReturn(1);
|
||||
assertTrue(service.hide(21L, "admin"));
|
||||
}
|
||||
|
||||
private static TodoTaskAdminCreateRequest createRequest() {
|
||||
TodoTaskAdminCreateRequest request = new TodoTaskAdminCreateRequest();
|
||||
request.setTaskType("MANUAL_TASK");
|
||||
request.setTitle("人工待办");
|
||||
request.setBusinessType("MANUAL_BUSINESS");
|
||||
request.setBusinessId("B100");
|
||||
request.setRouteName("TodoCenter");
|
||||
request.setAssigneeUserId(8L);
|
||||
return request;
|
||||
}
|
||||
|
||||
private static SysTodoTask task(String status) {
|
||||
SysTodoTask task = new SysTodoTask();
|
||||
task.setTodoId(20L);
|
||||
task.setTaskType("MANUAL_TASK");
|
||||
task.setBusinessType("MANUAL_BUSINESS");
|
||||
task.setBusinessId("B100");
|
||||
task.setTaskStatus(status);
|
||||
return task;
|
||||
}
|
||||
|
||||
private static SysUser activeUser(Long userId) {
|
||||
SysUser user = new SysUser();
|
||||
user.setUserId(userId);
|
||||
user.setStatus(Constants.ZERO);
|
||||
user.setDelFlag(Constants.ZERO);
|
||||
return user;
|
||||
}
|
||||
|
||||
private static void setField(Object target, String fieldName, Object value) {
|
||||
try {
|
||||
Field field = TodoTaskAdminServiceImpl.class.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
field.set(target, value);
|
||||
} catch (Exception exception) {
|
||||
throw new RuntimeException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
private static class Dependencies {
|
||||
private final SysTodoTaskMapper todoTaskMapper = mock(SysTodoTaskMapper.class);
|
||||
private final SysUserMapper userMapper = mock(SysUserMapper.class);
|
||||
private final TodoTaskService todoTaskService = mock(TodoTaskService.class);
|
||||
|
||||
private TodoTaskAdminServiceImpl createService() {
|
||||
TodoTaskAdminServiceImpl service = new TodoTaskAdminServiceImpl();
|
||||
setField(service, "todoTaskMapper", todoTaskMapper);
|
||||
setField(service, "userMapper", userMapper);
|
||||
setField(service, "todoTaskService", todoTaskService);
|
||||
return service;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.jsowell.web.controller.system;
|
||||
|
||||
import com.jsowell.system.domain.SysTodoTask;
|
||||
import com.jsowell.system.domain.dto.TodoTaskAdminCreateRequest;
|
||||
import com.jsowell.system.domain.dto.TodoTaskCancelRequest;
|
||||
import com.jsowell.system.service.TodoTaskAdminService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class SysTodoTaskAdminControllerTest {
|
||||
@Test
|
||||
void create_shouldPassCurrentOperator() {
|
||||
TodoTaskAdminService service = mock(TodoTaskAdminService.class);
|
||||
TestController controller = newController(service, 1L, "admin");
|
||||
TodoTaskAdminCreateRequest request = new TodoTaskAdminCreateRequest();
|
||||
when(service.create(request, "admin")).thenReturn(new SysTodoTask());
|
||||
|
||||
controller.create(request);
|
||||
|
||||
verify(service).create(request, "admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancel_shouldPassCurrentUserAndOperator() {
|
||||
TodoTaskAdminService service = mock(TodoTaskAdminService.class);
|
||||
TestController controller = newController(service, 1L, "admin");
|
||||
TodoTaskCancelRequest request = new TodoTaskCancelRequest();
|
||||
request.setReason("异常任务关闭");
|
||||
when(service.cancel(20L, 1L, "admin", "异常任务关闭")).thenReturn(true);
|
||||
|
||||
controller.cancel(20L, request);
|
||||
|
||||
verify(service).cancel(20L, 1L, "admin", "异常任务关闭");
|
||||
}
|
||||
|
||||
private static TestController newController(TodoTaskAdminService service,
|
||||
Long userId, String username) {
|
||||
TestController controller = new TestController(userId, username);
|
||||
try {
|
||||
Field field = SysTodoTaskAdminController.class.getDeclaredField("todoTaskAdminService");
|
||||
field.setAccessible(true);
|
||||
field.set(controller, service);
|
||||
} catch (Exception exception) {
|
||||
throw new RuntimeException(exception);
|
||||
}
|
||||
return controller;
|
||||
}
|
||||
|
||||
private static class TestController extends SysTodoTaskAdminController {
|
||||
private final Long userId;
|
||||
private final String username;
|
||||
|
||||
private TestController(Long userId, String username) {
|
||||
this.userId = userId;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user