完善店务端员工新增和删除

This commit is contained in:
2026-07-15 17:33:34 +08:00
parent ad1ae376f8
commit 00a435ecfb
12 changed files with 288 additions and 12 deletions
@@ -38,6 +38,7 @@ import com.xiaowang.jayucloud.core.modules.user.vo.UserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
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;
@@ -259,9 +260,34 @@ public class AdminManagerController {
dto.setShopId(managedShopId);
dto.setShopIds(List.of(managedShopId));
}
if (!StringUtils.hasText(dto.getWechatId())) {
dto.setWechatId(dto.getPhone());
}
return Result.success(userService.createUser(dto));
}
@DeleteMapping("/employees/{userId}")
@RequirePermission("user:unbind")
@OperationGuard(action = "manager-employee-delete", ttlSeconds = 10, message = "员工删除处理中,请勿重复点击")
public Result<Boolean> deleteEmployee(HttpServletRequest request,
@PathVariable Long userId) {
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
UserVO targetUser = userService.getUserById(userId);
if (targetUser == null) {
return Result.success(false);
}
if (!adminScopeHelper.isPlatformAdmin(currentUser)) {
adminScopeHelper.ensureUserAccessibleInCurrentShop(currentUser, userId, "无权删除其他店铺员工");
}
adminScopeHelper.ensureAssignableUserType(currentUser, targetUser.getType());
if (!Integer.valueOf(3).equals(targetUser.getType())) {
throw new RuntimeException("只能删除员工");
}
Long shopId = adminScopeHelper.resolveShopScope(currentUser, null);
adminScopeHelper.requireShopContext(currentUser, "请先选择店铺后再删除员工");
return Result.success(userService.unbindUserInShopWithOperator(userId, shopId, currentUser.getType()));
}
@GetMapping("/employees/schedules")
@RequirePermission("schedule:view")
public Result<List<ArtistScheduleVO>> schedules(HttpServletRequest request,
@@ -141,6 +141,29 @@ class AdminManagerControllerTest {
verify(adminScopeHelper).ensureAssignableUserType(manager, 3);
}
@Test
void createEmployeeUserShouldDefaultWechatToPhoneWithoutChangingAccount() {
MockHttpServletRequest request = new MockHttpServletRequest();
UserDO manager = new UserDO();
manager.setType(2);
manager.setShopId(8L);
UserDTO dto = new UserDTO();
dto.setPhone("13800138000");
dto.setAccount("artist001");
dto.setWechatId(" ");
when(adminScopeHelper.getCurrentUser(request)).thenReturn(manager);
when(adminScopeHelper.isPlatformAdmin(manager)).thenReturn(false);
when(adminScopeHelper.getManagedShopId(manager)).thenReturn(8L);
when(userService.createUser(dto)).thenReturn(104L);
Result<Long> result = controller.createEmployeeUser(request, dto);
assertThat(result.getData()).isEqualTo(104L);
assertThat(dto.getWechatId()).isEqualTo("13800138000");
assertThat(dto.getAccount()).isEqualTo("artist001");
verify(userService).createUser(dto);
}
@Test
void createCustomerShouldForceNormalUserInCurrentShop() {
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -321,6 +344,49 @@ class AdminManagerControllerTest {
verify(userService, never()).resetManagedEmployeePassword(any(), any());
}
@Test
void deleteEmployeeShouldUnbindCurrentShopWithoutDeletingAccount() {
MockHttpServletRequest request = new MockHttpServletRequest();
UserDO manager = new UserDO();
manager.setType(2);
manager.setShopId(8L);
UserVO targetUser = new UserVO();
targetUser.setId(7L);
targetUser.setType(3);
when(adminScopeHelper.getCurrentUser(request)).thenReturn(manager);
when(adminScopeHelper.isPlatformAdmin(manager)).thenReturn(false);
when(adminScopeHelper.resolveShopScope(manager, null)).thenReturn(8L);
when(userService.getUserById(7L)).thenReturn(targetUser);
when(userService.unbindUserInShopWithOperator(7L, 8L, 2)).thenReturn(true);
Result<Boolean> result = controller.deleteEmployee(request, 7L);
assertThat(result.getData()).isTrue();
verify(adminScopeHelper).ensureUserAccessibleInCurrentShop(manager, 7L, "无权删除其他店铺员工");
verify(adminScopeHelper).ensureAssignableUserType(manager, 3);
verify(userService).unbindUserInShopWithOperator(7L, 8L, 2);
verify(userService, never()).deleteUser(any());
}
@Test
void deleteEmployeeShouldRejectNonEmployee() {
MockHttpServletRequest request = new MockHttpServletRequest();
UserDO manager = new UserDO();
manager.setType(2);
UserVO targetUser = new UserVO();
targetUser.setId(7L);
targetUser.setType(0);
when(adminScopeHelper.getCurrentUser(request)).thenReturn(manager);
when(adminScopeHelper.isPlatformAdmin(manager)).thenReturn(false);
when(userService.getUserById(7L)).thenReturn(targetUser);
assertThatThrownBy(() -> controller.deleteEmployee(request, 7L))
.isInstanceOf(RuntimeException.class)
.hasMessage("只能删除员工");
verify(userService, never()).unbindUserInShopWithOperator(any(), any(), any());
verify(userService, never()).deleteUser(any());
}
@Test
void saveScheduleShouldResolveShopScopeBeforeSaving() {
MockHttpServletRequest request = new MockHttpServletRequest();