♻️ Refactoring code.Assert断言进行参数校验

This commit is contained in:
lishangbu 2020-05-14 04:45:16 +08:00
parent bea8d4e62b
commit c7fafda6e5
4 changed files with 15 additions and 25 deletions

View File

@ -29,6 +29,7 @@ import com.pig4cloud.pig.common.core.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
/**
* 字典项
@ -54,9 +55,7 @@ public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDi
SysDictItem dictItem = this.getById(id);
SysDict dict = dictService.getById(dictItem.getDictId());
// 系统内置
if (DictTypeEnum.SYSTEM.getType().equals(dict.getSystem())) {
return R.failed("系统内置字典项目不能删除");
}
Assert.state(!DictTypeEnum.SYSTEM.getType().equals(dict.getSystem()),"系统内置字典项目不能删除");
return R.ok(this.removeById(id));
}
@ -72,9 +71,7 @@ public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDi
//查询字典
SysDict dict = dictService.getById(item.getDictId());
// 系统内置
if (DictTypeEnum.SYSTEM.getType().equals(dict.getSystem())) {
return R.failed("系统内置字典项目不能删除");
}
Assert.state(!DictTypeEnum.SYSTEM.getType().equals(dict.getSystem()),"系统内置字典项目不能修改");
return R.ok(this.updateById(item));
}
}

View File

@ -31,6 +31,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
/**
* 字典表
@ -55,10 +56,7 @@ public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> impl
public R removeDict(Integer id) {
SysDict dict = this.getById(id);
// 系统内置
if (DictTypeEnum.SYSTEM.getType().equals(dict.getSystem())) {
return R.failed("系统内置字典不能删除");
}
Assert.state(!DictTypeEnum.SYSTEM.getType().equals(dict.getSystem()),"系统内置字典项目不能删除");
baseMapper.deleteById(id);
dictItemMapper.delete(Wrappers.<SysDictItem>lambdaQuery()
.eq(SysDictItem::getDictId, id));
@ -75,9 +73,7 @@ public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> impl
public R updateDict(SysDict dict) {
SysDict sysDict = this.getById(dict.getId());
// 系统内置
if (DictTypeEnum.SYSTEM.getType().equals(sysDict.getSystem())) {
return R.failed("系统内置字典不能修改");
}
Assert.state(!DictTypeEnum.SYSTEM.getType().equals(sysDict.getSystem()),"系统内置字典项目不能修改");
return R.ok(this.updateById(dict));
}
}

View File

@ -38,6 +38,7 @@ import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.Comparator;
import java.util.List;
@ -70,9 +71,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
// 查询父节点为当前节点的节点
List<SysMenu> menuList = this.list(Wrappers.<SysMenu>query()
.lambda().eq(SysMenu::getParentId, id));
if (CollUtil.isNotEmpty(menuList)) {
return R.failed("菜单含有下级不能删除");
}
Assert.isNull(menuList,"菜单含有下级不能删除");
sysRoleMenuMapper.delete(Wrappers.<SysRoleMenu>query()
.lambda().eq(SysRoleMenu::getMenuId, id));

View File

@ -46,6 +46,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.time.LocalDateTime;
import java.util.HashSet;
@ -164,15 +165,12 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
public R<Boolean> updateUserInfo(UserDTO userDto) {
UserVO userVO = baseMapper.getUserVoByUsername(userDto.getUsername());
SysUser sysUser = new SysUser();
if (StrUtil.isNotBlank(userDto.getPassword())
&& StrUtil.isNotBlank(userDto.getNewpassword1())) {
if (ENCODER.matches(userDto.getPassword(), userVO.getPassword())) {
sysUser.setPassword(ENCODER.encode(userDto.getNewpassword1()));
} else {
log.warn("原密码错误,修改密码失败:{}", userDto.getUsername());
return R.failed("原密码错误,修改失败");
}
}
Assert.notNull(userDto.getPassword(),"原密码不存在");
Assert.notNull(userDto.getNewpassword1(),"新密码不存在");
Assert.state(ENCODER.matches(userDto.getPassword(), userVO.getPassword()),"原密码错误,修改失败");
sysUser.setPassword(ENCODER.encode(userDto.getNewpassword1()));
sysUser.setPhone(userDto.getPhone());
sysUser.setUserId(userVO.getUserId());
sysUser.setAvatar(userDto.getAvatar());