//---------------------------------------------------------------------------
// 金仕达HIS：注意本脚本已定义了七个变量！
//---------------------------------------------------------------------------
// 已实现函数
//---------------------------------------------------------------------------
//bool PatsInHospital(String blh);
//String GetDeptCode(String zxks);
//String GetDeptName(String deptcode);
//String GetDoctorCode(String docName);
//String GetApplyDoctor(String docCode);
//String GetValueByName(String sCommaText, String sItem);
//String SetValueByName(String sCommaText, String sItem, String sText);
//String GetValueByIndex(String sText, int iOrder);
//String DelValueByIndex(String sText, int iOrder); 
//TDateTime DecodeDT(String sDateTime);
//String EncodeDT(TDateTime dtDateTime);
//String TrimX(String sText);
//
// BILL_DETAIL表
//double CalcTotalExamCharge(TADOQuery qryBillDetail);
//double CalcTotalExamCost(TADOQuery qryBillDetail);
//String CalcTotalExamItem(TADOQuery qryBillDetail);
//String CalcTotalExamCode(TADOQuery qryBillDetail);
//String CalcTotalBillDetail(TADOQuery qryBillDetail, String sFieldName);
//---------------------------------------------------------------------------

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

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

//---------------------------------------------------------------------------
bool PatsInHospital(String blh)
{
    // 病人是否出院
    String sQuery = "SELECT * FROM ZY_BRSYK WHERE brzt = 1 AND blh = '" +  blh + "'";
    _qryHisCommon.Close();
    _qryHisCommon.SQL.Text = sQuery;
    _qryHisCommon.Open();
    bool bInHospital = false;
    if (!_qryHisCommon.IsEmpty())
        bInHospital = true;

    return bInHospital;
}

//---------------------------------------------------------------------------
// 获取科室代码
String GetDeptCode(String zxks)
{
	int iResult = -100;
	String zxkscode;
	TClientDataSet cds = new TClientDataSet(nil);
	String sResultFile = "C:\\OpacsLog\\YY01.result.xml";
	try
	{
		if ((Time >= 0) && (Time <= 6))
			cds.LoadFromFile("c:\\OpacsLog\\不存在的文件.xml");  // 抛出异常，更新科室代码表
		cds.LoadFromFile(sResultFile);	
	}
	except
	{ 
		// 准备命令(YY01 查询执行科室代码)
		String sSendFile = "C:\\OpacsLog\\YY01.xml";
		cds.LoadFromFile("..\\Common\\YY01.xml");
		cds.First();
		cds.Edit();
		cds.SaveToFile(sSendFile);
				
		// 发送命令
		String sAppDir = GetApplicationPath();
		String sIniDir = sAppDir + "Interface";	
		String sCommand = sIniDir + "\\SendEmr.exe YY01 " + sSendFile + " " + sResultFile;
		iResult = RunConsoleProgram(sCommand);
		if (iResult == 0)
		{
			cds.LoadFromFile(sResultFile);	
		} 
    	//ShowMessage(iResult);
    }
    ////
    if (!cds.IsEmpty())
    {
		cds.First();
		while (!cds.Eof)
		{         
			if (Trim(cds.FieldByName("name").Value) == Trim(zxks))
			{
				zxkscode = Trim(cds.FieldByName("id").Value);
				break;		 
			}
			cds.Next();
		}
	}
	////
	if (cds != NULL)
	{
		delete cds;
		cds = NULL;
	}
	return zxkscode;
}
//---------------------------------------------------------------------------

// 获取科室名称
String GetDeptName(String deptcode)
{
	int iResult = -100;
	String deptname;
	TClientDataSet cds = new TClientDataSet(nil);
	String sResultFile = "C:\\OpacsLog\\YY01.result.xml";
	try
	{
		if ((Time >= 0) && (Time <= 6))
			cds.LoadFromFile("c:\\OpacsLog\\不存在的文件.xml");  // 抛出异常，更新科室名称表
		cds.LoadFromFile(sResultFile);	
	}
	except
	{ 
		// 准备命令(YY01 查询科室名称)
		String sSendFile = "C:\\OpacsLog\\YY01.xml";
		cds.LoadFromFile("..\\Common\\YY01.xml");
		cds.First();
		cds.Edit();
		cds.SaveToFile(sSendFile);
				
		// 发送命令
		String sAppDir = GetApplicationPath();
		String sIniDir = sAppDir + "Interface";	
		String sCommand = sIniDir + "\\SendEmr.exe YY01 " + sSendFile + " " + sResultFile;
		iResult = RunConsoleProgram(sCommand);
		if (iResult == 0)
		{
			cds.LoadFromFile(sResultFile);	
		}
    	//ShowMessage(iResult);
    }
    ////
    if (!cds.IsEmpty())
    {
		cds.First();
		while (!cds.Eof)
		{         
			if (Trim(cds.FieldByName("id").Value) == Trim(deptcode))
			{
				deptname = Trim(cds.FieldByName("name").Value);
				break;		 
			}
			cds.Next();
		}
	}
	////
	if (cds != NULL)
	{
		delete cds;
		cds = NULL;
	}
	return deptname;
}
//---------------------------------------------------------------------------

