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

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

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

//---------------------------------------------------------------------------
// 自定义变量
int g_iQueryType = 0;
String g_sPatientIDField = "";

//---------------------------------------------------------------------------
int ExecSQL()
{
	g_iQueryType = 3; // 取值范围：1,2,3 请根据现场情况修改!!!
	//g_sPatientIDField = "patient_guid"; // 超声等科室使用，请根据情况配置。
	g_sPatientIDField = "study_guid"; // 放射等科室使用，请根据情况配置。

	if (_requestid != "") // 住院号
	{
		if (!PatsInHospital(_requestid))
		{
			ShowMessage("不是住院病人，请重新查询！");
			return 0;
		}
	}

	String sHisQuery = "";
    int iFieldCount = 0;
    String sFieldPairs[2][100];
    ////
    if (g_iQueryType == 1)
    	sHisQuery = BuildQuery1(sFieldPairs, iFieldCount); 
    else if (g_iQueryType == 2)
    	sHisQuery = BuildQuery2(sFieldPairs, iFieldCount); 
    else if (g_iQueryType == 3)
    	sHisQuery = BuildQuery3(sFieldPairs, iFieldCount); 
    ////
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery;
    try
    {
        _qryHisCustom.Open();
    }
    except
    {
        ShowMessage("查询失败！请检查输入条件！");
    }
    ////
    if (!_qryHisCustom.IsEmpty())
    {
        _qryHisCustom.First();
        while (!_qryHisCustom.Eof)
        {
            _qryHisStudy.Append();
			_qryHisStudy.FieldByName("study_source").Value = "住院";
            for (int i = 0; i < iFieldCount; i++)
            {
            	if (sFieldPairs[1][i] == "")
            		continue;
                if (!_qryHisCustom.FieldByName(sFieldPairs[1][i]).IsNull)
                {
                    if (sFieldPairs[0][i] == "patient_sex")
                    {
                        String sPatientSex = _qryHisCustom.FieldByName(sFieldPairs[1][i]).Value;
                        if ((sPatientSex == "男") || (sPatientSex == "1"))
                            _qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = "男";
                        else if ((sPatientSex == "女") || (sPatientSex == "2"))
                            _qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = "女";
                    }
					else if (sFieldPairs[0][i] == "patient_birthday")
					{
						TDateTime dtStudy = Now(); // 此处值得斟酌！by boost
						_qryHisStudy.FieldByName("patient_studyage").Value =
							CalcStudyAge(_qryHisCustom.FieldByName(sFieldPairs[1][i]).Value, dtStudy);
						_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value =
							_qryHisCustom.FieldByName(sFieldPairs[1][i]).Value;
					}
                    else
                    {
                        _qryHisStudy.FieldByName(sFieldPairs[0][i]).Value =
                            _qryHisCustom.FieldByName(sFieldPairs[1][i]).Value;
                    }
                }
            }
            _qryHisStudy.Post();
            _qryHisCustom.Next();
        }
    }

    return 1;
}

