using IronXL; using NPOI.XWPF.UserModel; using RKISPATTERN.Command; using RKISPATTERN.Helpers; using RKISPATTERN.Models; using RKISPATTERN.View; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; namespace RKISPATTERN.ViewModels { class ProblemsInfo { public int ID { get; set; } public string Name { get; set; } public string Description { get; set; } public DateTime date { get; set; } public string Status { get; set; } public string CustomerName { get; set; } public string WorkerName { get; set; } public string LoginCreator { get; set; } public ProblemsInfo(int iD, string name, string description, DateTime date,string status,int idCustomer,int? idWorker) { ID = iD; Name = name; Description = description; this.date = date; Status = status; using(DBContext db = new DBContext()) { User customer = db.User.FirstOrDefault(u => db.Customer.FirstOrDefault(c => c.Id == idCustomer).Id == u.Id); CustomerName = $"{customer.FirstName} {customer.MiddleName} {customer.LastName}"; LoginCreator = customer.Login; User worker = db.User.FirstOrDefault(u => db.Worker.FirstOrDefault(w => w.Id == idWorker).Id == u.Id); if(worker != null) WorkerName = $"{worker.FirstName} {worker.MiddleName} {worker.LastName}"; } } } class ListProblemsViewModel : INotifyPropertyChanged { List problemsInfos = new List(); public event PropertyChangedEventHandler? PropertyChanged; public void OnPropertyChanged([CallerMemberName] string prop = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } private List _problems; public List Problems { get => _problems; set { _problems = value; OnPropertyChanged(); } } string seacrhLogin; public string SearchLogin { get { return seacrhLogin; } set { seacrhLogin = value; OnPropertyChanged("SearchLogin"); } } int changeProblem; public int ChangeProblem { get { return changeProblem; } set { changeProblem = value; OnPropertyChanged("ChangeProblem"); } } int newStatus; public int NewStatus { get { return newStatus; } set { newStatus = value; OnPropertyChanged("NewStatus"); } } int takenProblem; public int TakenProblem { get { return takenProblem; } set { takenProblem = value; OnPropertyChanged("TakenProblem"); } } private RelayCommand searchProblemByLogin; public RelayCommand SearchProblemByLogin { get { return searchProblemByLogin ?? (searchProblemByLogin = new RelayCommand(obj => { Problems = _problems.Where(el => el.LoginCreator == SearchLogin).ToList(); })); } } private RelayCommand sortDate; public RelayCommand SortDate { get { return sortDate ?? (sortDate = new RelayCommand(obj => { Problems = _problems.OrderBy(el => el.date).ToList(); })); } } private RelayCommand historyProblems; public RelayCommand HistoryProblems { get { return historyProblems ?? (historyProblems = new RelayCommand(obj => { string Name = $"{MainWindowViewModel.CurrentUser.FirstName} {MainWindowViewModel.CurrentUser.MiddleName} {MainWindowViewModel.CurrentUser.LastName}"; Problems = _problems.Where(el => el.WorkerName==Name).ToList(); })); } } private RelayCommand сanTake; public RelayCommand CanTake { get { return сanTake ?? (сanTake = new RelayCommand(obj => { Problems = _problems.Where(el => el.WorkerName == null).ToList(); })); } } public RelayCommand createReports; public RelayCommand CreateReports { get { return createReports ??= new RelayCommand( x => { static void CreateWord(List problemsInfos) { var db = new DBContext(); XWPFDocument doc = new XWPFDocument(); XWPFTable table = doc.CreateTable(2 + db.Problem.Count(), 7); table.SetColumnWidth(0, 1200); table.SetColumnWidth(1, 1200); table.SetColumnWidth(2, 1200); table.SetColumnWidth(3, 1200); table.SetColumnWidth(4, 1200); table.SetColumnWidth(5, 1200); table.SetColumnWidth(6, 1200); table.GetRow(0).GetCell(0).SetText("ID"); table.GetRow(0).GetCell(1).SetText("Название"); table.GetRow(0).GetCell(2).SetText("Описание"); table.GetRow(0).GetCell(3).SetText("Создатель"); table.GetRow(0).GetCell(4).SetText("Исполнитель"); table.GetRow(0).GetCell(5).SetText("Дата"); table.GetRow(0).GetCell(6).SetText("Статус"); int i = 1; foreach (var problem in problemsInfos) { table.GetRow(i).GetCell(0).SetText(problem.ID.ToString()); table.GetRow(i).GetCell(1).SetText(problem.Name); table.GetRow(i).GetCell(2).SetText(problem.Description); table.GetRow(i).GetCell(3).SetText(problem.CustomerName); table.GetRow(i).GetCell(4).SetText(problem.WorkerName ?? ""); table.GetRow(i).GetCell(5).SetText(problem.date.ToShortDateString()); table.GetRow(i).GetCell(6).SetText(problem.Status); i++; } } CreateWord(problemsInfos); static void CreateExcel(List problemsInfos) { WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX); var table = workbook.CreateWorkSheet("example_sheet"); table["A1"].Value = "ID"; table["B1"].Value = "Название"; table["C1"].Value = "Описание"; table["D1"].Value = "Создатель"; table["E1"].Value = "Исполнитель"; table["F1"].Value = "Дата"; table["G1"].Value = "Статус"; uint i = 2; var ctx = new DBContext(); foreach (var problem in problemsInfos) { table[$"A{i}"].Value = problem.ID.ToString(); table[$"B{i}"].Value = problem.Name; table[$"C{i}"].Value = problem.Description; table[$"D{i}"].Value = problem.CustomerName; table[$"E{i}"].Value = problem.WorkerName??""; table[$"F{i}"].Value = problem.date.ToString(); table[$"G{i}"].Value = problem.Status; table.Columns[0].Width = 4000; table.Columns[1].Width = 5000; table.Columns[2].Width = 5000; table.Columns[3].Width = 5000; table.Columns[4].Width = 5000; table.Columns[5].Width = 5000; table.Columns[6].Width = 5000; i++; } workbook.SaveAs("otchet.xlsx"); } CreateExcel(problemsInfos); }); } } private RelayCommand takeProblem; public RelayCommand TakeProblem { get { return takeProblem ?? (takeProblem = new RelayCommand(obj => { using (DBContext dbContext = new DBContext()) { if (TakenProblem >= problemsInfos.Count()) { MessageBox.Show("Неверный ввод!", "Ошибка", MessageBoxButton.YesNoCancel, MessageBoxImage.Asterisk); return; } int index = problemsInfos[TakenProblem].ID - 1; Problem problem = dbContext.Problem.FirstOrDefault(p => p.Id == index); if (problem == null || problem.worker != null) { MessageBox.Show("Неверный ввод!", "Ошибка", MessageBoxButton.YesNoCancel, MessageBoxImage.Asterisk); return; } dbContext.Problem.First(p => p.Id == index).workerId = MainWindowViewModel.CurrentUser.Id; dbContext.SaveChanges(); } })); } } private RelayCommand backToMenu; public RelayCommand BackToMenu { get { return backToMenu ?? (backToMenu = new RelayCommand(obj => { MainMenu mainMenu = new MainMenu(); mainMenu.Show(); Helper.CloseWindow(); })); } } private RelayCommand changeStatus; public RelayCommand ChangeStatus { get { return changeStatus ?? (changeStatus = new RelayCommand(obj => { if (ChangeProblem >= problemsInfos.Count()) { MessageBox.Show("Неверный ввод!", "Ошибка", MessageBoxButton.YesNoCancel, MessageBoxImage.Asterisk); return; } string Name = $"{MainWindowViewModel.CurrentUser.FirstName} {MainWindowViewModel.CurrentUser.MiddleName} {MainWindowViewModel.CurrentUser.LastName}"; if (Name == problemsInfos[ChangeProblem -1].CustomerName) { if (NewStatus == 0) { using (DBContext dbContext = new DBContext()) { int index = problemsInfos[ChangeProblem - 1].ID ; dbContext.Problem.First(p => p.Id == index).status = "Свободно"; dbContext.SaveChanges(); } } else if (NewStatus == 1) { using (DBContext dbContext = new DBContext()) { int index = problemsInfos[ChangeProblem - 1].ID ; dbContext.Problem.First(p => p.Id == index).status = "В процессе"; dbContext.SaveChanges(); } } else if (NewStatus == 2) { using (DBContext dbContext = new DBContext()) { int index = problemsInfos[ChangeProblem -1].ID; dbContext.Problem.First(p => p.Id == index).status = "Готово"; dbContext.SaveChanges(); } } } })); } } public ListProblemsViewModel() { using(DBContext dbContext = new DBContext()) { foreach (var el in dbContext.Problem) { problemsInfos.Add(new ProblemsInfo(el.Id, el.Name, el.Decription, el.date,el.status, el.customerId, el.workerId)); } _problems = new List(problemsInfos); } } } }