1. 程式人生 > >java 商品類目 遞迴

java 商品類目 遞迴

點選父類目查詢出來下面的所有類目的編號

private List<String>  findGoodsDirectoryNoListByParentNo(String directoryNo,List<String> dirNoList){
        if(directoryNo==null||directoryNo.equals("")){
            return  dirNoList;
        }else{
            if(!dirNoList.contains(directoryNo)){
                dirNoList.add(directoryNo);
            }
        }
       try{
           GoodsDirectoryDO gdo=goodsDirectoryService.getDirectoryNo(directoryNo);
           int maxRank=goodsDirectoryService.maxRank();
           if(gdo!=null&&gdo.getRank()!=null){
               if(maxRank==gdo.getRank()){
                   if(!dirNoList.contains(directoryNo)){
                       dirNoList.add(directoryNo);
                   }
               }else if(gdo.getRank()<maxRank){
                   if(goodsDirectoryService.getEntityByParentNo(directoryNo)!=null&&goodsDirectoryService.getEntityByParentNo(directoryNo).size()>0){
                       for(GoodsDirectoryDO d:goodsDirectoryService.getEntityByParentNo(directoryNo)){
                           if(!dirNoList.contains(d.getDirectoryNo())){
                               dirNoList.add(d.getDirectoryNo());
                           }
                           findGoodsDirectoryNoListByParentNo(d.getDirectoryNo(),dirNoList);
                       }
                   }else {
                       if(!dirNoList.contains(directoryNo)){
                           dirNoList.add(directoryNo);
                       }
                   }
               }
           }
       }catch (Exception e){
            e.printStackTrace();
       }finally {
           return  dirNoList;
       }

    }

mapper程式碼

<select id="getEntityByParentNo" resultType="com.zscat.erp.domain.GoodsDirectoryDO" parameterType="String">
        select `id`,`directory_no`,`directory_name`,`directory_parent_no`,`rank` from erp_goods_directory where directory_parent_no = #{value}
</select>


 <select id="maxRank" resultType="int">
        select MAX(rank) from erp_goods_directory
 </select>

資料庫建表語句

CREATE TABLE `erp_goods_directory` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `directory_no` varchar(225) DEFAULT NULL COMMENT '類目編號',
  `directory_name` varchar(225) DEFAULT NULL COMMENT '類目名稱',
  `directory_parent_no` varchar(225) DEFAULT NULL COMMENT '類目父級編碼',
  `directory_parent_name` varchar(225) DEFAULT NULL COMMENT '類目父級名稱',
  `rank` int(11) DEFAULT NULL COMMENT '級別',
  `wms_directory_id` bigint(20) DEFAULT NULL COMMENT 'wms商品類目id',
  PRIMARY KEY (`id`),
  KEY `index_name_directory_no` (`directory_no`)
) ENGINE=InnoDB AUTO_INCREMENT=169 DEFAULT CHARSET=utf8;