com.base4j.mvc.sys.service.impl.SysRoleResourceServiceImpl Maven / Gradle / Ivy
package com.base4j.mvc.sys.service.impl;
import com.base4j.mvc.base.service.impl.BaseServiceImpl;
import com.base4j.mvc.sys.entity.SysResource;
import com.base4j.mvc.sys.entity.SysRoleResource;
import com.base4j.mvc.sys.mapper.SysRoleResourceMapper;
import com.base4j.mvc.sys.model.Resource;
import com.base4j.mvc.sys.model.Resources;
import com.base4j.mvc.sys.service.SysResourceService;
import com.base4j.mvc.sys.service.SysRoleResourceService;
import com.base4j.mybatis.base.QueryParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class SysRoleResourceServiceImpl extends BaseServiceImpl implements SysRoleResourceService {
@Autowired
private SysResourceService sysResourceService;
@Autowired
private SysRoleResourceMapper sysRoleResourceMapper;
@Override
public void saveRoleResources(List roleResourceList) {
QueryParams queryParams = new QueryParams(SysRoleResource.class);
QueryParams.Criteria criteria = queryParams.createCriteria();
criteria.andEqualTo("sysRoleId", roleResourceList.get(0).getSysRoleId());
super.deleteByParams(queryParams);
super.insertList(roleResourceList);
}
@Override
public Resources selectResources(long roleId) {
Resources resources = new Resources(); //定义一个资源对象
resources.setResourceData(getResourceData());//获取所有资源
resources.setCheckIds(getCheckIds(roleId));
resources.setParentIds(resources.getCheckIds());
return resources;
}
/**
* 角色具有的资源
*
* @param checkIDs
* @return
*/
private List getCheckIds(long sysRoleId) {
QueryParams params = new QueryParams(SysRoleResource.class);
QueryParams.Criteria paramsCriteria = params.createCriteria();
paramsCriteria.andEqualTo("sysRoleId", sysRoleId);
List childId = sysRoleResourceMapper.selectListByParams(params);
List result = new ArrayList<>();
for (SysRoleResource sysRoleResource : childId) {
result.add(sysRoleResource.getSysResourceId());
}
return result;
}
/**
* @return
*/
private List getResourceData() {
return selectResourceByParentId(0);
}
/**
* 根据父节点ID查询子节点数据
*
* @param parentId
* @return
*/
private List selectResourceByParentId(long parentId) {
QueryParams params = new QueryParams(SysResource.class);
QueryParams.Criteria paramsCriteria = params.createCriteria();
paramsCriteria.andEqualTo("parentId", parentId);
List children = sysResourceService.selectListByParams(params);
List result = new ArrayList<>();
for (SysResource sysResource : children) {
Resource resource = new Resource();
resource.setId(sysResource.getId());
resource.setLabel(sysResource.getName());
List resources = selectResourceByParentId(sysResource.getId());
resource.setChildren(resources);
result.add(resource);
}
return result;
}
private Resource convertSysResourceToResource(SysResource sysResource) {
Resource resource = new Resource();
resource.setLabel(sysResource.getName());
resource.setId(sysResource.getId());
return resource;
}
}