Java递归生成省市区数据结构

Java递归生成省市区数据结构

在这里插入图片描述

    @PostMapping("/test")
    public ResultBean<List<SysDistrictOutDTO>> queryDistrictInfo() {
        return ResultBean.returnResponse(true, ResultCode.SERVICE_OK,commonService.test());
    }
    @Override
    public List<SysDistrictOutDTO> test() {
        //获取所有省市区数据
        List<SysDistrictOutDTO> sysDistrictOutDTOList = sdpCommonMapper.queryDistrictInfo();
        List<SysDistrictOutDTO> sysDistrictOutDTOTree = sysDistrictOutDTOList.stream().filter(sysDistrictOutDTO ->{
            //选出省级数据 2:省级 3:市级 4:区县级
            return sysDistrictOutDTO.getFtype().equals("2");
        }).map(sysDistrictOutDTO ->{
            sysDistrictOutDTO.setChildren(getDistrictChildrenInfo(sysDistrictOutDTO,sysDistrictOutDTOList));
            return sysDistrictOutDTO;
        }).sorted(Comparator.comparing(SysDistrictOutDTO::getFid)).collect(Collectors.toList());

        return sysDistrictOutDTOTree;
    }
        /**
     * 递归查找省市区子节点数据
     * @param root
     * @param all
     * @return
     */
    private List<SysDistrictOutDTO> getDistrictChildrenInfo(SysDistrictOutDTO root, List<SysDistrictOutDTO> all) {
        List<SysDistrictOutDTO> children = all.stream().filter(sysDistrictOutDTO -> sysDistrictOutDTO.getFparentid().equals(root.getFid()))
                .map(sysDistrictOutDTO  -> {
            //递归查找子节点
            sysDistrictOutDTO.setChildren(getDistrictChildrenInfo(sysDistrictOutDTO,all));
            return sysDistrictOutDTO;
        }).sorted(Comparator.comparing(SysDistrictOutDTO::getFid)).collect(Collectors.toList());

        return children;
    }
    <select id="queryDistrictInfo" resultType="com.ts.common.dto.SysDistrictOutDTO">
        select fid,fparentid,fname as value,fname as label,ftype from test where ftype in ('2','3','4')  and fisstop = 0 order by fcode asc
    </select>
    
	@Data
	@AllArgsConstructor
	@NoArgsConstructor
	public class SysDistrictOutDTO{
	
	    /**
	     * 前端展示参数
	     */
	    private String label;
	
	    /**
	     * 前端展示参数
	     */
	    private String value;
	
	    /**
	     * id
	     */
	    private String fid;
	
	    /**
	     * 父id
	     */
	    private String fparentid;
	
	    /**
	     * 类型
	     */
	    private String ftype;
	
	    //孩子节点数据
	    private List<SysDistrictOutDTO> children;
	}
/*==============================================================*/
/* Table: test                                          */
/*==============================================================*/
create table test(
   fid                  VARCHAR(255)               not null,
   fparentid            VARCHAR(255)               null,
   fcode                VARCHAR(255)               null,
   fname                VARCHAR(255)               null,
   ffullname            VARCHAR(255)               null,
   ftype                VARCHAR(255)               null,
   fisstop              INT2                       null,
   constraint PK_test primary key (fid)
);

comment on table test is
'省市区';

comment on column test.fid is
'主键id';

comment on column test.fparentid is
'父id';

comment on column test.fcode is
'代码';

comment on column test.fname is
'名称';

comment on column test.ffullname is
'全称';

comment on column test.ftype is
'类型';

comment on column test.fisstop is
'是否停用,0否 1是';

<FormItem label="不动产地址" prop="ssqs">
	<Cascader :disabled="typeCode == '2'" v-model="supplementFormConfigB.ssqs" placeholder="请选择不动产地址" change-on-select :data="CascaderFormatData" filterable transfer />
</FormItem>

formatDatahandle(row){
  queryDistrictInfoList().then((res) => {
    this.CascaderFormatData = res
    console.log(res)
  })
  
},
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值