


#include <SQLAPI.h>
#include <string>
#include <vector>
//#include <libxl.h>
//#include "StringCodeConverter.h"
#include "SystemDataQuery.h"

//using namespace libxl;
using namespace std;

#pragma comment( lib, "user32.lib")
#pragma comment( lib, "version.lib")
#pragma comment( lib, "oleaut32.lib")
#pragma comment( lib, "ole32.lib")
#pragma comment( lib, "libxl.lib")

#ifdef _DEBUG
#pragma comment( lib, "sqlapisd.lib")
#else
#pragma comment( lib, "sqlapis.lib")
#endif

const char g_cszConnectStringIDS6[] = 
	"DRIVER={};"
	"PROTOCOL=onsoctcp;"
	"SERVICE=16191;"
	"SERVER=xmcx1;"
	"HOST=10.39.0.91;"
	"DATABASE=ids6;"
	"DB_LOCALE=en_US.819;";

const static char g_szUserNameIDS6[] = "ccx99";
const static char g_szPasswordIDS6[] = "c91IT09";

const static char g_cszConnectStringIDSt0[] =
	"DRIVER={};"
	"PROTOCOL=onsoctcp;"
	"SERVICE=16193;"
	"SERVER=xmcx3;"
	"HOST=10.39.0.93;"
	"DATABASE=idst0;"
	"DB_LOCALE=en_US.819;";

const static char g_szUserNameIDSt0[] = "ccx99";
const static char g_szPasswordIDSt0[] = "c93IT09";

const static char g_cszConnstringYwgl[] =
	"DRIVER={};"
	"PROTOCOL=onsoctcp;"
	"SERVICE=16192;"
	"SERVER=xmcx2;"
	"HOST=10.39.0.92;"
	"DATABASE=ywgl_xm;"
	"DB_LOCALE=en_US.819;";

const static char g_cszUserNameYwgl[] = "ccx99";
const static char g_cszPasswordYwgl[] = "c92IT09";

void query_user_info(const string & strCode, string & strName, string & strPassword)
{
	SAConnection conn;
	SACommand command;

	string strCommand( "select rymc, rymm from rydm where rydm='" );

	strCommand += strCode;
	strCommand += "'";

	command.setConnection( &conn );
	command.setCommandText( strCommand.c_str() );

	try
	{
		conn.Connect(
			g_cszConnstringYwgl,
			g_cszUserNameYwgl,
			g_cszPasswordYwgl,
			SA_Informix_Client );

		command.Execute();
	}
	catch ( SAException &error )
	{
		//SAString strError = error.ErrText();

		throw runtime_error( error.ErrText() );
	}

	if ( command.FetchNext() == true )
	{
		//名字
		SAString sastrResult = command.Field("rymc").asString();
		sastrResult.TrimRight();

		strName = (const char *)sastrResult;

		//密码
		sastrResult = command.Field("rymm").asString();
		sastrResult.TrimRight();

		strPassword = (const char *)sastrResult;
	}

	conn.Disconnect();
}

void query_staff_info(const string & strOperatorCode, string & strOperatorName, string & strDeptCode, string & strDeptName, string & strOfficeCode, string & strOfficeName)
{
	SAConnection connection;
	SACommand    command;

	if ( strOperatorCode.size() == 0 )
	{
		return;
	}

	string strCommand = 
		"select a.staff_name, "
		"       b.department_code, "
		"       b.department_name, "
		"       c.section_office_c01, "
		"       c.section_office_n01 "
		"from rydm_t a, bm_t b, ks_t c  "
		"where a.department_code = b.department_code "
		"  and a.section_office_c01 = c.section_office_c01 "
		"  and a.staff_code = '";

	strCommand.append( strOperatorCode );
	strCommand.append( "'" );

	try
	{
		connection.Connect( 
			g_cszConnectStringIDSt0,
			g_szUserNameIDSt0,
			g_szPasswordIDSt0,
			SA_Informix_Client );

		command.setConnection( &connection );
		command.setCommandText( strCommand.c_str() );

		command.Execute();

		if ( command.FetchNext() == true )
		{
			strOperatorName = (const char *)( command.Field( "staff_name").asString() );
			strDeptCode = (const char *)( command.Field( "department_code").asString() );
			strDeptName = (const char *)( command.Field( "department_name").asString() );
			strOfficeCode = (const char *)( command.Field( "section_office_c01").asString() );
			strOfficeName = (const char *)( command.Field( "section_office_n01").asString() );
		}

		connection.Disconnect();
	}
	catch ( SAException & sqlError )
	{
		runtime_error error( sqlError.ErrText() );

		throw error;
	}
}

void query_auto_trader_info(const string & strAutoTraderCode, string & strAutoTraderName)
{
	SAConnection connection;
	SACommand    command;

	string strCommand = 
		"select name "
		"  from shop_code_t "
		" where code = '";

	strCommand.append( strAutoTraderCode );
	strCommand.append( "'" );

	try
	{
		connection.Connect( 
			g_cszConnectStringIDSt0,
			g_szUserNameIDSt0,
			g_szPasswordIDSt0,
			SA_Informix_Client );

		command.setConnection( &connection );
		command.setCommandText( strCommand.c_str() );

		command.Execute();

		if ( command.FetchNext() == true )
		{
			strAutoTraderName = (const char *)( command.Field( "name").asString() );
		}

		connection.Disconnect();
	}
	catch ( SAException & sqlError )
	{
		runtime_error error( sqlError.ErrText() );

		throw error;
	}
}
