Functions.cs 6.7 KB

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