Controller 控制层: @ApiOperation("资源树查询") @PostMapping(value = "/queryResourceTree") public R<List<ResourcePageView>> queryResourceTree(@RequestBody BaseProductResource vo,HttpServletRequest request){ String appCode = request.getHeader("productCode"); if(StringUtils.isBlank(appCode)){ throw new BusinessException(BusinessEnum.ERROR_PARAMETER.getCode(),"productCode 不能为空"); } vo.setProductCode(appCode); return R.success(resourceService.queryResourceTree(vo)); }
service 层: List<ResourcePageView> queryResourceTree(BaseProductResource vo); serviceImpl 实现层:
@Override public List<ResourcePageView> queryResourceTree(BaseProductResource vo) { List<ResourcePageView> result = Lists.newArrayList(); List<ResourcePageView> resourceList = resourceMapper.queryResourceTree(vo); if (CollectionUtils.isEmpty(resourceList)){ return Collections.EMPTY_LIST; } //一级节点 List<ResourcePageView> resourceOne = new ArrayList<>(); if (StringUtils.isNotBlank(vo.getParentCode())){ resourceOne = resourceList.stream().sorted(Comparator.comparing(ResourcePageView::getOrderNum)) .filter(one -> one.getParentCode().equals(vo.getParentCode())).collect(Collectors.toList()); }else{ resourceOne = resourceList.stream().sorted(Comparator.comparing(ResourcePageView::getOrderNum)) .filter(one -> one.getParentCode().equals("0")).collect(Collectors.toList()); } resourceOne.stream().forEach(resource->{ ResourcePageView view = new ResourcePageView(); view.setId(resource.getId()); view.setPath(resource.getPath()); view.setType(resource.getType()); view.setResourceType(resource.getResourceType()); view.setDescription(resource.getDescription()); view.setOrderNum(resource.getOrderNum()); view.setProductStatus(resource.getProductStatus()); view.setProductName(resource.getProductName()); view.setProductType(resource.getProductType()); view.setLogoUrl(resource.getLogoUrl()); view.setChildren(getchild(resource,resourceList)); view.setParentCode(resource.getParentCode()); view.setProductCode(resource.getProductCode()); view.setResourceCode(resource.getResourceCode()); view.setResourceName(resource.getResourceName()); result.add(view); }); return result; } private List<ResourcePageView> getchild(ResourcePageView resource,List<ResourcePageView> resourceList){ List<ResourcePageView> result = Lists.newArrayList(); //筛选出下一级的资源 List<ResourcePageView> resourceChilds = resourceList.stream().sorted(Comparator.comparing(ResourcePageView::getOrderNum)) .filter(one -> one.getParentCode().equals(resource.getResourceCode())).collect(Collectors.toList()); if (!CollectionUtils.isEmpty(resourceChilds)){ resourceChilds.stream().forEach(child->{ ResourcePageView view = new ResourcePageView(); view.setId(child.getId()); view.setPath(child.getPath()); view.setType(child.getType()); view.setResourceType(child.getResourceType()); view.setDescription(child.getDescription()); view.setOrderNum(child.getOrderNum()); view.setProductStatus(child.getProductStatus()); view.setProductName(child.getProductName()); view.setProductType(child.getProductType()); view.setLogoUrl(child.getLogoUrl()); view.setChildren(getchild(child,resourceList)); view.setParentCode(child.getParentCode()); view.setProductCode(child.getProductCode()); view.setResourceCode(child.getResourceCode()); view.setResourceName(child.getResourceName()); result.add(view); }); } return result; }
dao 持久层: List<ResourcePageView> queryResourceTree(BaseProductResource vo); queryMapper.xml 配置文件:
<select id="queryResourceTree" resultType="com.yonghui.o2o.support.idaas.application.view.ResourcePageView"> select a.id, a.parent_code parentCode, a.product_code productCode, a.resource_code resourceCode, a.resource_name resourceName, a.path, a.type, a.resource_type resourceType, a.description, a.order_num orderNum, b.product_status productStatus, b.product_name productName, b.product_type productType, b.logo_url logoUrl from app_product_resource as a left join app_product b on a.product_code = b.product_code where a.is_delete = 0 <if test="parentCode != null"> and a.parent_code = #{parentCode} </if> <if test="productCode != null"> and a.product_code = #{productCode} </if> <if test="resourceCode != null"> and a.resource_code = #{resourceCode} </if> <if test="resourceName != null"> and a.resource_name LIKE CONCAT('%',#{resourceName},'%') </if> </select>
sql 建表语句:
CREATE TABLE app_product_resource (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`product_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '应用code',
`resource_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '资源编码',
`parent_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '父级',
`resource_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '名称',
`order_num` int(5) NOT NULL COMMENT '排序',
`description` text CHARACTER SET utf8 COLLATE utf8_bin NULL COMMENT '描述信息',
`path` varchar(500) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '资源路径',
`type` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '类型:菜单,按钮,api',
`resource_type` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '资源请求类型:1 get 2 post 3 put 4 delete',
`expand1` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '扩展字段1',
`expand2` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '扩展字段2',
`expand3` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '扩展字段3',
`expand4` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '扩展字段4',
`expand5` json NULL COMMENT '扩展字段5,json字段',
`is_delete` tinyint(1) UNSIGNED NOT NULL DEFAULT 0 COMMENT '删除标记',
`created_by` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '创建人',
`updated_by` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '修改人',
`created_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`updated_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '修改时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `un_resource_code`(`resource_code`, `product_code`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 79 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '应用资源表' ROW_FORMAT = DYNAMIC;
CREATE TABLE app_product (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增id',
`product_name` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '应用名称',
`product_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '应用唯一码',
`logo_url` varchar(500) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'logo图片地址',
`product_type` varchar(15) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT 'web应用' COMMENT '应用类型:1 web 应用 2 移动应用 3 PC客户端',
`product_status` char(1) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT 'Y' COMMENT '应用启用状态: 0 正常 1 禁用',
`redirect_url` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '应用跳转地址',
`loginout_url` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`description` text CHARACTER SET utf8 COLLATE utf8_bin NULL COMMENT '描述信息',
`protocol` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '使用协议 :1 jwt 2 oauth ',
`is_delete` tinyint(1) UNSIGNED NOT NULL DEFAULT 0 COMMENT '删除标记:0 正常 1 删除',
`created_by` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '创建人',
`updated_by` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '修改人',
`created_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`updated_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '修改时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `product_code`(`product_code`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 46 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '应用表' ROW_FORMAT = Dynamic;
@Data public class ResourcePageView extends BaseProductResource implements Serializable { @ApiModelProperty(value = "id") private Integer id; @ApiModelProperty(value = "资源路径") private String path; @ApiModelProperty(value = "权限类型:1 菜单 2 按钮 3 api") private String type; @ApiModelProperty(value = "资源类型:1.get 2 post 3 put 4 delete") private String resourceType; @ApiModelProperty(value = "资源描述") private String description; @ApiModelProperty(value = "资源排序") private String orderNum; @ApiModelProperty(value = "应用状态") private String productStatus; @ApiModelProperty(value = "应用名称") private String productName; @ApiModelProperty(value = "应用类型") private String productType; @ApiModelProperty(value = "应用logo") private String logoUrl; private List<ResourcePageView> children; }