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

import DataEntryBean.TransactioDetailsBean;
import LoginDb.DbHandler;
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 oracle.jdbc.OracleTypes;
import org.codehaus.jackson.map.ObjectMapper;

/**
 *
 * @author 1004242
 */
public class WelcomeDao {

    public String getQueDetails(String tellerId, String pacsId) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        ResultSet rs = null;
        ResultSet rs2 = null;
        String txnType;
        int count = 0;
        int pQ=0;
        int rQ=0;
        int aQ=0;
        long cin=0;
        long cout=0;
        long cdiff=0;
        long cinH = 0;
        int iQ=0;
        int iQ2 = 0;

        res= "{ \"pQue\": \" "+ pQ+"\" , \"aQue\": \" "+ aQ+"\",\"rQue\": \" "+ rQ+"\",\"cIn\": \" "+ cin+"\",\"cOut\": \" "+ cout+"\",\"cDf\": \" "+ cdiff+"\",\"iQ\": \" "+ iQ+"\",\"cinH\": \" "+ cinH+"\",\"iQ2\": \" "+ iQ2+"\" }";

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            rs = qry.executeQuery(" SELECT A.*,A.TOTAL_CASH_IN-A.TOTAL_CASH_OUT AS CASH_DIFFERENCE FROM  						"
+" (SELECT                                                                                   "
+" (select SUM((C.DENO_2000_IN*2000)+(C.DENO_500_IN*500)+(C.DENO_200_IN*200)+(C.DENO_100_IN*100)+                "
+" (C.DENO_50_IN*50)+(C.DENO_20_IN*20) +(C.DENO_10_IN*10) +(C.DENO_5_IN*5)+(C.DENO_2_IN*2)+                      "
+" (C.DENO_1_IN*1)+(C.DENO_50p_IN*0.5)+(C.DENO_1p_IN*0.01))  from cash_txn_hist c                                                    "
+" where c.txn_date=(select system_date from system_date)                                    "
+" and c.pacs_id="+pacsId                                                                   
+" and c.status='A'                                                                          "
+" GROUP BY "+pacsId+") AS TOTAL_CASH_IN,                                                      "
+"                                                                                           "
+" (SELECT                                                                                   "
+" SUM((C.DENO_2000_OUT*2000)+(C.DENO_500_OUT*500)+(C.DENO_200_OUT*200)+(C.DENO_100_OUT*100)+   "
+" (C.DENO_50_OUT*50)+(C.DENO_20_OUT*20)+(C.DENO_10_OUT*10)+(C.DENO_5_OUT*5)+(C.DENO_2_OUT*2)+  "
+" (C.DENO_1_OUT*1)+(C.DENO_50p_OUT*0.5)+(C.DENO_1p_OUT*0.01))  from cash_txn_hist c          "
+" where c.txn_date=(select system_date from system_date)                                    "
+" and c.pacs_id="+pacsId                                                                    
+" and c.status='A'                                                                          "
+" GROUP BY "+pacsId+") AS TOTAL_CASH_OUT,                                                     "
+" (select g.cum_curr_val from gl_account g where g.pacs_id='"+pacsId+"' and g.gl_prod_id='201611000001184') as CASH_IN_HAND ,"
+" (SELECT                                                                                   "
+" (SELECT COUNT(*) FROM DEP_ACCOUNT_AUTHORIZATION DAA WHERE DAA.Pacs_Id="+pacsId            
+" AND DAA.TXN_STATUS='A' AND DAA.ACCT_OPEN_DT=(select system_date from system_date))+       "
+" (SELECT COUNT(*) FROM LOAN_ACCOUNT_AUTH DAA2 WHERE DAA2.pacs_id="+pacsId                  
+" AND DAA2.STATUS='A' AND DAA2.APPL_DT=(select system_date from system_date))+              "
+" (SELECT COUNT(*) FROM DEP_TXN_AUTHORIZATION D WHERE D.POST_DATE=                          "
+" (select system_date from system_date)                                                     "
+" and d.pacs_id="+pacsId+" and d.txn_status='A')+                                             "
+" (SELECT COUNT(*) FROM LOAN_TXN_AUTHORIZATION D WHERE D.POST_DATE=                         "
+" (select system_date from system_date)                                                     "
+" and d.pacs_id="+pacsId+" and d.txn_status='A') FROM DUAL) AS TOTAL_ACCEPTED_QUEUE,          "
+"                                                                                           "
+" (SELECT                                                                                   "
+" (SELECT COUNT(*) FROM DEP_ACCOUNT_AUTHORIZATION DAA WHERE DAA.Pacs_Id="+pacsId            
+" AND DAA.TXN_STATUS='R' AND DAA.ACCT_OPEN_DT=(select system_date from system_date))+       "
+" (SELECT COUNT(*) FROM LOAN_ACCOUNT_AUTH DAA2 WHERE DAA2.pacs_id="+pacsId                  
+" AND DAA2.STATUS='R' AND DAA2.APPL_DT=(select system_date from system_date))+              "
+" (SELECT COUNT(*) FROM DEP_TXN_AUTHORIZATION D WHERE D.POST_DATE=                          "
+" (select system_date from system_date)                                                     "
+" and d.pacs_id="+pacsId+" and d.txn_status='R')+                                             "
+" (SELECT COUNT(*) FROM LOAN_TXN_AUTHORIZATION D WHERE D.POST_DATE=                         "
+" (select system_date from system_date)                                                     "
+" and d.pacs_id="+pacsId+" and d.txn_status='R') FROM DUAL) AS TOTAL_REJECTED_QUEUE,          "
+"                                                                                           "
+" (SELECT                                                                                   "
+" (SELECT COUNT(*) FROM DEP_ACCOUNT_AUTHORIZATION DAA WHERE DAA.Pacs_Id="+pacsId            
+" AND DAA.TXN_STATUS='P' )+                                                                 "
+" (SELECT COUNT(*) FROM LOAN_ACCOUNT_AUTH DAA2 WHERE DAA2.pacs_id="+pacsId                  
+" AND DAA2.STATUS='P' )+                                                                    "
+" (SELECT COUNT(*) FROM DEP_TXN_AUTHORIZATION D WHERE D.POST_DATE=                          "
+" (select system_date from system_date)                                                     "
+" and d.pacs_id="+pacsId+" and d.txn_status='P')+                                             "
+" (SELECT COUNT(*) FROM LOAN_TXN_AUTHORIZATION D WHERE D.POST_DATE=                         "
+" (select system_date from system_date)                                                     "
+" and d.pacs_id="+pacsId+" and d.txn_status='P') FROM DUAL) AS TOTAL_PENDING_QUEUE            "
+" FROM DUAL)A                                                                               ");

