MainWindowViewModel.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using Acosta.Views;
  2. using Avalonia.Controls;
  3. using ReactiveUI;
  4. using Acosta.Models;
  5. using System.Linq;
  6. using System.Collections.Generic;
  7. using Microsoft.EntityFrameworkCore;
  8. using System.Collections.Immutable;
  9. namespace Acosta.ViewModels
  10. {
  11. public class MainWindowViewModel : ViewModelBase
  12. {
  13. public static SuharevaContext myConnection = new SuharevaContext();
  14. public static SuharevaContext myConnection1 = new SuharevaContext();
  15. AuthorizationViewModel authorizationVM = new AuthorizationViewModel();
  16. public AuthorizationViewModel AuthorizationVM { get => authorizationVM; set => authorizationVM = value; }
  17. AddTradeNetworksViewViewModel addTradeNetworksVM = new AddTradeNetworksViewViewModel(myConnection);
  18. public AddTradeNetworksViewViewModel AddTradeNetworksVM { get => addTradeNetworksVM; set => addTradeNetworksVM = value; }
  19. AddEmployeesViewModel addEmployeesVM = new AddEmployeesViewModel(myConnection1);
  20. public AddEmployeesViewModel AddEmployeesVM { get => addEmployeesVM; set => addEmployeesVM = value; }
  21. AddOutletsViewModel outletVM = new AddOutletsViewModel(myConnection);
  22. public AddOutletsViewModel OutletVM { get => outletVM; set => outletVM = value; }
  23. OutletsListViewModel outletListVM = new OutletsListViewModel(myConnection);
  24. public OutletsListViewModel OutletListVM { get => outletListVM; set => outletListVM = value; }
  25. PersonalAccountViewModel personalAccountVM = new PersonalAccountViewModel(myConnection);
  26. public PersonalAccountViewModel PersonalAccountVM { get => personalAccountVM; set => personalAccountVM = value; }
  27. /*public void SaveData()
  28. {
  29. myConnection.SaveChanges();
  30. UC = new PersonalAccountView();
  31. }*/
  32. public UserControl UC { get => uc; set => this.RaiseAndSetIfChanged(ref uc, value); }
  33. private UserControl uc = new AuthorizationView();
  34. public int curUsId;
  35. public void LoadPersonalAccount()
  36. {
  37. Employee? currentUser = myConnection.Employees.FirstOrDefault(x => x.Email == AuthorizationVM.Login && x.Password == AuthorizationVM.Password);
  38. if (currentUser == null)
  39. {
  40. AuthorizationVM.Message = "Пользователя с такими данными не существует.";
  41. }
  42. else if (currentUser.Role != 1)
  43. {
  44. AuthorizationVM.Message = "Ваша роль не соответсвует требованиям.";
  45. }
  46. else
  47. {
  48. AuthorizationVM.Message = "Успех!";
  49. curUsId = currentUser.Employeesid;
  50. PersonalAccountVM = new PersonalAccountViewModel(myConnection, curUsId);
  51. UC = new PersonalAccountView();
  52. }
  53. }
  54. public List<Project> ListProjects => myConnection.Projects.ToList();
  55. public List<Employee> ListEmployees => myConnection.Employees.ToList().Where(x => x.Employeesid != curUsId).ToList();
  56. public List<Outlet> ListOutlets => myConnection.Outlets.ToList();
  57. //public List<string> fkTrade => myConnection.TradeNetworks.ToList().Select(x => x.Title).Where()
  58. //List<a> newList = new List<a>(new a((from p in myConnection.Outlets.ToList() select p.Outletid).ToImmutableList().ToList().FirstOrDefault(), (from p in myConnection.Outlets.ToList() select p.Address).ToImmutableList().ToList().FirstOrDefault(), (from p in myConnection.Outlets.ToList() select p.Location).ToImmutableList().ToList().FirstOrDefault(), (from p in myConnection.TradeNetworks.ToList() select p.Title).ToImmutableList().ToList().FirstOrDefault()));
  59. //public struct a
  60. //{
  61. // int Outlerid;
  62. // string Address;
  63. // string Location;
  64. // string TradeNetworks;
  65. // public a(int oid, string ad, string loc, string tn)
  66. // {
  67. // Outlerid = oid;
  68. // Address = ad;
  69. // Location = loc;
  70. // TradeNetworks = tn;
  71. // }
  72. //}
  73. //public IEnumerable<dynamic> ListOutlets => myConnection.Outlets.Select(p => new
  74. //{
  75. // p.Outletid,
  76. // p.Address,
  77. // p.Location,
  78. // TradeTitles = ListTrades.Where(x => x.Tradeid == p.TradeNetworks).Select(x => x.Title),
  79. // p.TradeNetworks,
  80. // p.Visits
  81. //}).ToList();
  82. // public List<Outlet> ListOutlets => myConnection.Outlets
  83. //.Join(myConnection.TradeNetworks,
  84. // outlet => outlet.TradeNetworks,
  85. // tradeNetwork => tradeNetwork.Tradeid,
  86. // (outlet, tradeNetwork) => new Outlet
  87. // {
  88. // Outletid = outlet.Outletid,
  89. // Address = outlet.Address,
  90. // Location = outlet.Location,
  91. // TradeNetworks = tradeNetwork.Title, // replace this with the actual property name for the retail chain name
  92. // Visits = outlet.Visits
  93. // })
  94. //.ToList();
  95. public List<TradeNetwork> ListTrades => myConnection.TradeNetworks.ToList();
  96. /*public List<Role> rolesList => (from p in myConnection.Roles.ToList() where p.Title != "Оператор" select p).ToImmutableList().ToList();
  97. public List<Employee> userList => (from p in myConnection.Employees.ToList() where p.Role != 1 select p).ToImmutableList().ToList();*/
  98. public void AddVisitView()
  99. {
  100. UC = new AddVisitView();
  101. }
  102. public void BackVisitView()
  103. {
  104. UC = new VisitsView();
  105. }
  106. public void EditVisitView()
  107. {
  108. UC = new EditVisitView();
  109. }
  110. public void AddProjectsView()
  111. {
  112. UC = new AddProjectsView();
  113. }
  114. public void BackProjectsView()
  115. {
  116. UC = new ProjectsView();
  117. }
  118. public void EditProjectsView()
  119. {
  120. UC = new EditProjectsView();
  121. }
  122. public void BackPersonalAccountView()
  123. {
  124. UC = new PersonalAccountView();
  125. }
  126. public void ChangePasswordView()
  127. {
  128. UC = new ChangePasswordView();
  129. }
  130. public void ExitFromProfile()
  131. {
  132. UC = new AuthorizationView();
  133. }
  134. public void AddTradeNetworksView()
  135. {
  136. UC = new AddTradeNetworksView();
  137. }
  138. public void BackTradeNetworksView()
  139. {
  140. UC = new TradeNetworksView();
  141. }
  142. public void AddOutletsView()
  143. {
  144. UC = new AddOutletsView();
  145. }
  146. public void BackOutletsView()
  147. {
  148. UC = new OutletsView();
  149. }
  150. public void EditOutletsView()
  151. {
  152. UC = new EditOutletsView();
  153. }
  154. public void AddEmployeesView()
  155. {
  156. UC = new AddEmployeesView();
  157. }
  158. public void BackEmployeesView()
  159. {
  160. UC = new EmployeesView();
  161. }
  162. public void EditEmployeesView(int userID)
  163. {
  164. UC = new EditEmployeesView();
  165. }
  166. }
  167. }