//---------------------------------------------------------------------------
// ★此查询适用于使用JW系统确认并收费的情况，以EXAM_MASTER为主要依据。
//---------------------------------------------------------------------------
String BuildQuery1(Variant& sFieldPairs, int& iFieldCount)
{
	String sHisQuery = "";
		
    // 目标表字段 <= 源表字段
    SetArrayItem(sFieldPairs, iFieldCount, g_sPatientIDField,	"PATIENT_ID");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name", 		"NAME");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex", 		"SEX");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_birthday", 	"DATE_OF_BIRTH");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_address", 	"MAILING_ADDRESS");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_telephone", "PHONE_NUMBER_HOME");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_identity", 	"ID_NO");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_type", 		"IDENTITY");
    SetArrayItem(sFieldPairs, iFieldCount, "study_userdef1", 	"CHARGE_TYPE");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number0", 	"EXAM_NO"); // 用于回传报告时定位记录    
    SetArrayItem(sFieldPairs, iFieldCount, "study_number1", 	"");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number2", 	"INP_NO");
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickbed", 	"BED_NO");
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickregion", 	"DEPT_NAME");    
    SetArrayItem(sFieldPairs, iFieldCount, "study_department", 	"DEPT_NAME");
    SetArrayItem(sFieldPairs, iFieldCount, "study_applicant", 	"REQ_PHYSICIAN");
    SetArrayItem(sFieldPairs, iFieldCount, "study_clinicinfo", 	"DIAGNOSIS");      
    ////
	if (IsConnetAccessDB(_hisconnection) || IsConnetSQLServer(_hisconnection))
	{
        sHisQuery = "SELECT TOP 100 * FROM PAT_MASTER_INDEX";
	}
	else
	{
		sHisQuery = "SELECT DISTINCT a.PATIENT_ID, a.NAME, a.SEX, a.DATE_OF_BIRTH, a.DATE_OF_BIRTH, " +
			"a.MAILING_ADDRESS, a.PHONE_NUMBER_HOME, a.ID_NO, a.IDENTITY, a.CHARGE_TYPE, a.INP_NO, " + 
			"b.BED_NO, b.DOCTOR_IN_CHARGE, b.DIAGNOSIS, c.DEPT_NAME, " +
   			"d.EXAM_NO, d.EXAM_CLASS, d.EXAM_SUB_CLASS, d.REQ_DATE_TIME, d.REQ_PHYSICIAN " +
   			"FROM PAT_MASTER_INDEX a, PATS_IN_HOSPITAL b, DEPT_DICT c, EXAM_MASTER d " +
   			"WHERE a.PATIENT_ID = b.PATIENT_ID and b.PATIENT_ID = d.PATIENT_ID and " + 
   			"b.DEPT_CODE = c.DEPT_CODE and c.DEPT_CODE = d.REQ_DEPT and d.PATIENT_SOURCE = '2'"; // 住院
	}
    ////
    if (_requestid != "")
        sHisQuery = AddWhereCondition(sHisQuery, "a.INP_NO = '" + _requestid + "'");
    if (_patientname != "")
        sHisQuery = AddWhereCondition(sHisQuery, "a.NAME = '" + _patientname + "'");
    ////
    if (_examclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, "d.EXAM_CLASS = '" + _examclass + "'");
    if (_examsubclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, "d.EXAM_SUB_CLASS = '" + _examsubclass + "'");
	////
	String sStartDate = FormatDateTime("yyyy-mm-dd 0:0:0", _startdate);
    String sEndDate = FormatDateTime("yyyy-mm-dd 23:59:59", _enddate);
	if (IsConnetAccessDB(_hisconnection) || IsConnetSQLServer(_hisconnection))
	{
		if (_startdate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, "XXX  >=  #"+sStartDate+"#");
		if (_enddate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, "XXX  <=  #"+sEndDate+"#");
		////
		if (IsConnetSQLServer(_hisconnection))
			sHisQuery = AccessToSQL(sHisQuery);
		////
		_qryHisCustom.ParamCheck = false; // !!!
	}
	else
	{
		if (_startdate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, "d.REQ_DATE_TIME >= to_date('" + sStartDate + "', 'yyyy-mm-dd hh24:mi:ss')");
	    if (_enddate != 0)
	        sHisQuery = AddWhereCondition(sHisQuery, "d.REQ_DATE_TIME <= to_date('" + sEndDate + "', 'yyyy-mm-dd hh24:mi:ss')");
	    ////    
	    sHisQuery = AddWhereCondition(sHisQuery, "ROWNUM<=100");
	}
	return sHisQuery;
}

