WindowTasksViewModel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using System.Text.RegularExpressions;
  19. using System.Data.SqlClient;
  20. namespace PPC
  21. {
  22. public class WindowTasksViewModel :DependencyObject, INotifyPropertyChanged
  23. {
  24. private WindowTasksModel selectedTask;
  25. public ObservableCollection<WindowTasksModel> _active_task;
  26. public ObservableCollection<WindowTasksModel> _complete_task;
  27. public static readonly DependencyProperty SurnameProperty;
  28. public static readonly DependencyProperty NameProperty;
  29. public ObservableCollection<WindowTasksModel> Active_task
  30. {
  31. get { return _active_task; }
  32. set
  33. {
  34. _active_task = value;
  35. OnPropertyChanged("Active_task");
  36. }
  37. }
  38. public ObservableCollection<WindowTasksModel> Complete_task
  39. {
  40. get { return _complete_task; }
  41. set
  42. {
  43. _complete_task = value;
  44. OnPropertyChanged("Complete_task");
  45. }
  46. }
  47. public WindowTasksModel SelectedTask
  48. {
  49. get { return selectedTask; }
  50. set
  51. {
  52. selectedTask = value;
  53. OnPropertyChanged("SelectedTask");
  54. }
  55. }
  56. public string Surname
  57. {
  58. get { return (string)GetValue(SurnameProperty); }
  59. set { SetValue(SurnameProperty, value); }
  60. }
  61. public string Name
  62. {
  63. get { return (string)GetValue(NameProperty); }
  64. set { SetValue(NameProperty, value); }
  65. }
  66. static WindowTasksViewModel()
  67. {
  68. SurnameProperty = DependencyProperty.Register("Surname", typeof(string), typeof(WindowTasksViewModel));
  69. NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(WindowTasksViewModel));
  70. }
  71. public WindowTasksViewModel()
  72. {
  73. updateSourse();
  74. }
  75. private void updateSourse()
  76. {
  77. using (TheBestV2Entities DB = new TheBestV2Entities())
  78. {
  79. DB.Task.Where(a => a.ID_user == ((Date_Users)Application.Current.Resources["UserData"]).ID_user);
  80. Active_task = new ObservableCollection<WindowTasksModel>(ConvertTasksToWindowModel(
  81. DB.Task.ToList().Where(a => a.ID_user == ((Date_Users)Application.Current.Resources["UserData"]).ID_user).Where(a => a.Date_task.ID_status == 1).ToList()));
  82. Complete_task = new ObservableCollection<WindowTasksModel>(ConvertTasksToWindowModel(
  83. DB.Task.ToList().Where(a => a.ID_user == ((Date_Users)Application.Current.Resources["UserData"]).ID_user).Where(a => a.Date_task.ID_status == 2).ToList()));
  84. }
  85. }
  86. private List<WindowTasksModel> ConvertTasksToWindowModel(List<Task> b)
  87. {
  88. List<WindowTasksModel> a = new List<WindowTasksModel>();
  89. foreach(Task task in b)
  90. {
  91. a.Add(new WindowTasksModel() {
  92. Id_task = task.Date_task.ID_task,
  93. Name_task = task.Date_task.Name_task,
  94. Date_finish_job = task.Date_task.Date_finish_job,
  95. Date_start_job = task.Date_task.Date_start_job,
  96. Description_task = task.Date_task.Description_task });
  97. }
  98. return a;
  99. }
  100. public void updateDB()
  101. {
  102. if (selectedTask != null)
  103. {
  104. using (TheBestV2Entities DB = new TheBestV2Entities())
  105. {
  106. Date_task DT = DB.Date_task.Find(selectedTask.Id_task);
  107. DT.ID_status = 2;
  108. DB.SaveChanges();
  109. }
  110. updateSourse();
  111. }
  112. else
  113. {
  114. MessageBox.Show("Нет задач на выполнение");
  115. }
  116. }
  117. public event PropertyChangedEventHandler PropertyChanged;
  118. public void OnPropertyChanged([CallerMemberName] string prop = "")
  119. {
  120. if (PropertyChanged != null)
  121. PropertyChanged(this, new PropertyChangedEventArgs(prop));
  122. }
  123. }
  124. }