package com.reallinkbilling.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import org.apache.struts2.dispatcher.SessionMap;

import com.reallinkbilling.db.GetDBConnection;

public class GetSupplierNameById {
	
	
	public static String getSupplierNameById(String supplierId, SessionMap<String, Object> sessionMap)
	{
		String supplierName = "";
		
		
		Connection connection = null;
   		PreparedStatement preparedStatement= null;
   		ResultSet resultSet = null;
		
   		String sql = "";
   		String db = (String) sessionMap.get("db");
   		try {
   			
   			String shopId = (String) sessionMap.get("shopId");
    		String shopCode = (String) sessionMap.get("shopCode");
   			
			connection = GetDBConnection.getDBConnection(db);
   			
			
			
			sql = "SELECT "
					+ "suplier_name "
					+ "FROM "
					+ "shop_supplier_master "
					+ "WHERE "
					+ "supplier_id = ? "
					+ "AND "
					+ "shop_id = ? "
					+ "AND "
					+ "shop_code = ? ";
			
			preparedStatement = connection.prepareStatement(sql);
			
			preparedStatement.setString(1, supplierId);
			preparedStatement.setString(2, shopId);
			preparedStatement.setString(3, shopCode);
			
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				supplierName = resultSet.getString(1);
			}
			
   			
   		}   catch (SQLException e) {
			//e.printStackTrace();
			
		} catch (IOException 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 supplierName;
	}
	
	
	
	public static HashMap<String,String> getSupplierDetailsById(String supplierId, SessionMap<String, Object> sessionMap)
	{
		
		HashMap<String,String> supplierDetails = new HashMap<String,String>();
		
		Connection connection = null;
   		PreparedStatement preparedStatement= null;
   		ResultSet resultSet = null;
		
   		String sql = "";
   		String db = (String) sessionMap.get("db");
   		try {
   			
   			String shopId = (String) sessionMap.get("shopId");
    		String shopCode = (String) sessionMap.get("shopCode");
   			
			connection = GetDBConnection.getDBConnection(db);
   			
			
			
			sql = "SELECT "
					+ "suplier_name,"
					+ "contact_number,"
					+ "supplier_address "
					+ "FROM "
					+ "shop_supplier_master "
					+ "WHERE "
					+ "supplier_id = ? "
					+ "AND "
					+ "shop_id = ? "
					+ "AND "
					+ "shop_code = ? ";
			
			preparedStatement = connection.prepareStatement(sql);
			
			preparedStatement.setString(1, supplierId);
			preparedStatement.setString(2, shopId);
			preparedStatement.setString(3, shopCode);
			
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				supplierDetails.put("NAME", resultSet.getString(1));
				supplierDetails.put("MOBILE", resultSet.getString(2));
				supplierDetails.put("ADDRESS", resultSet.getString(3));
			}
			
   			
   		}   catch (SQLException e) {
			//e.printStackTrace();
			
		} catch (IOException 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 supplierDetails;
	}

}
