ViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Input;
  9. using Microsoft.Win32;
  10. using System.IO;
  11. using System.Windows;
  12. namespace ExamTry.Patterns
  13. {
  14. public class ViewModel : INotifyPropertyChanged
  15. {
  16. public event PropertyChangedEventHandler PropertyChanged;
  17. public static List<Agent> agentsList = BaseConnect.BaseModel.Agent.ToList();
  18. ObservableCollection<Agent> agentsCollection = new ObservableCollection<Agent>(agentsList);
  19. //получение агентов
  20. public ObservableCollection<Agent> GetAgents
  21. {
  22. get => agentsCollection;
  23. }
  24. //сортировка
  25. int indexOrderBy = -1;
  26. int indexSort = -1;
  27. public int GetIndexOrderBy
  28. {
  29. set {
  30. indexOrderBy = value;
  31. SortAgent();
  32. PropertyChanged(this, new PropertyChangedEventArgs("GetAgents"));
  33. }
  34. }
  35. public int GetIndexSort
  36. {
  37. set
  38. {
  39. indexSort = value;
  40. SortAgent();
  41. PropertyChanged(this, new PropertyChangedEventArgs("GetAgents"));
  42. }
  43. }
  44. private void SortAgent()
  45. {
  46. agentsCollection = new ObservableCollection<Agent>(agentsList);
  47. switch (indexSort)
  48. {
  49. case 0:
  50. if (indexOrderBy == 0)
  51. {
  52. agentsCollection = new ObservableCollection<Agent>(agentsCollection.OrderBy(x => x.Title).ToList());
  53. };
  54. if (indexOrderBy == 1)
  55. {
  56. agentsCollection = new ObservableCollection<Agent>(agentsCollection.OrderByDescending(x => x.Title).ToList());
  57. };
  58. break;
  59. case 1:
  60. if (indexOrderBy == 0)
  61. {
  62. agentsCollection = new ObservableCollection<Agent>(agentsCollection.OrderBy(x => x.GetProductionSale).ToList());
  63. };
  64. if (indexOrderBy == 1)
  65. {
  66. agentsCollection = new ObservableCollection<Agent>(agentsCollection.OrderByDescending(x => x.GetProductionSale).ToList());
  67. };
  68. break;
  69. case 2:
  70. if (indexOrderBy == 0)
  71. {
  72. agentsCollection = new ObservableCollection<Agent>(agentsCollection.OrderBy(x => x.Priority).ToList());
  73. };
  74. if (indexOrderBy == 1)
  75. {
  76. agentsCollection = new ObservableCollection<Agent>(agentsCollection.OrderByDescending(x => x.Priority).ToList());
  77. };
  78. break;
  79. default:
  80. agentsCollection = new ObservableCollection<Agent>(agentsList);
  81. break;
  82. }
  83. }
  84. string textForSale;
  85. public string SetTextForSort
  86. {
  87. set
  88. {
  89. textForSale = value;
  90. TextSort();
  91. PropertyChanged(this, new PropertyChangedEventArgs("GetAgents"));
  92. }
  93. }
  94. private void TextSort()
  95. {
  96. agentsCollection = new ObservableCollection<Agent>(agentsList);
  97. agentsCollection = new ObservableCollection<Agent>(agentsCollection.Where(x => x.Title.Contains(textForSale)).ToList());
  98. }
  99. private void TextindexType()
  100. {
  101. agentsCollection = new ObservableCollection<Agent>(agentsList);
  102. agentsCollection = new ObservableCollection<Agent>(agentsCollection.Where(x => x.AgentTypeID == indexTypeAgent).ToList());
  103. if (GetTypeAgent.Count == indexTypeAgent)
  104. {
  105. agentsCollection = new ObservableCollection<Agent>(agentsList);
  106. }
  107. }
  108. public List<string> GetTypeAgent
  109. {
  110. get
  111. {
  112. List<string> getTypeAgent = new List<string>();
  113. List<AgentType> agentTypes = BaseConnect.BaseModel.AgentType.ToList();
  114. foreach (var item in agentTypes)
  115. {
  116. getTypeAgent.Add(item.Title);
  117. }
  118. getTypeAgent.Add("Все типы");
  119. return getTypeAgent;
  120. }
  121. }
  122. int indexTypeAgent = -1;
  123. public int GetTypeIndex
  124. {
  125. set
  126. {
  127. indexTypeAgent = value + 1;
  128. TextindexType();
  129. PropertyChanged(this, new PropertyChangedEventArgs("GetAgents"));
  130. }
  131. }
  132. //редактирование агента
  133. Agent agentForEdit;
  134. public Agent GetAgent
  135. {
  136. get
  137. {
  138. return agentForEdit;
  139. }
  140. set
  141. {
  142. agentForEdit = value;
  143. }
  144. }
  145. public string GetImageForEdit
  146. {
  147. get
  148. {
  149. return Environment.CurrentDirectory + GetAgent.Logo;
  150. }
  151. }
  152. public RoutedCommand EditImageCommand { get; set; } = new RoutedCommand();
  153. public CommandBinding EditmageBinding;
  154. private void EditmageBinding_Executed(object sender, ExecutedRoutedEventArgs e)
  155. {
  156. OpenFileDialog openFile = new OpenFileDialog();
  157. if (openFile.ShowDialog() == true)
  158. {
  159. GetAgent.Logo = "/agents/";
  160. GetAgent.Logo += openFile.SafeFileName;
  161. try
  162. {
  163. File.Copy(openFile.FileName, Environment.CurrentDirectory + GetAgent.Logo);
  164. }
  165. catch (Exception)
  166. {
  167. }
  168. PropertyChanged(this, new PropertyChangedEventArgs("GetImageForEdit"));
  169. }
  170. }
  171. public RoutedCommand SaveEditCommand { get; set; } = new RoutedCommand();
  172. public CommandBinding SaveEditBinding;
  173. private void SaveEditBinding_Executed(object sender, ExecutedRoutedEventArgs e)
  174. { BaseConnect.BaseModel.SaveChanges();
  175. MessageBox.Show("Успешно сохранено");
  176. PropertyChanged(this,new PropertyChangedEventArgs("GetImage"));
  177. }
  178. public RoutedCommand RemoveCommand { get; set; } = new RoutedCommand();
  179. public CommandBinding RemoveEditBinding;
  180. private void RemoveEditBinding_Executed(object sender, ExecutedRoutedEventArgs e)
  181. {
  182. BaseConnect.BaseModel.Agent.Remove(GetAgent);
  183. BaseConnect.BaseModel.SaveChanges();
  184. MessageBox.Show("Успешно удалено");
  185. }
  186. //добавление агента
  187. public RoutedCommand addImage { get; set; } = new RoutedCommand();
  188. public CommandBinding AddImageBinding;
  189. public string addTitle { get; set; }
  190. public int addAgentId { get; set; }
  191. public string addAddres { get; set; }
  192. public string addINN { get; set; }
  193. public string addKPP { get; set; }
  194. public string addDirectorName { get; set; }
  195. public string addPhone { get; set; }
  196. public string addEmail { get; set; }
  197. public string addLogo { get; set; }
  198. public int addPriority { get; set; }
  199. public List<string> GetTypeAgentForAdd
  200. {
  201. get
  202. {
  203. List<string> getTypeAgent = new List<string>();
  204. List<AgentType> agentTypes = BaseConnect.BaseModel.AgentType.ToList();
  205. foreach (var item in agentTypes)
  206. {
  207. getTypeAgent.Add(item.Title);
  208. }
  209. return getTypeAgent;
  210. }
  211. }
  212. public string GetImageForAdd
  213. {
  214. get =>Environment.CurrentDirectory + addLogo;
  215. }
  216. Agent addAgent;
  217. private void AddAgentBinding_Executed(object sender, ExecutedRoutedEventArgs e)
  218. {
  219. addAgent = new Agent();
  220. addAgent.Address = addAddres;
  221. addAgent.Title = addTitle;
  222. addAgent.INN = addINN;
  223. addAgent.KPP = addKPP;
  224. addAgent.DirectorName = addDirectorName;
  225. addAgent.Phone = addPhone;
  226. addAgent.Email = addEmail;
  227. addAgent.Logo = addLogo;
  228. addAgent.Priority = addPriority;
  229. addAgent.AgentTypeID = addAgentId;
  230. BaseConnect.BaseModel.Agent.Add(addAgent);
  231. BaseConnect.BaseModel.SaveChanges();
  232. MessageBox.Show("Пользователь добавлен");
  233. }
  234. public RoutedCommand AddAgentCommand { get; set; } = new RoutedCommand();
  235. public CommandBinding AddAgentBinding;
  236. private void AddImageBinding_Executed(object sender, ExecutedRoutedEventArgs e)
  237. {
  238. OpenFileDialog openFile = new OpenFileDialog();
  239. if (openFile.ShowDialog() == true)
  240. {
  241. addLogo= "/agents/";
  242. addLogo += openFile.SafeFileName;
  243. try
  244. {
  245. File.Copy(openFile.FileName, Environment.CurrentDirectory + addLogo);
  246. }
  247. catch (Exception)
  248. {
  249. }
  250. PropertyChanged(this, new PropertyChangedEventArgs("GetImageForAdd"));
  251. }
  252. }
  253. public ViewModel()
  254. {
  255. AddImageBinding = new CommandBinding(addImage);
  256. AddImageBinding.Executed += AddImageBinding_Executed;
  257. AddAgentBinding = new CommandBinding(AddAgentCommand);
  258. AddAgentBinding.Executed += AddAgentBinding_Executed;
  259. EditmageBinding = new CommandBinding(EditImageCommand);
  260. EditmageBinding.Executed += EditmageBinding_Executed;
  261. SaveEditBinding = new CommandBinding(SaveEditCommand);
  262. SaveEditBinding.Executed += SaveEditBinding_Executed;
  263. RemoveEditBinding = new CommandBinding(RemoveCommand);
  264. RemoveEditBinding.Executed += RemoveEditBinding_Executed; ;
  265. }
  266. }
  267. }