//---------------------------------------------------------------------------
//注意：自动编号脚本在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& array3, int& i, String& first, String& second, String& third)
{
	array3[0][i] = first;
	array3[1][i] = second;
	array3[2][i] = third;
	i++;
}

//---------------------------------------------------------------------------
// 前缀 + 位数, 返回值中不包括前缀
//---------------------------------------------------------------------------
String CreateStudyGuid(int iBits)  
{
	String sQuery = "";
	if (Pos("Jet.OLEDB", _opacsconnection) > 0)
	{	
		sQuery = "select max(study_guid) as max_study_guid from study " + 
		"where YEAR(study_dttm)=YEAR(NOW()) " + 
		"and study_modalitytype='" + _modalitytype + "'";
	}
	else
	{
		sQuery = "select max(study_guid) as max_study_guid from study " + 
		"where YEAR(study_dttm)=YEAR(GETDATE()) " + 
		"and study_modalitytype='" + _modalitytype + "'";
	}
	if (sGuidPrefix == "")
	{
        sQuery = sQuery + " and ISNUMERIC(LEFT(study_guid,1))=1 and LEN(study_guid)=" + IntToStr(iBits);		
	}
	else
	{
		String sLength = IntToStr(Length(sGuidPrefix));
		sQuery = sQuery + " and LEFT(study_guid, " + sLength + ") = '" + sGuidPrefix + "'";
	}

	Variant a[1];
	a[0] = 1;
	String sFormat = "%." + IntToStr(iBits) + "d";
	String sStudyGuid = Format(sFormat, a);;
	////
	String sMaxGuid = "";
  	_qryCustom.Close();
  	_qryCustom.SQL.Text = sQuery;
	try
	{	  	
	  	_qryCustom.Open();
	  	sMaxGuid = _qryCustom.FieldByName("max_study_guid").AsString;
		if (sMaxGuid == "")
	  	{
			sStudyGuid = "00001";
		}
		else
		{			
			int iStudyGuid = StrToInt(Copy(Trim(sMaxGuid), Length(sGuidPrefix) + 1, 100)) + 1;
		    a[0] = iStudyGuid;
            sStudyGuid = Format(sFormat, a);
		}
	}
	except
	{
		ShowMessage(sMaxGuid + "是错误的检查号！请立即手工纠正！");  	
	}
	return sStudyGuid;
}

//---------------------------------------------------------------------------
// 前缀 + 年份 + 位数, 返回值中不包括前缀
//---------------------------------------------------------------------------
String CreateYearGuid(int iBits)    
{
	int iGuidBits = 4 + iBits;
	////
	// 根据安装情况，下面的连接和查询字符串可能需要修改。
	// 适用于SQL Server
	String sQuery = "select max(study_guid) as max_study_guid from study " + 
		"where YEAR(study_dttm)=YEAR(GETDATE()) " + 
		"and study_modalitytype='" + _modalitytype + "'";
	/*
	// 适用于Access
	String sQuery = "select max(study_guid) as max_study_guid from study " + 
		"where YEAR(study_dttm)=YEAR(NOW()) " + 
		"and study_modalitytype='" + _modalitytype + "'";
	*/

	if (sGuidPrefix == "")
	{
        sQuery = sQuery + " and ISNUMERIC(LEFT(study_guid,1))=1 and LEN(study_guid)=" + IntToStr(iGuidBits);		
	}
	else
	{
		String sLength = IntToStr(Length(sGuidPrefix));
		sQuery = sQuery + " and LEFT(study_guid, " + sLength + ") = '" + sGuidPrefix + "'";
	}

	Variant a[1];
	a[0] = 1;
	String sFormat = "%." + IntToStr(iBits) + "d";
	String sStudyGuid = Format(sFormat, a);;
	////
	String sMaxGuid = "";
  	_qryCustom.Close();
  	_qryCustom.SQL.Text = sQuery;
	try
	{	  	
	  	_qryCustom.Open();
	  	sMaxGuid = _qryCustom.FieldByName("max_study_guid").AsString;
	  	if (sMaxGuid == "")
	  	{
            String sYear = FormatDateTime("yyyy", Now());
            sStudyGuid = sYear + sStudyGuid;
		}
		else
		{			
			int iStudyGuid = StrToInt(Copy(Trim(sMaxGuid), Length(sGuidPrefix)+5, 100)) + 1;;
			a[0] = iStudyGuid;
            sStudyGuid = FormatDateTime("yyyy", Now()) + Format(sFormat, a);
		}
	}
	except
	{
		ShowMessage(sMaxGuid + "是错误的检查号！请立即手工纠正！");  		
	}
  	////
    return sStudyGuid;
}

//---------------------------------------------------------------------------
String GetStudyGuid()
{
	// 确定编号前缀
	int iFieldCount = 0;
    String sFieldPairs[3][100];
    SetArrayItem(sFieldPairs, iFieldCount, "组织", 		"常规",		"");
    SetArrayItem(sFieldPairs, iFieldCount, "细胞", 		"常规",		"");
    SetArrayItem(sFieldPairs, iFieldCount, "细胞", 		"液基",		"C");
	////
	String sExamKind = _qryStudy.FieldByName("study_examkind").AsString;
	String sExamName = _qryStudy.FieldByName("study_examname").AsString;
    for (int i = 0; i < iFieldCount; i++)
    {
		if ((Pos(sFieldPairs[0][i], sExamKind) > 0) && 
			(Pos(sFieldPairs[1][i], sExamName) > 0))
        {
        	sGuidPrefix = sFieldPairs[2][i];
        	break;
        }
    }
	/////
 	String sStudyGuid = "";
    if (sExamName == "液基")
    {
        sStudyGuid = CreateStudyGuid(5);
    }
    else if (sExamKind == "组织")
    {
        sStudyGuid = CreateYearGuid(5);
    }
    else if (sExamKind == "细胞")
    {
        sStudyGuid = CreateYearGuid(4);
    }
  	////
    return sGuidPrefix + sStudyGuid;
}
 
//---------------------------------------------------------------------------
// 主程序
//---------------------------------------------------------------------------
{
  	_guid = GetStudyGuid();
}
