//---------------------------------------------------------------------------
// 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\\JSDCommon.cpp"

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

int ExecQuery()
{   
    String	zxks1 = "CT室";    // (请根据现场情况修改，不能为空)
    String  zxks2 = "放射科";  // (请根据现场情况修改，可以为空) 
    
    int iResult = -100;
    String patid = "";
    String syxh = "";
    
    if (zxks1 != "")
    {
        TClientDataSet cds = new TClientDataSet(nil);
        try
        {
            if (Length(_requestid) > 0)
            {
                iResult = ExeJB01(_requestid);
                if (iResult != 0)
                    return iResult;
                ////
                cds.LoadFromFile("C:\\OpacsLog\\JB01.result.xml");	
                patid = Trim(cds.FieldByName("patientid").Value);
                syxh = Trim(cds.FieldByName("syxh").Value);
                ExeJB03(zxks1, patid, syxh);
            }
            else
            {
                iResult = ExeJB02(zxks1);
                if (iResult != 0)
                    return iResult;
            }
        }
        finally
        {
            if (cds != NULL)
            {
                delete cds;
                cds = NULL;
            }
        }
    }
    
    // 一并查询第二科室
    if (zxks2 != "")
    {
		TClientDataSet cdsJB01 = new TClientDataSet(nil);
		try
		{
            if (Length(_requestid) > 0)
            {		
                iResult = ExeJB01(_requestid);
                if (iResult != 0)
                    return iResult;	
                ////	
                cdsJB01.LoadFromFile("C:\\OpacsLog\\JB01.result.xml");	
                patid = Trim(cdsJB01.FieldByName("patientid").Value);
                syxh = Trim(cdsJB01.FieldByName("syxh").Value);
                ExeJB03(zxks2, patid, syxh);
            }
            else
            {
                iResult = ExeJB02(zxks2);
                if (iResult != 0)
                    return iResult;
            }            
		}
		finally
		{
			if (cdsJB01 != NULL)
			{
				delete cdsJB01;
				cdsJB01 = NULL;
			}
		}	
	}

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

int ExecConfirm()
{
	int iResult = ExecHIS("confirm");
	if (iResult == 0)
	{
		MessageDlg("确认成功！", mtinformation, mbok, 0);
		
		// 修改登记状态
		_qryStudy.Edit();
		int bDirectStarted = GetConfigInt("Register", "bDirectStarted");
		if (bDirectStarted)
			_qryStudy.FieldByName("login_state").Value = 2;  // 已分诊
		else
		    _qryStudy.FieldByName("login_state").Value = 0;  // 排队中
        _qryStudy.Post();
        ////
        return 1;
	}
	else
	{
		MessageDlg("确认失败！", mterror, mbok, 0);
		////
		return 0;
	}	
}
//---------------------------------------------------------------------------

int ExecCancel()
{
	int iResult = ExecHIS("cancel");
	if (iResult == 0)
	{
		MessageDlg("撤销成功！", mtinformation, mbok, 0);
		
		// 修改登记状态
		_qryStudy.Edit();
        _qryStudy.FieldByName("login_state").Value = -3;  // 已撤销
        _qryStudy.Post();
        ////
        return 1;
	}
	else
	{
		MessageDlg("撤销失败！", mterror, mbok, 0);
		////
		return 0;
	}	
}
//---------------------------------------------------------------------------

int ExeJB01(String id)
{
	int iFieldCount = 0;
    string sFieldPairs[2][100];
    SetArrayItem(sFieldPairs, iFieldCount, "patient_guid", 		"patientid"); // 病人唯一内码 
	SetArrayItem(sFieldPairs, iFieldCount, "study_number1",		"hospno"); // 门诊号!!!
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name", 		"patname");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex", 		"sex"); 
    SetArrayItem(sFieldPairs, iFieldCount, "patient_studyage", 	"age");  
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickbed", 	"bedno"); // 病床
    SetArrayItem(sFieldPairs, iFieldCount, "patient_identity", 	"idnum");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_telephone", "phone");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_address", 	"address");
    SetArrayItem(sFieldPairs, iFieldCount, "study_applicant", 	"todoc"); // 申请医生
    SetArrayItem(sFieldPairs, iFieldCount, "study_number0", 	"sendno"); // 申请单号
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickregion",	"bqmc"); // 病区
    SetArrayItem(sFieldPairs, iFieldCount, "study_department", 	"applydept"); // 科室    
 		
	int iResult = -100;
	TClientDataSet cds = new TClientDataSet(nil);
	try
	{
		// 准备命令(JB01)	
		String sSendFile = "C:\\OpacsLog\\JB01.xml"; 
		cds.LoadFromFile("..\\Common\\JB01.xml");
		cds.First();
		cds.Edit();
		if (Length(id) == 8)
            cds.FieldByName("codetype").Value = "2"; // 1=住院/门诊号, 2=卡号, 3=PatientID  
        else
            cds.FieldByName("codetype").Value = "3"; // 1=住院/门诊号, 2=卡号, 3=PatientID   
        cds.FieldByName("code").Value = id;
		cds.FieldByName("brlb").Value = "0"; // 0:门诊, 1:住院, 3:体检
		cds.Post();	
		cds.SaveToFile(sSendFile);
		
		// 发送命令
		String sResultFile = "C:\\OpacsLog\\JB01.result.xml";
		String sAppDir = ExtractFilePath(Application.ExeName);
		String sIniDir = sAppDir + "Interface";	
		String sCommand = sIniDir + "\\SendEmr.exe JB01 " + sSendFile + " " + sResultFile;
		iResult = RunConsoleProgram(sCommand);
    	//ShowMessage(iResult);

		// 获取结果
	    if (iResult != 0) // 0-成功，-1-失败，7-获取参数失败，8-参数无效，9-连接失败 
	    {   
	    	if (iResult == -1)
	    		MessageBox("查无此人！", "电子申请单", 1);
	    	if (iResult == 7)
	    		MessageBox("获取参数失败！", "电子申请单", 1);
	    	if (iResult == 8)
	    		MessageBox("参数无效！", "电子申请单", 1);
	    	if (iResult == 9)
	    		MessageBox("连接失败！", "电子申请单", 1);		    	
			////
			return iResult;
		}
	   	else    
	    {
	    	cds.LoadFromFile(sResultFile);
	        cds.First();
	        while (!cds.Eof)
	        {
	            _qryHisStudy.Append();
	            for (int i = 0; i < iFieldCount; i++)
	            {
	                if (!cds.FieldByName(sFieldPairs[1][i]).IsNull)
	                {
	              		if (sFieldPairs[0][i] == "patient_sex")
	              		{
	              			String sPatientSex = cds.FieldByName(sFieldPairs[1][i]).Value;
                    		if (sPatientSex == "1")
                    			_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = "男";
                    		else if (sPatientSex == "2")
                      			_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = "女"; 
                    		else
                    			_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = "";
	              		}
	              		else if (sFieldPairs[0][i] == "patient_studyage")
	              		{
	              			String sAge = cds.FieldByName(sFieldPairs[1][i]).Value; 
	              			String sUnit = cds.FieldByName("ageunit").Value;
	              			_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = sAge;
	              			_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value += sUnit;
							TDateTime dtStudy = Now(); // 此处值得斟酌！by boost	              			
							_qryHisStudy.FieldByName("patient_birthday").Value =
								CalcBirthday(sAge + sUnit, dtStudy);	              				
	              		}
	              		else if (sFieldPairs[0][i] == "study_applicant")
	              		{
	              			String sDocCode = cds.FieldByName(sFieldPairs[1][i]).Value;	
							_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = Trim(GetApplyDoctor(sDocCode));
	              		}
	              		else if (sFieldPairs[0][i] == "study_department")
	              		{
	              			String sDeptCode = cds.FieldByName(sFieldPairs[1][i]).Value;
							_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = Trim(GetDeptName(sDeptCode));
	              		}
	              		else
	              		{
	              			_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value =
								cds.FieldByName(sFieldPairs[1][i]).Value;	
	              		}	              	              		
	                }

	                String 		sSurveyInfo = "";
	                String sResult = cds.FieldByName("hospno").Value;
	            	sSurveyInfo += "hospno=" + Trim(sResult) + ",";
	            	
	                sResult = cds.FieldByName("syxh").Value;
	            	sSurveyInfo += "syxh=" + Trim(sResult) + ",";
	            	
	            	sResult = cds.FieldByName("cardno").Value;
	            	sSurveyInfo += "cardno=" + Trim(sResult) + ",";
	            	
	            	_qryHisStudy.FieldByName("study_surveyinfo").Value = sSurveyInfo;
	            }
	            _qryHisStudy.Post();
	            ////
	            cds.Next();
	        }
	    }
	}
	finally
	{	
		if (cds != NULL)
		{
			delete cds;
			cds = NULL;
		}
	}
	////
	return iResult;
}
//---------------------------------------------------------------------------