// 获取医生代码
String GetDoctorCode(String docName)
{
	int iResult = -100; 
	String docCode;                                                                           	
	TClientDataSet cds = new TClientDataSet(nil);                                             	 
	String sResultFile = "C:\\OpacsLog\\YY02.result.xml";                                     	
	try                                                                                       	
	{                                                                                         	
		if ((Time >= 0) && (Time <= 6))
			cds.LoadFromFile("c:\\OpacsLog\\不存在的文件.xml");  // 抛出异常，更新医生代码表
		cds.LoadFromFile(sResultFile);	                                                      	
	}                                                                                         	
	except                                                                                    	
	{                                                                                         	
		// 准备命令(YY02查询医生代码)                                                    		            
		String sSendFile = "C:\\OpacsLog\\YY02.xml";                                          	
		cds.LoadFromFile("..\\Common\\YY02.xml");                                                         	
		cds.First();                                                                          	
		cds.Edit();                                                                           	
		cds.SaveToFile(sSendFile);                                                            	
				                                                                              	
		// 发送命令                                                                           	
		String sAppDir = GetApplicationPath();                                	
		String sIniDir = sAppDir + "Interface";	                                              	
		String sCommand = sIniDir + "\\SendEmr.exe YY02 " + sSendFile + " " + sResultFile;    	                                                              	
		iResult = RunConsoleProgram(sCommand);
		if (iResult == 0)
		{
			cds.LoadFromFile(sResultFile);	
		}                                                                                                                     
    	//ShowMessage(iResult);                                                                   
    }                                                                                           
    ////                                                                                        
    if (!cds.IsEmpty())
    {                                                           	
		cds.First();                                                                              	
		while (!cds.Eof)                                                                          	
		{            
			if (Trim(cds.FieldByName("name").Value) == Trim(docName))                                     	
			{                                  
				docCode = Trim(cds.FieldByName("id").Value);
                //ShowMessage(docCode);                                                           
				break;		                                                                      	
			}                                                                                     	
			cds.Next();                                                                           	
		}
	}                                                                                                                                                             
	////                                                                                                                                                          
	if (cds != NULL)                                                                                                                                              
	{                                                                                                                                                             
		delete cds;                                                                                                                                               
		cds = NULL;                                                                                                                                               
	}                                                                                                                                                             
	return docCode;
}
//---------------------------------------------------------------------------

// 获取医生姓名
String GetApplyDoctor(String docCode)
{
	int iResult = -100; 
	String docname;                                                                           	
	TClientDataSet cds = new TClientDataSet(nil);                                             	 
	String sResultFile = "C:\\OpacsLog\\YY02.result.xml";                                     	
	try                                                                                       	
	{                                                                                         	
		if ((Time >= 0) && (Time <= 6))
			cds.LoadFromFile("c:\\OpacsLog\\不存在的文件.xml");  // 抛出异常，更新医生姓名表
		cds.LoadFromFile(sResultFile);	                                                      	
	}                                                                                         	
	except                                                                                    	
	{                                                                                         	
		// 准备命令(YY02查询医生姓名)                                                    		            
		String sSendFile = "C:\\OpacsLog\\YY02.xml";                                          	
		cds.LoadFromFile("..\\Common\\YY02.xml");                                                         	
		cds.First();                                                                          	
		cds.Edit();                                                                           	
		cds.SaveToFile(sSendFile);                                                            	
				                                                                              	
		// 发送命令                                                                           	
		String sAppDir = GetApplicationPath();                                	
		String sIniDir = sAppDir + "Interface";	                                              	
		String sCommand = sIniDir + "\\SendEmr.exe YY02 " + sSendFile + " " + sResultFile;    	                                                              	
		iResult = RunConsoleProgram(sCommand);
		if (iResult == 0)
		{
			cds.LoadFromFile(sResultFile);	
		}                                                                                                                     
    	//ShowMessage(iResult);                                                                   
    }                                                                                           
    ////                                                                                        
    if (!cds.IsEmpty())
    {                                                           	
		cds.First();                                                                              	
		while (!cds.Eof)                                                                          	
		{                                                                                         	
			// !!"id" 字符串后有若干空格，特殊处理
			String cdsCode = cds.FieldByName("id").Value;
			int iDocCode = Length(docCode);
			int iCdsCode = Length(cdsCode);
			DeleteStr(cdsCode, iDocCode+1, iCdsCode-iDocCode);
			if (Trim(cdsCode) == Trim(docCode))                                     	
			{                                                                                     	
				docname = Trim(cds.FieldByName("name").Value);
				break;		                                                                      	
			}                                                                                     	
			cds.Next();                                                                           	
		}
	}                                                                                                                                                             
	////                                                                                                                                                          
	if (cds != NULL)                                                                                                                                              
	{                                                                                                                                                             
		delete cds;                                                                                                                                               
		cds = NULL;                                                                                                                                               
	}                                                                                                                                                             
	return docname;
}

