//---------------------------------------------------------------------------
//注意：自动编号脚本在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, "心脏彩超", 		"H");
    SetArrayItem(sFieldPairs, iFieldCount, "四肢血管", 		"V");
    SetArrayItem(sFieldPairs, iFieldCount, "腹部彩超", 		"A");
    SetArrayItem(sFieldPairs, iFieldCount, "妇科彩超", 		"G");
    SetArrayItem(sFieldPairs, iFieldCount, "产科彩超", 		"O");
    SetArrayItem(sFieldPairs, iFieldCount, "专家会诊", 		"C");
    SetArrayItem(sFieldPairs, iFieldCount, "浅表小器官", 	"S");
    SetArrayItem(sFieldPairs, iFieldCount, "介入彩超", 		"I");

	////
	String sExamKind = _qryStudy.FieldByName("study_examkind").AsString;
    for (int i = 0; i < iFieldCount; i++)
    {
		if (Pos(sFieldPairs[0][i], sExamKind) > 0)
        {
        	sGuidPrefix = sFieldPairs[1][i];
        	break;
        }
    }
	////
	int iLoginState = _qryStudy.FieldByName("login_state").AsInteger;
	int iReportState = _qryStudy.FieldByName("report_state").AsInteger;
	////
	String sQuery = "";
	if (Pos("Jet.OLEDB", _opacsconnection) > 0) // Access
	{
		if ((iLoginState == -2) && (iReportState == 0)) // 预约待诊断
		{
			return "";
		}
		else
		{
			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
	{
		if ((iLoginState == -2) && (iReportState == 0)) // 预约待诊断
		{
			return "";
		}
		else
		{
			sQuery = "select max(login_guid) as max_login_guid from study " + 
			"where datediff(day, study_dttm, GETDATE())=0 or " +
			"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();
}