//---------------------------------------------------------------------------
// 注意：本脚本适用于已有电子申请单的金仕达HIS系统。
//---------------------------------------------------------------------------
// String _opacsconnection; 约定的输入变量名
// String _modalitytype; 	约定的输入变量名
// TADOQuery _qryStudy; 	外部已定义的对象
// TADOQuery _qryReport     外部已定义的对象
// TADOQuery _qryCustom     外部已定义的对象

//---------------------------------------------------------------------------
#include "..\\Common\\HisCommon.cpp", "..\\Common\\JSDCommon.cpp"

String _opacsconnection;	
String _modalitytype;
//---------------------------------------------------------------------------

int ExecReportSend()
{
	// 获取检查号
    int iStudyKey = _qryStudy.FieldByName("study_key").AsInteger;
    int iState = _qryStudy.FieldByName("patient_state").AsInteger;
    if (iStudyKey < 0)
    	return 0;
    if (iState == 0)
    {
    	MessageDlg("报告已撤销！", mtinformation, mbok, 0);
    	return 0;
    }   	

    // 数据传送
    TClientDataSet cds = new TClientDataSet(nil);
    int iResult = -100;
    String sSendLog = "";      
    ////
    String sPatientName = _qryStudy.FieldByName("patient_name").AsString; 
    String sSurveyInfo = _qryStudy.FieldByName("study_surveyinfo").AsString;
 
    // 准备报告发送命令(BG02)
    try
    {
    	if (Length(sSurveyInfo))
    	{
			String sSendFile = "C:\\OpacsLog\\BG02.xml";
			cds.LoadFromFile("..\\Common\\BG02.xml"); 
			cds.First();
			cds.Edit();
			cds.FieldByName("repno").Value = IntToStr(iStudyKey); 
			
			// 对应HIS数据库中的报告类型ID
			cds.FieldByName("replb").Value = _qryStudy.FieldByName("study_modalitytype").AsString;
				
			cds.FieldByName("hslb").Value = "0"; // 类别（0：回收1：查询）					
			cds.Post();	
		
			// 发送命令
			cds.SaveToFile(sSendFile);
			String sResultFile = "C:\\OpacsLog\\BG02.result.xml";
			String sAppDir = ExtractFilePath(Application.ExeName);
			String sIniDir = sAppDir + "Interface";	
			String sCommand = sIniDir + "\\SendEmr.exe BG02 " + 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 = 0;
    		    	_qryStudy.FieldByName("report_state").Value = 1;
    		    	_qryStudy.Post();
    		    }
    		    finally
    		    {
    		    	_qryStudy.Tag = iTag;
    		    } 
			}
    		else
			{
				sSendLog = "回收报告【" + sPatientName + "】...失败!";
			}
			MessageDlg(sSendLog, mtinformation, mbok, 0);   
    	} 
    }
	finally
	{
		if (cds != NULL)
		{
			delete cds;
			cds = NULL;
		}
	}
	////
	if (iResult == 0)
		return 1;
	else
		return 0;
}
//---------------------------------------------------------------------------

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