MainWindowViewModel.cs 7.0 KB

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