//---------------------------------------------------------------------------
// 说明：sCommaText仅支持逗号分割文本，不支持空格和“|”
// eg. sCommaText为a=1,b=2|x,c=3 sItem为b 返回值为2|x
//---------------------------------------------------------------------------
String GetValueByName(String sCommaText, String sItem)
{
	// 规格化参数
	for (int x=Pos(" ", sCommaText); (x != 0); x=Pos(" ", sCommaText))
	{
		DeleteStr(sCommaText, x, 1);
		//Insert("　", sCommaText, x);		
	}
	////
	sItem = Trim(sItem);
			
	String sResult = "";
	TStringList pText = new TStringList;
	pText.CommaText = sCommaText; // 空格和逗号默认支持
	sResult = pText.Values[sItem];
	delete pText;
	////
	return sResult;
}

//---------------------------------------------------------------------------
// 说明：sCommaText仅支持逗号分割文本，不支持空格和“|”
//---------------------------------------------------------------------------
String SetValueByName(String sCommaText, String sItem, String sText)
{
	// 规格化参数
	for (int x=Pos(" ", sCommaText); (x != 0); x=Pos(" ", sCommaText))
	{
		DeleteStr(sCommaText, x, 1);
		//Insert("　", sCommaText, x);		
	}
	////
	sItem = Trim(sItem);
	////
    for (x=Pos(" ", sText); (x != 0); x=Pos(" ", sText))
		DeleteStr(sText, x, 1);
    for (x=Pos(",", sText); (x != 0); x=Pos(",", sText))
		DeleteStr(sText, x, 1);
    for (x=Pos("\"", sText); (x != 0); x=Pos("\"", sText))
		DeleteStr(sText, x, 1);
	
	String sResult = "";
	TStringList pText = new TStringList;
	if (pText != NULL)
	{
		pText.CommaText = sCommaText; // 空格和逗号默认支持
		pText.Values[sItem] = sText;
		sResult = pText.CommaText;
		delete pText;
	    pText = NULL;
	}
	////
    for (x=Pos("\"", sResult); (x != 0); x=Pos("\"", sResult))
		DeleteStr(sResult, x, 1);
	return sResult;	
}

//---------------------------------------------------------------------------
// 说明：sText支持空格和“|”分割文本，不支持逗号
// eg. sText为a|b|c iOrder为2 返回值为c
//---------------------------------------------------------------------------
String GetValueByIndex(String sText, int iOrder)
{
	// 规格化参数
	for (int x=Pos(",", sText); (x != 0); x=Pos(",", sText))
	{
		DeleteStr(sText, x, 1);
		Insert("，", sText, x);
	}
	////
	String s = sText; 
 	int i = Pos("|", s);
	if (i != 0)
	{
		for (i; (i != 0); i=Pos("|", s))
	 	{
	  		DeleteStr(s,i,1);
	  		Insert(",",s,i);
		}	
	}

	String sUnit;
	TStringList pText = new TStringList;
	if (pText != NULL)
	{	
		pText.CommaText = s; // 空格和逗号默认支持
	     if ((iOrder >= 0) && (iOrder < pText.Count))
	        sUnit = pText[iOrder];	
	    delete pText;
	    pText = NULL;
	}
    ////
    return sUnit;
}

