//---------------------------------------------------------------------------
// ★★★★★辅助函数库★★★★★
//---------------------------------------------------------------------------
// 已经导出函数
//---------------------------------------------------------------------------
// String AddWhereCondition(String sSQL, String sWhere);		
// String AnsiToUtf8(String sAnsi);																																																																				
// String ConvertToDicom(int iImageKey, int iSwapField);																																																																						
// String ConvertToJpeg(int iImageKey);																																																																						
// String CreateGuid();																																																																						
// String CreateDicomGuid(int iKey);																																																																						
// String CreateSimpleGuid(int iKey);	
// Variant CallWebService(String sURL, String sMethod, String sValue);																																																																					
// int CStoreSCU(String sImageFile, String sServerName, String sServerTitle,																																																																						
//	 int iServerPort, int iCompressType, int iCompressFactor, int dwMilliseconds);																																																																						
// int DicomCompress(String sDicomFile, String sNewDicomFile, 
//	 String sTransferSyntax, int iCompressFactor);
// bool DeleteFile(String sFileName);
// int DeleteVPReport(String sQuerySQL);
// int DeleteVPFilm(String sQuerySQL);
// bool FileExists(String sFileName);
// bool FindField(TADOQuery dataset, String sFieldName);
// String GetApplicationPath();																																																																						
// String GetConfigStr(String sSection, String sItem);																																																																						
// int GetConfigInt(String sSection, String sItem);																																																																						
// int GetRecordCount(TADOQuery dataset);																																																																						
// String GetLocalPath();																																																																						
// String GetSharePath();																																																																						
// int GetTickCount();																																																																						
// String GetTableNameByIP(String sTablePrefix);// 根据现有表名返回一个带IP后缀的新表名																																																																						
// int HIWORD(int dwValue);																																																																						
// String InputDialog(String sCaption, String sPrompt);// 返回"MB_CANCEL"代表取消																																																																						
// int LOWORD(int dwValue);
// String JsonGetValue(String sJson, String sName);																																																																						
// bool RemoveDir(String sDirName);
// int RunConsoleProgram(String sCommandLine);
// void ShowLog(String sSection, String sLogInfo, bool bForceSave = false);																																																																						
// void SetRecNo(TADOQuery dataset, int iRecNo);																																																																						
// void SetFilter(TADOQuery dataset, String sFilter, bool bFiltered);																																																																						
// String SubtractString(String sSource, String sDelete, char cDelimiter = ' ');
// int BeginTrans(TADOConnection pConnection);
// SendNetMsg(String sMsgType, String sMsgPara, String sIPList="");
// void CommitTrans(TADOConnection pConnection);
// bool InTransaction(TADOConnection pConnection);
// void RollbackTrans(TADOConnection pConnection);
// int LoadLibraryM(String sDllName);	
// Variant CallLibraryM(int hInst, String sFunctionName, int iParamCount, 
// 	 Variant par1, Variant par2, Variant par3, Variant par4, Variant par5);
// bool __fastcall FreeLibraryM(int hInst);																																																																				
// int MessageBox(String sMessage, String sCaption, int iFlags);																																																																						
//	参数：iFlags取值: 0-询问对话框,1-提示对话框,2-警告对话框,3-错误对话框；																																																																						
//	返回：点击OK按钮返回1，否则返回0 					
// String Utf8ToAnsi(String sUtf8);																																																																	
// bool HasSubRole(String sRoleField);   
// bool IsFinishSlice(int iStudyKey);
// bool IsMaterialOwner(int iStudyKey);
// bool IsValidationOwner(int iStudyKey); 
      
//---------------------------------------------------------------------------
// Dicom TransferSyntax
//---------------------------------------------------------------------------
#define UID_IMPLICIT_VR_LITTLE_ENDIAN	"1.2.840.10008.1.2"
#define UID_EXPLICIT_VR_LITTLE_ENDIAN	"1.2.840.10008.1.2.1"
#define UID_EXPLICIT_VR_BIG_ENDIAN		"1.2.840.10008.1.2.2"
#define UID_JPEG_BASELINE_1				"1.2.840.10008.1.2.4.50" // 有损!
#define UID_JPEG_EXTENDED_2_4			"1.2.840.10008.1.2.4.51"
#define UID_JPEG_EXTENDED_3_5			"1.2.840.10008.1.2.4.52"
#define UID_JPEG_LOSSLESS_NONHIER_14	"1.2.840.10008.1.2.4.57"
#define UID_JPEG_LOSSLESS_NONHIER_15	"1.2.840.10008.1.2.4.58"
#define UID_JPEG_LOSSLESS_NONHIER_14B	"1.2.840.10008.1.2.4.70" // 无损!
#define UID_JPEG2000_LOSSLESS_ONLY 		"1.2.840.10008.1.2.4.90"
#define UID_JPEG2000					"1.2.840.10008.1.2.4.91"
#define UID_RLE_LOSSLESS				"1.2.840.10008.1.2.5"

//---------------------------------------------------------------------------
// 用于初始化二维数组
//---------------------------------------------------------------------------
void SetArrayItem(Variant& array2, int& i, String& first, String& second)
{
	array2[0][i] = first;
	array2[1][i] = second;
	i++;
}

