using Microsoft.EntityFrameworkCore; using static Back.Common.ExclusiveAttribute; using System.ComponentModel.DataAnnotations; using System.Data; using System.Globalization; using System.Reflection; using System.Security.Cryptography; using Shared.DTOs; namespace Back.Common { public static class ExtentionMethods { public static List Clone(this IList listToClone) where T : ICloneable { return listToClone.Select(item => (T)item.Clone()).ToList(); } public static string GetEnumDisplayName(this Enum enumType) { return enumType.GetType().GetMember(enumType.ToString()) .First() .GetCustomAttribute() .Name; } public static string GetEnumHexColor(this Enum enumType) { return enumType.GetType().GetMember(enumType.ToString()) .First() .GetCustomAttribute() ._ColorHex; } public static string encrypted(this string txt) { HashAlgorithm hash = new MD5CryptoServiceProvider(); var bytes = System.Text.Encoding.UTF8.GetBytes(txt); var hashedBytes = hash.ComputeHash(bytes); string res = BitConverter.ToString(hashedBytes).Replace("-", string.Empty); return res; } public static string ConvertMiladiToShamsi(this DateTime date) { PersianCalendar PersianCal = new PersianCalendar(); return PersianCal.GetYear(date).ToString("0000") + PersianCal.GetMonth(date).ToString("00") + PersianCal.GetDayOfMonth(date).ToString("00") ; } public static string ConvertMiladiToShamsiAndGetYearMonth(this DateTime date) { PersianCalendar PersianCal = new PersianCalendar(); return PersianCal.GetYear(date).ToString("0000") + PersianCal.GetMonth(date).ToString("00"); } public static string ConvertMiladiToShamsiByTime(this DateTime date) { PersianCalendar PersianCal = new PersianCalendar(); return PersianCal.GetYear(date).ToString("0000") + PersianCal.GetMonth(date).ToString("00") + PersianCal.GetDayOfMonth(date).ToString("00") + date.Hour.ToString("00") + date.Minute.ToString("00") + date.Second.ToString("00") ; } public static string ShamciToFormatShamci(this string str) { if (string.IsNullOrEmpty(str)) return ""; return str.Substring(0, 4) + "/" + str.Substring(4, 2) + "/" + str.Substring(6, 2); } public static DateTime ToMiladi(this string value) { PersianCalendar p = new PersianCalendar(); return p.ToDateTime(Convert.ToInt32(value.Substring(0, 4)), Convert.ToInt32(value.Substring(4, 2)), Convert.ToInt32(value.Substring(6, 2)), DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond); } public static DateTime ToMiladiByTime(this string value) { PersianCalendar p = new PersianCalendar(); return p.ToDateTime(Convert.ToInt32(value.Substring(0, 4)), Convert.ToInt32(value.Substring(4, 2)), Convert.ToInt32(value.Substring(6, 2)), Convert.ToInt32(value.Substring(8, 2)), Convert.ToInt32(value.Substring(10, 2)), Convert.ToInt32(value.Substring(12, 2)), 0); } public static async Task> Paging(this IQueryable values, int pageId, int take) { if (/*values.Count()<1000 && */pageId == 0 && take == 0) { return new PagingDto(values.Count(), 1, await values.ToListAsync()); } else { int skip = (pageId - 1) * take; int rowCount = values.Count(); int PageCount = rowCount % take == 0 ? rowCount / take : rowCount / take + 1; return new PagingDto(rowCount, PageCount, await values.Skip(skip).Take(take).ToListAsync()); } } public static async Task> Paging(this IEnumerable values, int pageId, int take) { if (/*values.Count()<1000 && */pageId == 0 && take == 0) { return new PagingDto(values.Count(), 1, values.ToList()); } else { int skip = (pageId - 1) * take; int rowCount = values.Count(); int PageCount = rowCount % take == 0 ? rowCount / take : rowCount / take + 1; return new PagingDto(rowCount, PageCount, values.Skip(skip).Take(take).ToList()); } } public static System.Linq.Expressions.Expression> GetFunc(string Fild, string Value) { Type type = typeof(TEntity); // System.Linq.Expressions.Expression> convert = s => type.GetProperty(Fild) != null && s.GetType().GetProperty(Fild).GetValue(s) == Value; return Entity => type.GetProperty(Fild) != null && Entity.GetType().GetProperty(Fild).GetValue(Entity) == Value; } public static DataTable ConvertStringBase64toDataTable(this string value, string SheetName = null) { //try //{ // MemoryStream stream1 = new MemoryStream(Convert.FromBase64String(value)); // WorkBook workbook = WorkBook.Load(stream1); // WorkSheet sheet = SheetName == null ? workbook.DefaultWorkSheet : workbook.GetWorkSheet(SheetName); // return sheet.ToDataTable(true); //} //catch (Exception) //{ return new DataTable(); // } } } }