/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import DataEntryBean.AccountAmendBean;
import LoginDb.DbHandler;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import oracle.jdbc.OracleTypes;
import org.codehaus.jackson.map.ObjectMapper;

/**
 *
 * @author 981898
 */
public class AccAmendDao {

    public String searchAccounts(String accNo, String pacsId) {
        Connection con = null;
        Statement qry = null;
        ResultSet rs = null;
        List<AccountAmendBean> tdBeanList = new ArrayList<AccountAmendBean>();
        AccountAmendBean tdBean = null;
        ObjectMapper objmap = new ObjectMapper();
        String jsonString = null;

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            rs = qry.executeQuery(" select 'DEP' accType, da.key_1, "
                    + "        dp.prod_name, "
                    + "        da.customer_no, "
                    + "        (k.first_name || ' ' || k.middle_name || ' ' || k.last_name) cust_name, "
                    + "        to_char(da.acct_open_dt, 'DD-MON-YYYY') accOpenDt, "
                    + "        da.avail_bal "
                    + "   from dep_account da, dep_product dp, kyc_hdr k "
                    + "  where da.dep_prod_id = dp.id "
                    + "    and da.customer_no = k.cif_no "
//                    + "    and da.curr_status = 'O' "
                    + "    and da.pacs_id = '" + pacsId + "'"
                    + "    and da.key_1 = '" + accNo + "' "
                    + " union all "
                    + " select 'LON' accType, la.key_1, "
                    + "        lp.prod_name, "
                    + "        la.customer_no, "
                    + "        (k.first_name || ' ' || k.middle_name || ' ' || k.last_name) cust_name, "
                    + "        to_char(la.acct_open_dt, 'DD-MON-YYYY') accOpenDt, "
                    + "        la.outst_bal "
                    + "   from loan_account la, loan_product lp, kyc_hdr k "
                    + "  where la.loan_prod_id = lp.id "
                    + "    and la.customer_no = k.cif_no "
//                    + "    and la.curr_status in ('02', '03') "
                    + "    and la.pacs_id = '" + pacsId + "'"
                    + "    and la.key_1 = '" + accNo + "' "
                    + " order by 1 ");

            while (rs.next()) {

                tdBean = new AccountAmendBean();
                tdBean.setAccType(rs.getString(1) == null ? "NA" : rs.getString(1));
                tdBean.setAccNo(rs.getString(2) == null ? "NA" : rs.getString(2));
                tdBean.setProdName(rs.getString(3) == null ? "NA" : rs.getString(3));
                tdBean.setCif1(rs.getString(4) == null ? "NA" : rs.getString(4));
                tdBean.setCustName(rs.getString(5) == null ? "NA" : rs.getString(5));
                tdBean.setAccOpenDt(rs.getString(6) == null ? "NA" : rs.getString(6));
                tdBean.setAvailBal(rs.getString(7) == null ? "NA" : rs.getString(7));
                tdBeanList.add(tdBean);
            }
            jsonString = objmap.writeValueAsString(tdBeanList);
        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in searchAccounts for pacs Id :" + pacsId);
           System.out.println("Error occurred during processing.");

        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return jsonString;
    }

