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

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

//---------------------------------------------------------------------------
String GetYearGuid()
{
	String sQuery = "";
	if (Pos("Jet.OLEDB", _opacsconnection) > 0)
	{
		sQuery = "select max(study_guid) as max_study_guid from study " + 
		"where ISNUMERIC(study_guid)<>0 and LEN(study_guid)=5 " + 
		"and YEAR(study_dttm)=YEAR(NOW()) " + 
		"and study_modalitytype='" + _modalitytype + "'";
	}
	else
	{
		sQuery = "select max(study_guid) as max_study_guid from study " + 
		"where ISNUMERIC(study_guid)=1 and LEN(study_guid)=5 " + 
		"and YEAR(study_dttm)=YEAR(GETDATE()) " + 
		"and study_modalitytype='" + _modalitytype + "'";
	}
	////
	String sMaxGuid = "";
	String sStudyGuid = "00001";
  	_qryCustom.Close();
  	_qryCustom.SQL.Text = sQuery;
	try
	{
	  	_qryCustom.Open();
	  	sMaxGuid = _qryCustom.FieldByName("max_study_guid").AsString;
	  	if (sMaxGuid == "")
	  	{
	  		sStudyGuid = "00001";
		}
		else
		{
			int iStudyGuid = StrToInt(Trim(sMaxGuid)) + 1;
		  	Variant a[1];
			a[0] = iStudyGuid;
			sStudyGuid = Format("%.5d", a);	
		}
	}
	except
	{
		ShowMessage(sMaxGuid + "是错误的检查号！请立即手工纠正！");    		
	}
  	////
    return sStudyGuid;
}
 
//---------------------------------------------------------------------------
// 主程序
//---------------------------------------------------------------------------
{
  	_guid = GetYearGuid();
}
