//---------------------------------------------------------------------------
// 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", "..\\Common\\LZCommon.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];
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name",		"BRXM");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex",		"XB");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_birthday",	"CHUSHENGRQ");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_studyage",	"NL");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_address",	"JIATINGDZ");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_telephone",	"DIANHUAHM");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_guid",		"BRXH");
    //SetArrayItem(sFieldPairs, iFieldCount, "study_guid",		"JCDXH");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number0",		"JCDXH");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_type",		"PATIENT_TYPE");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number1",		"MENZHENH");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number2",		"ZHUYUANH");
    SetArrayItem(sFieldPairs, iFieldCount, "study_examname", 	"EXAM_NAMES");
    SetArrayItem(sFieldPairs, iFieldCount, "study_ordinal", 	"YEBZ");
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickregion", 	"DQBQ");
    SetArrayItem(sFieldPairs, iFieldCount, "study_sickroom", 	"DQKS");
    SetArrayItem(sFieldPairs, iFieldCount, "login_extinfo1", 	"SHENQINGLY");
    SetArrayItem(sFieldPairs, iFieldCount, "study_applicant", 	"KDYS");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number3", 	"DQCW");
    SetArrayItem(sFieldPairs, iFieldCount, "study_bodypart", 	"JCDXH");
    SetArrayItem(sFieldPairs, iFieldCount, "study_department", 	"KDKS");
    SetArrayItem(sFieldPairs, iFieldCount, "study_userdef1", 	"JZBZ");
    SetArrayItem(sFieldPairs, iFieldCount, "study_userdef2", 	"SFCB");
    ////
	if (IsConnetAccessDB(_hisconnection))
	{
	    sHisQuery = "SELECT TOP 100 BRXM,XB,CHUSHENGRQ,NL,JIATINGDZ,DIANHUAHM,BRXH,JCDXH,SHENQINGLY,KDYS,DQCW,KDKS,SFCB,JZBZ,"+
	    	"iif(BINGRENLX='I','住院', iif(BINGRENLX='O','门诊', iif(BINGRENLX='J','急诊', iif(BINGRENLX='Y','预约')))) AS PATIENT_TYPE, "+
	    	"DJ, MENZHENH, ZHUYUANH, XMMC AS EXAM_NAMES,YEBZ,DQBQ,DQKS FROM YZ_JCSQD WHERE STATUS=4 AND DJBZ=0";
	}
	else
	{
	    sHisQuery = "SELECT BRXM,XB,CHUSHENGRQ,NL,JIATINGDZ,DIANHUAHM,BRXH,JCDXH,SHENQINGLY,KDYS,DQCW,KDKS, " +
	    	"DECODE(SFCB,1,'床边',0,'否','','否') as SFCB,DECODE(JZBZ,1,'急诊',0,'否') as JZBZ, "+
	    	"DECODE(BINGRENLX,'I','住院','O','门诊','P','体检','J','急诊', 'Y','预约') AS PATIENT_TYPE, DJ, "+
	    	"MENZHENH, ZHUYUANH, REPLACE(XMMC,',',' ') AS EXAM_NAMES, YEBZ, DQBQ, DQKS FROM YZ_JCSQD "+
	    	"WHERE STATUS=4 AND DJBZ=0 and modality='US'";
	}
    ////
    if (_examclass == "预约")
    {
        sHisQuery = AddWhereCondition(sHisQuery, "BINGRENLX='Y'");
        sHisQuery = AddWhereCondition(sHisQuery, "YYSJ is null");
    }
    else
    {
        sHisQuery = AddWhereCondition(sHisQuery, "BINGRENLX='I'");
    }
