开发中迭代使用

开发中迭代使用

需求结构是这种形式的,如下:

|-----------------第一级菜单
|----------------------第二级菜单
|-------------------------第三级菜单
|----------------------------…
以此类推,有子集菜单就回显出来;

注:
	先说明一下,因为项目是两家公司一起来做的,我们这边只负责业务的开发,
	数据底座维护(数据库我是看不到的)是另一家做的,所以,这个菜单的迭代,
	我是在他们提供的接口上,做了一层封装,然后进行回显,话不多讲,看业务实现;
一、实体类(vo):
忽略所用的注解,框架不一样,用的也就不一样;
@Data
public class ChildrenMenuListVo implements Serializable {



    @Schema(description = "菜单id", example = "2")
    private Long id;

    @Schema(description = "图标", example = "/xxx/xx")
    private String menuIcon;

    @Schema(description = "菜单名称", example = "xxx")
    private String menuName;

    @Schema(description = "菜单顺序", example = "1")
    private Integer menuOrder;

    @Schema(description = "菜单类型 1:菜单, 2, 按钮", example = "1")
    private Integer menuType;

    @Schema(description = "菜单地址", example = "/xx/xx")
    private String menuUrl;

    @Schema(description = "菜单描述", example = "xxxx")
    private String description;

    @Schema(description = "路由规则", example = "/xx/xx")
    private String urlMatchRule;

    @Schema(description = "权限编码", example = "x")
    private String permissionCode;

    @Schema(description = "/应用id", example = "1")
    private String appId;

    @Schema(description = "父级菜单id", example = "1")
    private Long parentId;


    @Schema(description = "id", example = "后端路由")
    private String mapperUrl;

    @Schema(description = "parentName", example = "父级名称")
    private String parentName;

    @Schema(description = "子节点")
    private List<ChildrenMenuListVo> newchildrenList;
}


二、先看一下我这边调接口返回的数据:
List<MenuListVo>数据格式类似如下:
{
	"data": [{
			"id": "10034030",
			"menuIcon": "",
			"menuName": "菜单一",
			"menuOrder": 1,
			"menuType": 1,
			"menuUrl": "/applyManage",
			"description": null,
			"urlMatchRule": null,
			"permissionCode": "/applyManage1",
			"appId": null,
			"parentId": null,
			"mapperUrl": "/applyManage1",
			"parentName": null
		},

		{
			"id": "10034032",
			"menuIcon": "",
			"menuName": "菜单一",
			"menuOrder": 1,
			"menuType": 1,
			"menuUrl": "/applyManage",
			"description": null,
			"urlMatchRule": null,
			"permissionCode": "/applyManage3",
			"appId": null,
			"parentId": "10034031",
			"mapperUrl": "/applyManage3",
			"parentName": "菜单一一",
			"newchildrenList": null
		}
	}],
	"success": true,
	"code": 200,
	"message": null
}
三、业务处理:

