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

import LoginDb.DbHandler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import oracle.jdbc.OracleTypes;
import org.codehaus.jackson.*;
import org.codehaus.jackson.map.ObjectMapper;

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

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

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            rs = qry.executeQuery("select login_id  from login_details where  pacs_id='"+pacsId+"' and user_role_id <> '201606000004041'");

            while (rs.next()) {
               
                tellesList.add(rs.getString("login_id"));
               
            }

            if(tellesList != null && tellesList.size()!=0){
             jsonString = objmap.writeValueAsString(tellesList);
            }
        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in gettellerDetails for user Id :" + tellerId);
           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 gettellerLoginDetails(String tellerId, String pacsId) {
        Connection con = null;
            Statement qry = null;
        String res = null;
        ResultSet rs = null;
        List<String> tellesList = new ArrayList<String>();
        ObjectMapper objmap = new ObjectMapper();
        String jsonString = null;
        String defTime= " ";

        try {
            con = DbHandler.getDBConnection();
            qry = con.createStatement();
            //rs = qry.executeQuery("select * from login_tym  where  pacs_id='"+pacsId+"' and tellerid = '"+tellerId+"'");

            if (rs.next()) {
                            // "{ \"pQue\": \" "+ pQ+"\" , \"aQue\": \" "+ aQ+"\",\"rQue\": \" "+ rQ+"\",\"cIn\": \" "+ cin+"\",\"cOut\": \" "+ cout+"\",\"cDf\": \" "+ cdiff+"\" }";
                  jsonString="{ \"mS\": \""+(rs.getString("MONDAY_START_TYM")==null?defTime:rs.getString("MONDAY_START_TYM"))+"\","+
                             "\"mE\": \""+(rs.getString("MONDAY_END_TYM")==null?defTime:rs.getString("MONDAY_END_TYM"))+"\"," +
                             "\"mC\": \""+(rs.getString("MONDAY_FLAG")==null?"N":rs.getString("MONDAY_FLAG"))+"\"," +
                             "\"tS\": \""+(rs.getString("TUESDAY_START_TYM")==null?defTime:rs.getString("TUESDAY_START_TYM"))+"\"," +
                             "\"tE\": \""+(rs.getString("TUESDAY_END_TYM")==null?defTime:rs.getString("TUESDAY_END_TYM"))+"\"," +
                             "\"tC\": \""+(rs.getString("TUESDAY_FLAG")==null?"N":rs.getString("TUESDAY_FLAG"))+"\"," +
                             "\"wS\": \""+(rs.getString("WEDNESDAY_START_TYM")==null?defTime:rs.getString("WEDNESDAY_START_TYM"))+"\"," +
                             "\"wE\": \""+(rs.getString("WEDNESDAY_END_TYM")==null?defTime:rs.getString("WEDNESDAY_END_TYM"))+"\"," +
                             "\"wC\": \""+(rs.getString("WEDNESDAY_FLAG")==null?"N":rs.getString("WEDNESDAY_FLAG"))+"\"," +
                             "\"thS\": \""+(rs.getString("THURSDAY_START_TYM")==null?defTime:rs.getString("THURSDAY_START_TYM"))+"\"," +
                             "\"thE\": \""+(rs.getString("THURSDAY_END_TYM")==null?defTime:rs.getString("THURSDAY_END_TYM"))+"\"," +
                             "\"thC\": \""+(rs.getString("THURSDAY_FLAG")==null?"N":rs.getString("THURSDAY_FLAG"))+"\"," +
                             "\"fS\": \""+(rs.getString("FRIDAY_START_TYM")==null?defTime:rs.getString("FRIDAY_START_TYM"))+"\"," +
                             "\"fE\": \""+(rs.getString("FRIDAY_END_TYM")==null?defTime:rs.getString("FRIDAY_END_TYM"))+"\"," +
                             "\"fC\": \""+(rs.getString("FRIDAY_FLAG")==null?"N":rs.getString("FRIDAY_FLAG"))+"\"," +
                             "\"saS\": \""+(rs.getString("SATURDAY_START_TYM")==null?defTime:rs.getString("SATURDAY_START_TYM"))+"\"," +
                             "\"saE\": \""+(rs.getString("SATURDAY_END_TYM")==null?defTime:rs.getString("SATURDAY_END_TYM"))+"\"," +
                             "\"saC\": \""+(rs.getString("SATURDAY_FLAG")==null?"N":rs.getString("SATURDAY_FLAG"))+"\"," +
                             "\"suS\": \""+(rs.getString("SUNDAY_START_TYM")==null?defTime:rs.getString("SUNDAY_START_TYM"))+"\"," +
                             "\"suE\": \""+(rs.getString("SUNDAY_END_TYM")==null?defTime:rs.getString("SUNDAY_END_TYM"))+"\"," +
                             "\"suC\": \""+(rs.getString("SUNDAY_FLAG")==null?"N":rs.getString("SUNDAY_FLAG"))+"\" }";
                          

            }else
            {
                 jsonString="{ \"mS\": \""+defTime+"\","+
                             "\"mE\": \""+defTime+"\"," +
                             "\"mC\": \""+"N"+"\"," +
                             "\"tS\": \""+defTime+"\"," +
                             "\"tE\": \""+defTime+"\"," +
                             "\"tC\": \""+"N"+"\"," +
                             "\"wS\": \""+defTime+"\"," +
                             "\"wE\": \""+defTime+"\"," +
                             "\"wC\": \""+"N"+"\"," +
                             "\"thS\": \""+defTime+"\"," +
                             "\"thE\": \""+defTime+"\"," +
                             "\"thC\": \""+"N"+"\"," +
                             "\"fS\": \""+defTime+"\"," +
                             "\"fE\": \""+defTime+"\"," +
                             "\"fC\": \""+"N"+"\"," +
                             "\"saS\": \""+defTime+"\"," +
                             "\"saE\": \""+defTime+"\"," +
                             "\"saC\": \""+"N"+"\"," +
                             "\"suS\": \""+defTime+"\"," +
                             "\"suE\": \""+defTime+"\"," +
                             "\"suC\": \""+"N"+"\" }";
            }

            
        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in gettellerDetails for user Id :" + tellerId);
           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 updateLoginTimings(HttpServletRequest request ,String teller , String pacsId) {
        Connection con = null;
        CallableStatement proc = null;
        String res = null;

               // String monS = request.getParameter("dayMonS");
               // String monE = request.getParameter("dayMonE");
               // String tueS = request.getParameter("dayTueS");
               // String tueE = request.getParameter("dayTueE");
               // String wedS = request.getParameter("dayWedS");
               // String wedE = request.getParameter("dayWedE");
               // String thuS = request.getParameter("dayThuS");
               // String thuE = request.getParameter("dayThuE");
               // String friS = request.getParameter("dayFriS");
               // String friE = request.getParameter("dayFriE");
               // String satS = request.getParameter("daySatS");
               // String satE = request.getParameter("daySatE");
               // String sunS = request.getParameter("daySunS");
               // String sunE = request.getParameter("daySunE");

                String monChk = request.getParameter("monCheck")!=null?"Y":"N";
                String tueChk = request.getParameter("tueCheck")!=null?"Y":"N";
                String wedChk = request.getParameter("wedCheck")!=null?"Y":"N";
                String thuChk = request.getParameter("thuCheck")!=null?"Y":"N";
                String friChk = request.getParameter("friCheck")!=null?"Y":"N";
                String satChk = request.getParameter("satCheck")!=null?"Y":"N";
                String sunChk = request.getParameter("sunCheck")!=null?"Y":"N";



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

            proc.setString(1, teller);
            proc.setString(2, pacsId);
            proc.setString(3, monChk);
            proc.setString(4, tueChk);
            proc.setString(5, wedChk);
            proc.setString(6, thuChk);
            proc.setString(7, friChk);
            proc.setString(8, satChk);
            proc.setString(9, sunChk);

            proc.setString(10, monS);
            proc.setString(11, monE);
            proc.setString(12, tueS);
            proc.setString(13, tueE);
            proc.setString(14, wedS);
            proc.setString(15, wedE);
            proc.setString(16, thuS);
            proc.setString(17, thuE);
            proc.setString(18, friS);
            proc.setString(19, friE);
            proc.setString(20, satS);
            proc.setString(21, satE);
            proc.setString(22, sunS);
            proc.setString(23, sunE);
            proc.registerOutParameter(24, OracleTypes.VARCHAR);

            proc.executeUpdate();

          //  res = proc.getString(24);//error message from dds procedure

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in updateLoginTimings for user Id :" + teller);
           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 res;
    }
}
