//---------------------------------------------------------------------------
// 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 vRisList";
    sQuerySQL = AddWhereCondition(sQuerySQL, "status=20");
    sQuerySQL = AddWhereCondition(sQuerySQL, "INSTRUMENT in ('2','8')");
    sQuerySQL = AddWhereCondition(sQuerySQL, "QualityDoctor is null");
    ////
    if (IsConnetSQLServer(_hisconnection))
        sQuerySQL = AccessToSQL(sQuerySQL);

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

int ExecStudyFetch()
{
    // 执行报告传送
    int iFieldCount = 0;
    String sFieldPairs[2][100];
    SetArrayItem(sFieldPairs, iFieldCount, "PATNAME",		"patient_name");
    SetArrayItem(sFieldPairs, iFieldCount, "TechNo",		"patient_guid"); // 影像号
    SetArrayItem(sFieldPairs, iFieldCount, "HOSPNO",		"study_guid");
    SetArrayItem(sFieldPairs, iFieldCount, "SEX",			"patient_sex");
    SetArrayItem(sFieldPairs, iFieldCount, "WARDORREG",		"patient_type");
    SetArrayItem(sFieldPairs, iFieldCount, "INSTRUMENT", 	"study_modalitytype");
    SetArrayItem(sFieldPairs, iFieldCount, "BW",			"study_examname");
    SetArrayItem(sFieldPairs, iFieldCount, "birthday",		"patient_birthday");
    SetArrayItem(sFieldPairs, iFieldCount, "ENGNAME",		"login_extinfo1"); // for"AQ检查Send.cpp"
    
    // 打开数据库
    _qryStudy.Close();
    _qryStudy.SQL.Text = "select * from study where 0 > 1";
    try
    {
    	_qryStudy.Open();
	}
	except
	{
		ShowLog("SyncServer", "study表打开失败！");
		return 0;
	}
    
    // 获取检查清单
    _qryHisCommon.Close();
    _qryHisCommon.SQL.Text = BuildQuerySQL();
    try
	{
		_qryHisCommon.Open();
	}
	except
	{
		ShowLog("SyncServer", "vRisList视图打开失败！");
		return 0;
	}
    
    // 数据传送
    bool bSuccess = true;
    String sSendLog = "";
    ////
    _qryHisCommon.First();
    while (!_qryHisCommon.Eof)
    {
    	String sPatientName = _qryHisCommon.FieldByName("PATNAME").AsString;
        String sLoginOrdinal = _qryHisCommon.FieldByName("INSTRUMENT").AsString;
        int iStudyAge = _qryHisCommon.FieldByName("AGE").AsInteger;
        String sAgeUnit = _qryHisCommon.FieldByName("AGEUNIT").AsString;
        String sStudyAge = IntToStr(iStudyAge) + sAgeUnit;
    	////
    	_qryStudy.Append();
    	for (int i = 0; i < iFieldCount; i++)
        {
            if (sFieldPairs[1][i] == "")
                continue;              
            if (!_qryHisCommon.FieldByName(sFieldPairs[0][i]).IsNull)
                _qryStudy.FieldByName(sFieldPairs[1][i]).Value =
                	_qryHisCommon.FieldByName(sFieldPairs[0][i]).Value;
        }
        _qryStudy.FieldByName("study_modalitytype").Value = _modalitytype;
        _qryStudy.FieldByName("login_guid").Value = GetLoginGuid(); // 登记号
        _qryStudy.FieldByName("patient_studyage").Value = sStudyAge;
        if (sLoginOrdinal != "")
        	_qryStudy.FieldByName("login_ordinal").Value = StrToInt(sLoginOrdinal);       
        ////
        bSuccess = true;
        try
        {
        	_qryStudy.Post();
        }
        except
        {
            _qryStudy.Close();
            _qryStudy.Open();
            bSuccess = false;
        }
        ////
        if (bSuccess)
            sSendLog = "传送记录【" + sPatientName + "】到目标系统【STUDY】...成功!" ;
        else
            sSendLog = "传送记录【" + sPatientName + "】到目标系统【STUDY】...失败!";
        ShowLog("SyncServer", sSendLog);
        ////
        if (!bSuccess)
        {
            _qryHisCommon.Next();
            continue;
        }
        ////
        if (bSuccess)
        {
        	_qryHisCommon.Edit();
        	_qryHisCommon.FieldByName("QUALITYDOCTOR").Value = 1; 
        }
        _qryHisCommon.Next();
    }
}
//---------------------------------------------------------------------------

String GetLoginGuid()
{
	String sQueryNum2 = "select * from system";
	sQueryNum2 = AddWhereCondition(sQueryNum2, "system_modalitytype = '" + _modalitytype + "'");
	sQueryNum2 = AddWhereCondition(sQueryNum2, "convert(varchar(12), system_today, 112) = "	
		+ "Convert(varchar(12), GetDate(), 112)");
	//// 
	_qryCustom.Close();
	_qryCustom.SQL.Text = sQueryNum2;
	try
	{
		_qryCustom.Open();
	}
	except
	{
		ShowLog("SyncServer", "SYSTEM表打开失败！");
		return "";
	}
		
	int iSystemNum2 = 0;
	if (!_qryCustom.IsEmpty())
	{
		iSystemNum2 = _qryCustom.FieldByName("system_number2").AsInteger;	
	}
	else
	{
		// 解决system表中无当天记录导致序号异常问题
		_qryCustom.Append();
		_qryCustom.FieldByName("system_modalitytype").Value = _modalitytype;
        _qryCustom.FieldByName("system_today").Value = Date();
        _qryCustom.FieldByName("system_number").Value = 0;
        _qryCustom.FieldByName("system_number2").Value = 0;
        _qryCustom.FieldByName("system_number3").Value = 0;        
        _qryCustom.Post();
	}
	////
	int a[1];
	a[0] = iSystemNum2 + 1;
	String sLoginGuid = Format("%.3d", a);
	////
	_qryCustom.Edit();
	_qrycustom.FieldByName("system_number2").Value = iSystemNum2 + 1;
	_qryCustom.Post();
	
	return sLoginGuid;
}
//---------------------------------------------------------------------------

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