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

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

//---------------------------------------------------------------------------
String sGuidPrefix = "";

//---------------------------------------------------------------------------
void SetArrayItem(Variant& array2, int& i, String& first, String& second)
{
	array2[0][i] = first;
	array2[1][i] = second;
	i++;
}

//---------------------------------------------------------------------------
String GetDayGuid()
{
	// 确定编号前缀,按设备类型分类编号
	int iFieldCount = 0;
    String sFieldPairs[2][100];
    SetArrayItem(sFieldPairs, iFieldCount, "CT", 		"C");
    SetArrayItem(sFieldPairs, iFieldCount, "MR", 		"M");
    //SetArrayItem(sFieldPairs, iFieldCount, "", 		"");    // 根据实际情况增加
    //SetArrayItem(sFieldPairs, iFieldCount, "", 		"");	// 根据实际情况增加
	////
	String sExamKind = _qryStudy.FieldByName("study_examkind").AsString;
	if (sExamKind == "")
		return ;
	////	
    for (int i = 0; i < iFieldCount; i++)
    {
		if (Pos(sFieldPairs[0][i], sExamKind) > 0)
        {
        	sGuidPrefix = sFieldPairs[1][i];
        	break;
        }
    }
	////
	String sQuery = "";
	if (Pos("Jet.OLEDB", _opacsconnection) > 0) // Access
	{
		sQuery = "select max(login_guid) as max_login_guid from study " + 
		"where datediff(day, study_dttm, NOW())=0 " + 
		"and study_modalitytype='" + _modalitytype + "'";
	}	
	else // SQL Server
	{
		sQuery = "select max(login_guid) as max_login_guid from study " + 
		"where datediff(day, study_dttm, GETDATE())=0 " + 
		"and study_modalitytype='" + _modalitytype + "'";
	}
	if (sGuidPrefix == "")
	{
		sQuery = sQuery + " and ISNUMERIC(login_guid)=1 and LEN(login_guid)=3 ";
	}
	else
	{
		String sLength = IntToStr(Length(sGuidPrefix));
		sQuery = sQuery + " and LEFT(login_guid, " + sLength + ") = '" + sGuidPrefix + "'";
	}
	////
	String sMaxGuid = "";
	String sLoginGuid = "001";
  	_qryCustom.Close();
  	_qryCustom.SQL.Text = sQuery;
	try
	{
	  	_qryCustom.Open();
	  	sMaxGuid = _qryCustom.FieldByName("max_login_guid").AsString;
	  	if (sMaxGuid == "")
	  	{
	  		sLoginGuid = "001";
		}
		else
		{
			int iLoginGuid = StrToInt(Copy(Trim(sMaxGuid), Length(sGuidPrefix)+1, 100)) + 1;
		  	Variant a[1];
			a[0] = iLoginGuid;
			sLoginGuid = Format("%.3d", a);	
		}
	}
	except
	{
		ShowMessage(sMaxGuid + "是错误的登记号！请立即手工纠正！");    		
	}
  	////
    return sGuidPrefix + sLoginGuid;
}
 
 //---------------------------------------------------------------------------
// 主程序
//---------------------------------------------------------------------------
{
  	_guid = GetDayGuid();
  	
  	// login_guid为空时，填入新的login_guid
    if (Length(_qryStudy.FieldByName("login_guid").AsString) == 0)
    	_qryStudy.FieldByName("login_guid").Value = _guid; 		  	
}