    public String getAccDtl(String accNo, String pacsId, String accType) {
        Connection con = null;
        Statement qry = null;
        ResultSet rs = null;
        String s;
        List<AccountAmendBean> tdBeanList = new ArrayList<AccountAmendBean>();
        AccountAmendBean tdBean = null;
        ObjectMapper objmap = new ObjectMapper();
        String jsonString = null;
        if (!accType.equalsIgnoreCase("DEP")) {
            try {
                con = DbHandler.getDBConnection();
                qry = con.createStatement();
                s = "select la.key_1, "
                        + "       lp.prod_name,la.customer_no, "
                        + "       (k.first_name || ' ' || k.middle_name || ' ' || k.last_name) name1, "
                        + "       to_date(la.acct_open_dt, 'DD-MON-YYYY') accOpenDt, "
                        + "       la.outst_bal "
                        + "  from loan_account la, loan_product lp, kyc_hdr k "
                        + " where la.loan_prod_id = lp.id "
                        + "   and la.customer_no = k.cif_no "
                        + "   and la.curr_status in ('02','03') "
                        + "   and la.pacs_id = '" + pacsId + "' "
                        + "   and la.key_1 = '" + accNo + "' ";
                System.out.println(s);
                rs = qry.executeQuery(s);

                while (rs.next()) {

                    tdBean = new AccountAmendBean();
                    tdBean.setAccNo(rs.getString(1) == null ? "NA" : rs.getString(1));
                    tdBean.setProdName(rs.getString(2) == null ? "NA" : rs.getString(2));
                    tdBean.setCif1(rs.getString(3) == null ? "NA" : rs.getString(3));
                    tdBean.setCustName(rs.getString(4) == null ? "NA" : rs.getString(4));
                    tdBeanList.add(tdBean);
                }
                jsonString = objmap.writeValueAsString(tdBeanList);
            } catch (Exception e) {
               // e.printStackTrace();
               // System.out.println("Exception occured  in searchAccounts for pacs Id :" + pacsId);
               System.out.println("Error occurred during processing.");

            } finally {
                try {
                    if (con != null) {
                       // con.commit();
                        con.close();
                    }
                } catch (Exception e) {
                   // e.printStackTrace();
                   System.out.println("Error occurred during connection close.");
                }
            }
        } else {

            try {
                con = DbHandler.getDBConnection();
                qry = con.createStatement();
                s = "select da.key_1,"
                        + "       dp.prod_name,"
                        + "       da.customer_no cif1,"
                        + "       (k.first_name || ' ' || k.middle_name || ' ' || k.last_name) name1,"
                        + "       nvl(da.cif_no2, 'NA'),"
                        + "       (case"
                        + "         when da.cif_no2 is not null then"
                        + "          (select nvl(h.first_name, '') || ' ' || nvl(h.middle_name, '') || ' ' ||"
                        + "                  nvl(h.last_name, '')"
                        + "             from kyc_hdr h"
                        + "            where h.cif_no = da.cif_no2)"
                        + "         else"
                        + "          'NA'"
                        + "       end) name2,"
                        + "       nvl(da.cif_no3, 'NA'),"
                        + "       (case"
                        + "         when da.cif_no3 is not null then"
                        + "          (select nvl(h.first_name, '') || ' ' || nvl(h.middle_name, '') || ' ' ||"
                        + "                  nvl(h.last_name, '')"
                        + "             from kyc_hdr h"
                        + "            where h.cif_no = da.cif_no3)"
                        + "         else"
                        + "          'NA'"
                        + "       end) name3,"
                        + "       nvl(da.cif_no4, 'NA'),"
                        + "       (case"
                        + "         when da.cif_no4 is not null then"
                        + "          (select nvl(h.first_name, '') || ' ' || nvl(h.middle_name, '') || ' ' ||"
                        + "                  nvl(h.last_name, '')"
                        + "             from kyc_hdr h"
                        + "            where h.cif_no = da.cif_no4)"
                        + "         else"
                        + "          'NA'"
                        + "       end) name4,"
                        + "       nvl(da.cif_no5, 'NA'),"
                        + "       (case"
                        + "         when da.cif_no5 is not null then"
                        + "          (select nvl(h.first_name, '') || ' ' || nvl(h.middle_name, '') || ' ' ||"
                        + "                  nvl(h.last_name, '')"
                        + "             from kyc_hdr h"
                        + "            where h.cif_no = da.cif_no5)"
                        + "         else"
                        + "          'NA'"
                        + "       end) name5,"
                        + "       nvl(da.cif_no6, 'NA'),"
                        + "       (case"
                        + "         when da.cif_no6 is not null then"
                        + "          (select nvl(h.first_name, '') || ' ' || nvl(h.middle_name, '') || ' ' ||"
                        + "                  nvl(h.last_name, '')"
                        + "             from kyc_hdr h"
                        + "            where h.cif_no = da.cif_no6)"
                        + "         else"
                        + "          'NA'"
                        + "       end) name6,"
                        + "       nvl(da.cif_no7, 'NA'),"
                        + "       (case"
                        + "         when da.cif_no7 is not null then"
                        + "          (select nvl(h.first_name, '') || ' ' || nvl(h.middle_name, '') || ' ' ||"
                        + "                  nvl(h.last_name, '')"
                        + "             from kyc_hdr h"
                        + "            where h.cif_no = da.cif_no7)"
                        + "         else"
                        + "          'NA'"
                        + "       end) name7,"
                        + "       nvl(da.cif_no8, 'NA'),"
                        + "       (case"
                        + "         when da.cif_no8 is not null then"
                        + "          (select nvl(h.first_name, '') || ' ' || nvl(h.middle_name, '') || ' ' ||"
                        + "                  nvl(h.last_name, '')"
                        + "             from kyc_hdr h"
                        + "            where h.cif_no = da.cif_no8)"
                        + "         else"
                        + "          'NA'"
                        + "       end) name8,"
                        + "       nvl(da.cif_no9, 'NA'),"
                        + "       (case"
                        + "         when da.cif_no9 is not null then"
                        + "          (select nvl(h.first_name, '') || ' ' || nvl(h.middle_name, '') || ' ' ||"
                        + "                  nvl(h.last_name, '')"
                        + "             from kyc_hdr h"
                        + "            where h.cif_no = da.cif_no9)"
                        + "         else"
                        + "          'NA'"
                        + "       end) name9,"
                        + "       nvl(da.cif_no10, 'NA'),"
                        + "       (case"
                        + "         when da.cif_no10 is not null then"
                        + "          (select nvl(h.first_name, '') || ' ' || nvl(h.middle_name, '') || ' ' ||"
                        + "                  nvl(h.last_name, '')"
                        + "             from kyc_hdr h"
                        + "            where h.cif_no = da.cif_no10)"
                        + "         else"
                        + "          'NA'"
                        + "       end) name10,"
                        + "       nvl(da.cif_no11, 'NA'),"
                        + "       (case"
                        + "         when da.cif_no11 is not null then"
                        + "          (select nvl(h.first_name, '') || ' ' || nvl(h.middle_name, '') || ' ' ||"
                        + "                  nvl(h.last_name, '')"
                        + "             from kyc_hdr h"
                        + "            where h.cif_no = da.cif_no11)"
                        + "         else"
                        + "          'NA'"
                        + "       end) name11,"
                        + "       to_char(da.acct_open_dt, 'DD-MON-YYYY') accOpenDt,"
                        + "       da.avail_bal"
                        + "  from dep_account da, dep_product dp, kyc_hdr k"
                        + " where da.dep_prod_id = dp.id"
                        + "   and da.customer_no = k.cif_no"
                       // + "   and da.curr_status = 'O'"
                        + "   and da.pacs_id = '" + pacsId + "'"
                        + "   and da.key_1 = '" + accNo + "'";
                System.out.println(s);
                rs = qry.executeQuery(s);

                while (rs.next()) {

                    tdBean = new AccountAmendBean();
                    tdBean.setAccNo(rs.getString(1) == null ? "NA" : rs.getString(1));
                    tdBean.setProdName(rs.getString(2) == null ? "NA" : rs.getString(2));
                    tdBean.setCif1(rs.getString(3) == null ? "NA" : rs.getString(3));
                    tdBean.setCustName(rs.getString(4) == null ? "NA" : rs.getString(4));
                    tdBean.setCif2(rs.getString(5));
                    tdBean.setName2(rs.getString(6));
                    tdBean.setCif3(rs.getString(7));
                    tdBean.setName3(rs.getString(8));
                    tdBean.setCif4(rs.getString(9));
                    tdBean.setName4(rs.getString(10));
                    tdBean.setCif5(rs.getString(11));
                    tdBean.setName5(rs.getString(12));
                    tdBean.setCif6(rs.getString(13));
                    tdBean.setName6(rs.getString(14));
                    tdBean.setCif7(rs.getString(15));
                    tdBean.setName7(rs.getString(16));
                    tdBean.setCif8(rs.getString(17));
                    tdBean.setName8(rs.getString(18));
                    tdBean.setCif9(rs.getString(19));
                    tdBean.setName9(rs.getString(20));
                    tdBean.setCif10(rs.getString(21));
                    tdBean.setName10(rs.getString(22));
                    tdBean.setCif11(rs.getString(23));
                    tdBean.setName11(rs.getString(24));
                    tdBeanList.add(tdBean);
                }
                jsonString = objmap.writeValueAsString(tdBeanList);
            } catch (Exception e) {
               // e.printStackTrace();
               // System.out.println("Exception occured  in searchAccounts for pacs Id :" + pacsId);
               System.out.println("Error occurred during processing.");

            } finally {
                try {
                    if (con != null) {
                       // con.commit();
                        con.close();
                    }
                } catch (Exception e) {
                   // e.printStackTrace();
                   System.out.println("Error occurred during connection close.");
                }
            }
        }

        return jsonString;
    }