//---------------------------------------------------------------------------
// ★此查询适用于使用RIS系统收费且有检查预约的情况，以EXAM_APPOINTS为主要依据。
//---------------------------------------------------------------------------
String BuildQuery2(Variant& sFieldPairs, int& iFieldCount)
{
	String sHisQuery = "";
	
    // 目标表字段 <= 源表字段
    SetArrayItem(sFieldPairs, iFieldCount, g_sPatientIDField,	"PATIENT_ID");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name", 		"NAME");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex", 		"SEX");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_birthday", 	"DATE_OF_BIRTH");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_address", 	"MAILING_ADDRESS");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_telephone", "PHONE_NUMBER_HOME");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_identity", 	"ID_NO");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_type", 		"IDENTITY");
    SetArrayItem(sFieldPairs, iFieldCount, "study_userdef1", 	"CHARGE_TYPE");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number0", 	"EXAM_NO"); // 用于回传报告时定位记录    
    SetArrayItem(sFieldPairs, iFieldCount, "study_number1", 	"");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number2", 	"INP_NO");
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickbed", 	"BED_NO");
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickregion", 	"DEPT_NAME");     
    SetArrayItem(sFieldPairs, iFieldCount, "study_department", 	"DEPT_NAME");
    SetArrayItem(sFieldPairs, iFieldCount, "study_applicant", 	"REQ_PHYSICIAN");
    SetArrayItem(sFieldPairs, iFieldCount, "study_clinicinfo", 	"DIAGNOSIS");    
    ////
	if (IsConnetAccessDB(_hisconnection) || IsConnetSQLServer(_hisconnection))
	{
        sHisQuery = "SELECT TOP 100 * FROM PAT_MASTER_INDEX";
	}
	else
	{
		sHisQuery =	"SELECT DISTINCT a.PATIENT_ID, a.NAME, a.SEX, a.DATE_OF_BIRTH, a.DATE_OF_BIRTH, " + 
 			"a.MAILING_ADDRESS, a.PHONE_NUMBER_HOME, a.ID_NO, a.IDENTITY, a.CHARGE_TYPE, a.INP_NO, " + 
 			"b.BED_NO, b.DOCTOR_IN_CHARGE, b.DIAGNOSIS, c.DEPT_NAME, " +
   			"d.EXAM_NO, d.EXAM_CLASS, d.EXAM_SUB_CLASS, d.REQ_DATE_TIME, d.REQ_PHYSICIAN " +
   			"FROM PAT_MASTER_INDEX a, PATS_IN_HOSPITAL b, DEPT_DICT c, EXAM_APPOINTS d " +
   			"WHERE a.PATIENT_ID = b.PATIENT_ID and b.PATIENT_ID = d.PATIENT_ID and " + 
   			"b.DEPT_CODE = c.DEPT_CODE and c.DEPT_CODE = d.REQ_DEPT and d.PATIENT_SOURCE = '2'"; // 住院
	}
    ////
    if (_requestid != "")
        sHisQuery = AddWhereCondition(sHisQuery, "a.INP_NO = '" + _requestid + "'");
    if (_patientname != "")
        sHisQuery = AddWhereCondition(sHisQuery, "a.NAME = '" + _patientname + "'");    
	////
    if (_examclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, "d.EXAM_CLASS = '" + _examclass + "'");
    if (_examsubclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, "d.EXAM_SUB_CLASS = '" + _examsubclass + "'");
	////
	String sStartDate = FormatDateTime("yyyy-mm-dd 0:0:0", _startdate);
    String sEndDate = FormatDateTime("yyyy-mm-dd 23:59:59", _enddate);
	if (IsConnetAccessDB(_hisconnection) || IsConnetSQLServer(_hisconnection))
	{
		if (_startdate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, "XXX  >=  #"+sStartDate+"#");
		if (_enddate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, "XXX  <=  #"+sEndDate+"#");
		////
		if (IsConnetSQLServer(_hisconnection))
			sHisQuery = AccessToSQL(sHisQuery);
		////
		_qryHisCustom.ParamCheck = false; // !!!
	}
	else
	{
		if (_startdate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, "d.REQ_DATE_TIME >= to_date('" + sStartDate + "', 'yyyy-mm-dd hh24:mi:ss')");
	    if (_enddate != 0)
	        sHisQuery = AddWhereCondition(sHisQuery, "d.REQ_DATE_TIME <= to_date('" + sEndDate + "', 'yyyy-mm-dd hh24:mi:ss')");
	    ////    
	    sHisQuery = AddWhereCondition(sHisQuery, "ROWNUM<=100");
	}
	return sHisQuery;
}