按照规范,代码的行数一般不要超过几十行,我这里把迭代部分提取了出来
analyChild(ChildrenMenuListVo bean, List<MenuListVo> childrenList );
ChildrenMenuListVo :用来存放子节点的数据;
List<MenuListVo>:调接口拿到的数据(也就是SQL查出来的结果);


   /**
     * 获取菜单列表查询
     * @param menuListQuery
     * @return
     */
    @LogPrint
    public Result<List<ChildrenMenuListVo>> queryMenuList(MenuListQuery menuListQuery) throws Exception {

        List<MenuListVo> listVoList = MenuServiceUtil.queryMenuList(menuListQuery).getData();
		/**
		 *	这里新new了一个list是因为调的接口返回的数据(也就是上面的 二)和我业务对不上,
		 *	因为要在子集菜单中塞一个父级的名称,第三方后端研发又不处理,所以只能自己来喽
		**/
        List<ChildrenMenuListVo> listVoListnew = new ArrayList<ChildrenMenuListVo>();

        // 数据封住转换
        for (MenuListVo menuListVo : listVoList) {
            // 当前节点便利组装
            ChildrenMenuListVo childrenMenuListVo1 = new ChildrenMenuListVo();
            BeanConvertUtil.copyProperties(menuListVo,childrenMenuListVo1);
            childrenMenuListVo1.setMapperUrl(menuListVo.getPermissionCode());
			// 获取子节点数据信息
            List<MenuListVo> childrenList = menuListVo.getChildrenList();
          	// 子节点不为null
			if(!Objects.equals(childrenList,null)){
				analyChild(childrenMenuListVo1,childrenList);
            }

            listVoListnew.add(childrenMenuListVo1);
        }

        return Result.success(listVoListnew);
    }

	/** 
	 *	把上边获取到的对象(ChildrenMenuListVo )和调接口返回的list(List<MenuListVo>)接收到,
	 *  然后进行遍历迭代
	**/ 
    private  void analyChild(ChildrenMenuListVo bean, List<MenuListVo> childrenList  ){
        
        // 这个new,需要看一下上面的vo类的最下面
        List<ChildrenMenuListVo> newChildrenMenuListVo = new ArrayList<ChildrenMenuListVo>();
        for (MenuListVo childe :  childrenList){
            ChildrenMenuListVo childrenMenuListVochile = new ChildrenMenuListVo();
            BeanConvertUtil.copyProperties(childe,childrenMenuListVochile);
            childrenMenuListVochile.setMapperUrl(childe.getPermissionCode());
            // 子节点临时塞了一个父级的名称
            childrenMenuListVochile.setParentName(bean.getMenuName());
            newChildrenMenuListVo.add(childrenMenuListVochile);

            if(childe.getChildrenList()!=null && !childe.getChildrenList().isEmpty()){
                ChildrenMenuListVo childrenMenuListVo1 = new ChildrenMenuListVo();
                BeanConvertUtil.copyProperties(childrenMenuListVochile.getNewchildrenList(),childrenMenuListVo1);
                childrenMenuListVo1.setMapperUrl(childrenMenuListVo1.getMenuUrl());
                childrenMenuListVo1.setParentName(childrenMenuListVo1.getMenuName());
                analyChild(childrenMenuListVochile,childe.getChildrenList());
            }

        }
        bean.setNewchildrenList(newChildrenMenuListVo);
    }


提取出来的部分有注释是:(子节点临时塞了一个父级的名称)这个是业务需求,就是返回的子集菜单也要包含父级的名称(就是二级菜单要有一级菜单的名称,三级菜单要有二级菜单的名称,以此类推),无需要的同学可以把 setParentName(bean.getMenuName()); 这个给注释掉;

四、处理后的结果:

返回的是个有三级的菜单列表
“menuName”: “菜单一”,是一级菜单名称;
“menuName”: “菜单一一”,是二级菜单名称;
“menuName”: “菜单一一一”,是第三级菜单名称;

{
	"data": [{
		"id": "10034030",
		"menuIcon": "",
		"menuName": "菜单一",
		"menuOrder": 1,
		"menuType": 1,
		"menuUrl": "/applyManage",
		"description": null,
		"urlMatchRule": null,
		"permissionCode": "/applyManage1",
		"appId": null,
		"parentId": null,
		"mapperUrl": "/applyManage1",
		"parentName": null,
		"newchildrenList": [{
			"id": "10034031",
			"menuIcon": "",
			"menuName": "菜单一一",
			"menuOrder": 2,
			"menuType": 1,
			"menuUrl": "/applyManage",
			"description": null,
			"urlMatchRule": null,
			"permissionCode": "/applyManage2",
			"appId": null,
			"parentId": "10034030",
			"mapperUrl": "/applyManage2",
			"parentName": "菜单一",
			"newchildrenList": [{
				"id": "10034034",
				"menuIcon": "",
				"menuName": "菜单一一一",
				"menuOrder": 1,
				"menuType": 1,
				"menuUrl": "http://1**.1*.4*.8*:8***/projectddd",
				"description": null,
				"urlMatchRule": null,
				"permissionCode": "http://1**.1*.4*.8*:8***/p顶顶顶",
				"appId": null,
				"parentId": "10034031",
				"mapperUrl": "http://1**.1*.4*.8*:8***/p顶顶顶",
				"parentName": "菜单一一",
				"newchildrenList": null
			}]
		}]
	}],
	"success": true,
	"code": 200,
	"message": null
}
到这里就结束了,希望有所帮助!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落榜书生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值