//---------------------------------------------------------------------------
// String _hisconnection; 	约定的输入变量名
// String _opacsconnection; 约定的输入变量名
// String _modalitytype; 	约定的输入变量名
// String _configsection; 	约定的输入变量名
// TADOQuery _qryStudy 	    外部已定义的对象
// TADOQuery _qryReport 	外部已定义的对象
// TADOQuery _qryImage      外部已定义的对象
// TADOQuery _qryCustom     外部已定义的对象
// TADOQuery _qryHisPatient 外部已定义的对象
// TADOQuery _qryHisReport  外部已定义的对象
// TADOQuery _qryHisImage   外部已定义的对象
// TADOQuery _qryHisCommon  外部已定义的对象
// TADOQuery _qryHisCustom  外部已定义的对象
//---------------------------------------------------------------------------
#include "..\\Common\\HisCommon.cpp"

String      _hisconnection;
String      _opacsconnection;
String      _modalitytype;
String 		_configsection;
//---------------------------------------------------------------------------

String BuildQuerySQL()
{
    // 构造查询语句
    String sQuerySQL = "select * from study";
    sQuerySQL = AddWhereCondition(sQuerySQL, "study_modalitytype='" + _modalitytype + "'");
    sQuerySQL = AddWhereCondition(sQuerySQL, "patient_state is null or patient_state=0");
    sQuerySQL = AddWhereCondition(sQuerySQL, "report_state=2");
    ////
    if (IsConnetSQLServer(_opacsconnection))
        sQuerySQL = AccessToSQL(sQuerySQL);

    return sQuerySQL;
}
//---------------------------------------------------------------------------

int ExecReportSend()
{
    // 执行报告传送
    int iFieldCount1 = 0;
    String sFieldPairs1[2][100];
    SetArrayItem(sFieldPairs1, iFieldCount1, "PATIENT_ID",      "study_guid");    
    SetArrayItem(sFieldPairs1, iFieldCount1, "EXAM_CLASS_NAME", "");      
    SetArrayItem(sFieldPairs1, iFieldCount1, "YYBH",        	"");                   
    SetArrayItem(sFieldPairs1, iFieldCount1, "NAME",    		"patient_name");            
    SetArrayItem(sFieldPairs1, iFieldCount1, "SEX",  			"patient_sex"); 
    SetArrayItem(sFieldPairs1, iFieldCount1, "DATE_OF_BIRTH",   "patient_birthday");
    SetArrayItem(sFieldPairs1, iFieldCount1, "DESCRIPTION",     "report_finding");
    SetArrayItem(sFieldPairs1, iFieldCount1, "IMPRESSION",      "report_diagnosis"); 
    SetArrayItem(sFieldPairs1, iFieldCount1, "REQ_DATE_TIME",   "study_dttm"); 
    SetArrayItem(sFieldPairs1, iFieldCount1, "SFXH",		    "patient_guid"); 
    SetArrayItem(sFieldPairs1, iFieldCount1, "IMAGEPATH",		""); 
    SetArrayItem(sFieldPairs1, iFieldCount1, "IMGSUN",		    ""); 
    
    // 打开数据库
    String sHisReport = "SELECT * FROM sq_ryjcxx WHERE 0 > 1";
    _qryHisReport.Close();
    _qryHisReport.SQL.Text = sHisReport;
    try
    {
        _qryHisReport.Open();
    }
    except
    {
        return 0;
    }

    // 获取检查清单
    _qryStudy.Close();
    _qryStudy.SQL.Text = BuildQuerySQL();
    _qryStudy.Open(); 
	
	// 卷标路径
	String sVolumePath = "\\\\SERVER\\Image";	
        
    // 数据传送
    bool bSuccess = true;
    String sSendLog = "";
    ////
    _qryStudy.First();
    while (!_qryStudy.Eof)
    {
        String sStudyKey = _qryStudy.FieldByName("study_key").AsString;
    	String sPatientName = _qryStudy.FieldByName("patient_name").AsString;
    	String sStudyGuid = _qryStudy.FieldByName("study_guid").AsString;
		String sImagePath = "";
		int iImageCount = 0;
        try
        {
	    	DeleteRYJC(sStudyGuid);
        }
        except
        {
            ShowLog("SyncServer", "删除数据失败！");
        }
        ////
		String sImagesSQL = "SELECT * FROM IMAGES WHERE study_key = " + sStudyKey;
		_qryImage.Close();
		_qryImage.SQL.Text = sImagesSQL;
		try
		{
			_qryImage.Open();
			if (!_qryImage.IsEmpty())
			{
				sImagePath = sVolumePath + "\\" + _qryImage.FieldByName("image_path").AsString; // 图像文件夹路径
				iImageCount = GetRecordCount(_qryImage);
			}
		}
		except
		{
			ShowLog("SyncServer", "IMAGES表打开失败");
		}
		////
        _qryHisReport.Append();
        for (int i = 0; i < iFieldCount1; i++)
        {
            if (sFieldPairs1[1][i] == "")
                continue;              
            if (!_qryStudy.FieldByName(sFieldPairs1[1][i]).IsNull)
                _qryHisReport.FieldByName(sFieldPairs1[0][i]).Value =
                            _qryStudy.FieldByName(sFieldPairs1[1][i]).Value;
        }
        if (_modalitytype == "US")
        {
        	_qryHisReport.FieldByName("EXAM_CLASS_NAME").Value = "超声"; 
        }
        if (_modalitytype == "XR")
        {
        	_qryHisReport.FieldByName("EXAM_CLASS_NAME").Value = "放射"; 
        }
        if (_modalitytype == "ES")
        {
        	_qryHisReport.FieldByName("EXAM_CLASS_NAME").Value = "胃镜"; 
        }
        _qryHisReport.FieldByName("YYBH").Value = "2201"; 
        _qryHisReport.FieldByName("VISIT_ID").Value = "1"; 
		_qryHisReport.FieldByName("IMAGEPATH").Value = sImagePath; // 图像文件夹路径
		_qryHisReport.FieldByName("IMGSUN").Value = iImageCount;   // 图像数量
        ////
        bSuccess = true;
        try
        {     
            _qryHisReport.Post();
        }
        except
        {
            _qryHisReport.Close();
            _qryHisReport.Open();
            bSuccess = false;
        }
        if (bSuccess)
            sSendLog = "传送报告【" + sPatientName + " " + sStudyGuid + "】到目标系统【sq_ryjcxx】...成功!" ;
        else
            sSendLog = "传送报告【" + sPatientName + " " + sStudyGuid + "】到目标系统【sq_ryjcxx】...失败!";
        ShowLog("SyncServer", sSendLog);
        if (!bSuccess)
        {
            _qryStudy.Next();
            continue;
        }
        ////
        if (bSuccess)
        {
            _qryCustom.Close();  // 绕过审核权限管理
            _qryCustom.SQL.Text = "update study set patient_state = 1 where study_key = " + sStudyKey;
            _qryCustom.ExecSQL();
        }
        _qryStudy.Next();
    }
}
//---------------------------------------------------------------------------

void DeleteRYJC(String sPatientID)
{   
	_qryHisCommon.Close();
    _qryHisCommon.SQL.Text = "DELETE FROM sq_ryjcxx WHERE PATIENT_ID = '" + sPatientID + "'";
    _qryHisCommon.ExecSQL();
}
//---------------------------------------------------------------------------

// 主程序
{
	try
	{
    	ExecReportSend();
    }
    except 
    {    	
	}    
}
//---------------------------------------------------------------------------