int ExeJB02(String zxks)
{
	int iFieldCount = 0;
    string sFieldPairs[2][100];
    SetArrayItem(sFieldPairs, iFieldCount, "patient_guid", 		"patid"); // 病人唯一内码 
	SetArrayItem(sFieldPairs, iFieldCount, "study_number1",		"blh"); // 门诊号!!!
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name", 		"hzxm");
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickregion", 	"bqmc"); // 病区
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickbed", 	"cwdm"); // 病床
    SetArrayItem(sFieldPairs, iFieldCount, "study_applicant", 	"ysmc"); // 申请医生
    SetArrayItem(sFieldPairs, iFieldCount, "study_number0", 	"sqdxh"); // 申请单号
    SetArrayItem(sFieldPairs, iFieldCount, "study_department", 	"kdysks"); // 科室    
 		
	String sStartDate = FormatDateTime("yyyymmdd00:00:00", _startdate);
    String sEndDate = FormatDateTime("yyyymmdd23:59:59", _enddate);
     		
	int iResult = -100;
	String blh = "";
	String patid = "";
	String syxh = "";
	TClientDataSet cds = new TClientDataSet(nil);
	TClientDataSet cds2 = new TClientDataSet(nil);
	try
	{
		// 准备命令(JB02)	
		String sSendFile = "C:\\OpacsLog\\JB02.xml"; 
		cds.LoadFromFile("..\\Common\\JB02.xml");
		cds.First();
		cds.Edit();
		cds.FieldByName("ksrq").Value = sStartDate; // 开始日期
		cds.FieldByName("jsrq").Value = sEndDate; // 结束日期
		cds.FieldByName("zxks").Value = Trim(GetDeptCode(zxks)); // 执行科室代码
		cds.FieldByName("brlb").Value = "0"; // 0:门诊, 1:住院, 3:体检
		cds.FieldByName("blh").Value = "0"; // 门诊号/住院号
		cds.FieldByName("fph").Value = "0"; //  发票号 
		cds.FieldByName("codetype").Value = "0"; // 0=磁卡号 1=体检号
		cds.Post();	
		cds.SaveToFile(sSendFile);
		
		// 发送命令
		String sResultFile = "C:\\OpacsLog\\JB02.result.xml";
		String sAppDir = ExtractFilePath(Application.ExeName);
		String sIniDir = sAppDir + "Interface";	
		String sCommand = sIniDir + "\\SendEmr.exe JB02 " + sSendFile + " " + sResultFile;
		iResult = RunConsoleProgram(sCommand);
    	//ShowMessage(iResult);

		// 获取结果
	    if (iResult != 0) // 0-成功，-1-失败，7-获取参数失败，8-参数无效，9-连接失败 
	    {   
	    	if (iResult == -1)
	    		MessageBox("查无此人！", "电子申请单", 1);
	    	if (iResult == 7)
	    		MessageBox("获取参数失败！", "电子申请单", 1);
	    	if (iResult == 8)
	    		MessageBox("参数无效！", "电子申请单", 1);
	    	if (iResult == 9)
	    		MessageBox("连接失败！", "电子申请单", 1);		    	
			////
			return iResult;
		}
	   	else    
	    {
	    	cds.LoadFromFile(sResultFile);
	        cds.First();
	        while (!cds.Eof)
	        {
	            _qryHisStudy.Append();
	            for (int i = 0; i < iFieldCount; i++)
	            {
	                if (!cds.FieldByName(sFieldPairs[1][i]).IsNull)
	                {
	              		if (sFieldPairs[0][i] == "study_department")
	              		{
	              			String sDeptCode = cds.FieldByName(sFieldPairs[1][i]).Value;
							_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = Trim(GetDeptName(sDeptCode));
	              		}
	              		else
	              		{
	              			_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value =
								cds.FieldByName(sFieldPairs[1][i]).Value;	
	              		}	              	              		
	                }

	                String 		sSurveyInfo = "";
	                String sResult = cds.FieldByName("blh").Value;
	            	sSurveyInfo += "hospno=" + Trim(sResult) + ",";

	                sResult = cds.FieldByName("syxh").Value;
	            	sSurveyInfo += "syxh=" + Trim(sResult) + ",";           	
	            	
	            	_qryHisStudy.FieldByName("study_surveyinfo").Value = sSurveyInfo;
	            }
	            _qryHisStudy.Post();
	            ////
	            cds.Next();
	        }
	    }
	}
	finally
	{	
		if (cds != NULL)
		{
			delete cds;
			cds = NULL;
		}
		if (cds2 != NULL)
		{
			delete cds2;
			cds2 = NULL;
		}
	}
	////
	return iResult;
}
//---------------------------------------------------------------------------