//---------------------------------------------------------------------------
// ★此查询适用于使用RIS系统收费且无检查预约的情况，以PAT_MASTER_INDEX为主要依据。
//---------------------------------------------------------------------------
String BuildQuery3(Variant& sFieldPairs, int& iFieldCount)
{
	String sHisQuery = "";
	
    // 目标表字段 <= 源表字段
    SetArrayItem(sFieldPairs, iFieldCount, g_sPatientIDField, 	"PATIENT_ID");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name", 		"NAME");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex", 		"SEX");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_birthday", 	"DATE_OF_BIRTH");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_address", 	"MAILING_ADDRESS");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_telephone", "PHONE_NUMBER_HOME");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_identity", 	"ID_NO");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_type", 		"IDENTITY");
    SetArrayItem(sFieldPairs, iFieldCount, "study_userdef1", 	"CHARGE_TYPE");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number0", 	""); // 用于回传报告时定位记录    
    SetArrayItem(sFieldPairs, iFieldCount, "study_number1", 	"");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number2", 	"INP_NO");
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickbed", 	"BED_NO");
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickregion", 	"DEPT_NAME");     
    SetArrayItem(sFieldPairs, iFieldCount, "study_department", 	"DEPT_NAME");
    SetArrayItem(sFieldPairs, iFieldCount, "study_applicant", 	"DOCTOR_IN_CHARGE");
    SetArrayItem(sFieldPairs, iFieldCount, "study_clinicinfo", 	"DIAGNOSIS");
    ////
	if (IsConnetAccessDB(_hisconnection) || IsConnetSQLServer(_hisconnection))
	{
        sHisQuery = "SELECT TOP 100 * FROM PAT_MASTER_INDEX";
	}
	else
	{
		sHisQuery = "SELECT DISTINCT a.PATIENT_ID, a.NAME, a.SEX, a.DATE_OF_BIRTH, a.DATE_OF_BIRTH, " + 
			"a.MAILING_ADDRESS, a.PHONE_NUMBER_HOME, a.ID_NO, a.IDENTITY, a.CHARGE_TYPE, a.INP_NO, " + 
			"b.BED_NO, b.DOCTOR_IN_CHARGE, b.DIAGNOSIS, c.DEPT_NAME " +
			"FROM PAT_MASTER_INDEX a, PATS_IN_HOSPITAL b, DEPT_DICT c " +
			"WHERE a.PATIENT_ID = b.PATIENT_ID and b.DEPT_CODE = c.DEPT_CODE";
	}
    ////
    if (_requestid != "")
        sHisQuery = AddWhereCondition(sHisQuery, "a.INP_NO = '" + _requestid + "'");
    if (_patientname != "")
        sHisQuery = AddWhereCondition(sHisQuery, "a.NAME = '" + _patientname + "'");
	////
/*
    if (_examclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, "a.EXAM_CLASS = '" + _examclass + "'");
    if (_examsubclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, "a.EXAM_SUB_CLASS = '" + _examsubclass + "'");
*/        
	////
	String sStartDate = FormatDateTime("yyyy-mm-dd 0:0:0", _startdate);
    String sEndDate = FormatDateTime("yyyy-mm-dd 23:59:59", _enddate);
	if (IsConnetAccessDB(_hisconnection) || IsConnetSQLServer(_hisconnection))
	{
		if (_startdate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, "XXX  >=  #"+sStartDate+"#");
		if (_enddate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, "XXX  <=  #"+sEndDate+"#");
		////
		if (IsConnetSQLServer(_hisconnection))
			sHisQuery = AccessToSQL(sHisQuery);
		////
		_qryHisCustom.ParamCheck = false; // !!!
	}
	else
	{
		/*
		if (_startdate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, "XXX >= to_date('" + sStartDate + "', 'yyyy-mm-dd hh24:mi:ss')");
	    if (_enddate != 0)
	        sHisQuery = AddWhereCondition(sHisQuery, "XXX <= to_date('" + sEndDate + "', 'yyyy-mm-dd hh24:mi:ss')");
	    */
	    ////    
	    sHisQuery = AddWhereCondition(sHisQuery, "ROWNUM<=100");
	}
	return sHisQuery;
}
//---------------------------------------------------------------------------

