123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877 |
- using Microsoft.AspNetCore.Http;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml;
- namespace Utils
- {
- public class ConvertRule
- {
- public static decimal ToDecimal(object val)
- {
- decimal result = default(decimal);
- if (val != null && val.ToString() != "")
- {
- try
- {
- try
- {
- result = Convert.ToDecimal(val);
- return result;
- }
- catch
- {
- result = decimal.Parse(val.ToString());
- return result;
- }
- }
- catch
- {
- }
- }
- return result;
- }
- public static object ToDecimalObj(object val)
- {
- object result = DBNull.Value;
- if (val != null && val.ToString() != "")
- {
- try
- {
- result = decimal.Parse(val.ToString());
- }
- catch
- {
- }
- }
- return result;
- }
- public static double ToDouble(object val)
- {
- double result = 0.0;
- if (val != null && val.ToString() != "")
- {
- try
- {
- result = double.Parse(val.ToString());
- }
- catch
- {
- }
- }
- return result;
- }
- public static object ToDoubleObj(object val)
- {
- object result = DBNull.Value;
- if (val != null && val.ToString() != "")
- {
- try
- {
- result = double.Parse(val.ToString());
- }
- catch
- {
- }
- }
- return result;
- }
- public static int ToInt(object val)
- {
- int result = 0;
- if (val != null && val.ToString() != "")
- {
- try
- {
- result = int.Parse(val.ToString().Replace(",", ""));
- }
- catch
- {
- }
- }
- return result;
- }
- public static int? ToIntNull(object val)
- {
- int? result = null;
- if (val != null && val.ToString() != "")
- {
- try
- {
- result = int.Parse(val.ToString().Replace(",", ""));
- return result;
- }
- catch
- {
- }
- }
- return result;
- }
- public static object ToIntObj(object val)
- {
- object result = DBNull.Value;
- if (val != null && val.ToString() != "")
- {
- try
- {
- result = int.Parse(val.ToString().Replace(",", ""));
- }
- catch
- {
- }
- }
- return result;
- }
- public static long ToLong(object val)
- {
- long result = 0L;
- if (val != null && val.ToString() != "")
- {
- try
- {
- result = long.Parse(val.ToString());
- }
- catch
- {
- }
- }
- return result;
- }
- public static object ToLongObj(object val)
- {
- object result = DBNull.Value;
- if (val != null && val.ToString() != "")
- {
- try
- {
- result = long.Parse(val.ToString());
- }
- catch
- {
- }
- }
- return result;
- }
- public static object ToDate(object val)
- {
- object result = DBNull.Value;
- if (val != null && val.ToString() != "")
- {
- try
- {
- result = DateTime.Parse(val.ToString());
- }
- catch
- {
- }
- }
- return result;
- }
- public static DateTime? ToDateTime(object val)
- {
- DateTime? result = null;
- if (val != null && val.ToString() != "")
- {
- try
- {
- result = DateTime.Parse(val.ToString());
- return result;
- }
- catch
- {
- }
- }
- return result;
- }
- public static string ToDateString(object val, string format)
- {
- string result = "";
- if (val != null && val.ToString() != "")
- {
- try
- {
- if (DateTime.Parse(val.ToString()) != DateTime.MinValue)
- {
- result = DateTime.Parse(val.ToString()).ToString(format);
- }
- }
- catch
- {
- }
- }
- return result;
- }
- public static string ToDateString(object val, string format, string nullTimeFormat)
- {
- string result = "";
- if (val != null && val.ToString() != "")
- {
- try
- {
- if (DateTime.Parse(val.ToString()) != DateTime.MinValue)
- {
- DateTime dateTime = DateTime.Parse(val.ToString());
- result = ((dateTime.Hour != 0 || dateTime.Minute != 0 || dateTime.Second != 0) ? dateTime.ToString(format) : dateTime.ToString(nullTimeFormat));
- }
- }
- catch
- {
- }
- }
- return result;
- }
- public static string ToDateString(object val)
- {
- return ToDateString(val, "yyyy-MM-dd");
- }
- public static string ToString(object val)
- {
- string result = "";
- try
- {
- if (val != null && val != DBNull.Value)
- {
- try
- {
- result = val.ToString();
- }
- catch
- {
- }
- }
- }
- catch
- {
- }
- return result;
- }
- public static bool ToBool(object val)
- {
- bool result = false;
- try
- {
- if (val != null && val != DBNull.Value)
- {
- try
- {
- result = bool.Parse(val.ToString());
- }
- catch
- {
- }
- }
- }
- catch
- {
- }
- return result;
- }
- public static string[] ArrayConcat(object[] arr1, object[] arr2)
- {
- int num = 0;
- if (arr1 != null)
- {
- num += arr1.Length;
- }
- if (arr2 != null)
- {
- num += arr2.Length;
- }
- string[] array = new string[num];
- int num2 = 0;
- if (arr1 != null)
- {
- arr1.CopyTo(array, num2);
- num2 += arr1.Length;
- }
- if (arr2 != null)
- {
- arr2.CopyTo(array, num2);
- num2 += arr2.Length;
- }
- return array;
- }
- public static string JoinArray(object[] arr, string separator)
- {
- string text = "";
- int num = arr.Length;
- for (int i = 0; i < num; i++)
- {
- if (i > 0)
- {
- text += separator;
- }
- text += arr[i].ToString();
- }
- return text;
- }
- public static string JoinArrayList(ArrayList al, string separator)
- {
- string text = "";
- int count = al.Count;
- for (int i = 0; i < count; i++)
- {
- if (i > 0)
- {
- text += separator;
- }
- text += ToString(al[i]);
- }
- return text;
- }
- public static string JoinTableColumn(DataTable tb, string ColumnName, string separator)
- {
- string text = "";
- int count = tb.Rows.Count;
- for (int i = 0; i < count; i++)
- {
- if (i > 0)
- {
- text += separator;
- }
- text += ToString(tb.Rows[i][ColumnName]);
- }
- return text;
- }
- public static int FindArray(string[] arr, string val)
- {
- return FindArray(arr, val, isEgnoreCase: false);
- }
- public static int FindArray(string[] arr, string val, bool isEgnoreCase)
- {
- int result = -1;
- if (arr == null)
- {
- return result;
- }
- int num = arr.Length;
- for (int i = 0; i < num; i++)
- {
- if (isEgnoreCase)
- {
- if (arr[i].ToUpper() == val.ToUpper())
- {
- return i;
- }
- }
- else if (arr[i] == val)
- {
- return i;
- }
- }
- return result;
- }
- public static int FindArrayLike(string[] arr, string val)
- {
- int num = arr.Length;
- int length = val.Length;
- for (int i = 0; i < num; i++)
- {
- string text = arr[i];
- if (text.Length > length)
- {
- text = text.Substring(0, length);
- }
- if (text == val)
- {
- return i;
- }
- }
- return -1;
- }
- public static object GetArrayItem(object[] arr, int index)
- {
- object result = null;
- if (arr.Length > index)
- {
- result = arr[index];
- }
- return result;
- }
- public static string GetArrayItemString(object[] arr, int index)
- {
- object arrayItem = GetArrayItem(arr, index);
- return ToString(arrayItem);
- }
- public static string Concat(DataTable tb, string ColumnName, string sep)
- {
- try
- {
- string text = "";
- return Concat(tb, ColumnName, sep, "");
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static string Concat(DataTable tb, string ColumnName, string sep, string filter)
- {
- try
- {
- string text = "";
- if (!tb.Columns.Contains(ColumnName))
- {
- throw new ApplicationException($"表中未包含列{ColumnName}");
- }
- DataRow[] drs = tb.Select(filter);
- return Concat(drs, ColumnName, sep);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static string Concat(DataRow[] drs, string ColumnName, string sep)
- {
- try
- {
- string text = "";
- int num = drs.Length;
- for (int i = 0; i < num; i++)
- {
- DataRow dataRow = drs[i];
- if (i > 0)
- {
- text += sep;
- }
- text += ToString(dataRow[ColumnName]);
- }
- return text;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static DataTable GetDistinct(DataTable tb, string ColumnName, string filter)
- {
- try
- {
- if (!tb.Columns.Contains(ColumnName))
- {
- throw new ApplicationException($"表中未包含列{ColumnName}");
- }
- DataRow[] drs = tb.Select(filter);
- return GetDistinct(drs, ColumnName);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static DataTable GetDistinct(DataRow[] drs, string ColumnName)
- {
- try
- {
- DataTable dataTable = new DataTable();
- if (drs.Length != 0)
- {
- dataTable = drs[0].Table.Clone();
- }
- else
- {
- Type type = typeof(string);
- if (drs.Length != 0)
- {
- type = drs[0].Table.Columns[ColumnName].DataType;
- }
- dataTable.Columns.Add(ColumnName, type);
- }
- int num = drs.Length;
- for (int i = 0; i < num; i++)
- {
- DataRow dataRow = drs[i];
- string str = ToString(dataRow[ColumnName]);
- if (dataTable.Select(ColumnName + "='" + str + "'").Length == 0)
- {
- dataTable.ImportRow(drs[i]);
- }
- }
- return dataTable;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static DataView GetDistinct(DataView dv, string ColumnName)
- {
- try
- {
- DataTable dataTable = new DataTable();
- Type type = typeof(string);
- if (dv.Count > 0 && dv[0].Row[ColumnName].GetType() != typeof(DBNull))
- {
- type = dv[0].Row[ColumnName].GetType();
- }
- dataTable.Columns.Add(ColumnName, type);
- int count = dv.Count;
- for (int i = 0; i < count; i++)
- {
- string text = ToString(dv[i].Row[ColumnName]);
- if (dataTable.Select(ColumnName + "='" + text + "'").Length == 0)
- {
- DataRow dataRow = dataTable.NewRow();
- dataRow[ColumnName] = text;
- dataTable.Rows.Add(dataRow);
- }
- }
- return new DataView(dataTable);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static string GetDistinctStr(DataTable tb, string ColumnName, string filter, string sep)
- {
- try
- {
- if (!tb.Columns.Contains(ColumnName))
- {
- throw new ApplicationException($"表中未包含列{ColumnName}");
- }
- DataRow[] drs = tb.Select(filter);
- return GetDistinctStr(drs, ColumnName, sep);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static string GetDistinctStr(DataRow[] drs, string ColumnName, string sep)
- {
- try
- {
- DataTable distinct = GetDistinct(drs, ColumnName);
- return Concat(distinct, ColumnName, sep);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static string ToEnumString(object objValue, Type enumType)
- {
- string result = "";
- string b = ToString(objValue);
- foreach (object value in Enum.GetValues(enumType))
- {
- if (((int)value).ToString() == b)
- {
- result = value.ToString();
- break;
- }
- }
- return result;
- }
- public static string ToEnumDescription(object objValue, Type enumType)
- {
- string result = "";
- string b = ToString(objValue);
- foreach (object value in Enum.GetValues(enumType))
- {
- if (((int)value).ToString() == b)
- {
- FieldInfo field = value.GetType().GetField(value.ToString());
- DescriptionAttribute[] array = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), inherit: false);
- result = ((array.Length == 0) ? value.ToString() : array[0].Description);
- break;
- }
- }
- return result;
- }
- public static int ToEnumIndex(object objValue, Type enumType)
- {
- int result = -1;
- string b = ToString(objValue);
- foreach (object value in Enum.GetValues(enumType))
- {
- if (value.ToString() == b)
- {
- result = (int)value;
- break;
- }
- }
- return result;
- }
- public static bool IsDateStartLessThanEnd(object StartDate, object EndDate)
- {
- try
- {
- bool flag = true;
- object obj = ToDate(StartDate);
- object obj2 = ToDate(EndDate);
- if (obj == DBNull.Value || obj2 == DBNull.Value)
- {
- return true;
- }
- DateTime t = (DateTime)obj;
- DateTime t2 = (DateTime)obj2;
- return t <= t2;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static void DataRowCopy(DataRow drSrc, DataRow drDst, DataTable tbSrc, DataTable tbDst)
- {
- try
- {
- DataRowCopy(drSrc, drDst, tbSrc, tbDst, null);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static void DataRowCopy(DataRow drSrc, DataRow drDst, DataTable tbSrc, DataTable tbDst, string[] ExceptField)
- {
- try
- {
- foreach (DataColumn column in tbDst.Columns)
- {
- if (tbSrc.Columns.Contains(column.ColumnName) && FindArray(ExceptField, column.ColumnName, isEgnoreCase: true) < 0)
- {
- drDst[column.ColumnName] = drSrc[column.ColumnName];
- }
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static void DataTableCopyRow(DataTable tbSrc, DataTable tbDst)
- {
- try
- {
- foreach (DataRow row in tbSrc.Rows)
- {
- DataRow dataRow = tbDst.NewRow();
- DataRowCopy(row, dataRow, tbSrc, tbDst);
- tbDst.Rows.Add(dataRow);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static void DataTableCopyRow(DataView dvSrc, DataTable tbDst)
- {
- try
- {
- DataTable table = dvSrc.Table;
- foreach (DataRowView item in dvSrc)
- {
- DataRow row = item.Row;
- DataRow dataRow = tbDst.NewRow();
- DataRowCopy(row, dataRow, table, tbDst);
- tbDst.Rows.Add(dataRow);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static void DataTableAddColumn(DataTable tbSrc, DataTable tbDst)
- {
- try
- {
- foreach (DataColumn column in tbSrc.Columns)
- {
- if (!tbDst.Columns.Contains(column.ColumnName))
- {
- tbDst.Columns.Add(column.ColumnName, column.DataType);
- }
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static void DataRowAddDecimal(DataRow drBase, DataRow drAdd, string ColumnName)
- {
- try
- {
- drBase[ColumnName] = ToDecimal(drBase[ColumnName]) + ToDecimal(drAdd[ColumnName]);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static void DataRowAddDecimal(DataRow drBase, DataRow drAdd, string[] ColumnNames)
- {
- try
- {
- foreach (string columnName in ColumnNames)
- {
- DataRowAddDecimal(drBase, drAdd, columnName);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static string GetArrayLinkString(ArrayList ar)
- {
- return GetArrayLinkString(ar, ",");
- }
- public static string GetArrayLinkString(ArrayList ar, string separator)
- {
- string text = "";
- int count = ar.Count;
- for (int i = 0; i < count; i++)
- {
- if (text != "")
- {
- text += separator;
- }
- text += (string)ar[i];
- }
- return text;
- }
- public static int GetIntDayOfWeek(DayOfWeek d)
- {
- try
- {
- int result = 0;
- switch (d.ToString())
- {
- case "Monday":
- result = 1;
- break;
- case "Tuesday":
- result = 2;
- break;
- case "Wednesday":
- result = 3;
- break;
- case "Thursday":
- result = 4;
- break;
- case "Friday":
- result = 5;
- break;
- case "Saturday":
- result = 6;
- break;
- case "Sunday":
- result = 7;
- break;
- }
- return result;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static string FormatMM(object objMonth)
- {
- try
- {
- string text = ToString(objMonth);
- return text.PadLeft(2, "0"[0]);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static string FormatYYYYMM(object objYear, object objMonth)
- {
- try
- {
- string text = ToString(objYear);
- string text2 = ToString(objMonth);
- return text.PadLeft(4, "0"[0]) + text2.PadLeft(2, "0"[0]);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static int GetDays(string StartDate, string FinishDate)
- {
- int result = 0;
- if (!string.IsNullOrEmpty(StartDate) && !string.IsNullOrEmpty(FinishDate))
- {
- TimeSpan timeSpan = new TimeSpan(DateTime.Parse(StartDate).Ticks);
- TimeSpan ts = new TimeSpan(DateTime.Parse(FinishDate).Ticks);
- result = timeSpan.Subtract(ts).Duration().Days;
- }
- return result;
- }
- }
- }
|