package com.reallinkbilling.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.struts2.dispatcher.SessionMap;

import com.reallinkbilling.db.GetDBConnection;

public class GetProductDealerPrice {
	
	
	public static float getDealarPrice(String productId, String productName, String productCode, SessionMap<String, Object> sessionMap)
	{
		float dealerPrice = 0.0F;
		
		
		
		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 "
					+ "dealer_sales_price "
					+ "FROM "
					+ "shop_product_master "
					+ "WHERE "
					+ "product_short_code = ? "
					+ "AND "
					+ "product_id = ? "
					+ "AND "
					+ "shop_id = ? "
					+ "AND "
					+ "shop_code = ? ";
			
			preparedStatement = connection.prepareStatement(sql);
			
			preparedStatement.setString(1, productCode);
			preparedStatement.setString(2, productId);
			preparedStatement.setString(3, shopId);
			preparedStatement.setString(4, shopCode);
			
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				dealerPrice = resultSet.getFloat(1);
			}
			
		}catch (SQLException e) {
	 		
			dealerPrice = 0.0F;
	 		
	 	}
		catch (Exception e) {
			dealerPrice = 0.0F;
		}
		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 dealerPrice;
		
		
	}

}