int Appoints2Master()
{	
	int iFieldCount = 0;
    String sFieldPairs[2][100]; // 目标表字段 <= 源表字段
    SetArrayItem(sFieldPairs, iFieldCount, "EXAM_NO", 			"EXAM_NO");
    SetArrayItem(sFieldPairs, iFieldCount, "LOCAL_ID_CLASS", 	"LOCAL_ID_CLASS");
    SetArrayItem(sFieldPairs, iFieldCount, "PATIENT_LOCAL_ID", 	"PATIENT_LOCAL_ID");
    SetArrayItem(sFieldPairs, iFieldCount, "PATIENT_ID", 		"PATIENT_ID");
    SetArrayItem(sFieldPairs, iFieldCount, "VISIT_ID", 			"VISIT_ID");
    SetArrayItem(sFieldPairs, iFieldCount, "NAME", 				"NAME");
    SetArrayItem(sFieldPairs, iFieldCount, "SEX", 				"SEX");
    SetArrayItem(sFieldPairs, iFieldCount, "DATE_OF_BIRTH", 	"DATE_OF_BIRTH");
    SetArrayItem(sFieldPairs, iFieldCount, "EXAM_CLASS", 		"EXAM_CLASS");
    SetArrayItem(sFieldPairs, iFieldCount, "EXAM_SUB_CLASS", 	"EXAM_SUB_CLASS");   
	SetArrayItem(sFieldPairs, iFieldCount, "SPM_RECVED_DATE", 	"");
    SetArrayItem(sFieldPairs, iFieldCount, "CLIN_SYMP", 		"CLIN_SYMP");
    SetArrayItem(sFieldPairs, iFieldCount, "PHYS_SIGN", 		"PHYS_SIGN");
    SetArrayItem(sFieldPairs, iFieldCount, "RELEVANT_LAB_TEST", "RELEVANT_LAB_TEST");
    SetArrayItem(sFieldPairs, iFieldCount, "RELEVANT_DIAG", 	"RELEVANT_DIAG");
    SetArrayItem(sFieldPairs, iFieldCount, "CLIN_DIAG", 		"CLIN_DIAG");
    SetArrayItem(sFieldPairs, iFieldCount, "EXAM_MODE", 		"EXAM_MODE");
    SetArrayItem(sFieldPairs, iFieldCount, "EXAM_GROUP", 		"EXAM_GROUP");
	SetArrayItem(sFieldPairs, iFieldCount, "DEVICE", 			"");
    SetArrayItem(sFieldPairs, iFieldCount, "PERFORMED_BY", 		"PERFORMED_BY");   
    SetArrayItem(sFieldPairs, iFieldCount, "PATIENT_SOURCE", 	"PATIENT_SOURCE");
    SetArrayItem(sFieldPairs, iFieldCount, "FACILITY", 			"FACILITY");
    SetArrayItem(sFieldPairs, iFieldCount, "REQ_DATE_TIME", 	"REQ_DATE_TIME");
    SetArrayItem(sFieldPairs, iFieldCount, "REQ_DEPT", 			"REQ_DEPT");
    SetArrayItem(sFieldPairs, iFieldCount, "REQ_PHYSICIAN", 	"REQ_PHYSICIAN");
    SetArrayItem(sFieldPairs, iFieldCount, "REQ_MEMO", 			"REQ_MEMO");
    SetArrayItem(sFieldPairs, iFieldCount, "SCHEDULED_DATE_TIME", 	"SCHEDULED_DATE"); 
    SetArrayItem(sFieldPairs, iFieldCount, "NOTICE", 			"NOTICE");
	SetArrayItem(sFieldPairs, iFieldCount, "EXAM_DATE_TIME", 	"");
	SetArrayItem(sFieldPairs, iFieldCount, "REPORT_DATE_TIME", 	"");      
  	SetArrayItem(sFieldPairs, iFieldCount, "TECHNICIAN", 		"");
	SetArrayItem(sFieldPairs, iFieldCount, "REPORTER", 			"");
  	SetArrayItem(sFieldPairs, iFieldCount, "RESULT_STATUS", 	"");
    SetArrayItem(sFieldPairs, iFieldCount, "COSTS", 			"COSTS");
    SetArrayItem(sFieldPairs, iFieldCount, "CHARGES", 			"CHARGES");
    SetArrayItem(sFieldPairs, iFieldCount, "CHARGE_INDICATOR", 	"BILLING_INDICATOR");
    SetArrayItem(sFieldPairs, iFieldCount, "CHARGE_TYPE", 		"CHARGE_TYPE");
    SetArrayItem(sFieldPairs, iFieldCount, "IDENTITY", 			"IDENTITY");
	SetArrayItem(sFieldPairs, iFieldCount, "RPTSTATUS", 		"");
	SetArrayItem(sFieldPairs, iFieldCount, "PRINT_STATUS", 		"");         
	SetArrayItem(sFieldPairs, iFieldCount, "EXAM_SUBDEPT", 		"");
	SetArrayItem(sFieldPairs, iFieldCount, "CONFIRM_DOCT", 		"");
	SetArrayItem(sFieldPairs, iFieldCount, "STUDY_UID", 		"");      
	////
	String sExamNo = _qryStudy.FieldByName("study_number0").AsString;
	String sHisQuery = "select * from EXAM_MASTER WHERE EXAM_NO = " + sExamNo;	
	
	// 验证EXAM_MASTER中数据是否存在
	_qryHisCustom.Close();
	_qryHisCustom.SQL.Text = sHisQuery;
	_qryHisCustom.Open();
	
	if (!_qryHisCustom.IsEmpty())
	{
		return 0;
	}
	
	// 复制记录
	_qryHisCommon.Close();
	_qryHisCommon.SQL.Text = "SELECT * FROM EXAM_APPOINTS WHERE EXAM_NO = " + sExamNo;
	_qryHisCommon.Open();
	if (!_qryHisCommon.IsEmpty())
	{			
		_qryHisCustom.Append();
		for (int i = 0; i < iFieldCount; i++)
        {
        	if (sFieldPairs[1][i] == "")        			
        		continue;
            if (!_qryHisCommon.FieldByName(sFieldPairs[1][i]).IsNull)
            {
                _qryHisCustom.FieldByName(sFieldPairs[0][i]).Value =
                    _qryHisCommon.FieldByName(sFieldPairs[1][i]).Value;
        	}
        }        
        _qryHisCustom.Post();			
	}
	
	// 验证复制是否成功
	_qryHisCustom.Close();
	_qryHisCustom.SQL.Text = sHisQuery;
	_qryHisCustom.Open();
	
	if (!_qryHisCustom.IsEmpty())
	{
		// 成功 删除预约表中的记录
		_qryHisCommon.Close();
		_qryHisCommon.SQL.Text = "DELETE FROM EXAM_APPOINTS WHERE EXAM_NO = " + sExamNo;
		_qryHisCommon.ExecSQL();
		
		MessageBox("确认成功！", "电子申请单", 1);
		return  1;
	}
	else
	{
		MessageBox("操作失败！", "电子申请单", 1);	
		return 0;
	}
}
//---------------------------------------------------------------------------

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

