/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.kneet.v3.common.db;

import com.ntels.kneet.v3.common.db.entity.TB_Code;
import com.ntels.kneet.v3.common.db.entity.TB_CodeList;
import com.ntels.kneet.v3.common.db.entity.TB_LoginInfo;
import com.ntels.kneet.v3.common.db.entity.TB_TimeZone;
import com.ntels.kneet.v3.common.util.Dlog;
import com.ntels.kneet.v3.common.util.Util;
import com.ntels.kneet.v3.view.dialog.CustomDialog;

public class SQLManager {

    private Util util = new Util();
    private CustomDialog customDialog = new CustomDialog();

    public String QryCode() {

        String sSql = "";
        sSql += "\n   SELECT * ";
        sSql += "\n     FROM " + TB_Code.TABLE_NAME;
        sSql += "\n ORDER BY list_id, id, ord";
        sSql += ";";
        Dlog.v("=========== QryCode ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryCode ===========");
        return sSql;
    }

    public String QryCode(Object oList_Id) {
        String sSql = "";
        sSql += "\n   SELECT b.*";
        sSql += "\n        , a.name as LIST_NAME ";
        sSql += "\n     FROM " + TB_CodeList.TABLE_NAME + " a ";
        sSql += "\n        , " + TB_Code.TABLE_NAME + " b ";
        sSql += "\n    WEHRE a.id = '" + util.getInsetValues(oList_Id) + "' ";
        sSql += "\n      AND a.id = b.list_id ";
        sSql += "\n ORDER BY list_id, id, ord";
        sSql += ";";
        Dlog.v("=========== QryCode ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryCode ===========");
        return sSql;
    }

    public String QryCode(Object oList_Id, Object oId) {
        String sSql = "";
        sSql += "\n   SELECT * ";
        sSql += "\n     FROM " + TB_Code.TABLE_NAME;
        sSql += "\n    WHERE list_id = '" + util.getInsetValues(oList_Id) + "'";
        sSql += "\n      and id = '" + util.getInsetValues(oId) + "'";
        sSql += "\n ORDER BY list_id, id, ord";
        sSql += ";";
        Dlog.v("=========== QryCode ===========");
        //		Dlog.v(sSql);
        Dlog.v("=========== QryCode ===========");
        return sSql;
    }

    public String QryCode_MaxDate() {
        String sSql = "";
        sSql += "\n   SELECT Max(max_code_date) as max_date ";
        sSql += "\n     FROM (";
        sSql += "\n             SELECT Max(update_date) as max_code_date ";
        sSql += "\n               FROM " + TB_Code.TABLE_NAME;
        sSql += "\n           Union all ";
        sSql += "\n             SELECT Max(update_date) as max_code_date ";
        sSql += "\n               FROM " + TB_CodeList.TABLE_NAME;
        sSql += "\n          )";
        sSql += ";";
        Dlog.v("=========== QryCode_MaxDate ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryCode_MaxDate ===========");
        return sSql;
    }

    public String QryCodeList() {
        String sSql = "";
        sSql += "\n   SELECT * ";
        sSql += "\n     FROM " + TB_CodeList.TABLE_NAME;
        sSql += "\n ORDER BY id";
        sSql += ";";
        Dlog.v("=========== QryCodeList ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryCodeList ===========");
        return sSql;
    }

    public String QryCodeList(Object oId) {
        String sSql = "";
        sSql += "\n   SELECT * ";
        sSql += "\n     FROM " + TB_CodeList.TABLE_NAME;
        sSql += "\n    WHERE id = '" + util.getInsetValues(oId) + "'";
        sSql += "\n ORDER BY id";
        sSql += ";";
        Dlog.v("=========== QryCodeList ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryCodeList ===========");
        return sSql;
    }

    public String QryTimeZone() {
        String sSql = "";
        sSql += "\n   SELECT * ";
        sSql += "\n     FROM " + TB_TimeZone.TABLE_NAME;
        sSql += "\n ORDER BY id";
        sSql += ";";
        Dlog.v("=========== QryTimeZone ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryTimeZone ===========");
        return sSql;
    }

    public String QryLoginInfo() {
        String sSql = "";
        sSql += "\n   SELECT * ";
        sSql += "\n     FROM " + TB_LoginInfo.TABLE_NAME;
        sSql += ";";
        Dlog.v("=========== QryLoginInfo ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryLoginInfo ===========");
        return sSql;
    }

    //푸쉬 여부 업데이트
    public String QryUpdateLoginInfo_Push(Boolean bPush) {
        String sSql = "";
        String sPush = "N";
        if ( bPush ) {
            sPush = "Y";
        }
        sSql = "";
        sSql += "\n UPDATE " + TB_LoginInfo.TABLE_NAME;
        sSql += "\n    SET " + TB_LoginInfo.PUSH_YN + " = '" + sPush + "'";
        sSql += ";";
        Dlog.v("=========== QryUpdateLoginInfo_Push ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryUpdateLoginInfo_Push ===========");
        return sSql;
    }

