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

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

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

//---------------------------------------------------------------------------
String GetYearGuid()
{
	// 确定编号前缀
	String sGuidPrefix = "";
	int iFieldCount = 0;
    String sFieldPairs[2][100];
    SetArrayItem(sFieldPairs, iFieldCount, "肠镜", 		"C");
    SetArrayItem(sFieldPairs, iFieldCount, "腹腔镜", 	"F");
    SetArrayItem(sFieldPairs, iFieldCount, "宫颈镜", 	"G");
    SetArrayItem(sFieldPairs, iFieldCount, "喉镜", 		"H");
    SetArrayItem(sFieldPairs, iFieldCount, "膀胱镜", 	"P");
    SetArrayItem(sFieldPairs, iFieldCount, "胃镜", 		"W");
    SetArrayItem(sFieldPairs, iFieldCount, "支气管镜",	"Z");
	////
	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;
        }
    }
	////
	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)=7 ";
	}
	else
	{             
		String sLength = IntToStr(Length(sGuidPrefix));              
		sQuery = sQuery + " and LEFT(study_guid, " + sLength + ") = '" + sGuidPrefix + "'";              
	}
	////
	String sMaxGuid = "";
	String sStudyGuid = "00001";
  	_qryCustom.Close();
  	_qryCustom.SQL.Text = sQuery;
	try
	{
	  	_qryCustom.Open();
	  	sMaxGuid = _qryCustom.FieldByName("max_study_guid").AsString;
	  	if (sMaxGuid == "")
	  	{
	  		String sYear = FormatDateTime("yymmdd", Now());
	    	sStudyGuid = sYear + "-0001";
		}
		else
		{	
			int iStudyGuid = StrToInt(Copy(Trim(sMaxGuid), Length(sGuidPrefix)+8, 100)) + 1;
		    Variant a[1];
			a[0] = iStudyGuid;
			sStudyGuid = FormatDateTime("yymmdd", Now()) + "-"  + Format("%.4d", a);
		}
	}
	except
	{
		ShowMessage(sMaxGuid + "是错误的检查号！请立即手工纠正！");  	
	}
  	////
    return sGuidPrefix + sStudyGuid;
}
 
//---------------------------------------------------------------------------
// 主程序
//---------------------------------------------------------------------------
{
  	_guid = GetYearGuid();
}