    public String getNewCif(String newCif, String pacsId) {
        Connection con = null;
        Statement qry = null;
        ResultSet rs = null;
        List<AccountAmendBean> tdBeanList = new ArrayList<AccountAmendBean>();
        AccountAmendBean tdBean = null;
        ObjectMapper objmap = new ObjectMapper();
        String jsonString = null;
        String s = "";

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            s = "select t.cif_no,"
                    + "                (t.first_name ||"
                    + "                nvl2(t.middle_name, ' ' || t.middle_name, '') ||"
                    + "                nvl2(t.last_name, ' ' || t.last_name, '')) name,"
                    + "                t.guardian_name"
                    + "  from kyc_hdr t"
                    + " where t.cif_no = '" + newCif + "' "
                    + " and t.pacs_id in (select pacs_id from pacs_master pm where pm.head_pacs_id in "
                    + " (select head_pacs_id from pacs_master where pacs_id = '" + pacsId + "')) order by 2";
            System.out.println(s);
            rs = qry.executeQuery(s);

            while (rs.next()) {

                tdBean = new AccountAmendBean();
                tdBean.setCif1(rs.getString(1) == null ? "NA" : rs.getString(1));
                tdBean.setCustName(rs.getString(2) == null ? "NA" : rs.getString(2));
                tdBean.setGuardianName(rs.getString(3) == null ? "NA" : rs.getString(3));
                tdBeanList.add(tdBean);
            }
            jsonString = objmap.writeValueAsString(tdBeanList);
        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in getNewCif for pacs Id :" + pacsId);
           System.out.println("Error occurred during processing.");

        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return jsonString;
    }