//---------------------------------------------------------------------------
// 用于初始化存储过程参数
//---------------------------------------------------------------------------
void AddParameter(TADOStoredProc sp, String name, TFieldType ft, int dir, int size, Variant v)
{
    TParameter param = nil;
    param = sp.Parameters.AddParameter();
    param.Name = name;
	param.DataType = ft;
	param.Direction = dir;
	param.Size = size;
	param.Value = v;
}

//---------------------------------------------------------------------------
// 用于初始化存储过程参数
//---------------------------------------------------------------------------
Variant GetParameterValue(TADOStoredProc sp, String name)
{
    TParameter param = nil;
    for (int i=0; i<sp.Parameters.Count; i++)
    {
    	param = sp.Parameters[i];
    	if (param.Name == name)
    		return param.Value;
	}
	return nil;
}

//---------------------------------------------------------------------------
// 用于判断字段是否存在
//---------------------------------------------------------------------------
TField IsExistField(TADOQuery dataset, String sFieldName)
{
    TField pField = nil;
    try
    {
    	pField = dataset.FieldByName(sFieldName);
    }
    except 
    {    	
	}
    return pField;
}       	

//---------------------------------------------------------------------------
// 根据年龄计算生日
//---------------------------------------------------------------------------
TDateTime CalcBirthday(String sStudyAge, TDateTime dtStudy)
{
    TDateTime dtBirthday = 0;
    if (sStudyAge != "")
    {
        String sAgeUnit = Copy(sStudyAge, Length(sStudyAge)-1, 2);
        sStudyAge = Copy(sStudyAge, 1, Length(sStudyAge)-2);
        if (sAgeUnit == "岁")
            dtBirthday = dtStudy - StrToInt(sStudyAge) * 365.25;
        else if (sAgeUnit == "月")
            dtBirthday = dtStudy - StrToInt(sStudyAge) * 30.5;
        else if (sAgeUnit == "周")
            dtBirthday = dtStudy - StrToInt(sStudyAge) * 7.0;
        else if (sAgeUnit == "天")
            dtBirthday = dtStudy - StrToInt(sStudyAge) * 1.0;
        else
            dtBirthday = dtStudy - StrToInt(sStudyAge) * 365.25;
    }
    return dtBirthday;
}

//---------------------------------------------------------------------------
// 根据生日计算年龄
//---------------------------------------------------------------------------
String CalcStudyAge(TDateTime dtBirthday, TDateTime dtStudy)
{
    String sStudyAge = "";
    if (dtBirthday < dtStudy)
    {
        double fStudyAge = dtStudy - dtBirthday;
        String sAgeUnit = "";
        if (fStudyAge >= 365.25)
        {
            fStudyAge = fStudyAge/365.25;
            sAgeUnit = "岁";
        }
        else if (fStudyAge >= 30.5)
        {
            fStudyAge = fStudyAge/30.5;
            sAgeUnit = "月";
        }
        else if (fStudyAge >= 7.0)
        {
            fStudyAge = fStudyAge/7.0;
            sAgeUnit = "周";
        }
        else
        {
            fStudyAge = fStudyAge;
            sAgeUnit = "天";
        }
        sStudyAge = IntToStr(int(fStudyAge+0.5)) + sAgeUnit;
    }
    return sStudyAge;
}

//---------------------------------------------------------------------------
// 判断是否链接的ACCESS数据库
//---------------------------------------------------------------------------
bool IsConnetAccessDB(String sConnection)
{
	if (Pos("Jet.OLEDB", sConnection) > 0)
		return true;
		
	return false;
}

//---------------------------------------------------------------------------
// 判断是否链接的SQLSERVER数据库
//---------------------------------------------------------------------------
bool IsConnetSQLServer(String sConnection)
{
	if (Pos("SQLOLEDB", sConnection) > 0)
		return true;
		
	return false;
}

//---------------------------------------------------------------------------
// 将ACCESS数据库下的查询语句转换为SQL语句
//---------------------------------------------------------------------------
String AccessToSQL(String sQuerySQL)
{
    while (Pos("#", sQuerySQL) > 0)
    {
        Insert("\'", sQuerySQL, Pos("#", sQuerySQL));
        Delete(sQuerySQL, Pos("#", sQuerySQL), 1);
    }

    return sQuerySQL;
}

//---------------------------------------------------------------------------
// 检测字符串是否能转换为数字
//---------------------------------------------------------------------------
bool IsNumString(String sString)
{
	String sStrNum = sString;
	try
	{
		StrToFloat(sStrNum);
	}
	except
	{
		return false;
	}
	
	return true;
}

//---------------------------------------------------------------------------
// 替换字符串分隔符
//---------------------------------------------------------------------------
String ReplaceCommaText(String sText, String sDelimiter)
{
	String sResult = "";
	TStringList pText = new TStringList;
	pText.CommaText = sText;
	for (int i = 0; i < pText.Count()-1; i++)
	{
		sResult += pText[i] + sDelimiter; 
	}
	sResult += pText[pText.Count()-1];
	delete pText;
	
	return sResult;
}

//---------------------------------------------------------------------------

// 主程序
{
    // Nothing need to do.
}
//---------------------------------------------------------------------------
