package com.reallinkbilling.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.reallinkbilling.db.GetDBConnection;

public class InsertNewSettingForEachDairy {
	
	public static void main(String[] args) {
		Connection connection = null;
		PreparedStatement preparedStatement= null;
		ResultSet  resultSet = null;
		
		try
		{
			
			
			
			String sql = "";
			String db = "reallinkshop";
			
			connection = GetDBConnection.getDBConnection(db);
			sql = "SELECT shop_id,"
					+ "shop_code "
					+ "FROM "
					+ "shop_info_master "
					+ "ORDER BY shop_id";
			
			Map<String,String> dairyDetailsMap = new HashMap<String,String>();
			
			preparedStatement = connection.prepareStatement(sql);
			
			resultSet = preparedStatement.executeQuery();
			
			while(resultSet.next())
			{
				dairyDetailsMap.put(resultSet.getString(1), resultSet.getString(2));
			}
			
			
			if(resultSet != null)
			{
				resultSet.close();
				resultSet = null;
			}
			if(preparedStatement != null)
			{
				preparedStatement.close();
				preparedStatement = null;
			}
			
			int k = 1;
			for(String shopId : dairyDetailsMap.keySet())
			{
				System.out.println("Shop Id : "+shopId+",Shop Code : "+dairyDetailsMap.get(shopId));
				
			sql = "INSERT INTO "
						+ "`shop_settings` "
						+ "(`shop_id`, `shop_code`, `setting_name`, `setting_value`) "
						+ "VALUES ('"+shopId+"', '"+dairyDetailsMap.get(shopId)+"', 'enable_salesman_commission', 'FALSE')";
				
				//sql = "INSERT INTO shop_bank_details_master (entry_id,shop_id,shop_code,shop_name) values (?,?,?,?)";
				preparedStatement = connection.prepareStatement(sql);
				/*preparedStatement.setString(1, ""+k);
				preparedStatement.setString(2, shopId);
				preparedStatement.setString(3, dairyDetailsMap.get(shopId));
				preparedStatement.setString(4, "NA");*/
				
				
				
				
				int i = preparedStatement.executeUpdate();
				
				if(i > 0)
				{
					System.out.println("Inserted");
				}
				k++;
			}
			
		}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) {}
			}
		}
		
		
	}

}