int ExeJB03(String zxks, String patid, String syxh)
{
	int iResult = -100;
	TClientDataSet cds = new TClientDataSet(nil);
	try
	{	
		// 准备命令(JB03)
		String sSendFile = "C:\\OpacsLog\\JB03.xml";
		cds.LoadFromFile("..\\Common\\JB03.xml");
		cds.First();
		cds.Edit();
		cds.FieldByName("blh").Value = ""; // 门诊号/住院号
		cds.FieldByName("brlb").Value = "0"; // 0:门诊, 1:住院, 3:体检
		cds.FieldByName("patid").Value = patid; // 病人唯一内码 
		cds.FieldByName("syxh").Value = syxh; // 首页序号 
		cds.FieldByName("qqxh").Value = "0"; // 申请序号
		cds.FieldByName("tjrybh").Value = "0"; // 体检人员编号
		if (_startdate > 0) 	
		 	cds.FieldByName("rq1").Value = FormatDateTime("yyyymmdd00:00:00", _startdate); // 开始日期 格式：2011063017:02:00
		else
			cds.FieldByName("rq1").Value = "";
		if (_enddate > 0)
			cds.FieldByName("rq2").Value = FormatDateTime("yyyymmdd23:59:59", _enddate); // 结束日期
		else
			cds.FieldByName("rq2").Value = "";
		cds.FieldByName("zxks").Value = Trim(GetDeptCode(zxks)); // 执行科室代码
		cds.FieldByName("sqdxh").Value = "0"; // 申请单序号
		cds.SaveToFile(sSendFile);
		
		// 发送命令
		String sResultFile = "C:\\OpacsLog\\JB03.result.xml";
		String sAppDir = ExtractFilePath(Application.ExeName);
		String sIniDir = sAppDir + "Interface";	
		String sCommand = sIniDir + "\\SendEmr.exe JB03 " + sSendFile + " " + sResultFile;
		iResult = RunConsoleProgram(sCommand);
    	//ShowMessage(iResult);
		
	    // 获取结果
	    if (iResult != 0) // 0-成功，-1-失败，7-获取参数失败，8-参数无效，9-连接失败 
	    {   
	    	MessageBox("执行JB03命令出错！", "电子申请单", 1);		    	
			return iResult;
		}
	   	else    
	    {
	    	_qryHisStudy.Edit();
	    	cds.LoadFromFile(sResultFile);
	       	cds.First();      
			String 	sSurveyInfo1 = _qryHisStudy.FieldByName("study_surveyinfo").AsString;
			////
			double fTotalMoney = 0; 
	        String sItemCode = "", sItemName = "", sBodyPart = "" , sClinicInfo = "", sSymptom = "";
	        String qqxh = "", qqmxxh = "", qqksmc = "", ysmc = "", qqrq = "";
	        String price = "", itemqty = "", itemunit = "", itemtype = "", state = "";   	
	        while (!cds.Eof)
	        {         
    			String sTempCode = TrimX(cds.FieldByName("itemcode").Value);
	       		sTempCode += " ";
	       		sItemCode += sTempCode;
	       		
	       		String sTempName = TrimX(cds.FieldByName("itemname").Value);
	       		sTempName += " ";
	       		sItemName += sTempName;
       			       			       			       		
	       		// 新增检查部位信息
	       		if (cds.FieldByName("jcmd").AsString == "0")
	       			sBodyPart = "";
	       		else 
	       			sBodyPart = Trim(cds.FieldByName("jcmd").Value); 
	       		
	       		// 新增临床诊断信息
	       		if (cds.FieldByName("lczd").AsString == "0")
	       			sClinicInfo = "";
	       		else
	       			sClinicInfo = Trim(cds.FieldByName("lczd").Value);
	       		
	       		// 新增临床病史信息
	       		if (cds.FieldByName("bszl").AsString == "0")
	       			sSymptom = "";
	       		else
	       			sSymptom = Trim(cds.FieldByName("bszl").Value);
	       		
    			String sTempQqxh;
    			if (!cds.FieldByName("qqxh").IsNull) // 可能为空
    				sTempQqxh = TrimX(cds.FieldByName("qqxh").Value);
    			else
    				sTempQqxh = "0";
    			sTempQqxh += "|";
    			qqxh += sTempQqxh;
    			
    			String sTempQqmxxh;
    			if (!cds.FieldByName("qqmxxh").IsNull) // 可能为空
    				sTempQqmxxh = TrimX(cds.FieldByName("qqmxxh").Value);
    			else
    				sTempQqmxxh = "0";
    			sTempQqmxxh += "|";
    			qqmxxh += sTempQqmxxh;
    			
	       		String sTemp1 = TrimX(cds.FieldByName("qqksmc").Value);
	       		sTemp1 += "|";
	       		qqksmc += sTemp1; 	       		

	       		String sTemp2 = TrimX(cds.FieldByName("ysmc").Value);
	       		sTemp2 += "|";
	       		ysmc += sTemp2; 	       		

	       		String sTemp3 = TrimX(cds.FieldByName("qqrq").Value);
	       		sTemp3 += "|";
	       		qqrq += sTemp3; 	       		
    			
	       		String sTempPrice = TrimX(cds.FieldByName("price").Value);
	       		String sTempItemqty = TrimX(cds.FieldByName("itemqty").Value);
	       		fTotalMoney += StrToFloat(sTempPrice) * StrToInt(sTempItemqty);

	       		sTempPrice += "|";
	       		price += sTempPrice;       		
	       		sTempItemqty += "|";
	       		itemqty += sTempItemqty;
	       		
	       		String sTempItemUnit = TrimX(cds.FieldByName("itemunit").Value);
	       		sTempItemUnit += "|";
	       		itemunit += sTempItemUnit; 	       		

	       		String sTempItemtype = TrimX(cds.FieldByName("itemtype").Value);
	       		sTempItemtype += "|";
	       		itemtype += sTempItemtype;
	            
	            state += "0" + "|";
	            
	            cds.Next();
	        }
	        
	        sSurveyInfo1 += "zxks=" + zxks + ",qqxh=" + qqxh + ",qqmxxh=" + qqmxxh + ",qqksmc=" + qqksmc + ",ysmc=" + ysmc + ",qqrq=" + qqrq
	        	+ ",price=" + price + ",itemqty=" + itemqty + ",itemunit=" + itemunit + ",itemtype=" + itemtype + ",state=" + state;
	        _qryHisStudy.FieldByName("study_surveyinfo").Value = sSurveyInfo1;
	        _qryHisStudy.FieldByName("study_examcode").Value = Trim(sItemCode);
	        _qryHisStudy.FieldByName("study_examname").Value = Trim(sItemName);
	        _qryHisStudy.FieldByName("study_bodypart").Value = Trim(sBodyPart);
	        _qryHisStudy.FieldByName("study_clinicinfo").Value = Trim(sClinicInfo);
	        _qryHisStudy.FieldByName("study_symptom").Value = Trim(sSymptom);
	        _qryHisStudy.FieldByName("study_charge").Value = fTotalMoney;
	        _qryHisStudy.FieldByName("study_cost").Value = fTotalMoney;
	        if (Trim(sItemCode) != "")
	        	_qryHisStudy.FieldByName("study_payment").Value = "待确认";
	        else
	        	_qryHisStudy.FieldByName("study_payment").Value = "已确认";
	        _qryHisStudy.FieldByName("study_source").Value = "门诊";
	        _qryHisStudy.Post();
	    }
	}
	finally
	{	
		if (cds != NULL)
		{
			delete cds;
			cds = NULL;
		}
	}
	////
	return iResult;
}
//---------------------------------------------------------------------------

