//---------------------------------------------------------------------------
// 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()
{
    String sHisQuery = "";
    ////
    int iFieldCount = 0;
    String sFieldPairs[2][100];
    // patient_guid => 患者条码号
    SetArrayItem(sFieldPairs, iFieldCount, "patient_guid", "病人唯一ID");
    // 姓名
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name", "病人姓名");
    // 性别
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex", "病人性别");
    // 年龄
    SetArrayItem(sFieldPairs, iFieldCount, "patient_birthday", "出生年月");
    // 门诊号 => 门诊条码号
    SetArrayItem(sFieldPairs, iFieldCount, "study_number1", "门诊号");
	// 申请单号 => 申请单号
    SetArrayItem(sFieldPairs, iFieldCount, "study_number0", "Card_Hardwareno");
	// 送检日期 => 送检日期
    SetArrayItem(sFieldPairs, iFieldCount, "study_dttm", "送检日期");
    // 送检科室 => 送检科室
    SetArrayItem(sFieldPairs, iFieldCount, "study_department", "送检科室");
    // 送检医生  => 送检医生(开立医生姓名)
    SetArrayItem(sFieldPairs, iFieldCount, "study_applicant", "送检医生");
	// 民族  => 病人民族
    SetArrayItem(sFieldPairs, iFieldCount, "patient_nation", "病人民族");
	// 收费金额  => sum_cost
    SetArrayItem(sFieldPairs, iFieldCount, "study_charge", "sum_cost");
	// 卡号 => Card_Hardwareno
    //SetArrayItem(sFieldPairs, iFieldCount, "study_applicant", "Card_Hardwareno");
    // 检查项目 => 申请项目名称
    SetArrayItem(sFieldPairs, iFieldCount, "study_examname", "申请项目名称");
    ////
    if (IsConnetAccessDB(_hisconnection) || IsConnetSQLServer(_hisconnection))
    {
		sHisQuery = "select  a.门诊号,a.病人民族,a.sum_cost,a.Card_Hardwareno,a.病人姓名,a.病人唯一id, a.病人性别, a.出生年月, a.申请单号, a.送检日期, a.申请项目名称, a.送检科室, a.送检医生 " +
			"from view_agfa_pax_cli_US a order by a.送检日期 desc";
    }
    else
    {
		sHisQuery = "select distinct a.pation_no, a.checkslip, a.Card_Hardwareno,a.deptname, a.item, a.docname, b.patient_code, b.patient_name, b.sex, b.birthday " +
			"from wxhis.view_checkslip a, wxhis.ut_patient_cost b " +
			"where a.recipe_no = b.recipe_no";
    }
    
    // patient_kind	!!string 患者来源（1门诊 0住院）
	// sHisQuery = AddWhereCondition(sHisQuery, "patient_kind = '1'");
    ////
    if (_requestid != "")
        sHisQuery = AddWhereCondition(sHisQuery, "a.Card_Hardwareno = '" + _requestid + "'");
    if (_patientname != "")
        sHisQuery = AddWhereCondition(sHisQuery, "a.病人姓名 = '" + _patientname + "'");
/*
    if (_examclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, " = '" + _examclass + "'");
    if (_examsubclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, " = '" + _examsubclass + "'");
*/
    String sStartDate = FormatDateTime("yyyy-mm-dd 0:0:0", _startdate);
    String sEndDate = FormatDateTime("yyyy-mm-dd 23:59:59", _enddate);
    if (IsConnetAccessDB(_hisconnection) || IsConnetSQLServer(_hisconnection))
    {
		if (_startdate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, "a.送检日期  >=  #"+sStartDate+"#");
		if (_enddate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, "a.送检日期  <=  #"+sEndDate+"#");
		if (IsConnetSQLServer(_hisconnection))
			sHisQuery = AccessToSQL(sHisQuery);
		////
        _qryHisCustom.ParamCheck = false; // !!!
    }
    else
    {
		if (_startdate != 0)
				sHisQuery = AddWhereCondition(sHisQuery, "a.送检日期 >= to_date('" + sStartDate + "', 'yyyy-mm-dd hh24:mi:ss')");
    	if (_enddate != 0)
        	sHisQuery = AddWhereCondition(sHisQuery, "a.送检日期 <= to_date('" + sEndDate + "', 'yyyy-mm-dd hh24:mi:ss')");
        ////  	
        sHisQuery = AddWhereCondition(sHisQuery, "ROWNUM<=100");
    }
    ////
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery;
    try
    {
        _qryHisCustom.Open();
    }
    except
    {
        ShowMessage("查询失败！请检查输入条件！");
        return 0;
    }
    ////
    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")
                    {
                        _qryHisStudy.FieldByName("patient_studyage").Value =
                            CalcStudyAge(_qryHisCustom.FieldByName(sFieldPairs[1][i]).Value, Now());
                        _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 _QueryHisTable()
{
    // 查询脚本
    return ExecSQL();
}
//---------------------------------------------------------------------------

int _ConfirmHisLogin()
{
    // 登记检查
    MessageBox("尚未配置！", "电子申请单", 1);
    return 0;
}
//---------------------------------------------------------------------------

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

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

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