//---------------------------------------------------------------------------
// string _opacsconnection; 约定的输入变量名
// string _hisconnection; 	约定的输入变量名   
// string _modalitytype; 	约定的输入变量名
// string _requestid; 		约定的输入变量名
// string _patientname; 	约定的输入变量名
// string _examclass; 		约定的输入变量名
// string _examsubclass; 	约定的输入变量名
// TDateTime _startdate; 	约定的输入变量名
// TDateTime _enddate; 	    约定的输入变量名
// TADOQuery _qryHisCustom  外部已定义的对象  
// TADOQuery _qryHisCommon  外部已定义的对象
// TADOQuery _qryHisStudy   外部已定义的对象
// TADOQuery _qryStudy		外部已定义的对象
//
// 脚本必须实现的函数 int _QueryHisTable();
// 脚本必须实现的函数 int _ConfirmHisLogin(); 	
// 脚本必须实现的函数 int _CancelHisLogin(); 	
// 脚本必须实现的函数 int _PatientValidate();
//---------------------------------------------------------------------------

#include "..\\Common\\HisCommon.cpp", "..\\Common\\LZCommon.cpp"

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", 		"XM");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_address", 	"JTZZ");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_telephone", "LXDH");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_guid", 		"card_number"); 
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex", 		"XB");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_birthday",	"CSRQ");        
    SetArrayItem(sFieldPairs, iFieldCount, "patient_studyage",	"NL");    
    SetArrayItem(sFieldPairs, iFieldCount, "study_guid",		"JCDXH");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_type",		"BRXZMC");
    SetArrayItem(sFieldPairs, iFieldCount, "study_source",		"PATIENT_TYPE");
    SetArrayItem(sFieldPairs, iFieldCount, "study_number1",		"card_number");
    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", 	"MBMC");
    SetArrayItem(sFieldPairs, iFieldCount, "study_department", 	"KDKS");
    SetArrayItem(sFieldPairs, iFieldCount, "study_userdef1", 	"JZBZ");
    SetArrayItem(sFieldPairs, iFieldCount, "study_userdef2", 	"SFCB");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_insurance",	"card_number");
    
    SetArrayItem(sFieldPairs, iFieldCount, "report_memoinfo", 	"BRZS");
    SetArrayItem(sFieldPairs, iFieldCount, "report_memoinfo", 	"BRZD");

    ////
    string sHisQuery = "select JZKH as card_number,a.XM, a.XB, a.CSRQ, a.LXDH, a.JTZZ, a.XZMC, b.NL, " + 		
			"b.JIATINGDZ, b.DIANHUAHM, b.BRXH, b.JCDXH, b.SHENQINGLY, b.KDYS, b.DQCW, '门诊' as KDKS, " +
	    	"DECODE(SFCB,1,'床边',0,'否','','否') as SFCB,DECODE(JZBZ,1,'急诊',0,'否') as JZBZ, BRXZMC, " +
	    	"DECODE(BINGRENLX,'I','住院','O','门诊','P','体检','J','急诊', 'Y','预约') AS PATIENT_TYPE, " +
			"DJ, MENZHENH, REPLACE(XMMC,',',' ') AS EXAM_NAMES, MBMC, YEBZ, DQBQ, DQKS, BRZS, BRZD " +
	    	"FROM PACS_MZVIEW a, YZ_JCSQD b WHERE a.JZKH = b.MENZHENH "+
	    	"AND b.BINGRENLX='O' AND b.STATUS=4 AND b.DJBZ=0 AND b.modality='US'";
    ////
    if (_requestid != "")
    {
        _requestid = ConvertJZKH(_requestid);
        sHisQuery = AddWhereCondition(sHisQuery, "a.JZKH = '" + _requestid + "'");
    }
    if (_patientname != "")
        sHisQuery = AddWhereCondition(sHisQuery, "XM = '" + _patientname + "'");
