//---------------------------------------------------------------------------
// 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"

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

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

// 下列函数被 ExecSQL() 调用
int ExecSQL_OLD()
{
//	if (_requestid != "")
//	{
//		if (PatsInHospital(_requestid))
//		{
//			ShowMessage("不是门诊病人，请重新查询！");
//			return 0;
//		}
//	}
// 这里是发票号 所以不存在非门诊问题（发票号还包括急诊的）
	ShowMessage( "此模式仅用于浏览HIS数据，请用输入发票号或门诊号方式登记！");
	
	String sHisQuery = "";
	////
    int iFieldCount = 0;
    String sFieldPairs[2][100];
    //SetArrayItem(sFieldPairs, iFieldCount, "patient_guid", 	"ID");//自动编号
    //SetArrayItem(sFieldPairs, iFieldCount, "study_guid", 		"FPHM");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name", 		"BRXM");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex", 		"BRXB");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_birthday", 	"CSNY");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_address", 	"HKDZ");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_telephone", "JTDH");
    SetArrayItem(sFieldPairs, iFieldCount, "study_dttm", 		"KDRQ"); // !!!
    SetArrayItem(sFieldPairs, iFieldCount, "study_examkind", 	"EXAM_CLASS");
    SetArrayItem(sFieldPairs, iFieldCount, "study_examname", 	"FYMC");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number0", 	"FPHM"); // 用于回传报告时定位记录
    ////
	if (IsConnetSQLServer(_hisconnection))
	{
        sHisQuery = "select top 100 a.id,a.fphm,a.yjxh,c.brxm,c.brxb,c.csny,a.kdrq, exam_class=case " + 
		"when charindex('(CT)',b.fymc)>0 then 'CT' "+
		"when charindex('(DR)',b.fymc)>0 then 'DX' "+ 
		"when charindex('(CR)',b.fymc)>0 then 'DX' "+
		"else '' end, "+
		"b.fymc,c.hkdz,c.jtdh "+
		"from ms_yj01 a,ms_yj02 b, ms_brda c where fygb in (7,45) and yblb='2' and zfpb<>1 and b.yjxh=a.yjxh and c.id=a.id";
	}
	else
	{
		return 0;//数据源不对
	}
    ////
    if (_requestid != "")
        sHisQuery = InsertWhereCondition(sHisQuery, "a.fphm = '" + _requestid + "'");
    if (_patientname != "")
        sHisQuery = AddWhereCondition(sHisQuery, "a.brxm = '" + _patientname + "'");    
	////
    if (_examclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, "charindex('" + _examclass + "',b.fymc)>0");
//    if (_examsubclass != "")
//        sHisQuery = AddWhereCondition(sHisQuery, "a.EXAM_SUB_CLASS = '" + _examsubclass + "'");
	////
	String sStartDate = FormatDateTime("yyyy-mm-dd 0:0:0", _startdate);
	if ( sStartDate >= "2000-01-01" )
		sHisQuery = AddWhereCondition(sHisQuery, "a.kdrq >= '" + sStartDate + "'" );
    String sEndDate = FormatDateTime("yyyy-mm-dd 23:59:59", _enddate);
	if ( sEndDate >= "2000-01-01" )
		sHisQuery = AddWhereCondition(sHisQuery, "a.kdrq <= '" + sEndDate + "'" );
    ////
    _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 (!_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;
}
//---------------------------------------------------------------------------

int ExecSQL()
{
//	if (_requestid != "")
//	{
//		if (PatsInHospital(_requestid))
//		{
//			ShowMessage("不是门诊病人，请重新查询！");
//			return 0;
//		}
//	}
// 这里是发票号或病历卡（ 所以不存在非门诊问题（发票号还包括急诊的）

	String sHisQuery = "";
	////
	if (!IsConnetSQLServer(_hisconnection))
		return 0;//数据源不对
		
    ////
    String sBRXM = "";
    String sBRXB = "0";	//numeric
    String sCSNY = "";	//date
    String sAge = "";
    String sHKDZ = "";
    String sJTDH = "";
    String sID = "0";	//numeric
    String sKDRQ = "";	//date
    String sFPHM = "";
    String sYGXM = "";
    String sKSMC = "";
    String sMZHM = "";
	    
    int iProgStep = 10;
    if (_requestid != "")
    {
    	if ( !IsNumString (_requestid) )
    		iProgStep = 22;
    	else
        	sHisQuery = "SELECT top 1 ID,BRXB,CSNY,HKDZ,JTDH,datediff(yy,CSNY,getdate()) as Age FROM MS_BRDA WHERE MZHM='" + _requestid +"'";
    }
    else
    {
	    if (_patientname != "")
	    {
	    	return ExecSQL_OLD();
	    }
	    else    
        	iProgStep = 23;//测试用
    }
	////
	if ( iProgStep == 10 )
	{
	    _qryHisCustom.Close();
	    _qryHisCustom.SQL.Text = sHisQuery;
	    try
	    {
	        _qryHisCustom.Open();
	    }
	    except
	    {
	        ShowMessage("查询失败！请检查输入条件！");
	        return 0;
	    }
	    if (_qryHisCustom.IsEmpty())
	    {
	    	if( iProgStep == 11 ) //按姓名未找到
	    		return 0; 
	    	else //输入的是发票号
	    	{
	    		iProgStep = 22;
	    	}
	    }
        else
        {
	       _qryHisCustom.First();
	        if (!_qryHisCustom.FieldByName("ID").IsNull)
	        	sID = _qryHisCustom.FieldByName("ID").Value;
	        if (!_qryHisCustom.FieldByName("BRXB").IsNull)
	        	sBRXB = _qryHisCustom.FieldByName("BRXB").Value;
	        if (!_qryHisCustom.FieldByName("CSNY").IsNull)
	        	sCSNY = _qryHisCustom.FieldByName("CSNY").Value;
	        if (!_qryHisCustom.FieldByName("HKDZ").IsNull)
	        	sHKDZ = _qryHisCustom.FieldByName("HKDZ").Value;
	        if (!_qryHisCustom.FieldByName("JTDH").IsNull)
	        	sJTDH = _qryHisCustom.FieldByName("JTDH").Value;
	        sAge = _qryHisCustom.FieldByName("Age").Value;
	        sMZHM = _requestid;
	        iProgStep = 21;
	    }
	}

	if ( iProgStep == 21 )
	{
		sHisQuery = "SELECT YJXH,ID FROM MS_YJ01 WHERE ID=" + sID;
		////
		String sStartDate = FormatDateTime("yyyy-mm-dd 0:0:0", _startdate);
		if ( sStartDate >= "2000-01-01" )
			sHisQuery = AddWhereCondition(sHisQuery, "KDRQ >= '" + sStartDate + "'" );
	    String sEndDate = FormatDateTime("yyyy-mm-dd 23:59:59", _enddate);
		if ( sEndDate >= "2000-01-01" )
			sHisQuery = AddWhereCondition(sHisQuery, "KDRQ <= '" + sEndDate + "'" );
	}
	else if ( iProgStep == 22 )
		sHisQuery = "SELECT YJXH,ID FROM MS_YJ01 WHERE FPHM=" + _requestid;
	else if ( iProgStep == 23 )
		sHisQuery = "SELECT top 1 YJXH,ID FROM MS_YJ01 WHERE GETDATE()-KDRQ<1";
	else
		return 0;
	sHisQuery = AddWhereCondition(sHisQuery, "ZFPB=0");//未作废发票
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery;
    try
    {
        _qryHisCustom.Open();
    }
    except
    {
        ShowMessage("查询失败！请检查输入条件！");
        return 0;
    }

    if (_qryHisCustom.IsEmpty())
    	return 0;
    ////
	String sYJXH="";
	String sYJXH_ALL = "(";
    _qryHisCustom.First();
    int iFound = 0;
    while (!_qryHisCustom.Eof)
    {
		if ( (sID == "0") && !_qryHisCustom.FieldByName("ID").IsNull )
        	sID = _qryHisCustom.FieldByName("ID").Value;
        ////
        if (!_qryHisCustom.FieldByName("YJXH").IsNull)
        {
        	sYJXH = _qryHisCustom.FieldByName("YJXH").Value;
        	sYJXH_ALL += sYJXH + ",";
        	iFound = 1;
        }
        _qryHisCustom.Next();
    }
    if ( iFound == 0 )
    	return 0;

	Delete(sYJXH_ALL, Length( sYJXH_ALL ),1);
	sYJXH_ALL += ")";
	
	if ( ((iProgStep == 22) || (iProgStep == 23)) && (sID != "0") )
	{
		sHisQuery = "SELECT top 1 ID,BRXB,CSNY,HKDZ,JTDH,datediff(yy,CSNY,getdate()) as Age FROM MS_BRDA WHERE ID=" + sID;
		////	
	    _qryHisCustom.Close();
	    _qryHisCustom.SQL.Text = sHisQuery;

	    try
	    {
	        _qryHisCustom.Open();
	    }
	    except
	    {
	        ShowMessage("查询失败！请检查输入条件！");
	        return 0;
	    }
	    if ( !_qryHisCustom.IsEmpty() )
	    {
	        _qryHisCustom.First();
	        if (!_qryHisCustom.FieldByName("ID").IsNull)
	        	sID = _qryHisCustom.FieldByName("ID").Value;
	        if (!_qryHisCustom.FieldByName("BRXB").IsNull)
	        	sBRXB = _qryHisCustom.FieldByName("BRXB").Value;
	        if (!_qryHisCustom.FieldByName("CSNY").IsNull)
	        	sCSNY = _qryHisCustom.FieldByName("CSNY").Value;
	        if (!_qryHisCustom.FieldByName("HKDZ").IsNull)
	        	sHKDZ = _qryHisCustom.FieldByName("HKDZ").Value;
	        if (!_qryHisCustom.FieldByName("JTDH").IsNull)
	        	sJTDH = _qryHisCustom.FieldByName("JTDH").Value;
	        sAge = _qryHisCustom.FieldByName("Age").Value;
	    }
	}

	// iProgStep = 30
	sHisQuery = "SELECT YJXH,YLXH,YLDJ*YLSL as JE FROM MS_YJ02 WHERE YJXH in "+ sYJXH_ALL + " and FYGB in(7,45)";
	if (_examclass != "")
	{
		if ( _examclass == "DR" )
        	sHisQuery = AddWhereCondition(sHisQuery, "YLXH in (30313,290)");
		else if ( _examclass == "CT" )
        	sHisQuery = AddWhereCondition(sHisQuery, "YLXH in (320,329,332,30177)");
		else if ( _examclass == "CR" )
        	sHisQuery = AddWhereCondition(sHisQuery, "YLXH in (289,290)");
    }
    ////
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery;
    //fDR_SUM
    try
    {
        _qryHisCustom.Open();
    }
    except
    {
        ShowMessage("查询失败！请检查输入条件！");
        return 0;
    }
    ////
    if (_qryHisCustom.IsEmpty())
    	return 0;

    _qryHisCustom.First();
    int iYLXH = 0;
    int iYJXH_ini = _qryHisCustom.FieldByName("YJXH").Value;
    int iYJXH_cur = 0;
    float fJE = 0.0;
	float fCT_SUM = 0.0;//numeric
	float fDR_SUM = 0.0;//numeric
	float fCR_SUM = 0.0;//numeric
	String sPatientSex="";

    while (!_qryHisCustom.Eof)
    {
    	iYJXH_cur = _qryHisCustom.FieldByName("YJXH").Value;
    	if ( iYJXH_cur != iYJXH_ini )
    	{
    		//输出
			// iProgStep = 40
			sHisQuery = "SELECT top 1 a.FPHM,a.BRXM,a.KDRQ,b.KSMC,c.YGXM FROM MS_YJ01 a, GY_KSDM b, GY_YGDM c WHERE YJXH="
				+ IntToStr(iYJXH_ini) + " and b.KSDM=a.KSDM and c.YGDM=a.YSDM";
		    _qryHisCommon.Close();
		    _qryHisCommon.SQL.Text = sHisQuery;
		    _qryHisCommon.Open();
		    if (!_qryHisCommon.IsEmpty())
		    {
			    _qryHisCommon.First();
				sFPHM = _qryHisCommon.FieldByName("FPHM").Value;
	        	sBRXM = _qryHisCommon.FieldByName("BRXM").Value;
				sKDRQ = _qryHisCommon.FieldByName("KDRQ").Value;
				sKSMC = _qryHisCommon.FieldByName("KSMC").Value;
				sYGXM = _qryHisCommon.FieldByName("YGXM").Value;
			}
			////
			if ( sBRXB == "1" )
				sPatientSex = "男";
			else if ( sBRXB == "2" )
				sPatientSex = "女";
				
		    if ( fDR_SUM > 0.0 )
		    {
		        _qryHisStudy.Append();
				_qryHisStudy.FieldByName("study_examkind").Value = "DX";
				_qryHisStudy.FieldByName("study_examname").Value = "数字化摄影(DR)";
				_qryHisStudy.FieldByName("study_charge").Value = fDR_SUM;
				////
				_qryHisStudy.FieldByName("study_source").Value = "门诊";
				//_qryHisStudy.FieldByName("patient_guid").Value = sID;//自动编号
				_qryHisStudy.FieldByName("patient_name").Value = sBRXM;
				_qryHisStudy.FieldByName("patient_sex").Value = sPatientSex;
				if (sCSNY != "")
				_qryHisStudy.FieldByName("patient_birthday").Value = sCSNY;
				_qryHisStudy.FieldByName("patient_studyage").Value = sAge;
				_qryHisStudy.FieldByName("patient_telephone").Value = sJTDH;
				_qryHisStudy.FieldByName("patient_address").Value = sHKDZ;
				_qryHisStudy.FieldByName("study_number0").Value = sFPHM;
				_qryHisStudy.FieldByName("study_number1").Value = sMZHM;
				_qryHisStudy.FieldByName("study_dttm").Value = sKDRQ;
				_qryHisStudy.FieldByName("study_department").Value = sKSMC;
				_qryHisStudy.FieldByName("study_applicant").Value = sYGXM;
			   _qryHisStudy.Post();
		   	}
		
		    if ( fCT_SUM > 0.0 )
		    {

		        _qryHisStudy.Append();
				_qryHisStudy.FieldByName("study_examkind").Value = "CT";
				_qryHisStudy.FieldByName("study_examname").Value = "X线计算机体层(CT)螺旋扫描";
				_qryHisStudy.FieldByName("study_charge").Value = fCT_SUM;
				////
				_qryHisStudy.FieldByName("study_source").Value = "门诊";
				//_qryHisStudy.FieldByName("patient_guid").Value = sID;//自动编号
				_qryHisStudy.FieldByName("patient_name").Value = sBRXM;
				_qryHisStudy.FieldByName("patient_sex").Value = sPatientSex;
				if (sCSNY != "")
				_qryHisStudy.FieldByName("patient_birthday").Value = sCSNY;
				_qryHisStudy.FieldByName("patient_studyage").Value = sAge;
				_qryHisStudy.FieldByName("patient_telephone").Value = sJTDH;
				_qryHisStudy.FieldByName("patient_address").Value = sHKDZ;
				_qryHisStudy.FieldByName("study_number0").Value = sFPHM;
				_qryHisStudy.FieldByName("study_number1").Value = sMZHM;
				_qryHisStudy.FieldByName("study_dttm").Value = sKDRQ;
				_qryHisStudy.FieldByName("study_department").Value = sKSMC;
				_qryHisStudy.FieldByName("study_applicant").Value = sYGXM;
			   _qryHisStudy.Post();
		   	}
		
		    if ( fCR_SUM > 0.0 )
		    {
		        _qryHisStudy.Append();
				_qryHisStudy.FieldByName("study_examkind").Value = "DX";
				_qryHisStudy.FieldByName("study_examname").Value = "数字化摄影(CR)";
				_qryHisStudy.FieldByName("study_charge").Value = fCR_SUM;
				////
				_qryHisStudy.FieldByName("study_source").Value = "门诊";
				//_qryHisStudy.FieldByName("patient_guid").Value = sID;//自动编号
				_qryHisStudy.FieldByName("patient_name").Value = sBRXM;
				_qryHisStudy.FieldByName("patient_sex").Value = sPatientSex;
				if (sCSNY != "")
				_qryHisStudy.FieldByName("patient_birthday").Value = sCSNY;
				_qryHisStudy.FieldByName("patient_studyage").Value = sAge;
				_qryHisStudy.FieldByName("patient_telephone").Value = sJTDH;
				_qryHisStudy.FieldByName("patient_address").Value = sHKDZ;
				_qryHisStudy.FieldByName("study_number0").Value = sFPHM;
				_qryHisStudy.FieldByName("study_number1").Value = sMZHM;
				_qryHisStudy.FieldByName("study_dttm").Value = sKDRQ;
				_qryHisStudy.FieldByName("study_department").Value = sKSMC;
				_qryHisStudy.FieldByName("study_applicant").Value = sYGXM;
			   _qryHisStudy.Post();
		   	}
    		
    		//初始化
    		fDR_SUM = 0.0;
    		fCT_SUM = 0.0;
    		fCR_SUM = 0.0;
    		sPatientSex="";
    		iYJXH_ini = iYJXH_cur;
    	}
    		
    	fJE = _qryHisCustom.FieldByName("JE").Value;
    	iYLXH = _qryHisCustom.FieldByName("YLXH").Value;
    	if ( iYLXH == 289 )
    		fCR_SUM += fJE;
    	else if ( iYLXH == 30313 )
    		fDR_SUM += fJE;
    	else if ( iYLXH == 290 )
    	{
    		if ( fCR_SUM > 0.0 )
    			fCR_SUM += fJE;
    		else
	    		fDR_SUM += fJE;
    	}
    	else if ( (iYLXH == 330) || (iYLXH == 329) || (iYLXH == 332) || (iYLXH == 30177) )
    		fCT_SUM += fJE;
    	
        _qryHisCustom.Next();
    }

	//输出
	// iProgStep = 40
	sHisQuery = "SELECT top 1 a.FPHM,a.BRXM,a.KDRQ,b.KSMC,c.YGXM FROM MS_YJ01 a, GY_KSDM b, GY_YGDM c WHERE YJXH="
		+ IntToStr(iYJXH_ini) + " and b.KSDM=a.KSDM and c.YGDM=a.YSDM";
    _qryHisCommon.Close();
    _qryHisCommon.SQL.Text = sHisQuery;
    _qryHisCommon.Open();
    if (!_qryHisCommon.IsEmpty())
    {
	    _qryHisCommon.First();
		sFPHM = _qryHisCommon.FieldByName("FPHM").Value;
    	sBRXM = _qryHisCommon.FieldByName("BRXM").Value;
		sKDRQ = _qryHisCommon.FieldByName("KDRQ").Value;
		sKSMC = _qryHisCommon.FieldByName("KSMC").Value;
		sYGXM = _qryHisCommon.FieldByName("YGXM").Value;
	}
	////
	if ( sBRXB == "1" )
		sPatientSex = "男";
	else if ( sBRXB == "2" )
		sPatientSex = "女";
		
    if ( fDR_SUM > 0.0 )
    {
        _qryHisStudy.Append();
		_qryHisStudy.FieldByName("study_examkind").Value = "DX";
		_qryHisStudy.FieldByName("study_examname").Value = "数字化摄影(DR)";
		_qryHisStudy.FieldByName("study_charge").Value = fDR_SUM;
		////
		_qryHisStudy.FieldByName("study_source").Value = "门诊";
		//_qryHisStudy.FieldByName("patient_guid").Value = sID;//自动编号
		_qryHisStudy.FieldByName("patient_name").Value = sBRXM;
		_qryHisStudy.FieldByName("patient_sex").Value = sPatientSex;
		if (sCSNY != "")
		_qryHisStudy.FieldByName("patient_birthday").Value = sCSNY;
		_qryHisStudy.FieldByName("patient_studyage").Value = sAge;
		_qryHisStudy.FieldByName("patient_telephone").Value = sJTDH;
		_qryHisStudy.FieldByName("patient_address").Value = sHKDZ;
		_qryHisStudy.FieldByName("study_number0").Value = sFPHM;
		_qryHisStudy.FieldByName("study_number1").Value = sMZHM;
		_qryHisStudy.FieldByName("study_dttm").Value = sKDRQ;
		_qryHisStudy.FieldByName("study_department").Value = sKSMC;
		_qryHisStudy.FieldByName("study_applicant").Value = sYGXM;
	   _qryHisStudy.Post();
   	}

    if ( fCT_SUM > 0.0 )
    {
        _qryHisStudy.Append();
		_qryHisStudy.FieldByName("study_examkind").Value = "CT";
		_qryHisStudy.FieldByName("study_examname").Value = "X线计算机体层(CT)螺旋扫描";
		_qryHisStudy.FieldByName("study_charge").Value = fCT_SUM;
		////
		_qryHisStudy.FieldByName("study_source").Value = "门诊";
		//_qryHisStudy.FieldByName("patient_guid").Value = sID;//自动编号
		_qryHisStudy.FieldByName("patient_name").Value = sBRXM;
		_qryHisStudy.FieldByName("patient_sex").Value = sPatientSex;
		if (sCSNY != "")
		_qryHisStudy.FieldByName("patient_birthday").Value = sCSNY;
		_qryHisStudy.FieldByName("patient_studyage").Value = sAge;
		_qryHisStudy.FieldByName("patient_telephone").Value = sJTDH;
		_qryHisStudy.FieldByName("patient_address").Value = sHKDZ;
		_qryHisStudy.FieldByName("study_number0").Value = sFPHM;
		_qryHisStudy.FieldByName("study_number1").Value = sMZHM;
		_qryHisStudy.FieldByName("study_dttm").Value = sKDRQ;
		_qryHisStudy.FieldByName("study_department").Value = sKSMC;
		_qryHisStudy.FieldByName("study_applicant").Value = sYGXM;
	   _qryHisStudy.Post();
   	}

    if ( fCR_SUM > 0.0 )
    {
        _qryHisStudy.Append();
		_qryHisStudy.FieldByName("study_examkind").Value = "DX";
		_qryHisStudy.FieldByName("study_examname").Value = "数字化摄影(CR)";
		_qryHisStudy.FieldByName("study_charge").Value = fCR_SUM;
		////
		_qryHisStudy.FieldByName("study_source").Value = "门诊";
		//_qryHisStudy.FieldByName("patient_guid").Value = sID;//自动编号
		_qryHisStudy.FieldByName("patient_name").Value = sBRXM;
		_qryHisStudy.FieldByName("patient_sex").Value = sPatientSex;
		if (sCSNY != "")
		_qryHisStudy.FieldByName("patient_birthday").Value = sCSNY;
		_qryHisStudy.FieldByName("patient_studyage").Value = sAge;
		_qryHisStudy.FieldByName("patient_telephone").Value = sJTDH;
		_qryHisStudy.FieldByName("patient_address").Value = sHKDZ;
		_qryHisStudy.FieldByName("study_number0").Value = sFPHM;
		_qryHisStudy.FieldByName("study_number1").Value = sMZHM;
		_qryHisStudy.FieldByName("study_dttm").Value = sKDRQ;
		_qryHisStudy.FieldByName("study_department").Value = sKSMC;
		_qryHisStudy.FieldByName("study_applicant").Value = sYGXM;
	   _qryHisStudy.Post();
   	}

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

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

int _ConfirmHisLogin()
{
    // 登记检查
	MessageBox("没有配置确认登记功能！", "电子申请单", 1);
    return 0;
}
//---------------------------------------------------------------------------

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

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

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