/// <summary> /// Naudotojo atnaujinimas duomenų bazėje. /// </summary> /// <param name="user">Atnaujinamas naudotojas</param> private static void UpdateUserDB(User user) { try { using (dlssdb db = new dlssdb()) { User existingUser = db.Users.Where(x => x.id == user.id).FirstOrDefault(); List <Entry> entries = db.Entries.Where(x => x.card_id == existingUser.card_id).ToList(); foreach (var entry in entries) { entry.card_id = user.card_id; } existingUser.name = user.name; existingUser.surname = user.surname; existingUser.password = user.password; existingUser.email = user.email; existingUser.card_id = user.card_id; existingUser.access_level = user.access_level; existingUser.work_hours_in_week = user.work_hours_in_week; db.SaveChanges(); } } catch { MessageBox.Show(Properties.Strings.errUser, Properties.Strings.errTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Sistemos nustatymų gavimas iš duomenų bazės. /// </summary> /// <returns>Sistemos nustatymai.</returns> public static Settings GetSettings() { try { using (dlssdb db = new dlssdb()) { Settings settings = db.Settings.FirstOrDefault(); return(settings); } } catch { } return(new Settings()); }
/// <summary> /// Naudotojo slaptažodžio atnaujinimas. /// </summary> /// <param name="user_id">Naudotojo ID</param> /// <param name="password">Naujas naudotojo slaptažodis</param> public static void UpdateUserPassword(int user_id, string password) { try { using (dlssdb db = new dlssdb()) { User user = db.Users.Where(x => x.id == user_id).FirstOrDefault(); user.password = BCrypt.Net.BCrypt.EnhancedHashPassword(password); db.SaveChanges(); } } catch { } }
/// <summary> /// Naudotojo slaptažodžio gavimas pagal naudotojo vardą. /// </summary> /// <param name="username">Naudotojo prisijungimo vardas</param> /// <returns>Naudotojo slaptažodis. Jei naudotojas nerastas, grąžinama tuščia reikšmė.</returns> public static string GetUserPassword(string username) { try { using (dlssdb db = new dlssdb()) { string password = db.Users.Where(x => x.username.Equals(username)).Select(x => x.password).FirstOrDefault(); return(password); } } catch { return(""); } }
/// <summary> /// Naujo naudotojo išsaugojimas duomenų bazėje. /// </summary> /// <param name="user">Išsaugomas naudotojas</param> private static void InsertNewUserDB(User user) { try { using (dlssdb db = new dlssdb()) { db.Users.Add(user); db.SaveChanges(); } } catch { MessageBox.Show(Properties.Strings.errUser, Properties.Strings.errTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Naudotojo duomenų gavimas pagal el. paštą. /// </summary> /// <param name="email">Naudotojo el. paštas</param> /// <returns>Naudotojo duomenys</returns> public static User GetUserByEmail(string email) { try { using (dlssdb db = new dlssdb()) { User user = db.Users.Where(x => x.email == email).FirstOrDefault(); return(user); } } catch { MessageBox.Show(Properties.Strings.errUser, Properties.Strings.errTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return(new User()); } }
/// <summary> /// Naujo naudotojo ID gavimas. /// </summary> /// <returns>Naujas naudotojo ID</returns> private static int GetNewID() { try { using (dlssdb db = new dlssdb()) { int last_id = db.Users.OrderByDescending(x => x.id).Select(x => x.id).FirstOrDefault(); return(last_id + 1); } } catch { MessageBox.Show(Properties.Strings.errUser, Properties.Strings.errTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return(-1); } }
/// <summary> /// Naudotojo egzistavimo duomenų bazėje patikrinimas pagal naudotojo prisijungimo vardą. /// </summary> /// <param name="username">Naudotojo prisijungimo vardas</param> /// <returns>True - naudotojas egzistuoja, false - naudotojas neegzistuoja.</returns> public static bool UsernameExists(string username) { try { using (dlssdb db = new dlssdb()) { User user = db.Users.Where(x => x.username == username).FirstOrDefault(); if (user != null) { return(true); } } } catch { } return(false); }
/// <summary> /// Naudotojo egzistavimo duomenų bazėje patikrinimas pagal ID. /// </summary> /// <param name="user_id">Naudotojo ID</param> /// <returns>0 - naudotojas rastas. 1 - naudotojas nerastas.</returns> public static int CheckUserByID(int user_id) { try { using (dlssdb db = new dlssdb()) { string password = db.Users.Where(x => x.id.Equals(user_id)).Select(x => x.password).FirstOrDefault(); if (string.IsNullOrEmpty(password)) { return(1); } } } catch { } return(0); }
/// <summary> /// Patikrinimas, ar įėjimo kortelės numeris jau naudojamas. /// </summary> /// <param name="card_id">Įėjimo kortelės numeris</param> /// <returns>True - kortelės numeris naudojamas, false - kortelės numeris nenaudojamas.</returns> public static bool CardIdIsTaken(string card_id) { try { using (dlssdb db = new dlssdb()) { User user = db.Users.Where(x => x.card_id == card_id).FirstOrDefault(); if (user != null) { return(true); } } } catch { } return(false); }
/// <summary> /// Naudotojo ID gavimas pagal prisijungimo vardą. /// </summary> /// <param name="username">Naudotojo prisijungimo vardas</param> /// <returns>Naudotojo ID. Jeigu naudotojas nerastas, grąžinama reikšmė -1.</returns> public static int GetUserID(string username) { try { using (dlssdb db = new dlssdb()) { int id; string response = db.Users.Where(x => x.username.Equals(username)).Select(x => x.id).FirstOrDefault().ToString(); if (int.TryParse(response, out id)) { return(id); } } } catch { } return(-1); }
/// <summary> /// Naudotojo pašalinimas iš duomenų bazės. /// </summary> /// <param name="user">Pašalinamas naudotojas</param> public static void RemoveUserDB(User user) { try { using (dlssdb db = new dlssdb()) { List <Entry> entries = db.Entries.Where(x => x.card_id == user.card_id).ToList(); db.Entries.RemoveRange(entries); User existingUser = db.Users.Where(x => x.card_id == user.card_id).FirstOrDefault(); db.Users.Remove(existingUser); db.SaveChanges(); } } catch { MessageBox.Show(Properties.Strings.errUser, Properties.Strings.errTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Naudotojo likusio darbo laiko šį mėnesį apskaičiavimas. /// </summary> /// <param name="remainingWorkingTimeMonth"></param> /// <returns>Likęs naudotojo darbo laikas šį mėnesį.</returns> public static TimeSpan RemainingWorktimeMonth(TimeSpan?remainingWorkingTimeMonth) { try { if (remainingWorkingTimeMonth == TimeSpan.MaxValue) { return(TimeSpan.MaxValue); } using (dlssdb db = new dlssdb()) { TimeSpan timeWorked = new TimeSpan(0); User current_user = UserController.GetUserByID(LocalCache.GetLastUserId()); DateTime firstDayOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); int daysInCurrentMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month); List <Entry> entries = db.Entries.Where(x => x.card_id.Equals(current_user.card_id)).Where(x => x.auth_date >= firstDayOfMonth && DbFunctions.DiffDays(x.auth_date, firstDayOfMonth) < daysInCurrentMonth && (DbFunctions.DiffDays(x.auth_datetime, DateTime.Today) < daysInCurrentMonth)).OrderBy(x => x.id).ToList(); if (entries.Count == 0) { return(new TimeSpan(current_user.work_hours_in_week / 5 * daysInCurrentMonth, 0, 0)); } if (entries.Count % 2 == 0) { for (int i = 0; i < entries.Count; i += 2) { timeWorked += (entries[i + 1].auth_datetime - entries[i].auth_datetime).Value; } return(new TimeSpan(current_user.work_hours_in_week / 5 * daysInCurrentMonth, 0, 0) - timeWorked); } for (int i = 0; i < entries.Count - 1; i += 2) { timeWorked += (entries[i + 1].auth_datetime - entries[i].auth_datetime).Value; } timeWorked += (DateTime.Now - entries.Last().auth_datetime).Value; return(new TimeSpan(current_user.work_hours_in_week / 5 * daysInCurrentMonth, 0, 0) - timeWorked); } } catch { MessageBox.Show(Properties.Strings.errDatabase, Properties.Strings.errTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return(TimeSpan.MaxValue); } }
/// <summary> /// Sistemos nustatymų išsaugojimas duomenų bazėje. /// </summary> /// <param name="settings">Įšsaugomi sistemos nustatymai</param> public static void SaveSettings(Settings settings) { try { using (dlssdb db = new dlssdb()) { Settings settingsExisting = db.Settings.FirstOrDefault(); settingsExisting.NVRIP = settings.NVRIP; settingsExisting.NVRPort = settings.NVRPort; settingsExisting.NVRUsername = settings.NVRUsername; settingsExisting.NVRPassword = settings.NVRPassword; settingsExisting.AccessControllerIP = settings.AccessControllerIP; settingsExisting.AccessControllerPort = settings.AccessControllerPort; settingsExisting.AccessControllerUsername = settings.AccessControllerUsername; settingsExisting.AccessControllerPassword = settings.AccessControllerPassword; db.SaveChanges(); } } catch { } }
/// <summary> /// Naudotojo likusio darbo laiko šią savaitę apskaičiavimas. /// </summary> /// <param name="remainingWorkingTimeWeek"></param> /// <returns>Likęs naudotojo darbo laikas šią savaitę.</returns> public static TimeSpan RemainingWorktimeWeek(TimeSpan?remainingWorkingTimeWeek) { try { if (remainingWorkingTimeWeek == TimeSpan.MaxValue) { return(TimeSpan.MaxValue); } using (dlssdb db = new dlssdb()) { TimeSpan timeWorked = new TimeSpan(0); User current_user = UserController.GetUserByID(LocalCache.GetLastUserId()); DateTime mondayThisWeek = DateTime.Today.DayOfWeek != DayOfWeek.Sunday ? DateTime.Today.AddDays(-((int)DateTime.Today.DayOfWeek - 1)) : DateTime.Today.AddDays(-6); List <Entry> entries = db.Entries.Where(x => x.card_id.Equals(current_user.card_id)).Where(x => x.auth_date >= mondayThisWeek && DbFunctions.DiffDays(x.auth_date, mondayThisWeek) < 7 && (DbFunctions.DiffHours(x.auth_datetime, DateTime.Today) < 24 * 5)).OrderBy(x => x.id).ToList(); if (entries.Count == 0) { return(new TimeSpan(current_user.work_hours_in_week, 0, 0)); } if (entries.Count % 2 == 0) { for (int i = 0; i < entries.Count; i += 2) { timeWorked += (entries[i + 1].auth_datetime - entries[i].auth_datetime).Value; } return(new TimeSpan(current_user.work_hours_in_week, 0, 0) - timeWorked); } for (int i = 0; i < entries.Count - 1; i += 2) { timeWorked += (entries[i + 1].auth_datetime - entries[i].auth_datetime).Value; } timeWorked += (DateTime.Now - entries.Last().auth_datetime).Value; return(new TimeSpan(current_user.work_hours_in_week, 0, 0) - timeWorked); } } catch { MessageBox.Show(Properties.Strings.errDatabase, Properties.Strings.errTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return(TimeSpan.MaxValue); } }
/// <summary> /// Naudotojo išdirbto laiko šį mėnesį apskaičiavimas. /// </summary> /// <param name="user_id">Naudotojo ID</param> /// <returns>Naudotojo išdirbtas laikas šį mėnesį</returns> public static string UserTimeWorkedThisMonth(int user_id) { try { using (dlssdb db = new dlssdb()) { TimeSpan timeWorked = new TimeSpan(0); User user = UserController.GetUserByID(user_id); DateTime firstDayOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); int daysInCurrentMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month); List <Entry> entries = db.Entries.Where(x => x.card_id.Equals(user.card_id)).Where(x => x.auth_date >= firstDayOfMonth && DbFunctions.DiffDays(x.auth_date, firstDayOfMonth) < daysInCurrentMonth && (DbFunctions.DiffDays(x.auth_datetime, DateTime.Today) < daysInCurrentMonth)).OrderBy(x => x.id).ToList(); if (entries.Count == 0) { return(Math.Round(timeWorked.TotalHours, 2).ToString("F") + " / " + user.work_hours_in_week / 5 * daysInCurrentMonth); } if (entries.Count % 2 == 0) { for (int i = 0; i < entries.Count; i += 2) { timeWorked += (entries[i + 1].auth_datetime - entries[i].auth_datetime).Value; } return(Math.Round(timeWorked.TotalHours, 2).ToString("F") + " / " + user.work_hours_in_week / 5 * daysInCurrentMonth); } for (int i = 0; i < entries.Count - 1; i += 2) { timeWorked += (entries[i + 1].auth_datetime - entries[i].auth_datetime).Value; } timeWorked += (DateTime.Now - entries.Last().auth_datetime).Value; return(Math.Round(timeWorked.TotalHours, 2).ToString("F") + " / " + user.work_hours_in_week / 5 * daysInCurrentMonth); } } catch { MessageBox.Show(Properties.Strings.errDatabase, Properties.Strings.errTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return(Properties.Strings.errTitle); } }
/// <summary> /// Naudotojo išdirbto laiko šią savaitę apskaičiavimas. /// </summary> /// <param name="user_id">Naudotojo ID</param> /// <returns>Naudotojo išdirbtas laikas šią savaitę</returns> public static string UserTimeWorkedThisWeek(int user_id) { try { using (dlssdb db = new dlssdb()) { TimeSpan timeWorked = new TimeSpan(0); User user = UserController.GetUserByID(user_id); DateTime mondayThisWeek = DateTime.Today.DayOfWeek != DayOfWeek.Sunday ? DateTime.Today.AddDays(-((int)DateTime.Today.DayOfWeek - 1)) : DateTime.Today.AddDays(-6); List <Entry> entries = db.Entries.Where(x => x.card_id.Equals(user.card_id)).Where(x => x.auth_date >= mondayThisWeek && DbFunctions.DiffDays(x.auth_date, mondayThisWeek) < 7 && (DbFunctions.DiffHours(x.auth_datetime, DateTime.Today) < 24 * 5)).OrderBy(x => x.id).ToList(); if (entries.Count == 0) { return(Math.Round(timeWorked.TotalHours, 2).ToString("F") + " / " + user.work_hours_in_week); } if (entries.Count % 2 == 0) { for (int i = 0; i < entries.Count; i += 2) { timeWorked += (entries[i + 1].auth_datetime - entries[i].auth_datetime).Value; } return(Math.Round(timeWorked.TotalHours, 2).ToString("F") + " / " + user.work_hours_in_week); } for (int i = 0; i < entries.Count - 1; i += 2) { timeWorked += (entries[i + 1].auth_datetime - entries[i].auth_datetime).Value; } timeWorked += (DateTime.Now - entries.Last().auth_datetime).Value; return(Math.Round(timeWorked.TotalHours, 2).ToString("F") + " / " + user.work_hours_in_week); } } catch { MessageBox.Show(Properties.Strings.errDatabase, Properties.Strings.errTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return(Properties.Strings.errTitle); } }