package com.reallinkbilling.utils;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.reallinkbilling.DAO.CustomerPaymentReminderDetailsDAO;
import com.reallinkbilling.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;



public class GetNotificationList extends ActionSupport implements SessionAware{
	
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 8007504384670429244L;
	private SessionMap<String,Object> sessionMap;  
	
	
	
	
	
	
	public static ArrayList<CustomerPaymentReminderDetailsDAO> getCustPaymentReminderList(SessionMap<String, Object> sessionMap)
	{
		 ArrayList<CustomerPaymentReminderDetailsDAO> customerPaymentReminderList = new ArrayList<CustomerPaymentReminderDetailsDAO>();


			Connection connection = null;
			PreparedStatement preparedStatement= null;
			ResultSet  resultSet = null;
			String sql = "";
			String db = (String) sessionMap.get("db");

			String shopId = (String) sessionMap.get("shopId");
			String shopCode = (String) sessionMap.get("shopCode");
    		
    		
    		
    		
				try
				{
					
				
					connection = GetDBConnection.getDBConnection(db);
					
					int numberOfDaysBefore = 0;
					String today = "";
					
					
					Date d = new Date();
		        	SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		        	today = dateFormat.format(d);
		        	
		        	sql = "SELECT "
		        			+ "entry_id,"
		        			+ "DATE_FORMAT(reminder_add_date,'%d-%M-%Y'),"
		        			+ "customer_id,"
		        			+ "customer_name,"
		        			+ "customer_mobile_number,"
		        			+ "outsatnding_amount,"
		        			+ "DATE_FORMAT(payment_promise_date,'%d-%M-%Y'),"
		        			+ "comments,"
		        			+ "status "
		        			+ "FROM "
		        			+ "shop_customer_payment_reminder_master "
		        			+ "WHERE "
		        			+ "shop_id = ? "
		        			+ "AND "
		        			+ "shop_code = ? "
		        			+ "AND "
		        			+ "(payment_promise_date = STR_TO_DATE(?,'%d-%m-%Y') "
		        			+ "OR "
		        			+ "(payment_promise_date < STR_TO_DATE(?,'%d-%m-%Y') AND status = 'NOTIFY')) "
		        			+ "ORDER BY DATE_FORMAT(payment_promise_date,'%d-%M-%Y')";
		        	
		        	preparedStatement = connection.prepareStatement(sql);
		        	
		        	preparedStatement.setString(1, shopId);
		        	preparedStatement.setString(2, shopCode);		        	
		        	preparedStatement.setString(3, today);
		        	preparedStatement.setString(4, today);
		        	
		        	resultSet = preparedStatement.executeQuery();
		        	
		        	while(resultSet.next())
		        	{
		        		CustomerPaymentReminderDetailsDAO dao = new CustomerPaymentReminderDetailsDAO();
		        		
		        		dao.setEntryId(resultSet.getString(1));
		        		dao.setPaymentReminderAddDate(resultSet.getString(2));
		        		dao.setCustomerId(resultSet.getString(3));
		        		dao.setCustomerName(resultSet.getString(4));
		        		dao.setCustomerMobileNumber(resultSet.getString(5));
		        		dao.setOutstandingAmount(resultSet.getFloat(6));
		        		dao.setPaymentPromiseDate(resultSet.getString(7));
		        		dao.setComments(resultSet.getString(8));
		        		dao.setKey(EncryptionDecryption.encrypt(resultSet.getString(1)+"#"+resultSet.getString(3)+"#"+resultSet.getString(7)));
		        		customerPaymentReminderList.add(dao);
		        	}
		        			
					
					
				}catch (SQLException e)
				{
					e.printStackTrace();
				
				}catch(Exception e)
				{
					e.printStackTrace();
				}
				finally {
				
				if(resultSet !=null)
				{
					try {resultSet.close(); resultSet = null;} catch (SQLException e) {}
				}
				if(preparedStatement != null)
				{
					try {preparedStatement.close(); preparedStatement = null;} catch (SQLException e) {}
				}
				if(connection != null)
				{
					
					try {connection.close();
					 connection = null;
					}catch (SQLException e) {}
				}
		
		}
		 
		 
		 return customerPaymentReminderList;
	}
	
	
	
	
	
	
	
	
	
	
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(SessionMap<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

	@Override  
	public void setSession(Map<String, Object> map) {  
	    sessionMap=(SessionMap<String, Object>)map;  
	}


}