MainWindowViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. using System.Collections.ObjectModel;
  10. using System;
  11. namespace Acosta.ViewModels
  12. {
  13. public class MainWindowViewModel : ViewModelBase
  14. {
  15. static SuharevaContext myConnection = new SuharevaContext();
  16. static SuharevaContext myConnection1 = new SuharevaContext();
  17. static SuharevaContext myConnection2 = new SuharevaContext();
  18. static SuharevaContext myConnection3 = new SuharevaContext();
  19. static SuharevaContext myConnection4 = new SuharevaContext();
  20. static SuharevaContext myConnection5 = new SuharevaContext();
  21. static SuharevaContext myConnection6 = new SuharevaContext();
  22. AuthorizationViewModel authorizationVM = new AuthorizationViewModel();
  23. public AuthorizationViewModel AuthorizationVM { get => authorizationVM; set => authorizationVM = value; }
  24. AddTradeNetworksViewViewModel addTradeNetworksVM = new AddTradeNetworksViewViewModel(myConnection2);
  25. public AddTradeNetworksViewViewModel AddTradeNetworksVM { get => addTradeNetworksVM; set => addTradeNetworksVM = value; }
  26. AddEmployeesViewModel addEmployeesVM = new AddEmployeesViewModel(myConnection1);
  27. public AddEmployeesViewModel AddEmployeesVM { get => addEmployeesVM; set => addEmployeesVM = value; }
  28. AddOutletsViewModel outletVM = new AddOutletsViewModel(myConnection3);
  29. public AddOutletsViewModel OutletVM { get => outletVM; set => outletVM = value; }
  30. EditEmployeeViewModel editUserVM;
  31. public EditEmployeeViewModel EditUserVM { get => editUserVM; set => editUserVM = value; }
  32. EditOutletViewModel editOutletVM;
  33. public EditOutletViewModel EditOutletVM { get => editOutletVM; set => editOutletVM = value; }
  34. //OutletsListViewModel outletListVM = new OutletsListViewModel(myConnection);
  35. //public OutletsListViewModel OutletListVM { get => outletListVM; set => outletListVM = value; }
  36. PersonalAccountViewModel personalAccountVM = new PersonalAccountViewModel(myConnection);
  37. public PersonalAccountViewModel PersonalAccountVM { get => personalAccountVM; set => personalAccountVM = value; }
  38. VisitsViewModel visitsVM = new VisitsViewModel(myConnection4);
  39. public VisitsViewModel VisitsVM { get => visitsVM; set => visitsVM = value; }
  40. UserViewModel userVM = new UserViewModel(myConnection);
  41. public UserViewModel UserVM { get => userVM; set => userVM = value; }
  42. public void SaveNetwork()
  43. {
  44. myConnection2.SaveChanges();
  45. UC = new TradeNetworksView();
  46. }
  47. public void SaveUser()
  48. {
  49. myConnection1.SaveChanges();
  50. UC = new EmployeesView();
  51. }
  52. public void SaveOutlet()
  53. {
  54. myConnection3.SaveChanges();
  55. UC = new OutletsView();
  56. }
  57. public void SaveVisit()
  58. {
  59. myConnection4.SaveChanges();
  60. UC = new VisitsView();
  61. }
  62. public void EditUser(int userId)
  63. {
  64. editUserVM = new EditEmployeeViewModel(myConnection5, userId);
  65. UC = new EditEmployeesView();
  66. }
  67. public void EditOutlet(int outletId)
  68. {
  69. editOutletVM = new EditOutletViewModel(myConnection6, outletId);
  70. UC = new EditOutletsView();
  71. }
  72. public void SaveEditUser()
  73. {
  74. myConnection5.SaveChanges();
  75. UC = new EmployeesView();
  76. }
  77. public void SaveEditOutlet()
  78. {
  79. myConnection6.SaveChanges();
  80. UC = new OutletsView();
  81. }
  82. public UserControl UC { get => uc; set => this.RaiseAndSetIfChanged(ref uc, value); }
  83. private UserControl uc = new AuthorizationView();
  84. public int curUsId;
  85. Employee? currentUser;
  86. public void LoadPersonalAccount()
  87. {
  88. currentUser = myConnection.Employees.FirstOrDefault(x => x.Email == AuthorizationVM.Login && x.Password == AuthorizationVM.Password);
  89. if (currentUser == null)
  90. {
  91. AuthorizationVM.Message = "Пользователя с такими данными не существует.";
  92. }
  93. else if (currentUser.Role != 1)
  94. {
  95. AuthorizationVM.Message = "Ваша роль не соответсвует требованиям.";
  96. }
  97. else
  98. {
  99. AuthorizationVM.Message = "Успех!";
  100. curUsId = currentUser.Employeesid;
  101. PersonalAccountVM = new PersonalAccountViewModel(myConnection, curUsId);
  102. UC = new PersonalAccountView();
  103. }
  104. }
  105. /*public void SavePassword()
  106. {
  107. ChangePasswordView changePasswordView = new ChangePasswordView();
  108. currentUser = myConnection.Employees.FirstOrDefault(x => x.Email == AuthorizationVM.Login && x.Password == AuthorizationVM.Password);
  109. changePasswordView.cp(currentUser);
  110. myConnection.SaveChanges();
  111. UC = new PersonalAccountView();
  112. }*/
  113. public List<Project> ListProjects => myConnection.Projects.ToList();
  114. public List<TradeNetwork> ListTrades => myConnection.TradeNetworks.ToList();
  115. public List<Employee> ListEmployees => myConnection.Employees.Include(x => x.RoleNavigation).ToList().Where(x => x.Employeesid != curUsId).ToList();
  116. public List<Outlet> ListOutlets => myConnection.Outlets.ToList();
  117. public List<Visit> ListVisits => myConnection.Visits.ToList();
  118. public List<Acceptance> ListAcceptance => myConnection.Acceptances.ToList();
  119. //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()));
  120. //public List<string> fkTrade => myConnection.TradeNetworks.ToList().Select(x => x.Title).Where()
  121. //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()));
  122. //public struct a
  123. //{
  124. // int Outlerid;
  125. // string Address;
  126. // string Location;
  127. // string TradeNetworks;
  128. // public a(int oid, string ad, string loc, string tn)
  129. // {
  130. // Outlerid = oid;
  131. // Address = ad;
  132. // Location = loc;
  133. // TradeNetworks = tn;
  134. // }
  135. //}
  136. //public IEnumerable<dynamic> ListOutlets => myConnection.Outlets.Select(p => new
  137. //{
  138. // p.Outletid,
  139. // p.Address,
  140. // p.Location,
  141. // TradeTitles = ListTrades.Where(x => x.Tradeid == p.TradeNetworks).Select(x => x.Title),
  142. // p.TradeNetworks,
  143. // p.Visits
  144. //}).ToList();
  145. // public List<Outlet> ListOutlets => myConnection.Outlets
  146. //.Join(myConnection.TradeNetworks,
  147. // outlet => outlet.TradeNetworks,
  148. // tradeNetwork => tradeNetwork.Tradeid,
  149. // (outlet, tradeNetwork) => new Outlet
  150. // {
  151. // Outletid = outlet.Outletid,
  152. // Address = outlet.Address,
  153. // Location = outlet.Location,
  154. // TradeNetworks = tradeNetwork.Title, // replace this with the actual property name for the retail chain name
  155. // Visits = outlet.Visits
  156. // })
  157. //.ToList();
  158. /*public List<Role> rolesList => (from p in myConnection.Roles.ToList() where p.Title != "Оператор" select p).ToImmutableList().ToList();
  159. public List<Employee> userList => (from p in myConnection.Employees.ToList() where p.Role != 1 select p).ToImmutableList().ToList();*/
  160. public void AddVisitView()
  161. {
  162. UC = new AddVisitView();
  163. }
  164. public void BackVisitView()
  165. {
  166. UC = new VisitsView();
  167. }
  168. public void EditVisitView(int userID)
  169. {
  170. UC = new EditVisitView();
  171. }
  172. public void AddProjectsView()
  173. {
  174. UC = new AddProjectsView();
  175. }
  176. public void BackProjectsView()
  177. {
  178. UC = new ProjectsView();
  179. }
  180. public void EditProjectsView(int userID)
  181. {
  182. UC = new EditProjectsView();
  183. }
  184. public void BackPersonalAccountView()
  185. {
  186. UC = new PersonalAccountView();
  187. }
  188. public void ChangePasswordView()
  189. {
  190. UC = new ChangePasswordView();
  191. }
  192. public void ExitFromProfile()
  193. {
  194. UC = new AuthorizationView();
  195. }
  196. public void AddTradeNetworksView()
  197. {
  198. UC = new AddTradeNetworksView();
  199. }
  200. public void BackTradeNetworksView()
  201. {
  202. UC = new TradeNetworksView();
  203. }
  204. public void AddOutletsView()
  205. {
  206. UC = new AddOutletsView();
  207. }
  208. public void BackOutletsView()
  209. {
  210. UC = new OutletsView();
  211. }
  212. public void EditOutletsView()
  213. {
  214. UC = new EditOutletsView();
  215. }
  216. public void AddEmployeesView()
  217. {
  218. UC = new AddEmployeesView();
  219. }
  220. public void BackEmployeesView()
  221. {
  222. UC = new EmployeesView();
  223. }
  224. public void EditEmployeesView(int userID)
  225. {
  226. UC = new EditEmployeesView();
  227. }
  228. }
  229. }