//---------------------------------------------------------------------------
// String _opacsconnection; 约定的输入变量名
// String _hisconnection; 	约定的输入变量名 
// String _modalitytype; 	约定的输入变量名
// String _requestid; 		约定的输入变量名
// String _patientname; 	约定的输入变量名
// String _examclass; 		约定的输入变量名
// String _examsubclass; 	约定的输入变量名
// TDateTime _startdate; 	约定的输入变量名
// TDateTime _enddate; 	    约定的输入变量名
// TADOQuery _qryHisCustom  外部已定义的对象  
// TADOQuery _qryHisCommon  外部已定义的对象
// TADOQuery _qryHisStudy   外部已定义的对象
// TADOQuery _qryStudy		外部已定义的对象
//
// 脚本必须实现的函数 int _QueryHisTable();
// 脚本必须实现的函数 int _ConfirmHisLogin(); 	
// 脚本必须实现的函数 int _CancelHisLogin(); 	
// 脚本必须实现的函数 int _PatientValidate();	
//---------------------------------------------------------------------------

#include "..\\Common\\HisCommon.cpp"

String 	    _hisconnection; 
String 	    _modalitytype; 
String      _requestid;
String      _patientname;
String      _examclass;
String      _examsubclass;
TDateTime   _startdate;
TDateTime   _enddate;

//---------------------------------------------------------------------------

int ExecSQL()
{
    String sHisQuery = "";
    ////
    int iFieldCount = 0;
    String sFieldPairs[2][100];
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name",		"patient_name");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex",		"patient_sex");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_birthday",	"patient_birthday");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_studyage",	"patient_studyage");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_address",	"patient_address");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_telephone",	"patient_telephone");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_guid",		"patient_guid");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_type",		"patient_type");
    SetArrayItem(sFieldPairs, iFieldCount, "study_examkind",	"study_examkind");
    SetArrayItem(sFieldPairs, iFieldCount, "study_examname",	"study_examname");
	SetArrayItem(sFieldPairs, iFieldCount, "study_number0",		"study_number0");    
    SetArrayItem(sFieldPairs, iFieldCount, "study_number1",		"study_number1");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number2",		"study_number2");
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickregion", 	"study_sickregion");
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickroom", 	"study_sickroom");
    SetArrayItem(sFieldPairs, iFieldCount, "login_extinfo1", 	"login_extinfo1");
    SetArrayItem(sFieldPairs, iFieldCount, "study_applicant", 	"study_applicant");
    SetArrayItem(sFieldPairs, iFieldCount, "study_bodypart", 	"study_bodypart");
    SetArrayItem(sFieldPairs, iFieldCount, "study_department", 	"study_department");
    SetArrayItem(sFieldPairs, iFieldCount, "study_userdef1", 	"study_userdef1");
    SetArrayItem(sFieldPairs, iFieldCount, "study_userdef2", 	"study_userdef2");
    SetArrayItem(sFieldPairs, iFieldCount, "report_memoinfo2", 	"report_memoinfo2");
    ////
    SetArrayItem(sFieldPairs, iFieldCount, "login_imagekey", 	"study_key");
    

	sHisQuery = "SELECT TOP 100 * FROM STUDY WHERE STUDY_MODALITYTYPE = '" + _modalitytype + "'";

    if (_examsubclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, _examsubclass);

    if (_requestid != "")
    {
    	if (IsNumString(_requestid))
    	{
        	sHisQuery = AddWhereCondition(sHisQuery, "STUDY_GUID = " + _requestid);
        }
    }
    if (_patientname != "")
        sHisQuery = AddWhereCondition(sHisQuery, "patient_name = '" + _patientname + "'");
    ////    
    String sStartDate = FormatDateTime("yyyy-mm-dd 0:0:0", _startdate);
    String sEndDate = FormatDateTime("yyyy-mm-dd 23:59:59", _enddate);
	if (IsConnetAccessDB(_hisconnection))
	{
	    if (_startdate != 0)
	        sHisQuery = AddWhereCondition(sHisQuery, "study_dttm >= #"+sStartDate+"#");
	    if (_enddate != 0)
	        sHisQuery = AddWhereCondition(sHisQuery, "study_dttm <= #"+sEndDate+"#");
	    _qryHisCustom.ParamCheck = false; // !!!
	}
	else
	{		
	    if (_startdate != 0)
	        sHisQuery = AddWhereCondition(sHisQuery, "study_dttm >= '" + sStartDate + "'");
	    if (_enddate != 0)
	        sHisQuery = AddWhereCondition(sHisQuery, "study_dttm <= '" + sEndDate + "'");	        
    }   
    ////
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery;
    try
    {
        _qryHisCustom.Open();
    }
    except
    {
        ShowMessage("查询失败！请检查输入条件！");
    }
    ////
    if (_qryHisCustom.IsEmpty())
    	return 0;
    ////
    _qryHisCustom.First();
    while (!_qryHisCustom.Eof)
    {
        _qryHisStudy.Append();
        for (int i = 0; i < iFieldCount; i++)
        {
            if (!_qryHisCustom.FieldByName(sFieldPairs[1][i]).IsNull)
            {
            	_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value =
                	_qryHisCustom.FieldByName(sFieldPairs[1][i]).Value;
            }
        }
        _qryHisStudy.Post();
        _qryHisCustom.Next();
    }

    return 1;
}
//---------------------------------------------------------------------------

int _QueryHisTable()
{
    // 查询脚本
    return ExecSQL();
}

//---------------------------------------------------------------------------
// 确认登记：1 成功，0 失败
//---------------------------------------------------------------------------
int _ConfirmHisLogin()
{
    return 0;
}

//---------------------------------------------------------------------------
// 撤销登记：1 成功，0 失败
//---------------------------------------------------------------------------
int _CancelHisLogin()
{
    return 0;
}
//---------------------------------------------------------------------------

int _PatientValidate()
{
	return 0;
}
//---------------------------------------------------------------------------

// 主程序
{
    // Nothing need to do.
}
//---------------------------------------------------------------------------