int ExecHIS(String sOperator)
{	
	String sSurveyInfo = _qryStudy.FieldByName("study_surveyinfo").AsString;
	String zxks = GetValueByName(sSurveyInfo, "zxks");  // 获取确认科室
	String zxys = GetConfigStr("SysLogon", "sLastAccounts");    // 获取确认医生
	
	String blh = GetValueByName(sSurveyInfo, "hospno");
	String xmlb = GetValueByName(sSurveyInfo, "itemtype");
	String patid = _qryStudy.FieldByName("patient_guid").AsString;
	String syxh = GetValueByName(sSurveyInfo, "syxh");
	String qqxh = GetValueByName(sSurveyInfo, "qqxh");
	String qqmxxh = GetValueByName(sSurveyInfo, "qqmxxh");
	String itemcode = _qryStudy.FieldByName("study_examcode").AsString;
	String itemname = _qryStudy.FieldByName("study_examname").AsString;
	String price = GetValueByName(sSurveyInfo, "price");
	String itemqty = GetValueByName(sSurveyInfo, "itemqty");
	String state = GetValueByName(sSurveyInfo, "state");
	String state2 = "";
			
	// HIS确认   
	int iResult = 0;
	TClientDataSet cds = new TClientDataSet(nil);
	////
	TStringList pText = new TStringList;
	pText.CommaText = itemcode;
	try
	{
		for (int i = 0; i < pText.Count(); i++)
		{		
			// 准备命令(QF01)
			String sSendFile = "C:\\OpacsLog\\QF01.xml";
			cds.LoadFromFile("..\\Common\\QF01.xml"); 
			cds.First();
			cds.Edit();
			cds.FieldByName("blh").Value = blh;  
			cds.FieldByName("brlb").Value = "0"; // 0：门诊1：住院 3：体检    
			cds.FieldByName("xmlb").Value = GetValueByIndex(xmlb, i);
			cds.FieldByName("patid").Value = patid;
			cds.FieldByName("syxh").Value = syxh;
			cds.FieldByName("zxksdm").Value = Trim(GetDeptCode(zxks)); // 确认科室代码  
			cds.FieldByName("zxysdm").Value = zxys; // 确认人员代码
			cds.FieldByName("qqxh").Value = GetValueByIndex(qqxh, i);
			cds.FieldByName("qqmxxh").Value = GetValueByIndex(qqmxxh, i);
			cds.FieldByName("itemcode").Value = GetValueByIndex(itemcode, i); // 项目代码 
			cds.FieldByName("itemname").Value = GetValueByIndex(itemname, i); // 项目名称 
			cds.FieldByName("price").Value = GetValueByIndex(price, i); // 项目单价 
			cds.FieldByName("itemqty").Value = GetValueByIndex(itemqty, i); // 项目数量
			if (sOperator == "confirm")
			{
				cds.FieldByName("xmstatus").Value = "1"; // 项目状态 (确认)
				cds.FieldByName("sfflag").Value = "0"; // 收费状态 (不收费)
			}
			else if (sOperator == "cancel")
			{
				cds.FieldByName("xmstatus").Value = "3"; // 项目状态 (撤销)
				cds.FieldByName("sfflag").Value = "0"; // 收费状态 (不收费)	
			}
			cds.FieldByName("djlb").Value = "0"; // 单价类别
			cds.FieldByName("bgdh").Value = "0"; // 报告单号 (确费时可为空)
			cds.FieldByName("bglx").Value = "0"; // 报告类型 (确费时可为空)
			cds.FieldByName("tssm").Value = "";
			cds.FieldByName("tjrybh").Value = "";
			cds.Post();	
			cds.SaveToFile(sSendFile);
			////	
			// 发送命令
			String sResultFile = "C:\\OpacsLog\\QF01.result.xml";
			String sAppDir = ExtractFilePath(Application.ExeName);
			String sIniDir = sAppDir + "Interface";	
			String sCommand = sIniDir + "\\SendEmr.exe QF01 " + sSendFile + " " + sResultFile;
			int iResult2 = RunConsoleProgram(sCommand);
			if (GetValueByIndex(state, i) == "1") // 已经确认的项目，再次确认时忽略失败。
				iResult2 = 0; 		
			iResult = iResult + iResult2;
			////
            if (iResult2 != 0) // 0-成功，-1-失败，7-获取参数失败，8-参数无效，9-连接失败 
            {   
            	state2 += "0" + "|";
                MessageBox("执行QF01命令出错(" + IntToStr(iResult2) + ")！", "电子申请单", 1);		    	
            }
            else
            {
            	state2 += "1" + "|";
        	}				
		}
    }
	finally
	{
		if (pText != NULL)
		{
			delete pText;
			pText = NULL;
		}
		if (cds != NULL)
		{
			delete cds;
			cds = NULL;
		}	
    }
    ////
    _qryStudy.Edit();
    sSurveyInfo = SetValueByName(sSurveyInfo, "state", state2);
    _qryStudy.FieldByName("study_surveyinfo").Value = sSurveyInfo;
    if (iResult == 0)
    	_qryStudy.FieldByName("study_payment").Value = "已确认";
    else    
    	_qryStudy.FieldByName("study_payment").Value = "待确认";
    _qryStudy.Post();    
    ////	
	return iResult;   
}
//---------------------------------------------------------------------------

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

int _ConfirmHisLogin()
{
    // 登记检查(HIS确认)    
    return ExecConfirm();
}
//---------------------------------------------------------------------------

int _CancelHisLogin()
{
    // 撤销登记
    MessageBox("请使用退费功能！", "电子申请单", 1);
    //return ExecCancel();
}
//---------------------------------------------------------------------------

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

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