            while (rs.next()) {
                pQ =rs.getInt("TOTAL_PENDING_QUEUE");
                rQ =rs.getInt("TOTAL_REJECTED_QUEUE");
                aQ =rs.getInt("TOTAL_ACCEPTED_QUEUE");
                cin =rs.getLong("TOTAL_CASH_IN");
                cout =rs.getLong("TOTAL_CASH_OUT");
                cdiff = rs.getLong("CASH_DIFFERENCE");
                cinH = rs.getLong("CASH_IN_HAND");
            }

          rs=null;
          qry = null;
          qry = con.createStatement();
          rs = qry.executeQuery(" select count(*) as count from investment_dtl l where l.pacs_id="+pacsId+" and l.maturity_date>=(select system_date from system_date) and l.maturity_date <=(select (system_date+15) from system_date) and l.active_flag='Yes'");
         while (rs.next()) {

             iQ= rs.getInt("count");
         }

         rs2 = qry.executeQuery(" select count(*) as count from nsc_collateral l where l.acc_no in (select key_1 from loan_account la where la.pacs_id = " +pacsId+ ") and l.exp_dt >= (select system_date from system_date) and l.exp_dt <= (select (system_date + 15) from system_date) and l.status = 'A' ");

          while (rs2.next()) {

              iQ2= rs2.getInt("count");
          }

