//---------------------------------------------------------------------------
// 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", "..\\Common\\JWCommon.cpp"

//---------------------------------------------------------------------------
int ExecReportSend()
{		
    // 执行报告传送
    int iFieldCount1 = 0;
	int iFieldCount2 = 0;
    String sFieldPairs1[2][100];
	String sFieldPairs2[2][100];
	// 门诊报告所需回传字段
    SetArrayItem(sFieldPairs1, iFieldCount1, "appbillid",		 "study_number0");		// 申请序号
    SetArrayItem(sFieldPairs1, iFieldCount1, "appbill_look",     "report_finding");  	// 检查所见
    SetArrayItem(sFieldPairs1, iFieldCount1, "appbill_diagnose", "report_diagnosis");	// 检查结果
    SetArrayItem(sFieldPairs1, iFieldCount1, "patient_name",  	 "patient_name");		// 姓名
	// 住院报告所需回传字段    
    SetArrayItem(sFieldPairs2, iFieldCount2, "appbillid",		 "study_number0");		// 申请序号
    SetArrayItem(sFieldPairs2, iFieldCount2, "appbill_look",     "report_finding");  	// 检查所见
    SetArrayItem(sFieldPairs2, iFieldCount2, "appbill_diagnose", "report_diagnosis");	// 检查结果
    SetArrayItem(sFieldPairs2, iFieldCount2, "patient_name",  	 "patient_name");		// 姓名
    
    // 打开门诊表adm_agfa_cli_report
    // 利用现有_qryHisPatient数据集查询门诊记录,与_qryHisReport无关
    String sHisMaster = "SELECT * FROM adm_agfa_cli_report WHERE 0 > 1";
    _qryHisPatient.Close();
    _qryHisPatient.SQL.Text = sHisMaster;
    try
    {
        _qryHisPatient.Open();
    }
    except
    {
    	ShowLog("SyncServer", "打开adm_agfa_cli_report表失败！");
        return 0;
    }
    // 打开住院表adm_agfa_inh_report
    // 利用现有_qryHisReport数据集查询住院记录,,与_qryHisPatient无关
    String sHisReport = "SELECT * FROM adm_agfa_inh_report WHERE 0 > 1";
    _qryHisReport.Close();
    _qryHisReport.SQL.Text = sHisReport;
    try
    {
        _qryHisReport.Open();
    }
    except
    {
    	ShowLog("SyncServer", "打开adm_agfa_inh_report表失败！");
        return 0;
    }

    // 获取检查清单
    _qryStudy.Close();
    _qryStudy.SQL.Text = BuildQuerySQL();
    _qryStudy.Open(); 
        
    // 数据传送
    bool bSuccess = true;
    String sSendLog = "";
    ////
    _qryStudy.First();
    while (!_qryStudy.Eof)
    {       
    	String sExamNo = _qryStudy.FieldByName("study_number0").AsString;
    	String sPatientName = _qryStudy.FieldByName("patient_name").AsString;
    	String sStudySource = _qryStudy.FieldByName("study_source").AsString;
        String sStudyKey = _qryStudy.FieldByName("study_key").AsString;
    	if (sStudySource == "门诊")
    	{
    		_qryHisPatient.Close();
	        _qryHisPatient.SQL.Text = "SELECT * FROM adm_agfa_cli_report WHERE appbillid = '" + sExamNo + "'";
	        _qryHisPatient.Open();
	        if (_qryHisPatient.IsEmpty())
	        {
	        	ShowLog("SyncServer", "准备插入adm_agfa_cli_report记录...");        	
		        _qryHisPatient.Append();
	        }  
	        else
	        {
	        	ShowLog("SyncServer", "准备修改adm_agfa_cli_report记录...");        	
	        	_qryHisPatient.Edit(); 
	        }
	        ////
	        for (int i = 0; i < iFieldCount1; i++)
	        {
	            if (sFieldPairs1[1][i] == "")
	                continue;              
	            if (!_qryStudy.FieldByName(sFieldPairs1[1][i]).IsNull)
	                _qryHisPatient.FieldByName(sFieldPairs1[0][i]).Value =
	                	_qryStudy.FieldByName(sFieldPairs1[1][i]).Value;
	        }   	   
	        ////
	        bSuccess = true;
	        try
	        {     
	            _qryHisPatient.Post();
	        }
	        except
	        {
	            bSuccess = false;
	        }
	        if (bSuccess)
	            sSendLog = "传送报告【" + sPatientName + " " + sExamNo + "】到目标系统【adm_agfa_cli_report】...成功!";
	        else
	            sSendLog = "传送报告【" + sPatientName + " " + sExamNo + "】到目标系统【adm_agfa_cli_report】...失败!";
	        ShowLog("SyncServer", sSendLog);
	        if (!bSuccess)
	        {
	            _qryStudy.Next();
	            continue;
	        }
    	}
    	else if (sStudySource == "住院")
    	{
    		_qryHisReport.Close();
	        _qryHisReport.SQL.Text = "SELECT * FROM adm_agfa_inh_report WHERE appbillid = '" + sExamNo + "'";
	        _qryHisReport.Open();
	        if (_qryHisReport.IsEmpty())
	        {
	        	ShowLog("SyncServer", "准备插入adm_agfa_inh_report记录...");        	
		        _qryHisReport.Append();
	        }  
	        else
	        {	
	        	ShowLog("SyncServer", "准备修改adm_agfa_inh_report记录...");        	
	        	_qryHisReport.Edit(); 
	        }
	        ////
	        for(int j = 0; j < iFieldCount2; j++)
	        {
	            if (sFieldPairs2[1][j] == "")
	                continue;
	            if (!_qryStudy.FieldByName(sFieldPairs2[1][j]).IsNull)
	                _qryHisReport.FieldByName(sFieldPairs2[0][j]).Value =
	                	_qryStudy.FieldByName(sFieldPairs2[1][j]).Value;
	        }
	        ////
	        bSuccess = true;
	        try
	        {
	            _qryHisReport.Post();
	        }
	        except
	        {
	            bSuccess = false;
	        }
	        if (bSuccess)
	            sSendLog = "传送报告【" + sPatientName + " " + sExamNo + "】到目标系统【adm_agfa_inh_report】...成功!";
	        else
	            sSendLog = "传送报告【" + sPatientName + " " + sExamNo + "】到目标系统【adm_agfa_inh_report】...失败!";
	        ShowLog("SyncServer", sSendLog);
	        if (!bSuccess)
	        {
	            _qryStudy.Next();
	            continue;
	        }
    	}
        // 未填写study_source的记录将直接修改状态为已传送
        if (bSuccess)
        {
            _qryCustom.Close();  // 绕过审核权限管理
            _qryCustom.SQL.Text = "update study set patient_state = 1 where study_key = " + sStudyKey;
            _qryCustom.ExecSQL();
        }
        _qryStudy.Next();
    }
}
//---------------------------------------------------------------------------

String BuildQuerySQL()
{
    // 构造查询语句
    String sQuerySQL = "select top 100 * 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;
}
//---------------------------------------------------------------------------

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

