//---------------------------------------------------------------------------
// String _hisconnection; 	约定的输入变量名
// String _opacsconnection; 约定的输入变量名
// String _modalitytype; 	约定的输入变量名
// String _configsection; 	约定的输入变量名
// TADOQuery _qryStudy 		外部已定义的对象
// TADOQuery _qryReport 	外部已定义的对象
// TADOQuery _qryImage      外部已定义的对象
// TADOQuery _qryCustom     外部已定义的对象
// TADOQuery _qryHisPatient 外部已定义的对象
// TADOQuery _qryHisReport  外部已定义的对象
// TADOQuery _qryHisImage   外部已定义的对象
// TADOQuery _qryHisCommon  外部已定义的对象
// TADOQuery _qryHisCustom  外部已定义的对象
//---------------------------------------------------------------------------
#include "..\\Common\\HisCommon.cpp", "..\\Common\\JSDCommon.cpp"

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

String BuildQuerySQL()
{
    // 构造查询语句
    String sQuerySQL = "select top 100 * from study";
    sQuerySQL = AddWhereCondition(sQuerySQL, "study_modalitytype='" + _modalitytype + "'");
    sQuerySQL = AddWhereCondition(sQuerySQL, "(patient_state is null or patient_state=0)");
    sQuerySQL = AddWhereCondition(sQuerySQL, "report_state=2");
    ////
    if (IsConnetSQLServer(_opacsconnection))
        sQuerySQL = AccessToSQL(sQuerySQL);

    return sQuerySQL;
}
//---------------------------------------------------------------------------

int ExecReportSend()
{
	// 获取检查清单
    _qryStudy.Close();
    _qryStudy.SQL.Text = BuildQuerySQL();
    _qryStudy.Open(); 
        
    // 数据传送
    TClientDataSet cds = new TClientDataSet(nil);
    int iResult = -100; 
    String sSendLog = "";
    ////
    try
    {
	    _qryStudy.First();
    	while (!_qryStudy.Eof)
    	{
    	    String sPatientName = _qryStudy.FieldByName("patient_name").AsString;
    	    String sSurveyInfo = _qryStudy.FieldByName("study_surveyinfo").AsString;
    	    String sStudySource = _qryStudy.FieldByName("study_source").AsString;
    	
    	    // 准备报告发送命令(BG01)
    	    if (Length(sSurveyInfo))
    	    {						
				String sSendFile = "C:\\OpacsLog\\BG01.xml";
				cds.LoadFromFile("..\\Common\\BG01.xml"); 
				cds.First();
				cds.Edit();
				cds.FieldByName("repno").Value = _qryStudy.FieldByName("study_key").AsString;  
				cds.FieldByName("reqno").Value = _qryStudy.FieldByName("study_number0").AsString;
				cds.FieldByName("syxh").Value = GetValueByName(sSurveyInfo, "syxh"); 
				cds.FieldByName("patid").Value = _qryStudy.FieldByName("patient_guid").AsString;
				cds.FieldByName("blh").Value = GetValueByName(sSurveyInfo, "blh");
				cds.FieldByName("cardno").Value = GetValueByName(sSurveyInfo, "cardno");   
				cds.FieldByName("hzxm").Value = _qryStudy.FieldByName("patient_name").AsString; 
				cds.FieldByName("sex").Value = GetValueByName(sSurveyInfo, "sex");
				cds.FieldByName("age").Value = GetValueByName(sSurveyInfo, "age");
				cds.FieldByName("sjksdm").Value = GetDeptCode(_qryStudy.FieldByName("study_department").AsString); 
				cds.FieldByName("sjksmc").Value = _qryStudy.FieldByName("study_department").AsString;
				cds.FieldByName("bqdm").Value =  GetValueByName(sSurveyInfo, "ward");
				cds.FieldByName("bqmc").Value = _qryStudy.FieldByName("study_sickregion").AsString;; 
				cds.FieldByName("cwdm").Value = _qryStudy.FieldByName("study_sickbed").AsString;
				cds.FieldByName("sjysdm").Value = GetDoctorCode(_qryStudy.FieldByName("study_applicant").AsString); 
				cds.FieldByName("sjysxm").Value = _qryStudy.FieldByName("study_applicant").AsString; 
				cds.FieldByName("sjrq").Value = GetValueByName(sSurveyInfo, "qqrq");
				
				// 对应HIS数据库中的报告类型ID 
				cds.FieldByName("replb").Value = _qryStudy.FieldByName("study_modalitytype").AsString;
				// 对应HIS数据库中的报告名称 
				cds.FieldByName("replbmc").Value = _qryStudy.FieldByName("study_modalitytype").AsString;
				
				cds.FieldByName("reprq").Value = _qryStudy.FieldByName("report_dttm").AsString;
				
				if (sStudySource == "门诊")
					cds.FieldByName("xtbz").Value = "0";  //系统标志 0：门诊 1：住院 3：体检
				else if (sStudySource == "住院")
					cds.FieldByName("xtbz").Value = "1";  //系统标志 0：门诊 1：住院 3：体检
				else
					cds.FieldByName("xtbz").Value = "3";  //系统标志 0：门诊 1：住院 3：体检	
					
				cds.FieldByName("jcbw").Value = _qryStudy.FieldByName("study_bodypart").AsString;	// 检查部位
				cds.FieldByName("jcysdm").Value = GetDoctorCode(_qryStudy.FieldByName("report_diagnostician").AsString);	
				cds.FieldByName("jcysxm").Value = _qryStudy.FieldByName("report_diagnostician").AsString;	
				cds.FieldByName("jcksdm").Value = GetDeptCode(GetConfigStr("SysLogon", "sLastDepartment"));	
				cds.FieldByName("jcksmc").Value = GetConfigStr("SysLogon", "sLastDepartment");	
				cds.FieldByName("pubtime").Value = "";	
				cds.Post();	
				
				// 发送命令
				cds.SaveToFile(sSendFile);
				String sResultFile = "C:\\OpacsLog\\BG01.result.xml";
				String sAppDir = GetApplicationPath();
				String sIniDir = sAppDir + "Interface";	
				String sCommand = sIniDir + "\\SendEmr.exe BG01 " + sSendFile + " " + sResultFile;
				iResult = RunConsoleProgram(sCommand);
				if (iResult == 0)
				{
					sSendLog = "传送报告【" + sPatientName + "】到目标系统...成功!" ;
					////
					// 修改报告状态 	        	
    	        	int iTag = _qryStudy.Tag;
    	        	try
    	        	{
    	        		_qryStudy.Tag = 99; // 避免触发事件，即绕过权限控制
    	        		_qryStudy.Edit(); 
    	        		_qryStudy.FieldByName("patient_state").Value = 1;
    	        		_qryStudy.Post();
    	        	}
    	        	finally
    	        	{
    	        		_qryStudy.Tag = iTag;
    	        	}
				}
				else
				{
					sSendLog = "传送报告【" + sPatientName + "】到目标系统...失败!";
				}
				ShowLog("SyncServer", sSendLog);   
    		}
    	    _qryStudy.Next(); 
   		}
    }
    finally
    {
    	if (cds != NULL)
    	{
    		delete cds;
    		cds = NULL;
    	}
	}
    ////
    if (iResult == 0)
    	return 1;
    else
    	return 0;    
}
//---------------------------------------------------------------------------

// 主程序
{
    try
    {
    	ExecReportSend();
    }
    except
    {
    }
}
//---------------------------------------------------------------------------