package com.reallinkbilling.ajax;

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 java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.db.GetDBConnection;

public class GetAllSalesman extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 998877665544332211L;
	private SessionMap<String,Object> sessionMap;
	private Map<String, String> salesmanList = new HashMap<String, String>();

	public String execute(){
		String result = SUCCESS;
		if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){
			return SUCCESS;
		}
		String shopId = (String) this.getSessionMap().get("shopId");
		String shopCode = (String) this.getSessionMap().get("shopCode");
		String db = (String) this.getSessionMap().get("db");
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try{
			connection = GetDBConnection.getDBConnection(db);
			String sql = "SELECT salesman_id, salesman_name, mobile_number, commission_percentage, active FROM shop_salesman_master WHERE shop_id=? AND shop_code=? ORDER BY salesman_name";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, shopId);
			preparedStatement.setString(2, shopCode);
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()){
				String key = resultSet.getInt(1)+"-"+resultSet.getString(2)+"-"+resultSet.getString(3)+"-"+resultSet.getBigDecimal(4)+"-"+resultSet.getString(5);
				this.getSalesmanList().put(key, key);
			}
		}catch(SQLException | IOException e){
			// ignore
		}finally{
			try{ if(resultSet!=null) resultSet.close(); }catch(Exception ig){}
			try{ if(preparedStatement!=null) preparedStatement.close(); }catch(Exception ig){}
			try{ if(connection!=null) connection.close(); }catch(Exception ig){}
		}
		return result;
	}

	public void setSession(java.util.Map<String, Object> map) {  sessionMap=(SessionMap<String, Object>)map; }
	public SessionMap<String, Object> getSessionMap() { return sessionMap; }
	public Map<String, String> getSalesmanList() { return salesmanList; }
	public void setSalesmanList(Map<String, String> salesmanList) { this.salesmanList = salesmanList; }
}