          res= "{ \"pQue\": \" "+ pQ+"\" , \"aQue\": \" "+ aQ+"\",\"rQue\": \" "+ rQ+"\",\"cIn\": \" "+ cin+"\",\"cOut\": \" "+ cout+"\",\"cDf\": \" "+ cdiff+"\",\"iQ\": \" "+ iQ+"\",\"cinH\": \" "+ cinH+"\",\"iQ2\": \" "+ iQ2+"\" }";

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in getQueDetails for user Id :" + tellerId);
           System.out.println("Error occurred during processing.");
            
        } finally {
            try {
                if (con != null) {
                   // con.commit();
                    con.close();
                }
            } catch (SQLException e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return res;
    }

//    public int getNSCKVPExp(String tellerId, String pacsId) {
//        Connection con = null;
//        CallableStatement proc = null;
//        Statement qry = null;
//        //String res = null;
//        ResultSet rs = null;
//        int iQ2 = 0;
//
//        try {
//            con = DbHandler.getDBConnection();
//            qry = con.createStatement();
//            rs = qry.executeQuery(" select count(*) as count from nsc_collateral l where l.acc_no in (select key_1 from loan_account la where la.pacs_id = " +pacsId+ ") and l.exp_dt >= (select system_date from system_date) and l.exp_dt <= (select (system_date + 15) from system_date) and l.status = 'A' ");
//
//            while (rs.next()) {
//             iQ2= rs.getInt("count");
//         }
//        }
//        catch(Exception e){
//            e.printStackTrace();
//            System.out.println("Exception occured  in getNSCKVPExp for user Id :" + tellerId);
//        }
//        finally {
//            try {
//                if (con != null) {
//                    con.commit();
//                    con.close();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        return iQ2;
//    }


      public String getAllTranDetails(String tellerId, String pacsId) {
        Connection con = null;
        CallableStatement proc = null;
        Statement qry = null;
        String res = null;
        ResultSet rs = null;
        List<TransactioDetailsBean> tdBeanList = new ArrayList<TransactioDetailsBean>();
        TransactioDetailsBean tdBean = null;
        ObjectMapper objmap = new ObjectMapper();
        String jsonString = null;

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            rs = qry.executeQuery(" SELECT authorized_queues, rejected_queues,            "
+ " pending_queues,                                                                       "
+ " TOTAL_CASH_IN,                                                                        "
+ " TOTAL_CASH_OUT,                                                                       "
+ " (TOTAL_CASH_IN - TOTAL_CASH_OUT) as CASH_difference,                                  "
+ " i.teller_id,                                                                          "
+ " i.pacs_id                                                                             "
+ " from (SELECT nvl(sum(a_1), 0) as authorized_queues, teller_id, pacs_id                "
+ " from (SELECT COUNT(*) AS a_1, daa.teller_id, daa.pacs_id                              "
+ " FROM DEP_ACCOUNT_AUTHORIZATION DAA                                                    "
+ " WHERE DAA.Pacs_Id ="+pacsId
+ " AND DAA.TXN_STATUS = 'A'                                                              "
+ " AND DAA.ACCT_OPEN_DT =                                                                "
+ " (select system_date from system_date)                                                 "
+ "        group by daa.pacs_id, daa.teller_id                                            "
+ "       union all                                                                       "
+ "       SELECT COUNT(*) AS a_2, daa2.teller_id, daa2.pacs_id                            "
+ "         FROM LOAN_ACCOUNT_AUTH DAA2                                                   "
+ "        WHERE DAA2.pacs_id ="+pacsId
+ "          AND DAA2.STATUS = 'A'                                                        "
+ "          AND DAA2.APPL_DT = (select system_date from system_date)                     "
+ "        group by daa2.pacs_id, daa2.teller_id                                          "
+ "       union all                                                                       "
+ "       SELECT COUNT(*) AS a_3, d.maker_id teller_id, d.pacs_id                         "
+ "         FROM DEP_TXN_AUTHORIZATION D                                                  "
+ "        WHERE D.POST_DATE = (select system_date from system_date)                      "
+ "          and d.pacs_id ="+pacsId
+ "          and d.txn_status = 'A'                                                       "
+ "        group by d.maker_id, d.pacs_id                                                 "
+ "       union all                                                                       "
+ "       SELECT COUNT(*) AS a_4, d.maker_id teller_id, d.pacs_id                         "
+ "         FROM LOAN_TXN_AUTHORIZATION D                                                 "
+ "        WHERE D.POST_DATE = (select system_date from system_date)                      "
+ "          and d.pacs_id ="+pacsId
+ "          and d.txn_status = 'A'                                                       "
+ "        group by d.maker_id, d.pacs_id)                                                "
+ "          group by teller_id, pacs_id) j,                                              "
+ "                                                                                       "
+ "        (SELECT nvl(sum(r_1), 0) as rejected_queues, teller_id, pacs_id                "
+ "           from (SELECT COUNT(*) AS r_1, daa.teller_id, daa.pacs_id                    "
+ "                   FROM DEP_ACCOUNT_AUTHORIZATION DAA                                  "
+ "                  WHERE DAA.Pacs_Id ="+pacsId
+ "                    AND DAA.TXN_STATUS = 'R'                                           "
+ "                    AND DAA.ACCT_OPEN_DT =                                             "
+ "                        (select system_date from system_date)                          "
+ "                  group by daa.pacs_id, daa.teller_id                                  "
+ "                 union all                                                             "
+ "                 SELECT COUNT(*) AS r_2, daa2.teller_id, daa2.pacs_id                  "
+ "                   FROM LOAN_ACCOUNT_AUTH DAA2                                         "
+ "                  WHERE DAA2.pacs_id ="+pacsId
+ "                    AND DAA2.STATUS = 'R'                                              "
+ "                    AND DAA2.APPL_DT = (select system_date from system_date)           "
+ "                  group by daa2.pacs_id, daa2.teller_id                                "
+ "                 union all                                                             "
+ "                 SELECT COUNT(*) AS r_3, d.maker_id teller_id, d.pacs_id               "
+ "                   FROM DEP_TXN_AUTHORIZATION D                                        "
+ "                  WHERE D.POST_DATE = (select system_date from system_date)            "
+ "                    and d.pacs_id ="+pacsId
+ "                    and d.txn_status = 'R'                                             "
+ "                  group by d.maker_id, d.pacs_id                                       "
+ "                 union all                                                             "
+ "                 SELECT COUNT(*) AS r_4, d.maker_id teller_id, d.pacs_id               "
+ "                   FROM LOAN_TXN_AUTHORIZATION D                                       "
+ "                  WHERE D.POST_DATE = (select system_date from system_date)            "
+ "                    and d.pacs_id ="+pacsId
+ "                    and d.txn_status = 'R'                                             "
+ "                  group by d.maker_id, d.pacs_id)                                      "
+ "          group by teller_id, pacs_id) h,                                              "
+ "                                                                                       "
+ "        (SELECT nvl(sum(p_1), 0) as pending_queues, teller_id, pacs_id                 "
+ "           from (SELECT COUNT(*) AS p_1, daa.teller_id, daa.pacs_id                    "
+ "                   FROM DEP_ACCOUNT_AUTHORIZATION DAA                                  "
+ "                  WHERE DAA.Pacs_Id ="+pacsId
+ "                    AND DAA.TXN_STATUS = 'P'                                           "
+ "                  group by daa.pacs_id, daa.teller_id                                  "
+ "                 union all                                                             "
+ "                 SELECT COUNT(*) AS p_2, daa2.teller_id, daa2.pacs_id                  "
+ "                   FROM LOAN_ACCOUNT_AUTH DAA2                                         "
+ "                  WHERE DAA2.pacs_id ="+pacsId
+ "                    AND DAA2.STATUS = 'P'                                              "
+ "                  group by daa2.pacs_id, daa2.teller_id                                "
+ "                 union all                                                             "
+"                      SELECT COUNT(*) AS p_3, d.maker_id teller_id, d.pacs_id		  "
+"                   FROM DEP_TXN_AUTHORIZATION D                                   "
+"                  WHERE D.POST_DATE = (select system_date from system_date)       "
+"                    and d.pacs_id ="+pacsId
+"                    and d.txn_status = 'P'                                        "
+"                  group by d.maker_id, d.pacs_id                                  "
+"                 union all                                                        "
+"                 SELECT COUNT(*) AS p_4, d.maker_id teller_id, d.pacs_id          "
+"                   FROM LOAN_TXN_AUTHORIZATION D                                  "
+"                  WHERE D.POST_DATE = (select system_date from system_date)       "
+"                    and d.pacs_id ="+pacsId
+"                    and d.txn_status = 'P'                                        "
+"                  group by d.maker_id, d.pacs_id)                                 "
+"          group by teller_id, pacs_id) i,                                         "
+"        (select nvl(SUM((C.DENO_2000_IN * 2000) + (C.DENO_500_IN * 500) +         "
+"                        (C.DENO_200_IN * 200) + (C.DENO_100_IN * 100) +           "
+"                        (C.DENO_50_IN * 50) + (C.DENO_20_IN * 20) +               "
+"                        (C.DENO_10_IN * 10) + (C.DENO_5_IN * 5) +                 "
+"                        (C.DENO_2_IN * 2) + (C.DENO_1_IN * 1) +               "
+"                        (c.deno_50p_in * 0.5) + (c.deno_1p_in * 0.01)),           "
+"                    0) AS TOTAL_CASH_IN,                                          "
+"                c.teller_id,                                                      "
+"                c.pacs_id                                                         "
+"           from cash_txn_hist c                                                   "
+"          where c.txn_date = (select system_date from system_date)                "
+"            and c.pacs_id ="+pacsId
+"            and c.status = 'A'                                                    "
+"          GROUP BY c.teller_id, c.pacs_id) b,                                     "
+"                                                                                  "
+"        (SELECT nvl(SUM((C.DENO_2000_OUT * 2000) + (C.DENO_500_OUT * 500) +       "
+"                        (C.DENO_200_OUT * 200) +(C.DENO_100_OUT * 100) +            "
+"                        (C.DENO_50_OUT * 50) + (C.DENO_20_OUT * 20) +           "
+"                        (C.DENO_10_out * 10) +  (C.DENO_5_OUT * 5) +                  "
+"                        (C.DENO_2_OUT * 2) + (C.DENO_1_OUT * 1) +              "
+"                        (c.deno_50p_out * 0.5) + (c.deno_1p_out * 0.01)),                                  "
+"                    0) AS TOTAL_CASH_OUT,                                         "
+"                c.teller_id,                                                      "
+"                c.pacs_id                                                         "
+"           from cash_txn_hist c                                                   "
+"          where c.txn_date = (select system_date from system_date)                "
+"            and c.pacs_id ="+pacsId
+"            and c.status = 'A'                                                    "
+"          GROUP BY c.teller_id, c.pacs_id) c                                      "
+"  where i.pacs_id = j.pacs_id(+)                                                 "
+"     and i.pacs_id = h.pacs_id(+)                                                  "
+"    and i.teller_id = j.teller_id(+)                                                   "
+"    and i.teller_id = h.teller_id(+)                                         "
+"   and i.pacs_id = b.pacs_id(+)                                               "
+"    and i.teller_id = b.teller_id(+)                                            "
+"    and i.pacs_id = c.pacs_id(+)                                              "
+"     and i.teller_id = c.teller_id(+)                                           " );

 








         while (rs.next()) {

                tdBean = new TransactioDetailsBean();
                tdBean.setAuthorisedQ(rs.getString("authorized_queues")==null?"0":rs.getString("authorized_queues"));
                tdBean.setRejectedQ(rs.getString("rejected_queues")==null?"0":rs.getString("rejected_queues"));
                tdBean.setPendingQ(rs.getString("pending_queues")==null?"0":rs.getString("pending_queues"));
                tdBean.setCashIn(rs.getString("TOTAL_CASH_IN")==null?"0":rs.getString("TOTAL_CASH_IN"));
                tdBean.setCashOut(rs.getString("TOTAL_CASH_OUT")==null?"0":rs.getString("TOTAL_CASH_OUT"));
                tdBean.setCashDiff(rs.getString("CASH_difference")==null?"0":rs.getString("CASH_difference"));
                tdBean.setTellerId(rs.getString("teller_id"));

                tdBeanList.add(tdBean);
            }
           jsonString = objmap.writeValueAsString(tdBeanList);
        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in getQueDetails for user Id :" + tellerId);
           System.out.println("Error occurred during processing.");

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

        return jsonString;
    }
}

