//---------------------------------------------------------------------------
// 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 top 100 * from study";
    sQuerySQL = AddWhereCondition(sQuerySQL, "study_modalitytype='" + _modalitytype + "'");
    sQuerySQL = AddWhereCondition(sQuerySQL, "(patient_state is null or patient_state=0)");
    sQuerySQL = AddWhereCondition(sQuerySQL, "study_guid is not null");
    sQuerySQL = AddWhereCondition(sQuerySQL, "study_source='住院'");
    sQuerySQL = AddWhereCondition(sQuerySQL, "report_state=2");
    ////
    if (IsConnetSQLServer(_opacsconnection))
        sQuerySQL = AccessToSQL(sQuerySQL);

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

int ExecReportSend()
{
    // 执行报告传送
    int iFieldCount = 0;
    String sFieldPairs[2][100];
    SetArrayItem(sFieldPairs, iFieldCount, "SHENQINGDH",    "study_guid");      	// 申请单号（主键）
    SetArrayItem(sFieldPairs, iFieldCount, "ZHUYUANHAO",    "patient_guid");   		// 住院号
    SetArrayItem(sFieldPairs, iFieldCount, "MENZHENHAO",    "study_number1");   	// 门诊号
    SetArrayItem(sFieldPairs, iFieldCount, "MOTAI",         "study_modalitytype");  // 检查类型:如 CR CT MR 等
    SetArrayItem(sFieldPairs, iFieldCount, "JIANCHASJ",     "report_finding");  	// 检查所见
    SetArrayItem(sFieldPairs, iFieldCount, "JIANCHAJG",     "report_diagnosis");	// 检查结果
    SetArrayItem(sFieldPairs, iFieldCount, "JIANCHAREN",    "study_technician");	// 检查技师
    SetArrayItem(sFieldPairs, iFieldCount, "JIANCHARQ",     "study_dttm");      	// 检查时间
    SetArrayItem(sFieldPairs, iFieldCount, "BAOGAOREN",     "report_diagnostician");// 报告医师
    SetArrayItem(sFieldPairs, iFieldCount, "BAOGAORQ",      "report_dttm");     	// 报告时间
    SetArrayItem(sFieldPairs, iFieldCount, "SHENHEREN",     "report_approver"); 	// 审核医师
    SetArrayItem(sFieldPairs, iFieldCount, "SHENHERQ",      "report_audittime");	// 审核时间
    SetArrayItem(sFieldPairs, iFieldCount, "BEIZHU",        "report_memoinfo"); 	// 备注
    SetArrayItem(sFieldPairs, iFieldCount, "YINGXIANGID",   "study_key");       	// 影像id:调影像报告用
    SetArrayItem(sFieldPairs, iFieldCount, "LUJING",      	"");               		// 图像路径

    // 打开数据库
    String sHisReport = "SELECT * FROM JC_JIANCHABG WHERE 0 > 1";
    _qryHisReport.SQL.Text = sHisReport;
    try
    {
        _qryHisReport.Open();
    }
    except
    {
        return 0;
    }

    // 获取检查清单
    _qryStudy.Close();
    _qryStudy.SQL.Text = BuildQuerySQL();
    _qryStudy.Open();
    if (_qryStudy.IsEmpty())
        return 0;

    // 数据传送
    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;
        ////
        if (!IsNumString(sStudyGuid))
        {
        	_qryStudy.Next();
            continue;
        }
        ////
        _qryHisReport.Close();
        _qryHisReport.SQL.Text = "SELECT * FROM jc_jianchabg WHERE SHENQINGDH = '" + sStudyGuid + "'";
        _qryHisReport.Open();
        if (!_qryHisReport.IsEmpty())
        	_qryHisReport.Edit();
        else
        	_qryHisReport.Append();
        ////
        for (int i = 0; i < iFieldCount; i++)
        {
            if (sFieldPairs[1][i] == "")
                continue;
            if (!_qryStudy.FieldByName(sFieldPairs[1][i]).IsNull)
                _qryHisReport.FieldByName(sFieldPairs[0][i]).Value =
                            _qryStudy.FieldByName(sFieldPairs[1][i]).Value;
        }
        ////
        _qryCustom.Close();
        _qryCustom.SQL.Text = "select * from images where study_key = " + sStudyKey + " and image_reportindex > 0";
        _qryCustom.Open;
        String sImagePath = "";
        String sImageName = "";
		String sLujing = "";
		String sSharePath = GetSharePath();
		_qryCustom.First();
		while (!_qryCustom.Eof)
		{
			if (!_qryCustom.FieldByName("image_path").IsNull)
				sImagePath = sSharePath + "\\" + _qryCustom.FieldByName("image_path").AsString;
			if (!_qryCustom.FieldByName("image_name").IsNull)
				sImageName = _qryCustom.FieldByName("image_name").AsString;			
			if(sImageName != "")
				sLujing = sLujing + sImagePath + "\\" + sImageName + ";";
			_qryCustom.Next();
		}
		_qryHisReport.FieldByName("LUJING").Value = sLujing; // 超声的图像路径
        ////
        bSuccess = true;
        try
        {
            _qryHisReport.Post();
        }
        except
        {
            _qryHisReport.Close();
            _qryHisReport.Open();
            bSuccess = false;
        }
        if (bSuccess)
            sSendLog = "传送报告【" + sPatientName + " " + sStudyGuid + "】到目标系统【jc_jianchabg】...成功!" ;
        else
            sSendLog = "传送报告【" + sPatientName + " " + sStudyGuid + "】到目标系统【jc_jianchabg】...失败!";
        ShowLog("SyncServer", sSendLog);
        if (!bSuccess)
        {
            _qryStudy.Next();
            continue;
        }
        ////
        if (bSuccess)
        {
			_qryHisCustom.Close();
			_qryHisCustom.SQL.Text = "update yz_jcsqd set baogaobz = 1, QRBZ=3 where JCDXH = " + sStudyGuid;
			_qryHisCustom.ExecSQL();
			////
            _qryCustom.Close();  // 绕过审核权限管理
            _qryCustom.SQL.Text = "update study set patient_state = 1 where study_key = " + sStudyKey;
            _qryCustom.ExecSQL();
            _qryCustom.SQL.Text = "update images set image_state = 1 where study_key = " + sStudyKey;
            _qryCustom.ExecSQL();
        }
        ////
        _qryStudy.Next();  
    }                        
}
//---------------------------------------------------------------------------

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

