From 64ad74c15ccc27b2ad04e69f802e9a460321d9c9 Mon Sep 17 00:00:00 2001 From: ah--desktop Date: Wed, 15 Oct 2025 00:59:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=B4=A6=E6=88=B7=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/controller/AccountController.java | 88 +++++ .../model/dto/account/AccountFilterDTO.java | 42 ++ .../model/dto/account/CreateAccountDTO.java | 52 +++ .../model/dto/account/UpdateAccountDTO.java | 39 ++ .../modules/org/model/vo/AccountPageVO.java | 29 ++ .../modules/org/model/vo/AccountVO.java | 95 +++++ .../modules/org/service/AccountService.java | 71 ++++ .../org/service/impl/AccountServiceImpl.java | 360 ++++++++++++++++++ 8 files changed, 776 insertions(+) create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/controller/AccountController.java create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/dto/account/AccountFilterDTO.java create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/dto/account/CreateAccountDTO.java create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/dto/account/UpdateAccountDTO.java create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/vo/AccountPageVO.java create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/vo/AccountVO.java create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/service/AccountService.java create mode 100644 ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/service/impl/AccountServiceImpl.java diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/controller/AccountController.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/controller/AccountController.java new file mode 100644 index 00000000..1a40423c --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/controller/AccountController.java @@ -0,0 +1,88 @@ +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.response.ApiResponse; +import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.AccountFilterDTO; +import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.CreateAccountDTO; +import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.UpdateAccountDTO; +import top.xyquan.ecomdmbbackend.modules.org.model.vo.AccountPageVO; +import top.xyquan.ecomdmbbackend.modules.org.model.vo.AccountVO; +import top.xyquan.ecomdmbbackend.modules.org.service.AccountService; + +/** + * 账户管理控制器 + */ +@Tag(name = "组织-账户管理", description = "账户管理相关接口") +@RestController +@RequestMapping("/account") +public class AccountController { + + private final AccountService accountService; + + public AccountController(AccountService accountService) { + this.accountService = accountService; + } + + // ==================== 超管接口 ==================== + + @Operation(summary = "创建账户(超管)", description = "超级管理员创建账户,可指定公司") + @PostMapping("/create") + public ApiResponse createAccount(@Valid @RequestBody CreateAccountDTO createAccountDTO) { + AccountVO accountVO = accountService.createAccount(createAccountDTO); + return ApiResponse.success(accountVO); + } + + @Operation(summary = "分页查询账户(超管)", description = "超级管理员分页查询所有账户") + @GetMapping("/page") + public ApiResponse getAccountPage(@Valid AccountFilterDTO filterDTO) { + AccountPageVO pageVO = accountService.getAccountPage(filterDTO); + return ApiResponse.success(pageVO); + } + + // ==================== 管理员接口 ==================== + + @Operation(summary = "创建账户(管理员)", description = "公司管理员创建本公司账户") + @PostMapping("/company/create") + public ApiResponse createAccountForCompany(@Valid @RequestBody CreateAccountDTO createAccountDTO) { + AccountVO accountVO = accountService.createAccountForCompany(createAccountDTO); + return ApiResponse.success(accountVO); + } + + @Operation(summary = "分页查询账户(管理员)", description = "公司管理员分页查询本公司账户") + @GetMapping("/company/page") + public ApiResponse getAccountPageForCompany(@Valid AccountFilterDTO filterDTO) { + AccountPageVO pageVO = accountService.getAccountPageForCompany(filterDTO); + return ApiResponse.success(pageVO); + } + + // ==================== 通用接口 ==================== + + @Operation(summary = "更新账户信息", description = "更新账户信息") + @PutMapping("/{accountId}") + public ApiResponse updateAccount( + @Parameter(description = "账户ID", example = "1") @PathVariable Long accountId, + @Valid @RequestBody UpdateAccountDTO updateAccountDTO) { + AccountVO accountVO = accountService.updateAccount(updateAccountDTO, accountId); + return ApiResponse.success(accountVO); + } + + @Operation(summary = "删除账户", description = "逻辑删除账户") + @DeleteMapping("/{accountId}") + public ApiResponse deleteAccount( + @Parameter(description = "账户ID", example = "1") @PathVariable Long accountId) { + accountService.deleteAccount(accountId); + return ApiResponse.success(); + } + + @Operation(summary = "获取账户详情", description = "获取账户详细信息") + @GetMapping("/{accountId}") + public ApiResponse getAccountDetail( + @Parameter(description = "账户ID", example = "1") @PathVariable Long accountId) { + AccountVO accountVO = accountService.getAccountDetail(accountId); + return ApiResponse.success(accountVO); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/dto/account/AccountFilterDTO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/dto/account/AccountFilterDTO.java new file mode 100644 index 00000000..d23b7955 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/dto/account/AccountFilterDTO.java @@ -0,0 +1,42 @@ +package top.xyquan.ecomdmbbackend.modules.org.model.dto.account; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +/** + * 账户查询过滤DTO + */ +@Data +@Schema(description = "账户查询过滤条件") +public class AccountFilterDTO { + + @Schema(description = "登录账号", example = "admin") + private String loginCode; + + @Schema(description = "用户姓名", example = "张三") + private String realName; + + @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") + private String role; + + @Schema(description = "状态(0-正常 1-删除 2-停用 3-冻结)", example = "0") + private Integer status; + + @Schema(description = "页码", example = "1") + private Integer pageNum = 1; + + @Schema(description = "每页大小", example = "10") + private Integer pageSize = 10; +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/dto/account/CreateAccountDTO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/dto/account/CreateAccountDTO.java new file mode 100644 index 00000000..623742d2 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/dto/account/CreateAccountDTO.java @@ -0,0 +1,52 @@ +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 lombok.Data; + +/** + * 创建账户DTO + */ +@Data +@Schema(description = "创建账户请求") +public class CreateAccountDTO { + + @NotBlank(message = "登录账号不能为空") + @Pattern(regexp = "^[a-zA-Z0-9_]{4,20}$", message = "登录账号只能包含字母、数字、下划线,长度4-20位") + @Schema(description = "登录账号", example = "admin001") + private String loginCode; + + @NotBlank(message = "密码不能为空") + @Pattern(regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d@$!%*?&]{8,}$", + message = "密码必须包含大小写字母、数字,长度至少8位") + @Schema(description = "登录密码", example = "Password123") + private String password; + + @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号码格式不正确") + @Schema(description = "手机号码", example = "13800138000") + private String phone; + + @NotBlank(message = "用户姓名不能为空") + @Schema(description = "用户姓名", example = "张三") + private String realName; + + @Schema(description = "头像图片ID", example = "1") + private Long avatarImageId; + + @NotNull(message = "部门ID不能为空") + @Schema(description = "部门ID", example = "1") + private Long departmentId; + + @NotNull(message = "岗位ID不能为空") + @Schema(description = "岗位ID", example = "1") + private Long positionId; + + @Schema(description = "备注信息", example = "新员工账户") + private String remark; + + // 超管创建账户时需要指定公司ID,管理员创建时不需要 + @Schema(description = "公司ID(仅超管创建时需要)", example = "1") + private Long companyId; +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/dto/account/UpdateAccountDTO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/dto/account/UpdateAccountDTO.java new file mode 100644 index 00000000..77a43c82 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/dto/account/UpdateAccountDTO.java @@ -0,0 +1,39 @@ +package top.xyquan.ecomdmbbackend.modules.org.model.dto.account; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.Pattern; +import lombok.Data; + +/** + * 更新账户DTO + */ +@Data +@Schema(description = "更新账户请求") +public class UpdateAccountDTO { + + @Pattern(regexp = "^[a-zA-Z0-9_]{4,20}$", message = "登录账号只能包含字母、数字、下划线,长度4-20位") + @Schema(description = "登录账号", example = "admin001") + private String loginCode; + + @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号码格式不正确") + @Schema(description = "手机号码", example = "13800138000") + private String phone; + + @Schema(description = "用户姓名", example = "张三") + private String realName; + + @Schema(description = "头像图片ID", example = "1") + private Long avatarImageId; + + @Schema(description = "部门ID", example = "1") + private Long departmentId; + + @Schema(description = "岗位ID", example = "1") + private Long positionId; + + @Schema(description = "状态(0-正常 1-删除 2-停用 3-冻结)", example = "0") + private Integer status; + + @Schema(description = "备注信息", example = "更新后的员工账户") + private String remark; +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/vo/AccountPageVO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/vo/AccountPageVO.java new file mode 100644 index 00000000..dc52d472 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/vo/AccountPageVO.java @@ -0,0 +1,29 @@ +package top.xyquan.ecomdmbbackend.modules.org.model.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.List; + +/** + * 账户分页响应VO + */ +@Data +@Schema(description = "账户分页响应") +public class AccountPageVO { + + @Schema(description = "账户列表") + private List records; + + @Schema(description = "总记录数", example = "100") + private Long total; + + @Schema(description = "当前页码", example = "1") + private Long current; + + @Schema(description = "每页大小", example = "10") + private Long size; + + @Schema(description = "总页数", example = "10") + private Long pages; +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/vo/AccountVO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/vo/AccountVO.java new file mode 100644 index 00000000..1f22bda5 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/model/vo/AccountVO.java @@ -0,0 +1,95 @@ +package top.xyquan.ecomdmbbackend.modules.org.model.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.time.LocalDateTime; + +/** + * 账户信息VO + */ +@Data +@Schema(description = "账户信息") +public class AccountVO { + + @Schema(description = "用户ID", example = "1") + private Long id; + + @Schema(description = "公司ID", example = "1") + private Long companyId; + + @Schema(description = "公司名称", example = "测试公司") + private String companyName; + + @Schema(description = "部门ID", example = "1") + private Long departmentId; + + @Schema(description = "部门名称", example = "技术部") + private String departmentName; + + @Schema(description = "岗位ID", example = "1") + private Long positionId; + + @Schema(description = "岗位名称", example = "软件工程师") + private String positionName; + + @Schema(description = "登录账号", example = "admin001") + private String loginCode; + + @Schema(description = "手机号码", example = "13800138000") + private String phone; + + @Schema(description = "用户姓名", example = "张三") + private String realName; + + @Schema(description = "头像路径", example = "https://example.com/avatar.jpg") + private String avatar; + + @Schema(description = "头像图片ID", example = "1") + private Long avatarImageId; + + @Schema(description = "角色", example = "user") + private String role; + + @Schema(description = "角色名称", example = "普通用户") + private String roleName; + + @Schema(description = "密码安全级别", example = "3") + private Integer pwdSecurityLevel; + + @Schema(description = "密码最后更新时间", example = "2023-12-01T10:00:00") + private LocalDateTime pwdUpdateTime; + + @Schema(description = "最后登录IP", example = "192.168.1.1") + private String lastLoginIp; + + @Schema(description = "最后登录时间", example = "2023-12-01T10:00:00") + private LocalDateTime lastLoginDate; + + @Schema(description = "冻结时间", example = "2023-12-01T10:00:00") + private LocalDateTime freezeDate; + + @Schema(description = "冻结原因", example = "违规操作") + private String freezeCause; + + @Schema(description = "状态", example = "0") + private Integer status; + + @Schema(description = "状态名称", example = "正常") + private String statusName; + + @Schema(description = "备注信息", example = "新员工账户") + private String remark; + + @Schema(description = "创建时间", example = "2023-12-01T10:00:00") + private LocalDateTime createTime; + + @Schema(description = "更新时间", example = "2023-12-01T10:00:00") + private LocalDateTime updateTime; + + @Schema(description = "创建者", example = "管理员") + private String createByName; + + @Schema(description = "更新者", example = "管理员") + private String updateByName; +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/service/AccountService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/service/AccountService.java new file mode 100644 index 00000000..3a7530ed --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/service/AccountService.java @@ -0,0 +1,71 @@ +package top.xyquan.ecomdmbbackend.modules.org.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.AccountFilterDTO; +import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.CreateAccountDTO; +import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.UpdateAccountDTO; +import top.xyquan.ecomdmbbackend.modules.sys.model.po.UserPO; +import top.xyquan.ecomdmbbackend.modules.org.model.vo.AccountPageVO; +import top.xyquan.ecomdmbbackend.modules.org.model.vo.AccountVO; + +/** + * 账户管理服务接口 + */ +public interface AccountService extends IService { + + /** + * 创建账户(超管接口) + * + * @param createAccountDTO 创建账户信息 + * @return 账户信息 + */ + AccountVO createAccount(CreateAccountDTO createAccountDTO); + + /** + * 创建账户(管理员接口,自动设置公司ID) + * + * @param createAccountDTO 创建账户信息 + * @return 账户信息 + */ + AccountVO createAccountForCompany(CreateAccountDTO createAccountDTO); + + /** + * 更新账户信息 + * + * @param updateAccountDTO 更新账户信息 + * @param accountId 账户ID + * @return 更新后的账户信息 + */ + AccountVO updateAccount(UpdateAccountDTO updateAccountDTO, Long accountId); + + /** + * 删除账户(逻辑删除) + * + * @param accountId 账户ID + */ + void deleteAccount(Long accountId); + + /** + * 获取账户详情 + * + * @param accountId 账户ID + * @return 账户详情 + */ + AccountVO getAccountDetail(Long accountId); + + /** + * 分页查询账户(超管接口) + * + * @param filterDTO 查询条件 + * @return 分页结果 + */ + AccountPageVO getAccountPage(AccountFilterDTO filterDTO); + + /** + * 分页查询账户(管理员接口,只能查询本公司账户) + * + * @param filterDTO 查询条件 + * @return 分页结果 + */ + AccountPageVO getAccountPageForCompany(AccountFilterDTO filterDTO); +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/service/impl/AccountServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/service/impl/AccountServiceImpl.java new file mode 100644 index 00000000..0645b1d6 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/org/service/impl/AccountServiceImpl.java @@ -0,0 +1,360 @@ +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.modules.sys.mapper.UserMapper; +import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.AccountFilterDTO; +import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.CreateAccountDTO; +import top.xyquan.ecomdmbbackend.modules.org.model.dto.account.UpdateAccountDTO; +import top.xyquan.ecomdmbbackend.modules.sys.model.po.UserPO; +import top.xyquan.ecomdmbbackend.modules.org.model.vo.AccountPageVO; +import top.xyquan.ecomdmbbackend.modules.org.model.vo.AccountVO; +import top.xyquan.ecomdmbbackend.modules.org.service.AccountService; +import top.xyquan.ecomdmbbackend.modules.org.service.CompanyService; +import top.xyquan.ecomdmbbackend.modules.org.service.DepartmentService; +import top.xyquan.ecomdmbbackend.modules.org.service.PositionService; +import top.xyquan.ecomdmbbackend.utils.CurrentUserContext; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.stream.Collectors; + +/** + * 账户管理服务实现类 + */ +@Service +public class AccountServiceImpl extends ServiceImpl implements AccountService { + + private final PasswordEncoder passwordEncoder; + private final CurrentUserContext currentUserContext; + + public AccountServiceImpl(PasswordEncoder passwordEncoder, + CurrentUserContext currentUserContext, + CompanyService companyService, + DepartmentService departmentService, + PositionService positionService) { + this.passwordEncoder = passwordEncoder; + this.currentUserContext = currentUserContext; + } + + @Override + public AccountVO createAccount(CreateAccountDTO createAccountDTO) { + // 验证登录账号是否已存在 + if (StringUtils.hasText(createAccountDTO.getLoginCode())) { + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + wrapper.eq(UserPO::getLoginCode, createAccountDTO.getLoginCode()); + if (this.count(wrapper) > 0) { + throw new BusinessException("登录账号已存在"); + } + } + + // 验证手机号码是否已存在 + if (StringUtils.hasText(createAccountDTO.getPhone())) { + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + wrapper.eq(UserPO::getPhone, createAccountDTO.getPhone()); + if (this.count(wrapper) > 0) { + throw new BusinessException("手机号码已存在"); + } + } + + // 创建账户 + UserPO user = new UserPO(); + BeanUtils.copyProperties(createAccountDTO, user); + user.setPassword(passwordEncoder.encode(createAccountDTO.getPassword())); + user.setRole("user"); // 默认角色 + user.setStatus(0); // 正常状态 + user.setPwdSecurityLevel(calculatePasswordSecurityLevel(createAccountDTO.getPassword())); + user.setPwdUpdateTime(LocalDateTime.now()); + user.setCreateBy(currentUserContext.getCurrentUserId()); + + // 保存账户 + this.save(user); + + // 转换为VO并返回 + 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) { + // 查询账户 + UserPO user = super.getById(accountId); + if (user == null) { + throw new BusinessException("账户不存在"); + } + + // 检查登录账号是否被其他用户使用 + if (StringUtils.hasText(updateAccountDTO.getLoginCode()) && + !updateAccountDTO.getLoginCode().equals(user.getLoginCode())) { + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + wrapper.eq(UserPO::getLoginCode, updateAccountDTO.getLoginCode()); + wrapper.ne(UserPO::getId, accountId); + if (this.count(wrapper) > 0) { + throw new BusinessException("登录账号已被其他用户使用"); + } + } + + // 检查手机号码是否被其他用户使用 + if (StringUtils.hasText(updateAccountDTO.getPhone()) && + !updateAccountDTO.getPhone().equals(user.getPhone())) { + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + wrapper.eq(UserPO::getPhone, updateAccountDTO.getPhone()); + wrapper.ne(UserPO::getId, accountId); + if (this.count(wrapper) > 0) { + throw new BusinessException("手机号码已被其他用户使用"); + } + } + + // 更新账户信息 + if (StringUtils.hasText(updateAccountDTO.getLoginCode())) { + user.setLoginCode(updateAccountDTO.getLoginCode()); + } + if (StringUtils.hasText(updateAccountDTO.getPhone())) { + user.setPhone(updateAccountDTO.getPhone()); + } + if (StringUtils.hasText(updateAccountDTO.getRealName())) { + user.setRealName(updateAccountDTO.getRealName()); + } + if (updateAccountDTO.getAvatarImageId() != null) { + user.setAvatarImageId(updateAccountDTO.getAvatarImageId()); + } + if (updateAccountDTO.getDepartmentId() != null) { + user.setDepartmentId(updateAccountDTO.getDepartmentId()); + } + if (updateAccountDTO.getPositionId() != null) { + user.setPositionId(updateAccountDTO.getPositionId()); + } + if (updateAccountDTO.getStatus() != null) { + user.setStatus(updateAccountDTO.getStatus()); + } + if (StringUtils.hasText(updateAccountDTO.getRemark())) { + user.setRemark(updateAccountDTO.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 AccountPageVO getAccountPage(AccountFilterDTO filterDTO) { + // 构建查询条件 + LambdaQueryWrapper wrapper = buildQueryWrapper(filterDTO); + + // 分页查询 + Page page = new Page<>(filterDTO.getPageNum(), filterDTO.getPageSize()); + IPage pageResult = this.page(page, wrapper); + + // 转换为VO + List records = pageResult.getRecords().stream() + .map(this::convertToVO) + .collect(Collectors.toList()); + + // 构建分页响应 + AccountPageVO pageVO = new AccountPageVO(); + pageVO.setRecords(records); + pageVO.setTotal(pageResult.getTotal()); + pageVO.setCurrent(pageResult.getCurrent()); + pageVO.setSize(pageResult.getSize()); + pageVO.setPages(pageResult.getPages()); + + return pageVO; + } + + @Override + public AccountPageVO getAccountPageForCompany(AccountFilterDTO filterDTO) { + // 从用户上下文获取公司ID + Long companyId = currentUserContext.getCurrentCompanyId(); + if (companyId == null) { + throw new BusinessException("无法获取公司信息"); + } + + // 设置公司ID过滤条件 + filterDTO.setCompanyId(companyId); + + // 调用通用分页查询方法 + return getAccountPage(filterDTO); + } + + /** + * 构建查询条件 + */ + private LambdaQueryWrapper buildQueryWrapper(AccountFilterDTO filterDTO) { + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + + // 登录账号 + 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; // 弱 + }; + } +}