package com.rq.authority.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rq.authority.common.handler.BusinessException;
import com.rq.authority.common.vo.MenuVo;
import com.rq.authority.dao.ResourceDao;
import com.rq.authority.entity.OrganizationResource;
import com.rq.authority.entity.Resource;
import com.rq.authority.entity.RoleResource;
import com.rq.authority.service.OrganizationResourceService;
import com.rq.authority.service.ResourceService;
import com.rq.authority.service.RoleResourceService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* <p>
* 资源权限表 服务实现类
* </p>
*
* @author
* @since 2018-12-29
*/
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceDao, Resource> implements ResourceService {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private ResourceDao resourceDao;
@Autowired
private RoleResourceService roleResourceService;
@Autowired
private OrganizationResourceService organizationResourceService;
@Override
@Cacheable(value = "resource-all-eager")
public List<Resource> findAllResourcesEagerLoading() {
return resourceDao.findAllResourcesEagerLoading();
}
@Override
public List<Resource> findAllResourcesLazyLoading() {
return resourceDao.findAllResourcesLazyLoading();
}
@Override
@Cacheable(value = "resource-eager")
public List<Resource> findResourcesByParentIdEagerLoading(String parentId) {
return resourceDao.findResourcesByParentIdEagerLoading(parentId);
}
@Override
public List<Resource> findResourcesByParentIdLazyLoading(String parentId) {
return resourceDao.findResourcesByParentIdLazyLoading(parentId);
}
/**
* 根据用户id返回菜单树
* @param userId
* @return
*/
@Override
public List<MenuVo> selectMenusByUserId(String userId) {
List<MenuVo> menus = resourceDao.selectMenusByUserId(userId);
return menus;
}
/**
* 根据用户id返回资源列表(非树形结构)
* @param userId
* @return
*/
@Override
public List<Resource> selectResourcesByUserId(String userId) {
List<Resource> resources = resourceDao.selectResourcesByUserId(userId);
return resources;
}
@Transactional(rollbackFor = Exception.class)
@Override
public Boolean deleteResourceById(String id) throws BusinessException {
boolean result;
if (StringUtils.isBlank(id) || id.toLowerCase().equals("null") ){
throw new BusinessException("资源id不能为空");
}
//删除该资源节点和树下资源与组织,角色的关系
List<String> resourceIds = getResourceIdsById(id);
for (String resource_id : resourceIds) {
result = roleResourceService.delete(new EntityWrapper<RoleResource>().eq("resource_id",resource_id));
result = organizationResourceService.delete(new EntityWrapper<OrganizationResource>().eq("resource_id",resource_id));
if (!result){
throw new BusinessException("解除该资源节点和树下资源与组织,角色的关系失败");
}
}
// 删除当前节点
Resource condition1 = new Resource();
condition1.setId(id);
condition1.setEnabled(Boolean.FALSE);
result = this.updateById(condition1);
if (!result){
throw new BusinessException("删除菜单失败");
}
// 删除父节点为当前节点的节点
List<Resource> childList = selectList(new EntityWrapper<Resource>().eq("parent_id", id));
if (childList != null && childList.size() > 0) {
Resource conditon2 = new Resource();
conditon2.setParentId(id);
Resource resource = new Resource();
resource.setEnabled(Boolean.FALSE);
result = this.update(resource, new EntityWrapper<>(conditon2));
if (!result) {
throw new BusinessException("删除菜单失败");
}
}
//待加入redis更新
return result;
}
/**
* 通过id获取该资源节点树id集合
* @param id
* @return
*/
private List<String> getResourceIdsById(String id) {
List<String> list = new ArrayList<>();
list.add(id);
List<Resource> childList = findResourcesByParentIdEagerLoading(id);
return getUnderResourceIdList(childList, list);
}
/**
*
* @param childList
* @param list
* @return
*/
public List<String> getUnderResourceIdList(List<Resource> childList ,List<String> list) {
if (childList != null && !"".equals(childList)){
for (Resource resource : childList) {
list.add(resource.getId());
if (resource.getChildren() != null && !"".equals(childList)){
getUnderResourceIdList(resource.getChildren(),list);
}
}
}
return list;
}
@Override
@CacheEvict(value = {"resource-all-eager", "resource-eager"}, allEntries = true)
public boolean insert(Resource entity) {
return super.insert(entity);
}
@Override
@CacheEvict(value = {"resource-all-eager", "resource-eager"}, allEntries = true)
public boolean insertAllColumn(Resource entity) {
return super.insertAllColumn(entity);
}
@Override
@CacheEvict(value = {"resource-all-eager", "resource-eager"}, allEntries = true)
public boolean insertBatch(List<Resource> entityList) {
return super.insertBatch(entityList);
}
@Override
@CacheEvict(value = {"resource-all-eager", "resource-eager"}, allEntries = true)
public boolean insertBatch(List<Resource> entityList, int batchSize) {
return super.insertBatch(entityList, batchSize);
}
@Override
@CacheEvict(value = {"resource-all-eager", "resource-eager"}, allEntries = true)
public boolean insertOrUpdate(Resource entity) {
return super.insertOrUpdate(entity);
}
@Override
@CacheEvict(value = {"resource-all-eager", "resource-eager"}, allEntries = true)
public boolean insertOrUpdateAllColumn(Resource entity) {
return super.insertOrUpdateAllColumn(entity);
}
@Override
@CacheEvict(value = {"resource-all-eager", "resource-eager"}, allEntries = true)
public boolean insertOrUpdateBatch(List<Resource> entityList) {
return super.insertOrUpdateBatch(entityList);
}
@Override
@CacheEvict(value = {"resource-all-eager", "resource-eager"}, allEntries = true)
public boolean insertOrUpdateBatch(List<Resource> entityList, int batchSize) {
return super.insertOrUpdateBatch(entityList, batchSize);
}
@Override
@CacheEvict(value = {"resource-all-eager", "resource-eager"}, allEntries = true)
public boolean insertOrUpdateAllColumnBatch(List<Resource> entityList) {
return super.insertOrUpdateAllColumnBatch(entityList);
}
@Override
@CacheEvict(value = {"resource-all-eager", "resource-eager"}, allEntries = true)
p