FinanceDataAccountViewModel.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Avalonia.Media;
  2. using FinancialManagement.Models;
  3. using FinancialManagement.Views;
  4. using Microsoft.EntityFrameworkCore;
  5. using ReactiveUI;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.ComponentModel;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace FinancialManagement.ViewModels
  14. {
  15. public class FinanceDataAccountViewModel : ReactiveObject
  16. {
  17. FinanceTableViewModel financeTableViewModel;
  18. ShmelevTeamContext ppContext;
  19. List<Finance> finances;
  20. Finance? financeItem;
  21. int userIdLoad;
  22. string titleFinance;
  23. decimal priceFinance;
  24. string categoryFinTitle;
  25. string categoryTitle;
  26. int categoryIdFinance;
  27. int categoryFinIdFinance;
  28. int startsIndex = 0;
  29. int startsIndex1 = 0;
  30. string message = "";
  31. private IBrush colorMessage;
  32. public int UserIdLoad { get => userIdLoad; set => this.RaiseAndSetIfChanged(ref userIdLoad, value); }
  33. public FinanceTableViewModel FinanceTableVM { get => financeTableViewModel; set => financeTableViewModel = value; }
  34. public List<Finance> Finance { get => finances; set => this.RaiseAndSetIfChanged(ref finances, value); }
  35. public IBrush ColorMessage { get => colorMessage; set => this.RaiseAndSetIfChanged(ref colorMessage, value); }
  36. public Finance? FinanceItem { get => financeItem; set => financeItem = value; }
  37. public string TitleFinance { get => titleFinance; set => this.RaiseAndSetIfChanged(ref titleFinance, value); }
  38. public decimal PriceFinance { get => priceFinance; set => this.RaiseAndSetIfChanged(ref priceFinance, value); }
  39. public int CategoryIdFinance { get => categoryIdFinance; set => this.RaiseAndSetIfChanged(ref categoryIdFinance, value); }
  40. public int StartsIndex { get => startsIndex; set => this.RaiseAndSetIfChanged(ref startsIndex, value); }
  41. public int StartsIndex1 { get => startsIndex1; set => this.RaiseAndSetIfChanged(ref startsIndex1, value); }
  42. public string CategoryFinTitle { get => categoryFinTitle; set => this.RaiseAndSetIfChanged(ref categoryFinTitle, value); }
  43. public string CategoryTitle { get => categoryTitle; set => this.RaiseAndSetIfChanged(ref categoryTitle, value); }
  44. public int CategoryFinIdFinance { get => categoryFinIdFinance; set => this.RaiseAndSetIfChanged(ref categoryFinIdFinance, value); }
  45. public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
  46. public List<string> TurnoverList
  47. {
  48. get
  49. {
  50. List<string> list_turnoverList = new List<string>();
  51. list_turnoverList.AddRange(ppContext.Categoriesfinances.Select(x => x.FinTitle).ToList());
  52. return list_turnoverList;
  53. }
  54. }
  55. public List<string> CategoriesList
  56. {
  57. get
  58. {
  59. List<string> list_categories = new List<string>();
  60. list_categories.AddRange(ppContext.Categories.Where(x => x.IdUser == UserIdLoad).Select(x => x.Title).ToList());
  61. return list_categories;
  62. }
  63. }
  64. public int LoadCategoryFinId(string categoryFinId)
  65. {
  66. int financeCategory1 = -1;
  67. using (ShmelevTeamContext ppContext = new ShmelevTeamContext())
  68. {
  69. var financeCategory = ppContext.Categoriesfinances
  70. .Where(f => f.FinTitle == categoryFinId)
  71. .Select(f => f.IdCatFin)
  72. .FirstOrDefault();
  73. financeCategory1 = financeCategory;
  74. }
  75. return financeCategory1;
  76. }
  77. public int LoadCategoryId(string categoryId)
  78. {
  79. int categoryId1 = -1;
  80. using (ShmelevTeamContext ppContext = new ShmelevTeamContext())
  81. {
  82. var financeCategory = ppContext.Categories
  83. .Where(f => f.Title == categoryId)
  84. .Select(f => f.CategoryId)
  85. .FirstOrDefault();
  86. categoryId1 = financeCategory;
  87. }
  88. return categoryId1;
  89. }
  90. public FinanceDataAccountViewModel()
  91. {
  92. financeItem = new Finance();
  93. }
  94. public FinanceDataAccountViewModel(ShmelevTeamContext ppContext, User currentUser)
  95. {
  96. UserIdLoad = currentUser.UserId;
  97. this.ppContext = ppContext;
  98. financeItem = new Finance();
  99. }
  100. public FinanceDataAccountViewModel(Finance financeItem, User currentUser, ShmelevTeamContext ppContext)
  101. {
  102. UserIdLoad = currentUser.UserId;
  103. this.ppContext = ppContext;
  104. FinanceTableVM = new FinanceTableViewModel(currentUser, ppContext);
  105. CategoryFinTitle = FinanceTableVM.LoadCategoryFinTitle(financeItem.CategoryFinId);
  106. CategoryTitle = FinanceTableVM.LoadCategoryTitle(financeItem.CategoryId);
  107. this.financeItem = financeItem;
  108. titleFinance = financeItem.Title;
  109. priceFinance = financeItem.Price;
  110. categoryIdFinance = LoadCategoryId(CategoryTitle);
  111. categoryFinIdFinance = LoadCategoryFinId(CategoryFinTitle);
  112. StartsIndex = categoryFinIdFinance-1;
  113. StartsIndex1 = categoryIdFinance - 1;
  114. }
  115. public Dictionary<string, Finance> GetDataFinance(User currentUser)
  116. {
  117. UserIdLoad = currentUser.UserId;
  118. if (string.IsNullOrEmpty(TitleFinance) || PriceFinance == null || CategoryIdFinance == null || CategoryFinIdFinance == null)
  119. return new Dictionary<string, Finance> { { "1", null } };
  120. else if (TitleFinance.Length > 50) return new Dictionary<string, Finance> { { "2", null } };
  121. else
  122. {
  123. FinanceItem.Title = TitleFinance;
  124. FinanceItem.UserId = currentUser.UserId;
  125. FinanceItem.Price = priceFinance;
  126. FinanceItem.CategoryId = LoadCategoryId(CategoryTitle);
  127. FinanceItem.CategoryFinId = LoadCategoryFinId(CategoryFinTitle);
  128. return new Dictionary<string, Finance> { { "4", FinanceItem } }; ;
  129. }
  130. }
  131. }
  132. }