    //로그인 정보 자동로그인 업데이트
    public String QryUpdateLoginInfo_AutoLogin(Boolean bAutoLogin) {
        String sSql = "";
        String sAutoLogin = "N";
        if ( bAutoLogin ) {
            sAutoLogin = "Y";
        }
        sSql = "";
        sSql += "\n UPDATE " + TB_LoginInfo.TABLE_NAME;
        sSql += "\n    SET " + TB_LoginInfo.AUTO_LOGIN_YN + " = '" + sAutoLogin + "'";
        sSql += ";";
        Dlog.v("=========== QryUpdateLoginInfo_AutoLogin ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryUpdateLoginInfo_AutoLogin ===========");
        return sSql;
    }

    //로그인 정보 아이디저장 업데이트
    public String QryUpdateLoginInfo_SaveId(Boolean bSaveId) {
        String sSql = "";
        String sSaveId = "N";
        if ( bSaveId ) {
            sSaveId = "Y";
        }
        sSql = "";
        sSql += "\n UPDATE " + TB_LoginInfo.TABLE_NAME;
        sSql += "\n    SET " + TB_LoginInfo.SAVE_ID_YN + " = '" + sSaveId + "'";
        sSql += ";";
        Dlog.v("=========== QryUpdateLoginInfo_SaveId ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryUpdateLoginInfo_SaveId ===========");
        return sSql;
    }

    //로그인 정보 아이디 업데이트
    public String QryUpdateLoginInfo_Login_Id(String sLogin_Id) {
        String sSql = "";
        sSql = "";
        sSql += "\n UPDATE " + TB_LoginInfo.TABLE_NAME;
        sSql += "\n    SET " + TB_LoginInfo.LOGIN_ID + " = '" + util.getInsetValues(sLogin_Id) + "'";
        sSql += ";";
        Dlog.v("=========== QryUpdateLoginInfo_Login_Id ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryUpdateLoginInfo_Login_Id ===========");
        return sSql;
    }

    //로그인 정보 아이디 업데이트
    public String QryUpdateLoginInfo_Login_Pass(String sPass) {
        String sSql = "";
        sSql = "";
        sSql += "\n UPDATE " + TB_LoginInfo.TABLE_NAME;
        sSql += "\n    SET " + TB_LoginInfo.LOGIN_PASS + " = '" + util.getInsetValues(sPass) + "'";
        sSql += ";";
        Dlog.v("=========== QryUpdateLoginInfo_Login_Pass ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryUpdateLoginInfo_Login_Pass ===========");
        return sSql;
    }

    public String QryUpdateLoginInfo_ManualView(Boolean bView) {
        String sSql = "";
        String sView = "N";
        if ( bView ) {
            sView = "Y";
        }
        sSql = "";
        sSql += "\n UPDATE " + TB_LoginInfo.TABLE_NAME;
        sSql += "\n    SET " + TB_LoginInfo.MANUAL_NOT_VIEW_YN + " = '" + sView + "'";
        sSql += ";";
        Dlog.v("=========== QryUpdateLoginInfo_ManualView ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryUpdateLoginInfo_ManualView ===========");
        return sSql;
    }

    //	sSql += "\n , " + TB_LoginInfo.LOGIN_ID + " TEXT ";
    //	sSql += "\n , " + TB_LoginInfo.LOGIN_PASS+ " TEXT ";
    //	sSql += "\n , " + TB_LoginInfo.SAVE_ID_YN+ " TEXT ";
    //	sSql += "\n , " + TB_LoginInfo.AUTO_LOGIN_YN+ " TEXT ";
    public String QryInsertLoginInfo(String sLogin_Id, String sPass, Boolean bSaveId, Boolean bAutoLogin, Boolean bView, Boolean bPush) {
        String sSql = "";
        String sSaveId = "N";
        if ( bSaveId ) {
            sSaveId = "Y";
        }
        String sAutoLogin = "N";
        if ( bAutoLogin ) {
            sAutoLogin = "Y";
        }
        String sView = "N";
        if ( bView ) {
            sView = "Y";
        }
        String sPush = "N";
        if ( bPush ) {
            sPush = "Y";
        }
        sSql = "";
        sSql += "\n Insert Into " + TB_LoginInfo.TABLE_NAME;
        sSql += "\n ( " + TB_LoginInfo.LOGIN_ID;
        sSql += "\n , " + TB_LoginInfo.LOGIN_PASS;
        sSql += "\n , " + TB_LoginInfo.SAVE_ID_YN;
        sSql += "\n , " + TB_LoginInfo.AUTO_LOGIN_YN;
        sSql += "\n , " + TB_LoginInfo.MANUAL_NOT_VIEW_YN;
        sSql += "\n , " + TB_LoginInfo.PUSH_YN;
        sSql += "\n ) values  ";
        sSql += "\n ( '" + util.getInsetValues(sLogin_Id) + "'";
        sSql += "\n , '" + util.getInsetValues(sPass) + "'";
        sSql += "\n , '" + util.getInsetValues(sSaveId) + "'";
        sSql += "\n , '" + util.getInsetValues(sAutoLogin) + "'";
        sSql += "\n , '" + util.getInsetValues(sView) + "'";
        sSql += "\n , '" + util.getInsetValues(sPush) + "'";
        sSql += "\n ) ";
        sSql += ";";
        Dlog.v("=========== QryInsertLoginInfo ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryInsertLoginInfo ===========");
        return sSql;
    }