int _ConfirmHisLogin()
{
    // 登记检查
    g_iQueryType = 2;
    if (g_iQueryType == 2)
    {
    	// 确认前判断是否已划价
    	double fStudyCost = _qryStudy.FieldByName("study_cost").AsFloat;
    	if (fStudyCost > 0)
    	{
    		return Appoints2Master();
        }
    	else
    	{
   			MessageBox("尚未划价，无法确认检查！", "电子申请单", 1);
			return 0;   	
   		}
    }
    else
    {
		MessageBox("没有配置确认登记功能！", "电子申请单", 1);
	    return 0;
	}	  
}
//---------------------------------------------------------------------------

int _CancelHisLogin()
{
    // 撤销登记
	MessageBox("没有配置撤销登记功能！", "电子申请单", 1);
    return 0;
}
//---------------------------------------------------------------------------

int _PatientValidate()
{	
	// http://ip:port/HL7WS/hl7wsservice/getInpatientStatus/{pid}/{vid}/
	// {"InPatient":{"patientId":123456,"status":1,"visitId":"123456_9742"}}
	
	String sPatientGuid = _qryStudy.FieldByName("patient_guid").AsString;
	String sStudyGuid = _qryStudy.FieldByName("study_guid").AsString;
	////
	String sURL = "http://ip:port/HL7WS/hl7wsservice/";
	String sMethod = "getInpatientStatus";
	String sValue = sPatientGuid + "//" + sStudyGuid;
	String sResult = CallWebService(sURL, sMethod, sValue);
	////
	String sStatus = JsonGetValue(sResult, "status");
	if (sStatus == "1")
		return 1;
		
	return 0;
}
//---------------------------------------------------------------------------

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