    public String updateCif(String user, String pacsId, String accNo, AccountAmendBean oAccountAmendBean, String remarks) {
        Connection con = null;
        CallableStatement proc = null;
        String retnMsg = null;
        String s = "";

        try {
            con = DbHandler.getDBConnection();
            try {
                proc = con.prepareCall("{ call operations1.ACCT_CIF_UPDATE(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");

            } catch (SQLException ex) {
               // Logger.getLogger(AccAmendDao.class.getName()).log(Level.SEVERE, null, ex);
               System.out.println("Error occurred during processing.");
            }

            proc.setString(1, user);
            proc.setString(2, pacsId);
            proc.setString(3, accNo);
            proc.setString(4, oAccountAmendBean.getCif1());
            proc.setString(5, oAccountAmendBean.getCif2());
            proc.setString(6, oAccountAmendBean.getCif3());
            proc.setString(7, oAccountAmendBean.getCif4());
            proc.setString(8, oAccountAmendBean.getCif5());
            proc.setString(9, oAccountAmendBean.getCif6());
            proc.setString(10, oAccountAmendBean.getCif7());
            proc.setString(11, oAccountAmendBean.getCif8());
            proc.setString(12, oAccountAmendBean.getCif9());
            proc.setString(13, oAccountAmendBean.getCif10());
            proc.setString(14, oAccountAmendBean.getCif11());
            proc.setString(15, remarks);
            proc.registerOutParameter(16, java.sql.Types.VARCHAR);

            proc.execute();
           // retnMsg = proc.getString(16);

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in getNewCif for pacs Id :" + pacsId);
           System.out.println("Error occurred during processing.");

        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return retnMsg;
    }
}
