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

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

/**
 * 사용자 관리 
 * 
 *
 */
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, USE_ATTR_ORDER) " + 
				" VALUES(nextval('SEQ_MP_USER_US'), :userId, :userName, :passWordValue, now(), " +
				" 0, :localCipherKeyValue, now(), 0)"; 
	}
	@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";
	}
	
	@Override
	public String getQueryForObject() {
		return QUERY_getQueryForObject; 
	}
	private static final String QUERY_getQueryForObject = 
			"SELECT " + 
			"  USER_ID " +
			", USER_NAME " +
			", USE_ATTR_ORDER " +
			"FROM MP_USER " +
			"WHERE USER_ID = :userId ";		
	/**
	 * 사용자의 디바이스 토큰 지우기 
	 * @return
	 */
	public String deleteUserDeviceToken() { 
		return "DELETE FROM MNS_RECEIVER_DEVICE WHERE RECEIVER_ID=:userId"; 
	}
	
	/**
	 * 사용자 ID | 사용자 이름 | 등록일
	 * @return
	 */
	public String selectUserList() { 
		return 
		"  SELECT " + 
		"    U.USER_ID, " + 
		"    U.USER_NAME, " + 
		"    U.REGISTER_DT AS REGISTER_DT, " + 
		"    U.USE_ATTR_ORDER " + 
		"  FROM MP_USER U  " +
		"  ORDER BY REGISTER_DT ASC  ";
	}
	
	/**
	 * selectUserList 질의 데이터 갯수 
	 * @return
	 */
	public String selectUserListCount() { 
		return "SELECT " + 
		"  COUNT(1) " + 
		"  FROM MP_USER U ";		
	}	
	
	/**
	 * 사용자검색
	 * @return
	 */
	public String selectOneUser() { 
		return 
		"  SELECT " + 
		"    U.USER_NAME, " + 
		"    U.REGISTER_DT AS REGISTER_DT, " + 
		"    U.USE_ATTR_ORDER " + 
		"  FROM MP_USER U  " + 
		" WHERE USER_ID = :searchUserId "; 
	}
	
	/**
	 * selectOneUser 질의 결과 갯수 구하기 
	 * @return
	 */
	public String selectOneUserCount() { 
		return "SELECT " + 
		"  COUNT(1) " + 
		"  FROM MP_USER U  " + 
		" WHERE USER_ID = :searchUserId "; 
	}	
}
