fix 同名校验

This commit is contained in:
shiziyuan9527 2020-06-02 18:12:29 +08:00
parent 5f1ed2611b
commit e39732882d
3 changed files with 28 additions and 52 deletions

View File

@ -77,27 +77,18 @@ public class TestResourcePoolService {
MSException.throwException(Translator.get("test_resource_pool_name_is_null"));
}
String id = testResourcePoolDTO.getId();
String name = testResourcePoolDTO.getName();
if (StringUtils.isNotBlank(id)) {
TestResourcePool pool = testResourcePoolMapper.selectByPrimaryKey(id);
if (!StringUtils.equals(pool.getName(), name)) {
checkPoolName(name);
}
} else {
checkPoolName(name);
TestResourcePoolExample example = new TestResourcePoolExample();
TestResourcePoolExample.Criteria criteria = example.createCriteria();
criteria.andNameEqualTo(resourcePoolName);
if (StringUtils.isNotBlank(testResourcePoolDTO.getId())) {
criteria.andIdNotEqualTo(testResourcePoolDTO.getId());
}
}
public void checkPoolName(String poolName) {
TestResourcePoolExample testResourcePoolExample = new TestResourcePoolExample();
testResourcePoolExample.createCriteria().andNameEqualTo(poolName);
if (testResourcePoolMapper.countByExample(testResourcePoolExample) > 0) {
if (testResourcePoolMapper.countByExample(example) > 0) {
MSException.throwException(Translator.get("test_resource_pool_name_already_exists"));
}
}
}
public void updateTestResourcePoolStatus(String poolId, String status) {
TestResourcePool testResourcePool = testResourcePoolMapper.selectByPrimaryKey(poolId);

View File

@ -92,7 +92,6 @@ public class UserService {
userRole1.setUpdateTime(System.currentTimeMillis());
userRole1.setCreateTime(System.currentTimeMillis());
userRole1.setSourceId(list.get(j));
// TODO 防止重复插入
userRoleMapper.insertSelective(userRole1);
}
}
@ -196,15 +195,15 @@ public class UserService {
if (!roles.isEmpty()) {
insertUserRole(roles, user.getId());
}
String email = user.getEmail();
User u = userMapper.selectByPrimaryKey(userId);
if (!StringUtils.equals(email, u.getEmail())) {
UserExample userExample = new UserExample();
userExample.createCriteria().andEmailEqualTo(email);
if (userMapper.countByExample(userExample) > 0) {
MSException.throwException(Translator.get("user_email_already_exists"));
}
UserExample example = new UserExample();
UserExample.Criteria criteria = example.createCriteria();
criteria.andEmailEqualTo(user.getEmail());
criteria.andIdNotEqualTo(user.getId());
if (userMapper.countByExample(example) > 0) {
MSException.throwException(Translator.get("user_email_already_exists"));
}
user.setUpdateTime(System.currentTimeMillis());
userMapper.updateByPrimaryKeySelective(user);
}

View File

@ -59,35 +59,21 @@ public class WorkspaceService {
workspace.setOrganizationId(currentOrgId);
long currentTime = System.currentTimeMillis();
String wsName = workspace.getName();
checkWorkspace(workspace);
if (StringUtils.isBlank(workspace.getId())) {
checkWsName(wsName, currentOrgId);
workspace.setId(UUID.randomUUID().toString());
workspace.setCreateTime(currentTime);
workspace.setUpdateTime(currentTime);
workspaceMapper.insertSelective(workspace);
} else {
Workspace ws = workspaceMapper.selectByPrimaryKey(workspace.getId());
if (!StringUtils.equals(ws.getName(), wsName)) {
checkWsName(wsName, currentOrgId);
}
workspace.setUpdateTime(currentTime);
workspaceMapper.updateByPrimaryKeySelective(workspace);
}
return workspace;
}
public void checkWsName(String wsName, String orgId) {
WorkspaceExample example = new WorkspaceExample();
example.createCriteria()
.andOrganizationIdEqualTo(orgId)
.andNameEqualTo(wsName);
if (workspaceMapper.countByExample(example) > 0) {
MSException.throwException(Translator.get("workspace_name_already_exists"));
}
}
public List<Workspace> getWorkspaceList(WorkspaceRequest request) {
WorkspaceExample example = new WorkspaceExample();
WorkspaceExample.Criteria criteria = example.createCriteria();
@ -269,17 +255,17 @@ public class WorkspaceService {
MSException.throwException(Translator.get("organization_id_is_null"));
}
String id = workspace.getId();
String orgId = workspace.getOrganizationId();
String name = workspace.getName();
if (StringUtils.isNotBlank(id)) {
Workspace ws = workspaceMapper.selectByPrimaryKey(id);
if (!StringUtils.equals(ws.getName(), name)) {
checkWsName(name, orgId);
}
} else {
checkWsName(name, orgId);
WorkspaceExample example = new WorkspaceExample();
WorkspaceExample.Criteria criteria = example.createCriteria();
criteria.andNameEqualTo(workspace.getName())
.andOrganizationIdEqualTo(workspace.getOrganizationId());
if (StringUtils.isNotBlank(workspace.getId())) {
criteria.andIdNotEqualTo(workspace.getId());
}
if (workspaceMapper.countByExample(example) > 0) {
MSException.throwException(Translator.get("workspace_name_already_exists"));
}
}
}