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

//---------------------------------------------------------------------------
// 自定义变量
String g_sExecDeptCode = "";

//---------------------------------------------------------------------------
int ExecSQL()
{
	g_sExecDeptCode = "0035"; // 执行科室代码, 请根据现场情况修改!!!
	////
    String sHisQuery = "";
    int iFieldCount = 0;
    String sFieldPairs[2][100];
    ////
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name", 		"name");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex", 		"sex_code");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_identity", 	"idenno"); 
    SetArrayItem(sFieldPairs, iFieldCount, "patient_birthday", 	"birthday");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_telephone", "work_tel");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_state", 	"patient_state");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number0", 	"inpatient_no"); // 住院流水号
    SetArrayItem(sFieldPairs, iFieldCount, "study_number1", 	"");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number2", 	"patient_no"); // 住院号 例如：0000053564
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickbed", 	"bed_no"); // 病床号 例如：N014902-1   
    SetArrayItem(sFieldPairs, iFieldCount, "study_department", 	"dept_name");    
    SetArrayItem(sFieldPairs, iFieldCount, "study_applicant", 	"study_applicant");
    SetArrayItem(sFieldPairs, iFieldCount, "study_examname", 	"");
    SetArrayItem(sFieldPairs, iFieldCount, "study_bodypart", 	"");
    SetArrayItem(sFieldPairs, iFieldCount, "study_clinicinfo", 	"clinic_diagnose"); 
    SetArrayItem(sFieldPairs, iFieldCount, "study_payment", 	"study_payment");
    ////
    if (IsConnetAccessDB(_hisconnection) || IsConnetSQLServer(_hisconnection))
    {
    	// 不支持
    }
    else
    {
    	// trans_type: 1?
		// in_state: R-住院登记,I-病房接诊,B-出院登记,O-出院结算,P-预约出院,N-无费退院			
		// exec_opercode: 执行人为空，表示未确认的检查。
		sHisQuery = "select a.patient_no, a.inpatient_no, a.name, a.sex_code, a.birthday, a.work_tel, a.out_date, " +
			" a.idenno, a.in_date, a.dept_name, a.bed_no, a.card_no, a.diag_name, a.clinic_diagnose, " +
			" b.inpatient_no, b.package_name, b.exec_opercode, b.fee_date,  " + 
			" (select e.empl_name from com_employee e where e.empl_code=b.recipe_doccode) as study_applicant, " + 			
			" (case when exec_opercode is null then -2 else -1 end) as patient_state, " + 
			" to_char(b.fee_date, 'yyyy-mm-dd') as study_payment " + 
			" from fin_ipb_itemlist b, fin_ipr_inmaininfo a " +
			" where a.inpatient_no = b.inpatient_no " +
			" and trans_type = '1' " + 
			" and (a.in_state = 'R' or a.in_state = 'I') " +
			" and (b.sequence_no = '1' or b.sequence_no = '3')" +
			" and b.mo_order is not null " +
			//" and b.qty = b.noback_num " +
			" and b.execute_deptcode = '" + g_sExecDeptCode + "'";
    }
    ////
    if (_requestid != "")
    {
    	int a[1];
    	try
    	{
    		a[0] = StrToInt(_requestid);
    	}
    	except
    	{
    		ShowMessage("查询条件输入错误！");
    		return 0;
    	}
    	
    	String sID = _requestid;
    	if (Length(_requestid) < 10)
    	{
    		
    		sID = Format("%.10d", a);
    	}
    	    	
        sHisQuery = AddWhereCondition(sHisQuery, "a.patient_no = '" + sID + "'");
    }
    if (_patientname != "")
        sHisQuery = AddWhereCondition(sHisQuery, "a.name = '" + _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))
    {
    	// 不支持
    }
    else
    {
    	// 计费日期
		if (_startdate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, " b.fee_date >= to_date('" + sStartDate + "', 'yyyy-mm-dd hh24:mi:ss')");
		if (_enddate != 0)
			sHisQuery = AddWhereCondition(sHisQuery, " b.fee_date <= to_date('" + sEndDate   + "', 'yyyy-mm-dd hh24:mi:ss')"); 
        ////        
		sHisQuery = AddWhereCondition(sHisQuery, " ROWNUM<=100 "); 
		sHisQuery = sHisQuery + " group by a.patient_no, a.inpatient_no, a.name, a.sex_code, a.birthday, a.work_tel, a.out_date, " +
                 "a.idenno, a.in_date, a.dept_name, a.bed_no, a.card_no, a.diag_name, a.clinic_diagnose, " +
                 "b.inpatient_no, b.package_name, b.exec_opercode, b.fee_date, b.recipe_doccode ";  	
    }
    ////
    _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 (sFieldPairs[1][i] == "")
            		continue;
                if (!_qryHisCustom.FieldByName(sFieldPairs[1][i]).IsNull)
                {
                    if (sFieldPairs[0][i] == "patient_sex")
                    {
                        String sPatientSex = _qryHisCustom.FieldByName(sFieldPairs[1][i]).Value;
                        if ((sPatientSex == "男") || (sPatientSex == "M"))
                            _qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = "男";
                        else if ((sPatientSex == "女") || (sPatientSex == "F"))
                            _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 if (sFieldPairs[0][i] == "study_sickbed")
                    {
                    	  // 格式化病床号
                        String sSickbed = _qryHisCustom.FieldByName(sFieldPairs[1][i]).Value;
                        int iLength = Length(sSickbed);
                        if (iLength > 5)
                        {
                        	Delete(sSickbed, 1, iLength - 5);
                        	Insert("-", sSickbed, 4);
                        }
                        _qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = sSickbed;
                    }
                    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.
}
//---------------------------------------------------------------------------
