123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Input;
- using Microsoft.Win32;
- using System.IO;
- using System.Windows;
- namespace ExamTry.Patterns
- {
- public class ViewModel : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- public static List<Agent> agentsList = BaseConnect.BaseModel.Agent.ToList();
- ObservableCollection<Agent> agentsCollection = new ObservableCollection<Agent>(agentsList);
- //получение агентов
- public ObservableCollection<Agent> GetAgents
- {
- get => agentsCollection;
- }
- //сортировка
- int indexOrderBy = -1;
- int indexSort = -1;
- public int GetIndexOrderBy
- {
- set {
- indexOrderBy = value;
- SortAgent();
- PropertyChanged(this, new PropertyChangedEventArgs("GetAgents"));
- }
- }
- public int GetIndexSort
- {
- set
- {
- indexSort = value;
- SortAgent();
- PropertyChanged(this, new PropertyChangedEventArgs("GetAgents"));
- }
- }
- private void SortAgent()
- {
- agentsCollection = new ObservableCollection<Agent>(agentsList);
- switch (indexSort)
- {
- case 0:
- if (indexOrderBy == 0)
- {
- agentsCollection = new ObservableCollection<Agent>(agentsCollection.OrderBy(x => x.Title).ToList());
- };
- if (indexOrderBy == 1)
- {
- agentsCollection = new ObservableCollection<Agent>(agentsCollection.OrderByDescending(x => x.Title).ToList());
- };
- break;
- case 1:
- if (indexOrderBy == 0)
- {
- agentsCollection = new ObservableCollection<Agent>(agentsCollection.OrderBy(x => x.GetProductionSale).ToList());
- };
- if (indexOrderBy == 1)
- {
- agentsCollection = new ObservableCollection<Agent>(agentsCollection.OrderByDescending(x => x.GetProductionSale).ToList());
- };
- break;
- case 2:
- if (indexOrderBy == 0)
- {
- agentsCollection = new ObservableCollection<Agent>(agentsCollection.OrderBy(x => x.Priority).ToList());
- };
- if (indexOrderBy == 1)
- {
- agentsCollection = new ObservableCollection<Agent>(agentsCollection.OrderByDescending(x => x.Priority).ToList());
- };
- break;
- default:
- agentsCollection = new ObservableCollection<Agent>(agentsList);
- break;
- }
- }
- string textForSale;
- public string SetTextForSort
- {
- set
- {
- textForSale = value;
- TextSort();
- PropertyChanged(this, new PropertyChangedEventArgs("GetAgents"));
- }
- }
- private void TextSort()
- {
- agentsCollection = new ObservableCollection<Agent>(agentsList);
- agentsCollection = new ObservableCollection<Agent>(agentsCollection.Where(x => x.Title.Contains(textForSale)).ToList());
- }
- private void TextindexType()
- {
- agentsCollection = new ObservableCollection<Agent>(agentsList);
- agentsCollection = new ObservableCollection<Agent>(agentsCollection.Where(x => x.AgentTypeID == indexTypeAgent).ToList());
- if (GetTypeAgent.Count == indexTypeAgent)
- {
- agentsCollection = new ObservableCollection<Agent>(agentsList);
- }
- }
- public List<string> GetTypeAgent
- {
- get
- {
- List<string> getTypeAgent = new List<string>();
- List<AgentType> agentTypes = BaseConnect.BaseModel.AgentType.ToList();
- foreach (var item in agentTypes)
- {
- getTypeAgent.Add(item.Title);
- }
- getTypeAgent.Add("Все типы");
- return getTypeAgent;
- }
- }
- int indexTypeAgent = -1;
- public int GetTypeIndex
- {
- set
- {
- indexTypeAgent = value + 1;
- TextindexType();
- PropertyChanged(this, new PropertyChangedEventArgs("GetAgents"));
- }
- }
- //редактирование агента
- Agent agentForEdit;
- public Agent GetAgent
- {
- get
- {
- return agentForEdit;
- }
- set
- {
- agentForEdit = value;
- }
- }
- public string GetImageForEdit
- {
- get
- {
- return Environment.CurrentDirectory + GetAgent.Logo;
- }
- }
- public RoutedCommand EditImageCommand { get; set; } = new RoutedCommand();
- public CommandBinding EditmageBinding;
- private void EditmageBinding_Executed(object sender, ExecutedRoutedEventArgs e)
- {
- OpenFileDialog openFile = new OpenFileDialog();
- if (openFile.ShowDialog() == true)
- {
- GetAgent.Logo = "/agents/";
- GetAgent.Logo += openFile.SafeFileName;
- try
- {
- File.Copy(openFile.FileName, Environment.CurrentDirectory + GetAgent.Logo);
- }
- catch (Exception)
- {
- }
- PropertyChanged(this, new PropertyChangedEventArgs("GetImageForEdit"));
- }
- }
- public RoutedCommand SaveEditCommand { get; set; } = new RoutedCommand();
- public CommandBinding SaveEditBinding;
- private void SaveEditBinding_Executed(object sender, ExecutedRoutedEventArgs e)
- { BaseConnect.BaseModel.SaveChanges();
- MessageBox.Show("Успешно сохранено");
- PropertyChanged(this,new PropertyChangedEventArgs("GetImage"));
- }
- public RoutedCommand RemoveCommand { get; set; } = new RoutedCommand();
- public CommandBinding RemoveEditBinding;
- private void RemoveEditBinding_Executed(object sender, ExecutedRoutedEventArgs e)
- {
- BaseConnect.BaseModel.Agent.Remove(GetAgent);
- BaseConnect.BaseModel.SaveChanges();
- MessageBox.Show("Успешно удалено");
- }
- //добавление агента
- public RoutedCommand addImage { get; set; } = new RoutedCommand();
- public CommandBinding AddImageBinding;
- public string addTitle { get; set; }
- public int addAgentId { get; set; }
- public string addAddres { get; set; }
- public string addINN { get; set; }
- public string addKPP { get; set; }
- public string addDirectorName { get; set; }
- public string addPhone { get; set; }
- public string addEmail { get; set; }
- public string addLogo { get; set; }
- public int addPriority { get; set; }
- public List<string> GetTypeAgentForAdd
- {
- get
- {
- List<string> getTypeAgent = new List<string>();
- List<AgentType> agentTypes = BaseConnect.BaseModel.AgentType.ToList();
- foreach (var item in agentTypes)
- {
- getTypeAgent.Add(item.Title);
- }
- return getTypeAgent;
- }
- }
- public string GetImageForAdd
- {
- get =>Environment.CurrentDirectory + addLogo;
- }
-
- Agent addAgent;
- private void AddAgentBinding_Executed(object sender, ExecutedRoutedEventArgs e)
- {
- addAgent = new Agent();
- addAgent.Address = addAddres;
- addAgent.Title = addTitle;
- addAgent.INN = addINN;
- addAgent.KPP = addKPP;
- addAgent.DirectorName = addDirectorName;
- addAgent.Phone = addPhone;
- addAgent.Email = addEmail;
- addAgent.Logo = addLogo;
- addAgent.Priority = addPriority;
- addAgent.AgentTypeID = addAgentId;
- BaseConnect.BaseModel.Agent.Add(addAgent);
- BaseConnect.BaseModel.SaveChanges();
- MessageBox.Show("Пользователь добавлен");
- }
- public RoutedCommand AddAgentCommand { get; set; } = new RoutedCommand();
- public CommandBinding AddAgentBinding;
- private void AddImageBinding_Executed(object sender, ExecutedRoutedEventArgs e)
- {
- OpenFileDialog openFile = new OpenFileDialog();
- if (openFile.ShowDialog() == true)
- {
- addLogo= "/agents/";
- addLogo += openFile.SafeFileName;
-
- try
- {
- File.Copy(openFile.FileName, Environment.CurrentDirectory + addLogo);
- }
- catch (Exception)
- {
-
- }
- PropertyChanged(this, new PropertyChangedEventArgs("GetImageForAdd"));
- }
- }
- public ViewModel()
- {
- AddImageBinding = new CommandBinding(addImage);
- AddImageBinding.Executed += AddImageBinding_Executed;
- AddAgentBinding = new CommandBinding(AddAgentCommand);
- AddAgentBinding.Executed += AddAgentBinding_Executed;
- EditmageBinding = new CommandBinding(EditImageCommand);
- EditmageBinding.Executed += EditmageBinding_Executed;
- SaveEditBinding = new CommandBinding(SaveEditCommand);
- SaveEditBinding.Executed += SaveEditBinding_Executed;
- RemoveEditBinding = new CommandBinding(RemoveCommand);
- RemoveEditBinding.Executed += RemoveEditBinding_Executed; ;
- }
- }
- }
|