    public String QryDelLoginInfo() {
        String sSql = "";
        sSql = "";
        sSql += "\n Delete From " + TB_LoginInfo.TABLE_NAME;
        sSql += ";";
        Dlog.v("=========== QryInsertLoginInfo ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryInsertLoginInfo ===========");
        return sSql;
    }

    public String QryUpdateCode(Object oList_ID, Object oID, Object oName, Object oOrd, Object oUse_Yn, Object oUpdate_Date) {
        String sSql = "";
        sSql = "";
        sSql += "\n UPDATE " + TB_Code.TABLE_NAME;
        sSql += "\n    SET " + TB_Code.NAME + " = '" + util.getInsetValues(oName) + "'";
        sSql += "\n      , " + TB_Code.ORD + " = '" + util.getInsetValues(oOrd) + "'";
        sSql += "\n      , " + TB_Code.USE_YN + " = '" + util.getInsetValues(oUse_Yn) + "'";
        sSql += "\n      , " + TB_Code.UPDATE_DATE + " = '" + util.getInsetValues(oUpdate_Date) + "'";
        sSql += "\n  WHERE " + TB_Code.LIST_ID + " = '" + util.getInsetValues(oList_ID) + "'";
        sSql += "\n    AND " + TB_Code.ID + " = '" + util.getInsetValues(oID) + "'";
        sSql += ";";
        Dlog.v("=========== QryUpdateCode ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryUpdateCode ===========");
        return sSql;
    }

    public String QryUpdateCodeList(Object oID, Object oName, Object oUse_Yn, Object oUpdate_Date) {
        String sSql = "";
        sSql = "";
        sSql += "\n UPDATE " + TB_CodeList.TABLE_NAME;
        sSql += "\n    SET " + TB_CodeList.NAME + " = '" + util.getInsetValues(oName) + "'";
        sSql += "\n      , " + TB_CodeList.USE_YN + " = '" + util.getInsetValues(oUse_Yn) + "'";
        sSql += "\n      , " + TB_CodeList.UPDATE_DATE + " = '" + util.getInsetValues(oUpdate_Date) + "'";
        sSql += "\n  WHERE " + TB_CodeList.ID + " = '" + util.getInsetValues(oID) + "'";
        sSql += ";";
        Dlog.v("=========== QryUpdateCodeList ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryUpdateCodeList ===========");
        return sSql;
    }

    public String QryInsertCodeList(Object oID, Object oName, Object oUse_Yn, Object oUpdate_Date) {
        String sSql = "";
        sSql = "";
        sSql += "\n Insert Into " + TB_CodeList.TABLE_NAME;
        sSql += "\n ( " + TB_CodeList.ID;
        sSql += "\n , " + TB_CodeList.NAME;
        sSql += "\n , " + TB_CodeList.USE_YN;
        sSql += "\n , " + TB_CodeList.UPDATE_DATE;
        sSql += "\n ) values  ";
        sSql += "\n ( '" + util.getInsetValues(oID) + "'";
        sSql += "\n , '" + util.getInsetValues(oName) + "'";
        sSql += "\n , '" + util.getInsetValues(oUse_Yn) + "'";
        sSql += "\n , '" + util.getInsetValues(oUpdate_Date) + "'";
        sSql += "\n ) ";
        sSql += ";";
        Dlog.v("=========== QryInsertCodeList ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryInsertCodeList ===========");
        return sSql;
    }

    public String QryInsertCode(Object oList_ID, Object oID, Object oName, Object oOrd, Object oUse_Yn, Object oUpdate_Date) {
        String sSql = "";
        sSql = "";
        sSql += "\n Insert Into " + TB_Code.TABLE_NAME;
        sSql += "\n ( " + TB_Code.LIST_ID;
        sSql += "\n , " + TB_Code.ID;
        sSql += "\n , " + TB_Code.NAME;
        sSql += "\n , " + TB_Code.ORD;
        sSql += "\n , " + TB_Code.USE_YN;
        sSql += "\n , " + TB_Code.UPDATE_DATE;
        sSql += "\n ) values  ";
        sSql += "\n ( '" + util.getInsetValues(oList_ID) + "'";
        sSql += "\n , '" + util.getInsetValues(oID) + "'";
        sSql += "\n , '" + util.getInsetValues(oName) + "'";
        sSql += "\n , '" + util.getInsetValues(oOrd) + "'";
        sSql += "\n , '" + util.getInsetValues(oUse_Yn) + "'";
        sSql += "\n , '" + util.getInsetValues(oUpdate_Date) + "'";
        sSql += "\n ) ";
        sSql += ";";
        Dlog.v("=========== QryInsertCode ===========");
        Dlog.v(sSql);
        Dlog.v("=========== QryInsertCode ===========");
        return sSql;
    }
}
