//---------------------------------------------------------------------------
// 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 ExecSQL()
{
    int iFieldCount = 0;
    string sFieldPairs[2][100];
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name", 		"hzxm");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex", 		"sex");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_birthday", 	"birth");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_address", 	"lxrdz");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_telephone", "lxrdh");
    SetArrayItem(sFieldPairs, iFieldCount, "study_userdef1", 	"fpxmmc");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number2", 	"blh");
    SetArrayItem(sFieldPairs, iFieldCount, "study_charge", 		"xmje");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number3", 	"cwdm");
    SetArrayItem(sFieldPairs, iFieldCount, "study_exammethod", 	"dxmmc");
    SetArrayItem(sFieldPairs, iFieldCount, "study_userdef2", 	"jsxh");
    ////
    string sHisQuery = "SELECT top 100 a.blh as zy_number,a.hzxm,cast(a.sex as varchar(2))as sex, cast(a.birth as datetime)as birth,a.blh, a.lxrdh ,a.lxrdz,c.jsxh,c.xmje,c.dxmmc,c.fpxmmc,b.cwdm,a.lrrq from ZY_BRSYK as a,ZY_BRFYMXK as b,ZY_BRJSMXK as c where a.syxh=b.syxh and c.dxmdm='04' and b.jsxh=c.jsxh";
    ////
    if (_requestid != "")
        sHisQuery = AddWhereCondition(sHisQuery, "blh = '" + _requestid + "'");
    if (_patientname != "")
        sHisQuery = AddWhereCondition(sHisQuery, "hzxm = '" + _patientname + "'");
/*
    if (_examclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, );
    if (_examsubclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, );
    if (_startdate != "")
        sHisQuery = AddWhereCondition(sHisQuery, );
    if (_enddate != "")
        sHisQuery = AddWhereCondition(sHisQuery, );
*/
    ////
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery;
    _qryHisCustom.Open();
    ////
    if (!_qryHisCustom.IsEmpty())
    {
        _qryHisCustom.First();
        while (!_qryHisCustom.Eof)
        {
            _qryHisStudy.Append();
            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")
                    {
                        _qryHisStudy.FieldByName(sFieldPairs[0][i]).Value =
                            _qryHisCustom.FieldByName(sFieldPairs[1][i]).Value;
                        _qryHisStudy.FieldByName("patient_studyage").Value =
                            CalcStudyAge(_qryHisCustom.FieldByName(sFieldPairs[1][i]).Value, Now());
                    }
                    else
                    {
                        _qryHisStudy.FieldByName(sFieldPairs[0][i]).Value =
                            _qryHisCustom.FieldByName(sFieldPairs[1][i]).Value;
                    }
                }
            }
            _qryHisStudy.Post();
            _qryHisCustom.Next();
        }
    }

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

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

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

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

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

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