//---------------------------------------------------------------------------
// 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 = "";
    sQuerySQL = "select * from images left join study on images.study_key = study.study_key "
    	+ "where study.study_modalitytype='" + _modalitytype + "' and study.patient_state=1 "
    	+ "and study.report_state=2 and (images.image_state is null or images.image_state=0)";
    ////
    if (IsConnetSQLServer(_opacsconnection))
        sQuerySQL = AccessToSQL(sQuerySQL);
    
    return sQuerySQL;
}
//---------------------------------------------------------------------------

int ExecDicomSend()
{
    // 设置连接参数
    String sServerName = "10.0.90.164";
    String sServerTitle = "PACSSERVER1";
    int iServerPort = 104;
    int iCompressType = 0;
    int iCompressFactor = 0;
    int dwMilliseconds = 30 * 1000;

    // 获取传送图像清单
    _qryImage.Close();
    _qryImage.SQL.Text = BuildQuerySQL();
    _qryImage.Open();
    if (_qryImage.IsEmpty())
        return 0;

    // 图像传送
    bool bSuccess = true;
    String sSendLog = "";
    ////
    _qryImage.First();
    while (!_qryImage.Eof)
    {
        int iImageKey = _qryImage.FieldByName("image_key").AsInteger;
        String sImageFormat = _qryImage.FieldByName("image_format").AsString;
        ////
        String sImageFile = ConvertToDicom(iImageKey);
        int iErrorCode = CStoreSCU(sImageFile, sServerName, sServerTitle, iServerPort,
            iCompressType, iCompressFactor, dwMilliseconds);
        if (Uppercase(sImageFormat) != "DCM")
        	DeleteFile(sImageFile);
        if (iErrorCode == 0)
        {
            bSuccess = true;
            sSendLog = "发送图像【" + sImageFile + "】到目标系统成功！";
        }
        else
        {
            bSuccess = false;
            sSendLog = "发送图像【" + sImageFile + "】到目标系统失败！错误代码【"+ IntToStr(iErrorCode) +"】";
        }
        ShowLog("SyncServer", sSendLog);
        ////
        if (bSuccess)
        {
            _qryImage.Edit();
            _qryImage.FieldByName("image_state").Value = 1;
            _qryImage.Post();
        }
        ////
        _qryImage.Next();
    }
}
//---------------------------------------------------------------------------

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

