//---------------------------------------------------------------------------
// 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_sExecDept = "";

//---------------------------------------------------------------------------
int ExecSQL()
{
	g_sExecDept = "放射"; // 放射等科室使用, 请根据情况配置。
	//g_sExecDept = "超声"; // 超声等科室使用, 请根据情况配置。
	////
    String sHisQuery = "";
    int iFieldCount = 0;
    String sFieldPairs[2][100];
	////
    SetArrayItem(sFieldPairs, iFieldCount, "study_number3", 	"GUID");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_guid", 		"SelfBH");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_name", 		"YYRXM");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_sex", 		"SEX");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_studyage", 	"AGE");
    SetArrayItem(sFieldPairs, iFieldCount, "study_dttm", 		"TJRQ");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_address", 	"DWMC");
    SetArrayItem(sFieldPairs, iFieldCount, "study_examname", 	"DXMC");
    SetArrayItem(sFieldPairs, iFieldCount, "study_charge", 		"DXJG");
    SetArrayItem(sFieldPairs, iFieldCount, "patient_telephone", "yyryddh");
    SetArrayItem(sFieldPairs, iFieldCount, "login_bookdttm", 	"TJRQ");

    // 生成查询
	//SET_KSSZ.KStype: 约定固定为“放射”两字
	//SET_DX.ksid: 11=CR, 20=CT, 1=UCG, 2=US_B, 28=US
    if (g_sExecDept == "放射")
    {
	    //sHisQuery = "SELECT top 100 GUID,SelfBH,YYRXM,SEX,AGE,TJRQ,DWMC from v_toPacs ";//order by GUID desc";
	    sHisQuery = "select top 100 dbo.SET_GRXX.GUID, dbo.SET_GRXX.SelfBH, dbo.SET_GRXX.TJRQ, dbo.SET_GRXX.YYRXM, " +
	    	" dbo.SET_GRXX.AGE, dbo.SET_GRXX.SEX, ISNULL(dbo.SET_DW.DWMC, '') AS DWMC, SET_DX.dxmc, SET_DX.dxjg, " +
	    	" SET_DX.ksid, SET_GRXX.yyryddh, YY_TJDJ.sftj " +
	    	" FROM  dbo.SET_DW INNER JOIN dbo.YY_TJDJ ON dbo.SET_DW.DWID = dbo.YY_TJDJ.DWID " +
	    	" RIGHT OUTER JOIN dbo.SET_GRXX INNER JOIN  dbo.YY_SJDJDX ON dbo.SET_GRXX.GUID = dbo.YY_SJDJDX.GUID " +
	    	" INNER JOIN dbo.SET_DX ON dbo.YY_SJDJDX.DXID = dbo.SET_DX.DXID INNER JOIN  dbo.SET_KSSZ ON " + 
	    	" dbo.SET_DX.KSID = dbo.SET_KSSZ.KSID ON dbo.YY_TJDJ.YYID = dbo.SET_GRXX.YYID " +
	    	" WHERE (dbo.SET_GRXX.QRDJ > 0) AND (dbo.SET_KSSZ.KStype = '放射') and (SET_DX.ksid = 11 or SET_DX.ksid = 20)" +	
	    	"";
	    	// and YY_SJDJDX.sftj = 1
    }
    else if (g_sExecDept == "超声")
    {
	    //sHisQuery = "SELECT top 100 GUID,SelfBH,YYRXM,SEX,AGE,TJRQ,DWMC from v_toPacs ";//order by GUID desc";
	    sHisQuery = "select top 100 dbo.SET_GRXX.GUID, dbo.SET_GRXX.SelfBH, dbo.SET_GRXX.TJRQ, dbo.SET_GRXX.YYRXM, " +
	    	" dbo.SET_GRXX.AGE, dbo.SET_GRXX.SEX, ISNULL(dbo.SET_DW.DWMC, '') AS DWMC, SET_DX.dxmc, SET_DX.dxjg, " +
	    	" SET_DX.ksid, SET_GRXX.yyryddh, YY_TJDJ.sftj " +
	    	" FROM  dbo.SET_DW INNER JOIN dbo.YY_TJDJ ON dbo.SET_DW.DWID = dbo.YY_TJDJ.DWID " +
	    	" RIGHT OUTER JOIN dbo.SET_GRXX INNER JOIN  dbo.YY_SJDJDX ON dbo.SET_GRXX.GUID = dbo.YY_SJDJDX.GUID " +
	    	" INNER JOIN dbo.SET_DX ON dbo.YY_SJDJDX.DXID = dbo.SET_DX.DXID INNER JOIN  dbo.SET_KSSZ ON " + 
	    	" dbo.SET_DX.KSID = dbo.SET_KSSZ.KSID ON dbo.YY_TJDJ.YYID = dbo.SET_GRXX.YYID " +
	    	" WHERE (dbo.SET_GRXX.QRDJ > 0) AND (dbo.SET_KSSZ.KStype = '放射') and (SET_DX.ksid = 1 or SET_DX.ksid = 2 or SET_DX.ksid = 28)" +
	    	"";
	    	// and YY_TJDJ.sftj = 0
    }
	//// 
    if (_requestid != "")
    {
    	if (IsNumString(_requestid))
    	{
        	sHisQuery = AddWhereCondition(sHisQuery, "SelfBH = '" + _requestid + "'");
        }
        else
        {
    		ShowMessage("查询条件输入错误！");
        	return 0;
        }
    }
    if (_patientname != "")
        sHisQuery = AddWhereCondition(sHisQuery, "YYRXM = '" + _patientname + "'");
