//---------------------------------------------------------------------------
// 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()
{
    // 获取图像清单
    _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 sImageFile = ConvertToDicom(iImageKey);
        if (sImageFile != "")
        {
            bSuccess = true;
            sSendLog = "转换图像【" + sImageFile + "】为DICOM格式成功！";
        }
        else
        {
            bSuccess = false;
            sSendLog = "转换图像【" + sImageFile + "】为DICOM格式失败！";
        }
        ShowLog("SyncServer", sSendLog);
        ////
        if (bSuccess)
        {
            _qryImage.Edit();
            _qryImage.FieldByName("image_state").Value = 1;
            _qryImage.Post();
        }
        ////
        _qryImage.Next();
    }
}
//---------------------------------------------------------------------------

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

