package com.dbs.mplus.service.mobile.user.repository.oracle;

import com.dbs.mplus.framework.template.MPlusQuery;

/**
 * 사용자 관리 
 * @author hyeeun
 *
 */
public class MPlusUserMPlusQuery extends MPlusQuery {
	@Override
	public String getQueryForList(){
		return "SELECT USER_SEQ, USER_ID, LOGIN_FAIL_CNT, REGISTER_DT FROM MP_USER";
	}
	@Override 
	public String getQueryForInsert() {
		return "INSERT INTO MP_USER (USER_SEQ, USER_ID, USER_NAME, PASSWORD_VALUE, PASSWORD_UPDATE_DT, " + 
				" LOGIN_FAIL_CNT, LOCAL_CIPHER_KEY_VALUE, REGISTER_DT) " + 
				" VALUES(SEQ_MP_USER_US.NEXTVAL, :userId, :userName, :passWordValue, SYSDATE, " +
				" 0, :localCipherKeyValue, SYSDATE)"; 
	}
	@Override
	public String getQueryForUpdate() {	
		return "UPDATE MP_USER SET LOGIN_FAIL_CNT = 0 " +
				"WHERE USER_SEQ=:userSeq"; 
	}
	@Override
	public String getQueryForDelete() {	
		return "DELETE FROM MP_USER WHERE USER_ID=:userId"; 
	}
	
	@Override
	public String getQueryForCount() {
		return "SELECT COUNT(1) FROM MP_USER";
	}
	
	/**
	 * 사용자의 디바이스 토큰 지우기 
	 * @return
	 */
	public String deleteUserDeviceToken() { 
		return "DELETE FROM MNS_RECEIVER_DEVICE WHERE RECEIVER_ID=:userId"; 
	}
	
	/**
	 * 환자번호 | 로그인실패횟수 | 최근 접속일 | 비접속기간 | 등록일 | 가입기간 
	 * @return
	 */
	public String selectUserAccessDay() { 
		return "SELECT " + 
		"  A.USER_ID, A.LOGIN_FAIL_CNT, NVL(A.ACCESS_DT,A.REGISTER_DT) AS ACCESS_DT, A.LAST_ACCESS_DAY, A.REGISTER_DT, A.REGISTER_DAY " + 
		" FROM ( " + 
		"  SELECT " + 
		"    U.USER_ID, " + 
		"    U.LOGIN_FAIL_CNT, " + 
		"    NVL(HJ.ACCESS_DT,U.REGISTER_DT) AS ACCESS_DT, " + 
		"    U.REGISTER_DT AS REGISTER_DT, " + 
		"    CAST(SYSDATE - NVL(HJ.ACCESS_DT, U.REGISTER_DT) AS INTEGER) AS LAST_ACCESS_DAY,  " + 
		"    CAST(SYSDATE - U.REGISTER_DT AS INTEGER) AS REGISTER_DAY " + 
		"  FROM MP_USER U  " + 
		"  LEFT JOIN ( " + 
		"    SELECT DISTINCT(H.USER_ID) AS USER_ID, MAX(H.ACCESS_DT) AS ACCESS_DT " + 
		"    FROM MP_ACCESS_HISTORY H  " + 
		"    GROUP BY H.USER_ID ) HJ " + 
		"  ON U.USER_ID=HJ.USER_ID) A "; 
	}
	
	/**
	 * selectUserAccessDay 질의 데이터 갯수 
	 * @return
	 */
	public String selectUserAccessDayCount() { 
		return "SELECT " + 
		"  COUNT(1) " + 
		" FROM ( " + 
		"  SELECT " + 
		"    U.USER_ID, " + 
		"    U.LOGIN_FAIL_CNT, " + 
		"    NVL(HJ.ACCESS_DT,U.REGISTER_DT) AS ACCESS_DT, " + 
		"    U.REGISTER_DT AS REGISTER_DT, " + 
		"    CAST(SYSDATE - NVL(HJ.ACCESS_DT, U.REGISTER_DT) AS INTEGER) AS LAST_ACCESS_DAY,  " + 
		"    CAST(SYSDATE - U.REGISTER_DT AS INTEGER) AS REGISTER_DAY " + 
		"  FROM MP_USER U  " + 
		"  LEFT JOIN ( " + 
		"    SELECT DISTINCT(H.USER_ID) AS USER_ID, MAX(H.ACCESS_DT) AS ACCESS_DT " + 
		"    FROM MP_ACCESS_HISTORY H  " + 
		"    GROUP BY H.USER_ID ) HJ " + 
		"  ON U.USER_ID=HJ.USER_ID) A "; 
		
	}	
	
	/**
	 * 환자번호 | 로그인실패횟수 | 최근 접속일 | 비접속기간 | 등록일 | 가입기간 
	 * @return
	 */
	public String selectOneUserAccessDay() { 
		return "SELECT " + 
		"  A.USER_ID, A.LOGIN_FAIL_CNT, NVL(A.ACCESS_DT,A.REGISTER_DT) AS ACCESS_DT, A.LAST_ACCESS_DAY, A.REGISTER_DT, A.REGISTER_DAY " + 
		" FROM ( " + 
		"  SELECT " + 
		"    U.USER_ID, " + 
		"    U.LOGIN_FAIL_CNT, " + 
		"    NVL(HJ.ACCESS_DT,U.REGISTER_DT) AS ACCESS_DT, " + 
		"    U.REGISTER_DT AS REGISTER_DT, " + 
		"    CAST(SYSDATE - NVL(HJ.ACCESS_DT, U.REGISTER_DT) AS INTEGER) AS LAST_ACCESS_DAY,  " + 
		"    CAST(SYSDATE - U.REGISTER_DT AS INTEGER) AS REGISTER_DAY " + 
		"  FROM MP_USER U  " + 
		"  LEFT JOIN ( " + 
		"    SELECT DISTINCT(H.USER_ID) AS USER_ID, MAX(H.ACCESS_DT) AS ACCESS_DT " + 
		"    FROM MP_ACCESS_HISTORY H  " + 
		"    GROUP BY H.USER_ID ) HJ " + 
		"  ON U.USER_ID=HJ.USER_ID) A "+
		" WHERE USER_ID = :searchUserId "; 
	}
	
	/**
	 * selectOneUserAccessDay 질의 결과 갯수 구하기 
	 * @return
	 */
	public String selectOneUserAccessDayCount() { 
		return "SELECT " + 
		"  COUNT(1) " + 
		" FROM ( " + 
		"  SELECT " + 
		"    U.USER_ID, " + 
		"    U.LOGIN_FAIL_CNT, " + 
		"    NVL(HJ.ACCESS_DT,U.REGISTER_DT) AS ACCESS_DT, " + 
		"    U.REGISTER_DT AS REGISTER_DT, " + 
		"    CAST(SYSDATE - NVL(HJ.ACCESS_DT, U.REGISTER_DT) AS INTEGER) AS LAST_ACCESS_DAY,  " + 
		"    CAST(SYSDATE - U.REGISTER_DT AS INTEGER) AS REGISTER_DAY " + 
		"  FROM MP_USER U  " + 
		"  LEFT JOIN ( " + 
		"    SELECT DISTINCT(H.USER_ID) AS USER_ID, MAX(H.ACCESS_DT) AS ACCESS_DT " + 
		"    FROM MP_ACCESS_HISTORY H  " + 
		"    GROUP BY H.USER_ID ) HJ " + 
		"  ON U.USER_ID=HJ.USER_ID) A "+
		" WHERE USER_ID = :searchUserId "; 
	}	
}
