/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package kr.co.kihyun.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jdo.PersistenceManager;
import kr.co.kihyun.service.vo.CodeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Kts
 */
public class DeptService extends BaseService {
    private static final Logger LOG = LoggerFactory.getLogger(DeptService.class);

    //쿼리구분자 정의
    public static enum TYPE {SAME_ROOT_DEPT, DEPT_OF_SAME_ORGAN, SCHOOL_OF_SAME_ORGAN;}

    //동일한 root dept를 가진 부서 검색쿼리
    private static final StringBuilder deptLov1 = new StringBuilder()
        .append("SELECT                  \n")
        .append("  ID AS value           \n")
        .append("  , NAME AS code        \n")
        .append("FROM                    \n")
        .append("  MOUMI_DEPT            \n")
        .append("WHERE                   \n")
        .append("  ROOT_DEPT = :deptId   \n")
        .append("  AND ID = ORGAN        \n")
        .append("  AND SEL_TYPE = 'Y'    \n")
        .append("  AND IS_OUT = 'N'      \n")
        .append("ORDER BY                \n")
        .append("  ID ASC                \n");
    //동일한 organ를 가진 부서 검색쿼리
    private static final StringBuilder deptLov2 = new StringBuilder()
        .append("SELECT               \n")
        .append("  ID AS value        \n")
        .append("  , NAME AS code     \n")
        .append("FROM                 \n")
        .append("  MOUMI_DEPT         \n")
        .append("WHERE                \n")
        .append("  ORGAN = :deptId    \n")
        .append("  AND GRADE IS NULL  \n")
        .append("  AND SEL_TYPE = 'Y' \n")
        .append("  AND IS_OUT = 'N'   \n")
        .append("  AND ID <> ORGAN    \n")
        .append("ORDER BY             \n")
        .append("  GRADE ASC          \n")
        .append("  , NAME ASC         \n");
    //동일한 organ를 가진 학교 검색쿼리
    private static final StringBuilder deptLov3 = new StringBuilder()
        .append("SELECT                  \n")
        .append("  ID AS value           \n")
        .append("  , NAME AS code        \n")
        .append("FROM                    \n")
        .append("  MOUMI_DEPT            \n")
        .append("WHERE                   \n")
        .append("  ORGAN = :deptId       \n")
        .append("  AND GRADE IS NOT NULL \n")
        .append("  AND SEL_TYPE = 'Y'    \n")
        .append("  AND IS_OUT = 'N'      \n")
        .append("  AND ID <> ORGAN       \n")
        .append("ORDER BY                \n")
        .append("  GRADE ASC             \n")
        .append("  , NAME ASC            \n");

    /**
     * 기본생성자
     */
    public DeptService() {
        /**
         * 기본생성자
         */
    }

    /**
     * 생성자
     * @param pm PersistenceManager
     */
    public DeptService(PersistenceManager pm) {
        super(pm);
    }

    /**
     * 쿼리구분자별로 부서ID를 만족하는 LOV를 반환하는 메소드
     * @param type - 쿼리구분자
     * @param id - 부서ID
     * @return - 부서의 LOV
     */
    public List getLOVFromDept(TYPE type, String id) {
        StringBuilder sbQuery = null;

        if( type == TYPE.SAME_ROOT_DEPT ) {                 //동일한 root dept를 가진 부서 검색쿼리
            sbQuery = deptLov1;
        }else if( type == TYPE.DEPT_OF_SAME_ORGAN ) {       //동일한 organ를 가진 부서 검색쿼리
            sbQuery = deptLov2;
        }else if( type == TYPE.SCHOOL_OF_SAME_ORGAN ) {     //동일한 organ를 가진 학교 검색쿼리
            sbQuery = deptLov3;
        }
        Map lvParams = new HashMap();
        lvParams.put("deptId", id);

        return (List)this.executeQuery(sbQuery, lvParams, CodeVO.class);
    }
}