/*
    if (_examsubclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, "");
*/
    if (_requestid != "")
    {
    	if (IsNumString(_requestid))
    	{
        	sHisQuery = AddWhereCondition(sHisQuery, "JCDXH = " + _requestid);
        }
        else
        {
        	ShowMessage("查询失败！请检查输入条件！");
        	return 0;
        }
    }
    if (_patientname != "")
        sHisQuery = AddWhereCondition(sHisQuery, "BRXM = '" + _patientname + "'");
    ////    
    String sStartDate = FormatDateTime("yyyy-mm-dd 0:0:0", _startdate);
    String sEndDate = FormatDateTime("yyyy-mm-dd 23:59:59", _enddate);
	if (IsConnetAccessDB(_hisconnection))
	{
		/*
	    if (_startdate != 0)
	        sHisQuery = AddWhereCondition(sHisQuery, "SQRQ >= #"+sStartDate+"#");
	    if (_enddate != 0)
	        sHisQuery = AddWhereCondition(sHisQuery, "SQRQ <= #"+sEndDate+"#");
	    */
	    _qryHisCustom.ParamCheck = false; // !!!
	}
	else
	{		
	    if (_startdate != 0)
	        sHisQuery = AddWhereCondition(sHisQuery, "SQRQ>= to_date('" + sStartDate + "', 'yyyy-mm-dd hh24:mi:ss')");
	    if (_enddate != 0)
	        sHisQuery = AddWhereCondition(sHisQuery, "SQRQ<= 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("查询失败！请检查输入条件！");
    }
    ////
    if (_qryHisCustom.IsEmpty())
    	return 0;
    ////
    _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 if (sFieldPairs[0][i] == "study_bodypart")
	            {
	            	_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = 
	            		GetBodyPart(_qryHisCustom.FieldByName(sFieldPairs[1][i]).Value);
	            }
	            else if (sFieldPairs[0][i] == "study_department")
	            {
	            	_qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = 
	            		GetDeptName(_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();
}

//---------------------------------------------------------------------------
// 确认登记：1 成功，0 失败
//---------------------------------------------------------------------------
int _ConfirmHisLogin()
{
   	if (_qryStudy.FieldByName("study_number0").Value == "")
   	{
   		MessageBox("HIS系统没有查询到该病人信息！无法划价收费！", "电子申请单", 1);
    	return 0;
    }
    if (!IsNumString(_qryStudy.FieldByName("study_number0").Value))
    {
    	MessageBox("HIS系统没有查询到该病人信息！无法划价收费！", "电子申请单", 1);
    	return 0;
    }
    if (_qryStudy.FieldByName("patient_guid").Value == "")
   	{
   		MessageBox("HIS系统没有查询到该病人信息！无法划价收费！", "电子申请单", 1);
    	return 0;
    }
    if (!IsNumString(_qryStudy.FieldByName("patient_guid").Value))
    {
    	MessageBox("HIS系统没有查询到该病人信息！无法划价收费！", "电子申请单", 1);
    	return 0;
    }
    ////
    float fStudyNumber0 = _qryStudy.FieldByName("study_number0").Value;
    float fPatientGuid = _qryStudy.FieldByName("patient_guid").Value;
    ////
    if (PatsIsCancel(fStudyNumber0))
    {
    	MessageBox("该病人已撤销申请单！无法确认登记！", "电子申请单", 1);
        return 0;
    }
    if (PatsIsRefund(fStudyNumber0))
   	{
   		MessageBox("该病人已执行过退费！无法确认登记！", "电子申请单", 1);
        return 0;
	}
	bool bPatsIsCharging = PatsIsCharging(fStudyNumber0, fPatientGuid);
    ////
    String sLoginDoctor = GetConfigStr("SysLogon", "sLastAccounts");
    String sOperator = GetUserID(sLoginDoctor);
    int iLoginState = _qryStudy.FieldByName("login_state").AsInteger;
	TDateTime dtLoginBook = _qryStudy.FieldByName("login_bookdttm").Value;
	String sHisQuery = "";	
    ////
    sHisQuery = "SELECT DJBZ, DJGH, DJSJ, TUIDANLY, SHENQINGLY, JIANCHABZ, BINGRENLX, YYSJ "+
    	"FROM YZ_JCSQD WHERE STATUS=4 AND (DJBZ=0 OR DJBZ=1) AND (BINGRENLX='I' OR BINGRENLX='Y')";
    sHisQuery = AddWhereCondition(sHisQuery, "JCDXH=" + FloatToStr(fStudyNumber0)); 
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery;
    _qryHisCustom.Open();
    if (_qryHisCustom.IsEmpty())
    {
   		MessageBox("HIS系统没有查询到该病人信息！无法划价收费！", "电子申请单", 1);
    	return 0;
    }
    ////
	if (_qryHisCustom.FieldByName("BINGRENLX").Value == "Y")
	{
		if (iLoginState == -2)
		{
			String sJianChaBZ = InputDialog("请填写注意事项！", "注意事项");
			if (sJianChaBZ == "MB_CANCEL")
		        return 0;
		    ////
			_qryHisCustom.Edit();	
			_qryHisCustom.FieldByName("YYSJ").Value = dtLoginBook;			// 预约时间
			_qryHisCustom.FieldByName("JIANCHABZ").Value = sJianChaBZ;		// 注意事项
			_qryHisCustom.FieldByName("DJBZ").Value = 0;					// 登记标志
			_qryHisCustom.Post();
			////
			MessageBox("预约登记已完成！", "电子申请单", 1);
			return 1;
		}
	}
	////
	if (!bPatsIsCharging)
    {
    	MessageBox("该病人还没有进行划价计费，无法确认登记！", "电子申请单", 1);
    	return 0;
    }
    ////
	_qryHisCustom.Edit();
	_qryHisCustom.FieldByName("BINGRENLX").Value = "I";				// 病人类型
	_qryHisCustom.FieldByName("DJBZ").Value = 1;           			// 登记标志
	_qryHisCustom.FieldByName("DJGH").Value = sOperator;   			// 登记工号
	_qryHisCustom.FieldByName("DJSJ").Value = Now();     			// 登记时间
    _qryHisCustom.Post();
    ////
    if (IsConnetAccessDB(_hisconnection))
	{
		sHisQuery = "UPDATE YZ_JCSQXM SET DJBZ=1, DJGH='" + sOperator+ "', DJSJ=Now() WHERE (DJBZ=0 OR DJBZ=1)";
	}
	else
	{
		sHisQuery = "UPDATE YZ_JCSQXM SET DJBZ=1, DJGH='" + sOperator+ "', DJSJ=sysdate WHERE (DJBZ=0 OR DJBZ=1)";
	}
	sHisQuery = AddWhereCondition(sHisQuery, "JCDXH = " + FloatToStr(fStudyNumber0));
	_qryHisCustom.Close();
	_qryHisCustom.SQL.Text = sHisQuery;
	_qryHisCustom.ExecSQL();
	
    MessageBox("确认登记已完成！", "电子申请单", 1);
    return 1;
}

//---------------------------------------------------------------------------
// 撤销登记：1 成功，0 失败
//---------------------------------------------------------------------------
int _CancelHisLogin()
{
	MessageBox("不允许撤销登记！", "电子申请单", 1);
	return 0;
	
	if (_qryStudy.FieldByName("study_number0").Value == "")
   	{
   		MessageBox("HIS系统没有查询到该病人信息！无法撤销登记！", "电子申请单", 1);
    	return 0;
    }
    if (!IsNumString(_qryStudy.FieldByName("study_number0").Value))
    {
    	MessageBox("HIS系统没有查询到该病人信息！无法撤销登记！", "电子申请单", 1);
    	return 0;
    }
    if (_qryStudy.FieldByName("patient_guid").Value == "")
   	{
   		MessageBox("HIS系统没有查询到该病人信息！无法撤销登记！", "电子申请单", 1);
    	return 0;
    }
    if (!IsNumString(_qryStudy.FieldByName("patient_guid").Value))
    {
    	MessageBox("HIS系统没有查询到该病人信息！无法撤销登记！", "电子申请单", 1);
    	return 0;
    }
    ////
	float fPatientGuid = _qryStudy.FieldByName("patient_guid").Value;
    float fStudyNumber0 = _qryStudy.FieldByName("study_number0").Value;
    String sLoginDoctor = GetConfigStr("SysLogon", "sLastAccounts");
    String sOperator = GetUserID(sLoginDoctor);
	String sHisQuery = "";
	if (PatsIsCharging(fStudyNumber0, fPatientGuid))
	{
		MessageBox("该病人已执行过收费操作！无法撤销申请单！" , "电子申请单", 1);
		return 0;
	}
	if (PatsIsConfirm(fStudyNumber0))
	{
		MessageBox("该病人已执行过HIS确认登记！无法撤销申请单！" , "电子申请单", 1);
		return 0;
	}
	////
	String sCancelReason = InputDialog("请填写退单理由！", "退单理由");
    if (sCancelReason == "MB_CANCEL")
        return 0;
	if (IsConnetAccessDB(_hisconnection))
    {
    	sHisQuery = "UPDATE YZ_JCSQXM SET DJBZ=2, DJGH='" + sOperator + "', DJSJ=Now() WHERE (DJBZ=0 OR DJBZ=1)";
    }
    else
    {
    	sHisQuery = "UPDATE YZ_JCSQXM SET DJBZ=2, DJGH='" + sOperator + "', DJSJ=sysdate WHERE (DJBZ=0 OR DJBZ=1)";
    }
    sHisQuery = AddWhereCondition(sHisQuery, "JCDXH = " + FloatToStr(fStudyNumber0));
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery;
    _qryHisCustom.ExecSQL();
    //
    if (IsConnetAccessDB(_hisconnection))
    {
        sHisQuery = "UPDATE YZ_JCSQD SET DJBZ=2, DJGH='" + sOperator + "', DJSJ=Now(), TUIDANLY='"
            + sCancelReason + "' WHERE (DJBZ=0 OR DJBZ=1)";
    }
    else
    {
    	sHisQuery = "UPDATE YZ_JCSQD SET DJBZ=2, DJGH='" + sOperator + "', DJSJ=sysdate, TUIDANLY='"
            + sCancelReason + "' WHERE (DJBZ=0 OR DJBZ=1)";
	}
    sHisQuery = AddWhereCondition(sHisQuery, "JCDXH = " + FloatToStr(fStudyNumber0));
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery;
    _qryHisCustom.ExecSQL();
    ////
    _qryStudy.Delete();
    
    MessageBox("撤销登记已完成！", "电子申请单", 1);
    return 1;
}
//---------------------------------------------------------------------------

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

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