//---------------------------------------------------------------------------
// 说明：sText支持空格和“|”分割文本，不支持逗号
//---------------------------------------------------------------------------
String DelValueByIndex(String sText, int iOrder)
{
	// 规格化参数	
	for (int x=Pos(",", sText); (x != 0); x=Pos(",", sText))
	{
		DeleteStr(sText, x, 1);
		Insert("，", sText, x);
	}
	////
	String s = sText; 
 	int i = Pos("|", s);
 	int ii = i;
 	int jj = Pos(" ", s);
	if (i != 0)
	{
		for (i; (i != 0); i=Pos("|", s))
	 	{
	  		DeleteStr(s,i,1);
	  		Insert(",",s,i);
		}	
	}

	TStringList pText = new TStringList;
	if (pText != NULL)
	{ 	
		pText.CommaText = s; // 空格和逗号默认支持	
	     if ((iOrder >= 0) && (iOrder < pText.Count))
	        pText.Delete(iOrder);
	    s = pText.CommaText; 
	    delete pText;
	    pText = NULL;	    
	}
    ////
    if (ii > 0) // 替换为竖线
    {
 		i = Pos(",", s);
		for (i; (i != 0); i=Pos(",", s))
	 	{
	  		DeleteStr(s,i,1);
	  		Insert("|",s,i);
		}	    	
	}
	else if (jj > 0) // 替换为空格
	{
 		i = Pos(",", s);
		for (i; (i != 0); i=Pos(",", s))
	 	{
	  		DeleteStr(s,i,1);
	  		Insert(" ",s,i);
		}	    	
	}
    ////
    return s;
}

//---------------------------------------------------------------------------
TDateTime DecodeDT(String sDateTime)
{
	if (Length(sDateTime) < 1)
		return Now();
		
 	// eg 2011063017:02:00
	Insert(" ", sDateTime, 9);
  	Insert("-", sDateTime, 7);
  	Insert("-", sDateTime, 5);
  	return StrToDateTime(sDateTime);
}

//---------------------------------------------------------------------------
String EncodeDT(TDateTime dtDateTime)
{
 	// eg 2011063017:02:00
 	String dt = FormatDateTime("yyyymmddhh:nn:ss", dtDateTime);
  	return dt;
}

//---------------------------------------------------------------------------
String TrimX(String sText)
{
	sText = Trim(sText);
	int x = 0;
	for (x=Pos(",", sText); (x != 0); x=Pos(",", sText))
	{
		DeleteStr(sText, x, 1);
		Insert("，", sText, x);
	}	
	for (x=Pos(" ", sText); (x != 0); x=Pos(" ", sText))
	{
		DeleteStr(sText, x, 1);
		Insert("　", sText, x);
	}	
	return sText;
}

//---------------------------------------------------------------------------
double CalcTotalExamCharge(TADOQuery qryBillDetail)
{
	// 获取“费用明细”中检查项目的总应付款
	double fExamCharge = 0.0;
    for (int i = 0; i < GetRecordCount(qryBillDetail); i++)
    {
    	SetRecNo(qryBillDetail, i+1);
        fExamCharge += qryBillDetail.FieldByName("bill_examcharge").AsFloat;
    }
    return fExamCharge;
} 
//---------------------------------------------------------------------------

double CalcTotalExamCost(TADOQuery qryBillDetail)
{
	// 获取“费用明细”中检查项目的总应付款
	double fExamCost = 0.0;
    for (int i = 0; i < GetRecordCount(qryBillDetail); i++)
    {
    	SetRecNo(qryBillDetail, i+1);
        fExamCost += qryBillDetail.FieldByName("bill_examcost").AsFloat;
    }
    return fExamCost;
} 
//---------------------------------------------------------------------------

String CalcTotalExamItem(TADOQuery qryBillDetail)
{
	// 获取“费用明细”中检查项目的总应付款
	String sExamItem = "";
    for (int i = 0; i < GetRecordCount(qryBillDetail); i++)
    {
    	SetRecNo(qryBillDetail, i+1);
        sExamItem = sExamItem +  " "  + TrimX(qryBillDetail.FieldByName("bill_examname").AsString);
    }
    return Trim(sExamItem);
} 
//---------------------------------------------------------------------------

String CalcTotalExamCode(TADOQuery qryBillDetail)
{
	// 获取“费用明细”中检查项目的总应付款
	String sExamCode = "";
    for (int i = 0; i < GetRecordCount(qryBillDetail); i++)
    {
    	SetRecNo(qryBillDetail, i+1);
        sExamCode = sExamCode +  " "  + TrimX(qryBillDetail.FieldByName("bill_itemcode").AsString);
    }
    return Trim(sExamCode);
} 
//---------------------------------------------------------------------------

String CalcTotalBillDetail(TADOQuery qryBillDetail, String sFieldName)
{
	// 获取“费用明细”中检查项目的总应付款
	String sTotal = "";
    for (int i = 0; i < GetRecordCount(qryBillDetail); i++)
    {
    	SetRecNo(qryBillDetail, i+1);
        sTotal = sTotal +  " "  + TrimX(qryBillDetail.FieldByName(sFieldName).AsString);
    }
    return Trim(sTotal);
} 
//---------------------------------------------------------------------------

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