Functions.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4. using Microsoft.Win32;
  5. using System.IO;
  6. using System.Windows.Media.Imaging;
  7. namespace RaspisKusach
  8. {
  9. public class Functions
  10. {
  11. // Получение направления по маршруту или поездке
  12. public static string GetRouteDirection(Trips trip)
  13. {
  14. return GetDepartureStationLocation(trip.Routes) + " - " + GetArrivalStationLocation(trip.Routes);
  15. }
  16. // Получение времени прибытия поезда на станцию
  17. public static DateTime GetArrivalTime(Stations station, Trips trip)
  18. {
  19. DateTime date = trip.TripStartDate;
  20. foreach (RoutesStations item in cnt.db.RoutesStations.Where(item => item.IdRoute == trip.IdRoute))
  21. {
  22. if (item.IdStation == station.IdStation)
  23. break;
  24. date += item.StopTime + item.TravelTime;
  25. }
  26. return date;
  27. }
  28. // Получение времени отбытия поезда со станции
  29. public static DateTime GetDepartureTime(Stations station, Trips trip)
  30. {
  31. DateTime date = trip.TripStartDate;
  32. foreach (RoutesStations item in cnt.db.RoutesStations.Where(item => item.IdRoute == trip.IdRoute))
  33. {
  34. date += item.StopTime;
  35. if (item.IdStation == station.IdStation)
  36. break;
  37. date += item.TravelTime;
  38. }
  39. return date;
  40. }
  41. // Получение станции отправления (первой)
  42. public static string GetDepartureStationLocation(Routes route)
  43. {
  44. return cnt.db.RoutesStations.Where(item => item.IdRoute == route.IdRoute).OrderByDescending(item => item.IdRouteStation).Select(item => item.Stations.Location).FirstOrDefault();
  45. }
  46. // Получение станции прибытия (последней)
  47. public static string GetArrivalStationLocation(Routes route)
  48. {
  49. return cnt.db.RoutesStations.Where(item => item.IdRoute == route.IdRoute).Select(item => item.Stations.Location).FirstOrDefault();
  50. }
  51. // Получение количества свободных мест в вагоне
  52. public static int GetAvailableSeats(Carriages carriage)
  53. {
  54. //temp
  55. return 0;
  56. }
  57. // Валидация номера телефона
  58. public static bool IsPhoneNumberCorrect(string phoneNumber)
  59. {
  60. foreach (char c in phoneNumber)
  61. if (!char.IsDigit(c))
  62. return false;
  63. if (phoneNumber.Length != 11)
  64. return false;
  65. return true;
  66. }
  67. // Проверка электронной почты на правильность ввода
  68. public static bool IsEmailCorrect(string email)
  69. {
  70. return Regex.IsMatch(email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
  71. }
  72. // Проверка на уникальность электронной почты
  73. public static bool IsEmailAlreadyTaken(string Email)
  74. {
  75. return cnt.db.Users.Select(item => item.Email).Contains(Email);
  76. }
  77. // Валидация дня рождения
  78. public static bool IsDateOfBirthdayCorrect(DateTime Date)
  79. {
  80. return Date <= DateTime.Now;
  81. }
  82. // Валидация логина и пароля при входе
  83. public static bool IsLogAndPassCorrect(string login, string password)
  84. {
  85. return login != "" && password != "";
  86. }
  87. // Валидация логина и пароля
  88. public static bool IsLogEqualPass(string login, string password)
  89. {
  90. return login != password;
  91. }
  92. // Валидация логина и пароля
  93. public static bool IsLengthCorrect(string str)
  94. {
  95. return str.Trim().Length >= 5;
  96. }
  97. // Проверка на правильность введеных данных при входе
  98. public static bool LoginCheck(string login, string password)
  99. {
  100. return cnt.db.Users.Select(item => item.Login + item.Password).Contains(login + Encrypt.GetHash(password));
  101. }
  102. // Проверка на уникальность логина
  103. public static bool IsLoginAlreadyTaken(string login)
  104. {
  105. return cnt.db.Users.Select(item => item.Login).Contains(login);
  106. }
  107. // Преобразует из "string" в "String"
  108. public static string ToUlower(string str)
  109. {
  110. return str.Substring(0, 1).ToUpper() + str.Substring(1, str.Length);
  111. }
  112. // Получение всех станций в маршруте в виде строки
  113. public static string GetDirection(Routes route)
  114. {
  115. string stationsList = "";
  116. foreach (RoutesStations rs in cnt.db.RoutesStations.Where(item => item.IdRoute == route.IdRoute))
  117. stationsList += rs.Stations.Location == GetDepartureStationLocation(route) ? rs.Stations.Name : $"{rs.Stations.Name} → ";
  118. return stationsList;
  119. }
  120. //// Проверка на уникальность номера телефона
  121. //public static bool IsPhoneNumberAlreadyTaken(string Phone)
  122. //{
  123. // return cnt.db.Users.Select(item => item.).Contains(Phone);
  124. //}
  125. //Кодирование картинки
  126. public static byte[] BitmapSourceToByteArray(BitmapSource image)
  127. {
  128. using (var stream = new MemoryStream())
  129. {
  130. var encoder = new PngBitmapEncoder();
  131. encoder.Frames.Add(BitmapFrame.Create(image));
  132. encoder.Save(stream);
  133. return stream.ToArray();
  134. }
  135. }
  136. // Выбор картинки
  137. public static BitmapImage SelectImage()
  138. {
  139. #region Выбор картинки
  140. OpenFileDialog op = new OpenFileDialog
  141. {
  142. Title = "Выбрать изображение",
  143. Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
  144. "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
  145. "Portable Network Graphic (*.png)|*.png"
  146. };
  147. return op.ShowDialog() == true ? new BitmapImage(new Uri(op.FileName)) : null;
  148. #endregion
  149. }
  150. ////Декодирование картинки
  151. //public static BitmapImage NewImage(Users user)
  152. //{
  153. // MemoryStream ms = new MemoryStream(user.Image);
  154. // BitmapImage image = new BitmapImage();
  155. // image.BeginInit();
  156. // image.StreamSource = ms;
  157. // image.EndInit();
  158. // return image;
  159. //}
  160. }
  161. }