/*
    if (_examclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, " = '" + _examclass + "'" );
    if (_examsubclass != "")
        sHisQuery = AddWhereCondition(sHisQuery, " = '" + _examsubclass + "'");
*/
    // 体检仅显示30天内的检查记录
    if (_startdate != 0)
    {
    	String sStartDate = FormatDateTime("yyyy-mm-dd 0:0:0", _startdate - 30);    	
        sHisQuery = AddWhereCondition(sHisQuery, "SQRQ>= '"+sStartDate+"'");
    }
    if (_enddate != 0)
  	{
	    String sEndDate = FormatDateTime("yyyy-mm-dd 23:59:59", _enddate);
        sHisQuery = AddWhereCondition(sHisQuery, "SQRQ<= '"+sEndDate+"'");
    }
    
    // 执行查询并检查返回值
    _qryHisCustom.Close();
    _qryHisCustom.SQL.Text = sHisQuery + " order by SET_GRXX.TJRQ asc ";
    try
    {
        _qryHisCustom.Open();
    }
    except
    {
        ShowMessage("查询语句语法错误！");
        return 0;
    }
    ////
    if (!_qryHisCustom.IsEmpty())
	{
        _qryHisCustom.First();
        while (!_qryHisCustom.Eof)
        {
	    	// 过滤检查项目
	    	String sExamName = _qryHisCustom.FieldByName("DXMC").AsString;
	    	if ((sExamName == "放射结论") || (sExamName == "CT结论"))
	    	{
	        	_qryHisCustom.Next();
	    		continue;
	    	}
	    	////
            _qryHisStudy.Append();
	        _qryHisStudy.FieldByName("study_source").Value = "体检";
	        _qryHisStudy.FieldByName("study_department").Value = "体检中心";  
	        int iKsid = _qryHisCustom.FieldByName("ksid").AsInteger;
	        if (iKsid == 11)
	        	_qryHisStudy.FieldByName("study_examkind").Value = "CR";
	        else if (iKsid == 20)
	        	_qryHisStudy.FieldByName("study_examkind").Value = "CT";	
	        else if (iKsid == 1)
	        	_qryHisStudy.FieldByName("study_examkind").Value = "UCG";
	        else if (iKsid == 2)
	        	_qryHisStudy.FieldByName("study_examkind").Value = "US_B";	
	        else if (iKsid == 28)
	        	_qryHisStudy.FieldByName("study_examkind").Value = "US";	
            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")
                    {
	                	String sStudyAge = _qryHisCustom.FieldByName(sFieldPairs[1][i]).Value;
	                	sStudyAge += "岁";           	
	                    _qryHisStudy.FieldByName("patient_birthday").Value =
							CalcBirthday(sStudyAge,Now());
	                    _qryHisStudy.FieldByName(sFieldPairs[0][i]).Value = sStudyAge;
                    }
                    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.
}
//---------------------------------------------------------------------------
