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

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

int ExecProc()
{
    int iFieldCount = 0;
    String sFieldPairs[2][100];
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name", 		"xm");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex", 		"xb");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_studyage", 	"nl");
    SetArrayItem(sFieldPairs, iFieldCount, "study_examname", 	"xmmc");
    SetArrayItem(sFieldPairs, iFieldCount, "study_clinicinfo", 	"zdmc");
    SetArrayItem(sFieldPairs, iFieldCount, "study_department", "kdksm");
    SetArrayItem(sFieldPairs, iFieldCount, "study_applicant", 	"kdysm");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number0", 	"sqdh");
    SetArrayItem(sFieldPairs, iFieldCount, "study_dttm", 		"sqrq");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_type", 		"");
    SetArrayItem(sFieldPairs, iFieldCount, "study_exammethod", 	"xmmc");

    // 调用存储过程
    TADOStoredProc spHisCustom = new TADOStoredProc(nil);
    spHisCustom.Connection = _qryHisCustom.Connection;
    spHisCustom.Parameters.Clear();
    spHisCustom.ProcedureName = "p_tjjcxx";
    // 为存储过程准备参数
    AddParameter(spHisCustom, "sqdh", ftString, 1, 8, 	_requestid);
    AddParameter(spHisCustom, "jdh", ftString, 1, 8, 	nil);
    AddParameter(spHisCustom, "ksrq", ftDateTime, 1, 0, _startdate);
    AddParameter(spHisCustom, "zzrq", ftDateTime, 1, 0, _enddate);
    AddParameter(spHisCustom, "jclx", ftString, 1, 4, 	"US");
    AddParameter(spHisCustom, "return", ftInteger, 2, 0, nil);
    // 执行过程并检查返回值
    spHisCustom.Open();
    Variant vResult = GetParameterValue(spHisCustom, "return");
    if (vResult.VInteger <= 0)
    {
        ShowMessage("没有查询到该病人信息!");
        return 0;
    }
    if (!spHisCustom.IsEmpty())
    {
        spHisCustom.First();
        while (!spHisCustom.Eof)
        {
            _qryHisStudy.Append();
            for (int i = 0; i < iFieldCount; i++)
            {
                if (!spHisCustom.FieldByName(sFieldPairs[1][i]).IsNull)
                {
                    if (sFieldPairs[0][i] == "patient_sex")
                    {
                        String sPatientSex = spHisCustom.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
                    {
                        _qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = spHisCustom.FieldByName(sFieldPairs[1][i]).Value;
                    }
                }
            }
            _qryHisStudy.Post();
            spHisCustom.Next();
        }
    }
    delete spHisCustom;

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

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

int _ConfirmHisLogin()
{
    // 登记检查
    return 0;
}
//---------------------------------------------------------------------------

int _CancelHisLogin()
{
    // 撤销登记
    return 0;
}
//---------------------------------------------------------------------------

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

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