//---------------------------------------------------------------------------
//注意：自动编号脚本在OnBeforePost()事件中执行!
//---------------------------------------------------------------------------
// String _opacsconnection; 	约定的输入变量名
// String _modalitytype; 		约定的输入变量名
// int _number; 				约定的输入变量名：值1为测试模式，0为非测试。
// String _guid; 				约定的输出变量名
// TADOQuery _qryStudy; 		外部已定义的对象
// TADOQuery _qryCustom; 		外部已定义的对象

//---------------------------------------------------------------------------
String _opacsconnection;	
String _modalitytype;
int _number;
String _guid = "";

//---------------------------------------------------------------------------
int GetNewExamineID()
{
	// 根据安装情况，下面的连接和查询字符串可能需要修改。
	String sRisConnection = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=eRadMiniPacs;Data Source=Server"; 
	String sRisQuery = "select MAX(ExamineID) examid from study";
	int iExamineID = 0;
	////
  	TADOQuery qryRisCustom = new TADOQuery(nil);
  	qryRisCustom.ConnectionString = sRisConnection; 
  	qryRisCustom.SQL.Text = sRisQuery;
  	qryRisCustom.Open();
  	if (!qryRisCustom.Bof || !qryRisCustom.Eof)
    	iExamineID = qryRisCustom.FieldByName("examid").AsInteger + 1;
  	delete qryRisCustom;
  	////
    return iExamineID;
}

//---------------------------------------------------------------------------
int GetNewStudyGuid()
{
	// 根据安装情况，下面的连接和查询字符串可能需要修改。
	String sConnection = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=opacs;Data Source=Server"; 
	String sQuery = "select max(study_guid) study_guid from study where ISNUMERIC(study_guid)=1 and study_modalitytype='US'";
	int iStudyGuid = 0;
	////
  	TADOQuery qryCustom = new TADOQuery(nil);
  	qryCustom.ConnectionString = sConnection; 
  	qryCustom.SQL.Text = sQuery;
  	qryCustom.Open();
  	if (!qryCustom.Bof || !qryCustom.Eof)
  	{
  		try
  		{
    		iStudyGuid = qryCustom.FieldByName("study_guid").AsInteger + 1;
    	}
    	except
    	{
    	}
    }
  	delete qryCustom;
  	////
    return iStudyGuid;
}

//---------------------------------------------------------------------------
// 主程序：生成RIS检查号
//---------------------------------------------------------------------------
{
	int iExamineID = GetNewExamineID();
	int iStudyGuid = GetNewStudyGuid();
	if (iStudyGuid > iExamineID)
		_guid = IntToStr(iStudyGuid);
	else
		_guid = IntToStr(iExamineID);
}
