본문 바로가기

개발중/Java

카테고리 반환 코드 기록

728x90
반응형

카테고리 반환 코드를 만들었는데 나로썬 최선이었으나(;´д`)ゞ

 

jw 선배 코드는 ,,, 대박 (>人<;)

package com.rsn.POMS.api.code.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rsn.POMS.api.code.dao.CodeDao;
import com.rsn.POMS.api.code.vo.CodeVo;

/**
 * 
 * @author soobin
 * 
 * sys_code  == 0 ? ALL : ( 시스템 코드 조건 )
 * max_depth == 0 ? ALL : ( 하위 카테고리 깊이 단계 지정 )
 * 
 */

@Service
public class CodeService  implements Cloneable {

	@Autowired
	private CodeDao codeDao;
	private static List<CodeVo> commonCodeList;
	
	/**
	 *  Category List 생성
	 */
	@PostConstruct
	public void init() {

		List<CodeVo> commonCodeList =  codeDao.codeListCreate();

		for ( CodeVo vo : commonCodeList ) {
			vo.setChildList(new ArrayList<>());
		}

		for ( CodeVo children : commonCodeList ) {
			for ( CodeVo parent : commonCodeList ) {
				if ( children.getCode_p_seq() == parent.getCode_seq() ) {
					parent.getChildList().add(children);
					break;
				}
			}
		}
		this.commonCodeList = commonCodeList;
	}

	
	
	/**
	 * Code 를 모두 반환 
	 * @throws CloneNotSupportedException 
	 */
	public List<CodeVo> codeListGet( int max_depth , int sys_code )  {

		List<CodeVo> codeList = createCodeList( sys_code );

		codeList = codeList.stream()
				           .filter(x -> x.getCode_p_seq() == 0)
				           .filter(x -> x.getCode_depth() == 0)
				           .filter(x -> x.getUi_type().equals("tree")).collect(Collectors.toList());
		
		if( max_depth != 0 ) { removeDepth( codeList , max_depth );}
		
		// Test Print
		System.out.println(" max_depth : " + max_depth + "/ sys_code : " + sys_code);
		print( codeList );
		return codeList;
	}
	
	
	
	/**
	 * code_group -> 검색
	 */
	public List<CodeVo> codeListGetGroupCode( String code_group , int max_depth, int sys_code  ) {
		
		List<CodeVo> codeList = createCodeList( sys_code );
		
		Optional<String> nullCheck = Optional.ofNullable("");
		
		codeList = codeList.stream()
		                   .filter(x -> x.getCode_group().equals(code_group))
		                   .filter(x -> x.getCode_depth() == 1)
				           .filter(x -> nullCheck.orElse( x.getCode_type() ).equals(""))
				           .collect(Collectors.toList());
		
		if( max_depth != 0 ) { removeDepth( codeList , max_depth );}
		
		// Test Print
		System.out.println("code_group : " + code_group + "/  max_depth : " + max_depth + "/ sys_code : " + sys_code);
		print( codeList );
		return codeList;
	}

	
	
	/**
	 * code_type  -> 검색 
	 */
	public List<CodeVo> codeListGetTypeCode(String code_group, String code_type, int max_depth, int sys_code  ) {
		
		List<CodeVo> codeList = createCodeList( sys_code );
		
		codeList = codeList .stream()
				            .filter(x -> x.getCode_group().equals(code_group))
				            .filter(x -> Optional.ofNullable(x.getCode_type()).orElseGet(() -> "").equals(code_type))
				            .filter(x -> x.getCode_depth() == 1)
				            .collect(Collectors.toList());
		
		if( max_depth != 0 ) { removeDepth( codeList , max_depth );}
		
		// Test Print
		System.out.println("code_group : " + code_group +" / code_type : " + code_type + "/  max_depth : " + max_depth + "/ sys_code : " + sys_code);
	    print( codeList );
		return codeList;

	}
	

	
	/**
	 * code_p_seq -> 검색 
	 */
	public List<CodeVo> codeListGetSeq(int code_seq , int max_depth, int sys_code  ) {
		
		List<CodeVo> codeList = createCodeList( sys_code );
		
		codeList = codeList.stream()
				           .filter( x -> x.getCode_p_seq() == code_seq )
				           .collect(Collectors.toList());
		
		if( max_depth != 0 ) { removeDepth( codeList , max_depth );}
		
		// Test Print
		System.out.println("code_p_seq : " + code_seq + "/  max_depth : " + max_depth + "/ sys_code : " + sys_code + "/ sys_code : " + sys_code);
		print( codeList );
		return codeList;
	}
	 
	
	/**
	 * 사용자가 원하는 depth 까지의 Data 만 반환
	 */
	public  List<CodeVo> removeDepth( List<CodeVo> list , int depth  ) {
		
		if( depth == 1 ) {
			for(CodeVo vo : list) {
				List<CodeVo> childList = vo.getChildList();
				
				for( Iterator<CodeVo> it = childList.iterator() ; it.hasNext() ; ) {
				    it.next();
				    it.remove();
				}
			}
			return list;
			
		}else{
			depth = depth-1;
			
			for(CodeVo vo : list) {
				 removeDepth( vo.getChildList() , depth );
			}
		}
		return list;
	}
	
	
	
	/**
	 *  new List Create.
	 */
	public List<CodeVo> createCodeList( int sys_code  ) {
		 
		List<CodeVo> newList = new ArrayList<>();
		 
		commonCodeList.forEach( x -> {
			try {
				if( sys_code == 0 ) {
					newList.add((CodeVo) x.clone());
					
				}else if( sys_code != 0 && x.getSys_code() == sys_code ) {
					newList.add((CodeVo) x.clone());
				}
			} catch (CloneNotSupportedException e) {
				e.printStackTrace();
			}
		});

		for ( CodeVo vo : newList ) {
			vo.setChildList(new ArrayList<>());
		}

		for ( CodeVo child : newList ) {
			for ( CodeVo parent : newList ) {
				if ( child.getCode_p_seq() == parent.getCode_seq() ) {
					parent.getChildList().add(child);
					break;
				}
			}
		}
		return newList;
	}
	
	
	/**
	 * TEST Print
	 */
	public void print ( List<CodeVo> codeList ) {

		for( CodeVo v1 : codeList ){
			List<CodeVo> child1 = v1.getChildList();
			
			System.out.printf("⇨ " + v1.getCode_seq() + " / ( p : "+ v1.getCode_p_seq() +") : %10s" + " / sys : " + v1.getSys_code()+ "\n" , v1.getCode_name());
			
			for( CodeVo v2 : child1 ){
				List<CodeVo> child2 = v2.getChildList();
				
				System.out.printf( "     ⇨ " + v2.getCode_seq() + " / ( p : "+ v2.getCode_p_seq() +") : %10s  / sys : " + v2.getSys_code()+ "\n" , v1.getCode_name());
				
				for( CodeVo v3 : child2 ){
					List<CodeVo> child3 = v3.getChildList();
					
					System.out.printf( "          ⇨ " + v3.getCode_seq() + " / ( p : "+ v3.getCode_p_seq() +") : %10s / sys : " + v3.getSys_code() + "\n", v1.getCode_name());
					for( CodeVo v4 : child3 ){
						System.out.printf( "               ⇨ " + v4.getCode_seq() + " / ( p : "+ v4.getCode_p_seq() +") : %10s  / sys : " + v4.getSys_code() + "\n", v1.getCode_name());
					}
				}
			}
		}
	}
}
728x90
반응형

'개발중 > Java' 카테고리의 다른 글

java 에서 문자열이 date 형식인지 확인 후 20200101 형식으로 변환  (0) 2021.05.03
카테고리 완성 기록  (0) 2021.04.21
Field 활용  (0) 2021.04.19
Map 람다식  (0) 2021.04.16
카테고리 부모 자식 매핑  (0) 2021.04.13