mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:19:19 +08:00
fix: 修复公司成员删除删掉其他公司其他同一成员问题
This commit is contained in:
+1
-1
@@ -53,7 +53,7 @@ public class AccountController {
|
||||
return ApiResponse.success("账户更新成功", accountVO);
|
||||
}
|
||||
|
||||
/** 逻辑删除:停用 sys_user 并移除公司/部门成员关系 */
|
||||
/** 删除本公司账户:解绑本公司成员;用户无其他公司时才停用 sys_user */
|
||||
@DeleteMapping("/delete/{accountId}")
|
||||
@RequirePermission(value = "org:account:delete", description = "删除账户", allowSuper = true)
|
||||
public ApiResponse<Void> deleteAccount(
|
||||
|
||||
+2
-2
@@ -17,7 +17,7 @@ public interface AccountService extends IService<UserPO> {
|
||||
/**
|
||||
* 创建公司账户(拥有者接口)
|
||||
* <p>
|
||||
* 公司 ID 从当前登录上下文获取;手机号已存在则拉入公司,不存在则新建用户。
|
||||
* 手机号已存在则拉入/恢复本公司成员(停用用户会重新启用),不存在则新建用户。
|
||||
* 仅公司拥有者可调用,超管不受限。
|
||||
*
|
||||
* @param createAccountDTO 创建账户信息
|
||||
@@ -35,7 +35,7 @@ public interface AccountService extends IService<UserPO> {
|
||||
CompanyAccountVO updateAccountForCompany(UpdateAccountDTO updateAccountDTO, Long accountId);
|
||||
|
||||
/**
|
||||
* 删除账户(逻辑删除)
|
||||
* 删除本公司账户:解绑本公司成员;用户无其他公司时才停用
|
||||
*
|
||||
* @param accountId 账户ID
|
||||
*/
|
||||
|
||||
+49
-21
@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
@@ -28,6 +29,8 @@ import top.xyquan.ecomdmbbackend.modules.org.company.member.enums.CompanyMemberR
|
||||
import top.xyquan.ecomdmbbackend.modules.org.company.member.model.vo.CompanyMemberVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.position.service.PositionService;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.position.model.po.PositionPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.department.model.po.DepartmentPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.department.service.DepartmentService;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
@@ -38,7 +41,9 @@ import java.util.stream.Collectors;
|
||||
* 公司账户管理服务实现类
|
||||
* <p>
|
||||
* 面向公司拥有者(及超管)的本公司账户 CRUD。
|
||||
* 创建账户时:手机号已注册则拉入当前公司,未注册则新建用户并加入公司。
|
||||
* <p>
|
||||
* 边界:sys_user 按手机号全局唯一,可跨公司;本公司「账户」本质是 company_member。
|
||||
* 删除只解绑本公司;停用用户仅当其已无任何公司成员关系。创建可恢复软删成员并重新启用用户。
|
||||
*/
|
||||
@Service
|
||||
public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implements AccountService {
|
||||
@@ -48,6 +53,7 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
private final CompanyService companyService;
|
||||
private final CompanyMemberService companyMemberService;
|
||||
private final DepartmentMemberService departmentMemberService;
|
||||
private final DepartmentService departmentService;
|
||||
private final PositionService positionService;
|
||||
|
||||
public AccountServiceImpl(PasswordEncoder passwordEncoder,
|
||||
@@ -55,12 +61,14 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
CompanyService companyService,
|
||||
CompanyMemberService companyMemberService,
|
||||
DepartmentMemberService departmentMemberService,
|
||||
DepartmentService departmentService,
|
||||
PositionService positionService) {
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
this.currentUserContext = currentUserContext;
|
||||
this.companyService = companyService;
|
||||
this.companyMemberService = companyMemberService;
|
||||
this.departmentMemberService = departmentMemberService;
|
||||
this.departmentService = departmentService;
|
||||
this.positionService = positionService;
|
||||
}
|
||||
|
||||
@@ -80,6 +88,7 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
* @return 账户VO
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public CompanyAccountVO createAccountForCompany(CreateAccountDTO createAccountDTO) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
if (companyId == null) {
|
||||
@@ -122,19 +131,17 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
|
||||
/**
|
||||
* 将已存在用户加入当前公司
|
||||
* 不修改密码;可选更新姓名、备注;同时建立公司成员及部门关联
|
||||
* <p>
|
||||
* 活跃成员直接报错;软删成员由 companyMemberService.create 恢复。
|
||||
* 用户若因离开全部公司被停用,加入时重新启用(不改密码)。
|
||||
*/
|
||||
private CompanyAccountVO addExistingUserToCompany(
|
||||
UserPO user,
|
||||
Long companyId,
|
||||
CreateAccountDTO createAccountDTO,
|
||||
CompanyMemberRole memberRole) {
|
||||
if (user.getStatus() == null || user.getStatus() != 1) {
|
||||
throw new BusinessException("该手机号对应账户状态异常,无法加入公司");
|
||||
}
|
||||
|
||||
CompanyMemberPO existingMember = companyMemberService.getByUserIdAndCompanyId(user.getId(), companyId);
|
||||
if (existingMember != null) {
|
||||
CompanyMemberPO activeMember = companyMemberService.getByUserIdAndCompanyId(user.getId(), companyId);
|
||||
if (activeMember != null) {
|
||||
throw new BusinessException("该用户已是公司成员");
|
||||
}
|
||||
|
||||
@@ -144,6 +151,10 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
if (StringUtils.hasText(createAccountDTO.getRemark())) {
|
||||
user.setRemark(createAccountDTO.getRemark());
|
||||
}
|
||||
// 停用用户拉入公司时恢复启用,否则跨公司共用的同一账号无法登录
|
||||
if (user.getStatus() == null || user.getStatus() != 1) {
|
||||
user.setStatus(1);
|
||||
}
|
||||
user.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
this.updateById(user);
|
||||
|
||||
@@ -263,30 +274,47 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除账户
|
||||
* @param accountId 账户ID
|
||||
* 删除本公司账户:仅解除当前公司成员/部门关系。
|
||||
* sys_user 全局共用,仅当用户已不属于任何公司时才停用,避免误伤其他公司。
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteAccount(Long accountId) {
|
||||
// 查询账户
|
||||
UserPO user = super.getById(accountId);
|
||||
if (user == null) {
|
||||
throw new BusinessException("账户不存在");
|
||||
}
|
||||
|
||||
// 逻辑删除:停用账户(sys_user.status:0=停用,1=正常)
|
||||
user.setStatus(0);
|
||||
user.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
this.updateById(user);
|
||||
|
||||
// 删除公司成员关系
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
companyMemberService.removeByUserIdAndCompanyId(user.getId(), companyId);
|
||||
if (companyId == null) {
|
||||
throw new BusinessException("无法获取公司信息");
|
||||
}
|
||||
|
||||
// 删除所有部门成员关系
|
||||
List<DepartmentMemberVO> deptMembers = departmentMemberService.getByUserId(user.getId());
|
||||
CompanyMemberPO member = companyMemberService.getByUserIdAndCompanyId(user.getId(), companyId);
|
||||
if (member == null) {
|
||||
throw new BusinessException("账户不属于当前公司");
|
||||
}
|
||||
|
||||
companyMemberService.removeByUserIdAndCompanyId(user.getId(), companyId);
|
||||
removeDepartmentMembershipsForCompany(user.getId(), companyId);
|
||||
|
||||
// 仍属其他公司则保持启用
|
||||
List<CompanyMemberVO> remaining = companyMemberService.getByUserId(user.getId());
|
||||
if (remaining.isEmpty()) {
|
||||
user.setStatus(0);
|
||||
user.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
this.updateById(user);
|
||||
}
|
||||
}
|
||||
|
||||
/** 只移除属于当前公司的部门成员关系,不碰其他公司部门 */
|
||||
private void removeDepartmentMembershipsForCompany(Long userId, Long companyId) {
|
||||
List<DepartmentMemberVO> deptMembers = departmentMemberService.getByUserId(userId);
|
||||
for (DepartmentMemberVO dm : deptMembers) {
|
||||
departmentMemberService.removeMember(dm.getDepartmentId(), user.getId());
|
||||
DepartmentPO department = departmentService.getById(dm.getDepartmentId());
|
||||
if (department != null && companyId.equals(department.getCompanyId())) {
|
||||
departmentMemberService.removeMember(dm.getDepartmentId(), userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-3
@@ -69,12 +69,21 @@ public class CompanyMemberServiceImpl extends ServiceImpl<CompanyMemberMapper, C
|
||||
|
||||
@Override
|
||||
public CompanyMemberVO create(CreateCompanyMemberDTO dto, Long createBy) {
|
||||
// 检查是否已存在
|
||||
// 含软删:活跃则拒绝;已退出则恢复,避免「已是成员」却无法再加入
|
||||
LambdaQueryWrapper<CompanyMemberPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CompanyMemberPO::getUserId, dto.getUserId())
|
||||
.eq(CompanyMemberPO::getCompanyId, dto.getCompanyId());
|
||||
if (this.count(wrapper) > 0) {
|
||||
throw new BusinessException("该用户已是公司成员");
|
||||
CompanyMemberPO existing = this.getOne(wrapper);
|
||||
if (existing != null) {
|
||||
if (existing.getStatus() != null && existing.getStatus() == 1) {
|
||||
throw new BusinessException("该用户已是公司成员");
|
||||
}
|
||||
existing.setRole(CompanyMemberRole.fromCode(dto.getRole()));
|
||||
existing.setPositionId(dto.getPositionId());
|
||||
existing.setStatus(1);
|
||||
existing.setUpdateBy(createBy);
|
||||
this.updateById(existing);
|
||||
return convertToVO(existing);
|
||||
}
|
||||
|
||||
CompanyMemberPO member = new CompanyMemberPO();
|
||||
|
||||
+10
-3
@@ -50,12 +50,19 @@ public class DepartmentMemberServiceImpl extends ServiceImpl<DepartmentMemberMap
|
||||
|
||||
@Override
|
||||
public DepartmentMemberVO addMember(AddDepartmentMemberDTO dto) {
|
||||
// 检查是否已存在
|
||||
// 含软删:活跃则拒绝;已退出则恢复
|
||||
LambdaQueryWrapper<DepartmentMemberPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(DepartmentMemberPO::getUserId, dto.getUserId())
|
||||
.eq(DepartmentMemberPO::getDepartmentId, dto.getDepartmentId());
|
||||
if (this.count(wrapper) > 0) {
|
||||
throw new BusinessException("该用户已是部门成员");
|
||||
DepartmentMemberPO existing = this.getOne(wrapper);
|
||||
if (existing != null) {
|
||||
if (existing.getStatus() != null && existing.getStatus() == 1) {
|
||||
throw new BusinessException("该用户已是部门成员");
|
||||
}
|
||||
existing.setStatus(1);
|
||||
existing.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
this.updateById(existing);
|
||||
return convertToVO(existing);
|
||||
}
|
||||
|
||||
DepartmentMemberPO member = new DepartmentMemberPO();
|
||||
|
||||
Reference in New Issue
Block a user