public Authorization updateAuthorization(Authorization authorization) {
return merge(authorization);
}
public Authorization merge(Authorization authorization) {
Authorization dbAuthorization = authorizationDao.findByAppUser(authorization.getAppId(), authorization.getUserId());
if(dbAuthorization == null) {//如果数据库中不存在相应记录 直接新增
return authorizationDao.createAuthorization(authorization);
}
if(dbAuthorization.equals(authorization)) {//如果是同一条记录直接更新即可
return authorizationDao.updateAuthorization(authorization);
}
for(Long roleId : authorization.getRoleIds()) {//否则合并
if(!dbAuthorization.getRoleIds().contains(roleId)) {
dbAuthorization.getRoleIds().add(roleId);
}
}
if(dbAuthorization.getRoleIds().isEmpty()) {//如果没有角色 直接删除记录即可
authorizationDao.deleteAuthorization(dbAuthorization.getId());
return dbAuthorization;
}
//否则更新
return authorizationDao.updateAuthorization(dbAuthorization);
}