mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 02:39:19 +08:00
wip(account): 开发账户管理相关
This commit is contained in:
+8
-28
@@ -19,7 +19,7 @@ import top.xyquan.ecomdmbbackend.modules.org.service.AccountService;
|
||||
*/
|
||||
@Tag(name = "组织-账户管理", description = "账户管理相关接口")
|
||||
@RestController
|
||||
@RequestMapping("/account")
|
||||
@RequestMapping("/company/account")
|
||||
public class AccountController {
|
||||
|
||||
private final AccountService accountService;
|
||||
@@ -28,51 +28,31 @@ public class AccountController {
|
||||
this.accountService = accountService;
|
||||
}
|
||||
|
||||
// ==================== 超管接口 ====================
|
||||
|
||||
@Operation(summary = "创建账户(超管)", description = "超级管理员创建账户,可指定公司")
|
||||
@PostMapping("/create")
|
||||
public ApiResponse<AccountVO> createAccount(@Valid @RequestBody CreateAccountDTO createAccountDTO) {
|
||||
AccountVO accountVO = accountService.createAccount(createAccountDTO);
|
||||
return ApiResponse.success(accountVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "分页查询账户(超管)", description = "超级管理员分页查询所有账户")
|
||||
@PostMapping("/page")
|
||||
public ApiResponse<PageResultVO<AccountVO>> getAccountPage(@Valid @RequestBody PageQueryDTO<AccountFilterDTO> pageQuery) {
|
||||
PageResultVO<AccountVO> pageResult = accountService.getAccountPage(pageQuery);
|
||||
return ApiResponse.success(pageResult);
|
||||
}
|
||||
|
||||
// ==================== 管理员接口 ====================
|
||||
|
||||
@Operation(summary = "创建账户(管理员)", description = "公司管理员创建本公司账户")
|
||||
@PostMapping("/company/create")
|
||||
@PostMapping("/create")
|
||||
public ApiResponse<AccountVO> createAccountForCompany(@Valid @RequestBody CreateAccountDTO createAccountDTO) {
|
||||
AccountVO accountVO = accountService.createAccountForCompany(createAccountDTO);
|
||||
return ApiResponse.success(accountVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "分页查询账户(管理员)", description = "公司管理员分页查询本公司账户")
|
||||
@PostMapping("/company/page")
|
||||
@PostMapping("/page")
|
||||
public ApiResponse<PageResultVO<AccountVO>> getAccountPageForCompany(@Valid @RequestBody PageQueryDTO<AccountFilterDTO> pageQuery) {
|
||||
PageResultVO<AccountVO> pageResult = accountService.getAccountPageForCompany(pageQuery);
|
||||
return ApiResponse.success(pageResult);
|
||||
}
|
||||
|
||||
// ==================== 通用接口 ====================
|
||||
|
||||
@Operation(summary = "更新账户信息", description = "更新账户信息")
|
||||
@PutMapping("/{accountId}")
|
||||
@Operation(summary = "更新账户信息", description = "公司管理员更新本公司账户信息")
|
||||
@PutMapping("/update/{accountId}")
|
||||
public ApiResponse<AccountVO> updateAccount(
|
||||
@Parameter(description = "账户ID", example = "1") @PathVariable Long accountId,
|
||||
@Valid @RequestBody UpdateAccountDTO updateAccountDTO) {
|
||||
AccountVO accountVO = accountService.updateAccount(updateAccountDTO, accountId);
|
||||
AccountVO accountVO = accountService.updateAccountForCompany(updateAccountDTO, accountId);
|
||||
return ApiResponse.success(accountVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "删除账户", description = "逻辑删除账户")
|
||||
@DeleteMapping("/{accountId}")
|
||||
@DeleteMapping("/delete/{accountId}")
|
||||
public ApiResponse<Void> deleteAccount(
|
||||
@Parameter(description = "账户ID", example = "1") @PathVariable Long accountId) {
|
||||
accountService.deleteAccount(accountId);
|
||||
@@ -80,7 +60,7 @@ public class AccountController {
|
||||
}
|
||||
|
||||
@Operation(summary = "获取账户详情", description = "获取账户详细信息")
|
||||
@GetMapping("/{accountId}")
|
||||
@GetMapping("/detail/{accountId}")
|
||||
public ApiResponse<AccountVO> getAccountDetail(
|
||||
@Parameter(description = "账户ID", example = "1") @PathVariable Long accountId) {
|
||||
AccountVO accountVO = accountService.getAccountDetail(accountId);
|
||||
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.org.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.AccountFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.SuperCreateAccountDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.SuperUpdateAccountDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.vo.AccountVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.SuperAccountService;
|
||||
import top.xyquan.ecomdmbbackend.common.annotation.RequireRole;
|
||||
|
||||
/**
|
||||
* 超级管理员账户管理控制器
|
||||
*/
|
||||
@Tag(name = "超级管理员-账户管理", description = "超级管理员账户管理相关接口")
|
||||
@RestController
|
||||
@RequestMapping("/super/account")
|
||||
public class SuperAccountController {
|
||||
|
||||
private final SuperAccountService superAccountService;
|
||||
|
||||
public SuperAccountController(SuperAccountService superAccountService) {
|
||||
this.superAccountService = superAccountService;
|
||||
}
|
||||
|
||||
@Operation(summary = "创建账户", description = "超级管理员创建账户,可指定公司")
|
||||
@PostMapping("/create")
|
||||
@RequireRole(description = "创建账户", allowCompany = false)
|
||||
public ApiResponse<AccountVO> createAccount(@Valid @RequestBody SuperCreateAccountDTO superCreateAccountDTO) {
|
||||
AccountVO accountVO = superAccountService.createAccount(superCreateAccountDTO);
|
||||
return ApiResponse.success(accountVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "分页查询账户", description = "超级管理员分页查询所有账户")
|
||||
@PostMapping("/page")
|
||||
@RequireRole(description = "查询账户列表", allowCompany = false)
|
||||
public ApiResponse<PageResultVO<AccountVO>> getAccountPage(@Valid @RequestBody PageQueryDTO<AccountFilterDTO> pageQuery) {
|
||||
PageResultVO<AccountVO> pageResult = superAccountService.getAccountPage(pageQuery);
|
||||
return ApiResponse.success(pageResult);
|
||||
}
|
||||
|
||||
@Operation(summary = "更新账户信息", description = "超级管理员更新账户信息")
|
||||
@PutMapping("/update/{accountId}")
|
||||
@RequireRole(description = "更新账户", allowCompany = false)
|
||||
public ApiResponse<AccountVO> updateAccount(
|
||||
@Parameter(description = "账户ID", example = "1") @PathVariable Long accountId,
|
||||
@Valid @RequestBody SuperUpdateAccountDTO superUpdateAccountDTO) {
|
||||
AccountVO accountVO = superAccountService.updateAccount(superUpdateAccountDTO, accountId);
|
||||
return ApiResponse.success(accountVO);
|
||||
}
|
||||
|
||||
@Operation(summary = "删除账户", description = "超级管理员逻辑删除账户")
|
||||
@DeleteMapping("/delete/{accountId}")
|
||||
@RequireRole(description = "删除账户", allowCompany = false)
|
||||
public ApiResponse<Void> deleteAccount(
|
||||
@Parameter(description = "账户ID", example = "1") @PathVariable Long accountId) {
|
||||
superAccountService.deleteAccount(accountId);
|
||||
return ApiResponse.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "获取账户详情", description = "超级管理员获取账户详细信息")
|
||||
@GetMapping("/detail/{accountId}")
|
||||
@RequireRole(description = "查询账户详情", allowCompany = false)
|
||||
public ApiResponse<AccountVO> getAccountDetail(
|
||||
@Parameter(description = "账户ID", example = "1") @PathVariable Long accountId) {
|
||||
AccountVO accountVO = superAccountService.getAccountDetail(accountId);
|
||||
return ApiResponse.success(accountVO);
|
||||
}
|
||||
}
|
||||
-3
@@ -46,7 +46,4 @@ public class CreateAccountDTO {
|
||||
@Schema(description = "备注信息", example = "新员工账户")
|
||||
private String remark;
|
||||
|
||||
// 超管创建账户时需要指定公司ID,管理员创建时不需要
|
||||
@Schema(description = "公司ID(仅超管创建时需要)", example = "1")
|
||||
private Long companyId;
|
||||
}
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.org.model.dto.account;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 超级管理员创建账户DTO
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "超级管理员创建账户请求")
|
||||
public class SuperCreateAccountDTO {
|
||||
|
||||
@NotBlank(message = "登录账号不能为空")
|
||||
@Size(min = 3, max = 20, message = "登录账号长度必须在3-20个字符之间")
|
||||
@Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "登录账号只能包含字母、数字和下划线")
|
||||
@Schema(description = "登录账号", example = "admin")
|
||||
private String loginCode;
|
||||
|
||||
@NotBlank(message = "密码不能为空")
|
||||
@Size(min = 8, max = 20, message = "密码长度必须在8-20个字符之间")
|
||||
@Schema(description = "密码", example = "12345678")
|
||||
private String password;
|
||||
|
||||
@NotBlank(message = "用户姓名不能为空")
|
||||
@Size(max = 50, message = "用户姓名长度不能超过50个字符")
|
||||
@Schema(description = "用户姓名", example = "张三")
|
||||
private String realName;
|
||||
|
||||
@Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号码格式不正确")
|
||||
@Schema(description = "手机号码", example = "13800138000")
|
||||
private String phone;
|
||||
|
||||
@NotNull(message = "公司ID不能为空")
|
||||
@Schema(description = "公司ID", example = "1")
|
||||
private Long companyId;
|
||||
|
||||
@Schema(description = "部门ID", example = "1")
|
||||
private Long departmentId;
|
||||
|
||||
@Schema(description = "岗位ID", example = "1")
|
||||
private Long positionId;
|
||||
|
||||
@NotBlank(message = "角色不能为空")
|
||||
@Schema(description = "角色", example = "user", allowableValues = {"super", "admin", "user"})
|
||||
private String role;
|
||||
|
||||
@Schema(description = "头像图片ID", example = "1")
|
||||
private Long avatarImageId;
|
||||
|
||||
@Schema(description = "备注", example = "备注信息")
|
||||
private String remark;
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.org.model.dto.account;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 超级管理员更新账户DTO
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "超级管理员更新账户请求")
|
||||
public class SuperUpdateAccountDTO {
|
||||
|
||||
@Size(min = 3, max = 20, message = "登录账号长度必须在3-20个字符之间")
|
||||
@Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "登录账号只能包含字母、数字和下划线")
|
||||
@Schema(description = "登录账号", example = "admin")
|
||||
private String loginCode;
|
||||
|
||||
@Size(max = 50, message = "用户姓名长度不能超过50个字符")
|
||||
@Schema(description = "用户姓名", example = "张三")
|
||||
private String realName;
|
||||
|
||||
@Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号码格式不正确")
|
||||
@Schema(description = "手机号码", example = "13800138000")
|
||||
private String phone;
|
||||
|
||||
@Schema(description = "公司ID", example = "1")
|
||||
private Long companyId;
|
||||
|
||||
@Schema(description = "部门ID", example = "1")
|
||||
private Long departmentId;
|
||||
|
||||
@Schema(description = "岗位ID", example = "1")
|
||||
private Long positionId;
|
||||
|
||||
@Schema(description = "角色", example = "user", allowableValues = {"super", "admin", "user"})
|
||||
private String role;
|
||||
|
||||
@Schema(description = "头像图片ID", example = "1")
|
||||
private Long avatarImageId;
|
||||
|
||||
@Schema(description = "状态(0-正常 1-删除 2-停用 3-冻结)", example = "0")
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "备注", example = "备注信息")
|
||||
private String remark;
|
||||
}
|
||||
+2
-10
@@ -14,14 +14,6 @@ import top.xyquan.ecomdmbbackend.modules.org.model.vo.AccountVO;
|
||||
*/
|
||||
public interface AccountService extends IService<UserPO> {
|
||||
|
||||
/**
|
||||
* 创建账户(超管接口)
|
||||
*
|
||||
* @param createAccountDTO 创建账户信息
|
||||
* @return 账户信息
|
||||
*/
|
||||
AccountVO createAccount(CreateAccountDTO createAccountDTO);
|
||||
|
||||
/**
|
||||
* 创建账户(管理员接口,自动设置公司ID)
|
||||
*
|
||||
@@ -31,13 +23,13 @@ public interface AccountService extends IService<UserPO> {
|
||||
AccountVO createAccountForCompany(CreateAccountDTO createAccountDTO);
|
||||
|
||||
/**
|
||||
* 更新账户信息
|
||||
* 更新账户信息(管理员接口)
|
||||
*
|
||||
* @param updateAccountDTO 更新账户信息
|
||||
* @param accountId 账户ID
|
||||
* @return 更新后的账户信息
|
||||
*/
|
||||
AccountVO updateAccount(UpdateAccountDTO updateAccountDTO, Long accountId);
|
||||
AccountVO updateAccountForCompany(UpdateAccountDTO updateAccountDTO, Long accountId);
|
||||
|
||||
/**
|
||||
* 删除账户(逻辑删除)
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.org.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.AccountFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.SuperCreateAccountDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.SuperUpdateAccountDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.po.UserPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.vo.AccountVO;
|
||||
|
||||
/**
|
||||
* 超级管理员账户管理服务接口
|
||||
*/
|
||||
public interface SuperAccountService extends IService<UserPO> {
|
||||
|
||||
/**
|
||||
* 创建账户(超管接口)
|
||||
*
|
||||
* @param superCreateAccountDTO 创建账户信息
|
||||
* @return 账户信息
|
||||
*/
|
||||
AccountVO createAccount(SuperCreateAccountDTO superCreateAccountDTO);
|
||||
|
||||
/**
|
||||
* 更新账户信息(超管接口)
|
||||
*
|
||||
* @param superUpdateAccountDTO 更新账户信息
|
||||
* @param accountId 账户ID
|
||||
* @return 更新后的账户信息
|
||||
*/
|
||||
AccountVO updateAccount(SuperUpdateAccountDTO superUpdateAccountDTO, Long accountId);
|
||||
|
||||
/**
|
||||
* 删除账户(逻辑删除)
|
||||
*
|
||||
* @param accountId 账户ID
|
||||
*/
|
||||
void deleteAccount(Long accountId);
|
||||
|
||||
/**
|
||||
* 获取账户详情
|
||||
*
|
||||
* @param accountId 账户ID
|
||||
* @return 账户详情
|
||||
*/
|
||||
AccountVO getAccountDetail(Long accountId);
|
||||
|
||||
/**
|
||||
* 分页查询账户(超管接口)
|
||||
*
|
||||
* @param pageQuery 分页查询参数
|
||||
* @return 分页结果
|
||||
*/
|
||||
PageResultVO<AccountVO> getAccountPage(PageQueryDTO<AccountFilterDTO> pageQuery);
|
||||
}
|
||||
+10
-16
@@ -45,8 +45,15 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public AccountVO createAccount(CreateAccountDTO createAccountDTO) {
|
||||
public AccountVO createAccountForCompany(CreateAccountDTO createAccountDTO) {
|
||||
// 从用户上下文获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
if (companyId == null) {
|
||||
throw new BusinessException("无法获取公司信息");
|
||||
}
|
||||
|
||||
// 验证登录账号是否已存在
|
||||
if (StringUtils.hasText(createAccountDTO.getLoginCode())) {
|
||||
LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>();
|
||||
@@ -68,6 +75,7 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
// 创建账户
|
||||
UserPO user = new UserPO();
|
||||
BeanUtils.copyProperties(createAccountDTO, user);
|
||||
user.setCompanyId(companyId); // 设置公司ID
|
||||
user.setPassword(passwordEncoder.encode(createAccountDTO.getPassword()));
|
||||
user.setRole("user"); // 默认角色
|
||||
user.setStatus(0); // 正常状态
|
||||
@@ -82,23 +90,9 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
return convertToVO(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccountVO createAccountForCompany(CreateAccountDTO createAccountDTO) {
|
||||
// 从用户上下文获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
if (companyId == null) {
|
||||
throw new BusinessException("无法获取公司信息");
|
||||
}
|
||||
|
||||
// 设置公司ID
|
||||
createAccountDTO.setCompanyId(companyId);
|
||||
|
||||
// 调用通用创建方法
|
||||
return createAccount(createAccountDTO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccountVO updateAccount(UpdateAccountDTO updateAccountDTO, Long accountId) {
|
||||
public AccountVO updateAccountForCompany(UpdateAccountDTO updateAccountDTO, Long accountId) {
|
||||
// 查询账户
|
||||
UserPO user = super.getById(accountId);
|
||||
if (user == null) {
|
||||
|
||||
+330
@@ -0,0 +1,330 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.org.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.mapper.UserMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.AccountFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.SuperCreateAccountDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.SuperUpdateAccountDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.model.po.UserPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.model.vo.AccountVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.service.SuperAccountService;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 超级管理员账户管理服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class SuperAccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implements SuperAccountService {
|
||||
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public SuperAccountServiceImpl(PasswordEncoder passwordEncoder,
|
||||
CurrentUserContext currentUserContext) {
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccountVO createAccount(SuperCreateAccountDTO superCreateAccountDTO) {
|
||||
// 验证登录账号是否已存在
|
||||
if (StringUtils.hasText(superCreateAccountDTO.getLoginCode())) {
|
||||
LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(UserPO::getLoginCode, superCreateAccountDTO.getLoginCode());
|
||||
if (this.count(wrapper) > 0) {
|
||||
throw new BusinessException("登录账号已存在");
|
||||
}
|
||||
}
|
||||
|
||||
// 验证手机号码是否已存在
|
||||
if (StringUtils.hasText(superCreateAccountDTO.getPhone())) {
|
||||
LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(UserPO::getPhone, superCreateAccountDTO.getPhone());
|
||||
if (this.count(wrapper) > 0) {
|
||||
throw new BusinessException("手机号码已存在");
|
||||
}
|
||||
}
|
||||
|
||||
// 创建账户
|
||||
UserPO user = new UserPO();
|
||||
BeanUtils.copyProperties(superCreateAccountDTO, user);
|
||||
user.setPassword(passwordEncoder.encode(superCreateAccountDTO.getPassword()));
|
||||
user.setStatus(0); // 正常状态
|
||||
user.setPwdSecurityLevel(calculatePasswordSecurityLevel(superCreateAccountDTO.getPassword()));
|
||||
user.setPwdUpdateTime(LocalDateTime.now());
|
||||
user.setCreateBy(currentUserContext.getCurrentUserId());
|
||||
|
||||
// 保存账户
|
||||
this.save(user);
|
||||
|
||||
// 转换为VO并返回
|
||||
return convertToVO(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccountVO updateAccount(SuperUpdateAccountDTO superUpdateAccountDTO, Long accountId) {
|
||||
// 查询账户
|
||||
UserPO user = super.getById(accountId);
|
||||
if (user == null) {
|
||||
throw new BusinessException("账户不存在");
|
||||
}
|
||||
|
||||
// 检查登录账号是否被其他用户使用
|
||||
if (StringUtils.hasText(superUpdateAccountDTO.getLoginCode()) &&
|
||||
!superUpdateAccountDTO.getLoginCode().equals(user.getLoginCode())) {
|
||||
LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(UserPO::getLoginCode, superUpdateAccountDTO.getLoginCode());
|
||||
wrapper.ne(UserPO::getId, accountId);
|
||||
if (this.count(wrapper) > 0) {
|
||||
throw new BusinessException("登录账号已被其他用户使用");
|
||||
}
|
||||
}
|
||||
|
||||
// 检查手机号码是否被其他用户使用
|
||||
if (StringUtils.hasText(superUpdateAccountDTO.getPhone()) &&
|
||||
!superUpdateAccountDTO.getPhone().equals(user.getPhone())) {
|
||||
LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(UserPO::getPhone, superUpdateAccountDTO.getPhone());
|
||||
wrapper.ne(UserPO::getId, accountId);
|
||||
if (this.count(wrapper) > 0) {
|
||||
throw new BusinessException("手机号码已被其他用户使用");
|
||||
}
|
||||
}
|
||||
|
||||
// 更新账户信息
|
||||
if (StringUtils.hasText(superUpdateAccountDTO.getLoginCode())) {
|
||||
user.setLoginCode(superUpdateAccountDTO.getLoginCode());
|
||||
}
|
||||
if (StringUtils.hasText(superUpdateAccountDTO.getPhone())) {
|
||||
user.setPhone(superUpdateAccountDTO.getPhone());
|
||||
}
|
||||
if (StringUtils.hasText(superUpdateAccountDTO.getRealName())) {
|
||||
user.setRealName(superUpdateAccountDTO.getRealName());
|
||||
}
|
||||
if (superUpdateAccountDTO.getAvatarImageId() != null) {
|
||||
user.setAvatarImageId(superUpdateAccountDTO.getAvatarImageId());
|
||||
}
|
||||
if (superUpdateAccountDTO.getCompanyId() != null) {
|
||||
user.setCompanyId(superUpdateAccountDTO.getCompanyId());
|
||||
}
|
||||
if (superUpdateAccountDTO.getDepartmentId() != null) {
|
||||
user.setDepartmentId(superUpdateAccountDTO.getDepartmentId());
|
||||
}
|
||||
if (superUpdateAccountDTO.getPositionId() != null) {
|
||||
user.setPositionId(superUpdateAccountDTO.getPositionId());
|
||||
}
|
||||
if (StringUtils.hasText(superUpdateAccountDTO.getRole())) {
|
||||
user.setRole(superUpdateAccountDTO.getRole());
|
||||
}
|
||||
if (superUpdateAccountDTO.getStatus() != null) {
|
||||
user.setStatus(superUpdateAccountDTO.getStatus());
|
||||
}
|
||||
if (StringUtils.hasText(superUpdateAccountDTO.getRemark())) {
|
||||
user.setRemark(superUpdateAccountDTO.getRemark());
|
||||
}
|
||||
|
||||
user.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
|
||||
// 保存更新
|
||||
this.updateById(user);
|
||||
|
||||
// 转换为VO并返回
|
||||
return convertToVO(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAccount(Long accountId) {
|
||||
// 查询账户
|
||||
UserPO user = super.getById(accountId);
|
||||
if (user == null) {
|
||||
throw new BusinessException("账户不存在");
|
||||
}
|
||||
|
||||
// 逻辑删除
|
||||
user.setStatus(1); // 删除状态
|
||||
user.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
this.updateById(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccountVO getAccountDetail(Long accountId) {
|
||||
// 查询账户
|
||||
UserPO user = super.getById(accountId);
|
||||
if (user == null) {
|
||||
throw new BusinessException("账户不存在");
|
||||
}
|
||||
|
||||
// 转换为VO并返回
|
||||
return convertToVO(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResultVO<AccountVO> getAccountPage(PageQueryDTO<AccountFilterDTO> pageQuery) {
|
||||
// 构建查询条件
|
||||
LambdaQueryWrapper<UserPO> wrapper = buildQueryWrapper(pageQuery.getFilters());
|
||||
|
||||
// 分页查询
|
||||
Page<UserPO> page = new Page<>(pageQuery.getPage(), pageQuery.getPageSize());
|
||||
IPage<UserPO> pageResult = this.page(page, wrapper);
|
||||
|
||||
// 转换为VO
|
||||
List<AccountVO> records = pageResult.getRecords().stream()
|
||||
.map(this::convertToVO)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 构建分页响应
|
||||
return PageResultVO.create(records, pageResult.getTotal(), pageQuery.getPage(), pageQuery.getPageSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建查询条件
|
||||
*/
|
||||
private LambdaQueryWrapper<UserPO> buildQueryWrapper(AccountFilterDTO filterDTO) {
|
||||
LambdaQueryWrapper<UserPO> wrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
// 如果过滤条件为空,直接返回基础查询条件
|
||||
if (filterDTO == null) {
|
||||
wrapper.ne(UserPO::getStatus, 1);
|
||||
wrapper.orderByDesc(UserPO::getCreateTime);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
// 登录账号
|
||||
if (StringUtils.hasText(filterDTO.getLoginCode())) {
|
||||
wrapper.like(UserPO::getLoginCode, filterDTO.getLoginCode());
|
||||
}
|
||||
|
||||
// 用户姓名
|
||||
if (StringUtils.hasText(filterDTO.getRealName())) {
|
||||
wrapper.like(UserPO::getRealName, filterDTO.getRealName());
|
||||
}
|
||||
|
||||
// 手机号码
|
||||
if (StringUtils.hasText(filterDTO.getPhone())) {
|
||||
wrapper.like(UserPO::getPhone, filterDTO.getPhone());
|
||||
}
|
||||
|
||||
// 公司ID
|
||||
if (filterDTO.getCompanyId() != null) {
|
||||
wrapper.eq(UserPO::getCompanyId, filterDTO.getCompanyId());
|
||||
}
|
||||
|
||||
// 部门ID
|
||||
if (filterDTO.getDepartmentId() != null) {
|
||||
wrapper.eq(UserPO::getDepartmentId, filterDTO.getDepartmentId());
|
||||
}
|
||||
|
||||
// 岗位ID
|
||||
if (filterDTO.getPositionId() != null) {
|
||||
wrapper.eq(UserPO::getPositionId, filterDTO.getPositionId());
|
||||
}
|
||||
|
||||
// 角色
|
||||
if (StringUtils.hasText(filterDTO.getRole())) {
|
||||
wrapper.eq(UserPO::getRole, filterDTO.getRole());
|
||||
}
|
||||
|
||||
// 状态
|
||||
if (filterDTO.getStatus() != null) {
|
||||
wrapper.eq(UserPO::getStatus, filterDTO.getStatus());
|
||||
}
|
||||
|
||||
// 排除删除状态
|
||||
wrapper.ne(UserPO::getStatus, 1);
|
||||
|
||||
// 按创建时间倒序
|
||||
wrapper.orderByDesc(UserPO::getCreateTime);
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为VO
|
||||
*/
|
||||
private AccountVO convertToVO(UserPO user) {
|
||||
AccountVO accountVO = new AccountVO();
|
||||
BeanUtils.copyProperties(user, accountVO);
|
||||
|
||||
// 设置状态名称
|
||||
accountVO.setStatusName(getStatusName(user.getStatus()));
|
||||
|
||||
// 设置角色名称
|
||||
accountVO.setRoleName(getRoleName(user.getRole()));
|
||||
|
||||
// TODO: 设置公司名称、部门名称、岗位名称等关联信息
|
||||
// 这里可以根据需要查询关联表获取详细信息
|
||||
|
||||
return accountVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取状态名称
|
||||
*/
|
||||
private String getStatusName(Integer status) {
|
||||
if (status == null) return "未知";
|
||||
return switch (status) {
|
||||
case 0 -> "正常";
|
||||
case 1 -> "删除";
|
||||
case 2 -> "停用";
|
||||
case 3 -> "冻结";
|
||||
default -> "未知";
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色名称
|
||||
*/
|
||||
private String getRoleName(String role) {
|
||||
if (role == null) return "未知";
|
||||
return switch (role) {
|
||||
case "super" -> "超级管理员";
|
||||
case "admin" -> "公司管理员";
|
||||
case "user" -> "普通用户";
|
||||
default -> "未知";
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算密码安全级别
|
||||
*/
|
||||
private Integer calculatePasswordSecurityLevel(String password) {
|
||||
if (password == null || password.length() < 8) {
|
||||
return 1; // 很弱
|
||||
}
|
||||
|
||||
boolean hasLower = password.matches(".*[a-z].*");
|
||||
boolean hasUpper = password.matches(".*[A-Z].*");
|
||||
boolean hasDigit = password.matches(".*\\d.*");
|
||||
boolean hasSpecial = password.matches(".*[@$!%*?&].*");
|
||||
|
||||
int score = 0;
|
||||
if (hasLower) score++;
|
||||
if (hasUpper) score++;
|
||||
if (hasDigit) score++;
|
||||
if (hasSpecial) score++;
|
||||
|
||||
if (password.length() >= 12) score++;
|
||||
|
||||
return switch (score) {
|
||||
case 0, 1 -> 1; // 很弱
|
||||
case 2 -> 2; // 弱
|
||||
case 3 -> 3; // 安全
|
||||
case 4, 5 -> 4; // 很安全
|
||||
default -> 2; // 弱
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user