会员: 删除用户标签, 用户等级时, 校验是否关联了用户

This commit is contained in:
owen 2023-08-22 01:33:28 +08:00
parent 7fd497fcc9
commit 5a2d9d14df
3 changed files with 33 additions and 3 deletions

View File

@ -67,4 +67,14 @@ public interface MemberUserMapper extends BaseMapperX<MemberUserDO> {
default Long selectCountByGroupId(Long groupId) {
return selectCount(MemberUserDO::getGroupId, groupId);
}
default Long selectCountByLevelId(Long levelId) {
return selectCount(MemberUserDO::getLevelId, levelId);
}
default Long selectCountByTagId(Long tagId) {
return selectCount(new LambdaQueryWrapperX<MemberUserDO>()
.apply("FIND_IN_SET({0}, tag_ids)", tagId));
}
}

View File

@ -74,9 +74,10 @@ public class MemberLevelServiceImpl implements MemberLevelService {
@Override
public void deleteLevel(Long id) {
// TODO @疯狂校验是否有用户使用该等级
// 校验存在
validateLevelExists(id);
// 校验分组下是否有用户
validateLevelHasUser(id);
// 删除
levelMapper.deleteById(id);
}
@ -149,6 +150,14 @@ public class MemberLevelServiceImpl implements MemberLevelService {
validateExperienceOutRange(list, id, level, experience);
}
@VisibleForTesting
void validateLevelHasUser(Long id) {
Long count = memberUserMapper.selectCountByLevelId(id);
if (count > 0) {
throw exception(GROUP_HAS_USER);
}
}
@Override
public MemberLevelDO getLevel(Long id) {
return levelMapper.selectById(id);

View File

@ -10,6 +10,7 @@ import cn.iocoder.yudao.module.member.controller.admin.tag.vo.MemberTagUpdateReq
import cn.iocoder.yudao.module.member.convert.tag.MemberTagConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.tag.MemberTagDO;
import cn.iocoder.yudao.module.member.dal.mysql.tag.MemberTagMapper;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -18,8 +19,7 @@ import java.util.Collection;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.TAG_NAME_EXISTS;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.TAG_NOT_EXISTS;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;
/**
* 会员标签 Service 实现类
@ -32,6 +32,8 @@ public class MemberTagServiceImpl implements MemberTagService {
@Resource
private MemberTagMapper tagMapper;
@Resource
private MemberUserMapper memberUserMapper;
@Override
public Long createTag(MemberTagCreateReqVO createReqVO) {
@ -59,6 +61,8 @@ public class MemberTagServiceImpl implements MemberTagService {
public void deleteTag(Long id) {
// 校验存在
validateTagExists(id);
// 校验标签下是否有用户
validateTagHasUser(id);
// 删除
tagMapper.deleteById(id);
}
@ -87,6 +91,13 @@ public class MemberTagServiceImpl implements MemberTagService {
}
}
void validateTagHasUser(Long id) {
Long count = memberUserMapper.selectCountByTagId(id);
if (count > 0) {
throw exception(TAG_HAS_USER);
}
}
@Override
public MemberTagDO getTag(Long id) {
return tagMapper.selectById(id);