/*
    if (_examclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, );
    if (_examsubclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, );

    if (_startdate != "")
        sHisQuery = AddWhereCondition(sHisQuery, );
    if (_enddate != "")
        sHisQuery = AddWhereCondition(sHisQuery, );
*/
	sHisQuery = AddWhereCondition(sHisQuery, "ROWNUM<=100");
	////
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery;
    try
    {
    	_qryHisCustom.Open();  
    }
    except
    {
    	ShowMessage("查询失败！请检查输入条件！");
    	return 0;
	}
    ////
    if (_qryHisCustom.IsEmpty())
    {
	    sHisQuery = "select JZKH as card_number,XM,XB,CSRQ,LXDH,JTZZ,XZMC, '' as NL, " + 		
		" '' as JIATINGDZ, '' as DIANHUAHM, '' as BRXH, '' as JCDXH, '' as SHENQINGLY, " + 
		" '' as KDYS, '' as DQCW, '门诊' as KDKS, '' as SFCB, '' as JZBZ, '' as BRXZMC, " +
    		" '门诊' AS PATIENT_TYPE, '0' as DJ, JZKH as MENZHENH, '' AS EXAM_NAMES, '' AS MBMC," + 
    		" '0' AS YEBZ, '0' AS DQBQ, '' AS  DQKS, '' AS BRZS, '' AS BRZD " +
    		"FROM PACS_MZVIEW WHERE 1>0 ";
	    if (_requestid != "")
	    {
	        _requestid = ConvertJZKH(_requestid);
	        sHisQuery = AddWhereCondition(sHisQuery, "JZKH = '" + _requestid + "'");	
	    }
	    if (_patientname != "")
	        sHisQuery = AddWhereCondition(sHisQuery, "XM = '" + _patientname + "'");

		sHisQuery = AddWhereCondition(sHisQuery, "ROWNUM<=100");
		
		_qryHisCustom.Close();
    	_qryHisCustom.SQL.Text = sHisQuery;
	    try
	    {
	    	_qryHisCustom.Open();  
	    }
	    except
	    {
	    	ShowMessage("查询失败！请检查输入条件！");
	    	return 0;
		}
    }
    if (_qryHisCustom.IsEmpty())
    {
    	String sTemp = "卡号：" + _requestid + "无病人信息";
    	ShowMessage(sTemp);
    }
    else 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("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()
{
    // 登记检查
    if (_qryStudy.FieldByName("study_guid").AsString == "")
   	{
   		MessageBox("HIS系统没有查询到该病人信息！无法确认登记！", "电子申请单", 1);
    	return 0;
    }
    if (!IsNumString(_qryStudy.FieldByName("study_guid").AsString))
    {
    	MessageBox("HIS系统没有查询到该病人信息！无法确认登记！", "电子申请单", 1);
    	return 0;
    }
    if (_qryStudy.FieldByName("patient_guid").AsString == "")
   	{
   		MessageBox("HIS系统没有查询到该病人信息！无法确认登记！", "电子申请单", 1);
    	return 0;
    }
    if (!IsNumString(_qryStudy.FieldByName("patient_guid").AsString))
    {
    	MessageBox("HIS系统没有查询到该病人信息！无法确认登记！", "电子申请单", 1);
    	return 0;
    }
    ////
    float fStudyGuid = _qryStudy.FieldByName("study_guid").Value;
    float fPatientGuid = _qryStudy.FieldByName("patient_guid").Value;
    ////
    if (PatsIsCancel(fStudyGuid))
    {
    	MessageBox("该病人已撤销申请单！无法确认登记！", "电子申请单", 1);
        return 0;
    }
    if (PatsIsRefund(fStudyGuid))
   	{
   		MessageBox("该病人已执行过退费！无法确认登记！", "电子申请单", 1);
        return 0;
	}
    ////
    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='O')";
    sHisQuery = AddWhereCondition(sHisQuery, "JCDXH=" + FloatToStr(fStudyGuid)); 
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery;
    _qryHisCustom.Open();
    if (_qryHisCustom.IsEmpty())
    {
   		MessageBox("HIS系统没有查询到该病人信息！无法确认登记！", "电子申请单", 1);
    	return 0;
    }
    ////
	_qryHisCustom.Edit();
	_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(fStudyGuid));
	_qryHisCustom.Close();
	_qryHisCustom.SQL.Text = sHisQuery;
	_qryHisCustom.ExecSQL();
	
    MessageBox("确认登记已完成！", "电子申请单", 1);
    return 1;;
}
//---------------------------------------------------------------------------

int _CancelHisLogin()
{
    // 撤销登记
    if (_qryStudy.FieldByName("study_guid").Value == "")
   	{
   		MessageBox("HIS系统没有查询到该病人信息！无法撤销登记！", "电子申请单", 1);
    	return 0;
    }
    if (!IsNumString(_qryStudy.FieldByName("study_guid").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 fStudyGuid = _qryStudy.FieldByName("study_guid").Value;
    String sLoginDoctor = GetConfigStr("SysLogon", "sLastAccounts");
    String sOperator = GetUserID(sLoginDoctor);
	String sHisQuery = "";
	////
	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(fStudyGuid));
    _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(fStudyGuid));
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery;
    _qryHisCustom.ExecSQL();
    ////
    _qryStudy.Delete();
    
    MessageBox("撤销登记已完成！", "电子申请单", 1);
    return 1;
}
//---------------------------------------------------------------------------

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

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