AccountPageViewModel.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using LogoForceTestApp.Modules.MainModule.Models;
  2. using Newtonsoft.Json;
  3. using Prism.Commands;
  4. using Prism.Ioc;
  5. using Prism.Mvvm;
  6. using Repository;
  7. using Repository.Entiies;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace LogoForceTestApp.Modules.MainModule.ViewModels
  17. {
  18. public class AccountPageViewModel: BindableBase
  19. {
  20. private readonly IContainerProvider _container;
  21. #region 属性
  22. private MulProductModel _Products;
  23. public MulProductModel Products
  24. {
  25. get { return _Products; }
  26. set { SetProperty(ref _Products, value); }
  27. }
  28. private string _PTName;
  29. public string PTName
  30. {
  31. get { return _PTName; }
  32. set { SetProperty(ref _PTName, value); }
  33. }
  34. private int _PTNameNumber;
  35. public int PTNameNumber
  36. {
  37. get { return _PTNameNumber; }
  38. set { SetProperty(ref _PTNameNumber, value); }
  39. }
  40. private ObservableCollection<string> _Question1=new ObservableCollection<string>();
  41. public ObservableCollection<string> Question1
  42. {
  43. get { return _Question1; }
  44. set { SetProperty(ref _Question1, value); }
  45. }
  46. private ObservableCollection<string> _Question2 = new ObservableCollection<string>();
  47. public ObservableCollection<string> Question2
  48. {
  49. get { return _Question2; }
  50. set { SetProperty(ref _Question2, value); }
  51. }
  52. private ObservableCollection<string> _Question3 = new ObservableCollection<string>();
  53. public ObservableCollection<string> Question3
  54. {
  55. get { return _Question3; }
  56. set { SetProperty(ref _Question3, value); }
  57. }
  58. private int _Selected1;
  59. public int Selected1
  60. {
  61. get { return _Selected1; }
  62. set { SetProperty(ref _Selected1, value); }
  63. }
  64. private int _Selected2;
  65. public int Selected2
  66. {
  67. get { return _Selected2; }
  68. set { SetProperty(ref _Selected2, value); }
  69. }
  70. private int _Selected3;
  71. public int Selected3
  72. {
  73. get { return _Selected3; }
  74. set { SetProperty(ref _Selected3, value); }
  75. }
  76. private string _Text1;
  77. public string Text1
  78. {
  79. get { return _Text1; }
  80. set { SetProperty(ref _Text1, value); }
  81. }
  82. private string _Text2;
  83. public string Text2
  84. {
  85. get { return _Text2; }
  86. set { SetProperty(ref _Text2, value); }
  87. }
  88. private string _Text3;
  89. public string Text3
  90. {
  91. get { return _Text3; }
  92. set { SetProperty(ref _Text3, value); }
  93. }
  94. #endregion
  95. #region 命令
  96. public DelegateCommand AddCmm { get; set; }
  97. public DelegateCommand AcquireCmm { get; set; }
  98. public DelegateCommand ModificationCmm { get; set; }
  99. private DelegateCommand _RemoveQuestion1Command;
  100. public DelegateCommand RemoveQuestion1Command =>
  101. _RemoveQuestion1Command ?? (_RemoveQuestion1Command = new DelegateCommand(ExecuteRemoveQuestion1Command));
  102. private DelegateCommand _RemoveQuestion2Command;
  103. public DelegateCommand RemoveQuestion2Command =>
  104. _RemoveQuestion2Command ?? (_RemoveQuestion2Command = new DelegateCommand(ExecuteRemoveQuestion2Command));
  105. private DelegateCommand _RemoveQuestion3Command;
  106. public DelegateCommand RemoveQuestion3Command =>
  107. _RemoveQuestion3Command ?? (_RemoveQuestion3Command = new DelegateCommand(ExecuteRemoveQuestion3Command));
  108. private DelegateCommand<string> _AddQuesetion1;
  109. public DelegateCommand<string> AddQuesetion1 =>
  110. _AddQuesetion1 ?? (_AddQuesetion1 = new DelegateCommand<string>(ExecuteAddQuesetion1));
  111. private DelegateCommand<string> _AddQuesetion2;
  112. public DelegateCommand<string> AddQuesetion2 =>
  113. _AddQuesetion2 ?? (_AddQuesetion2 = new DelegateCommand<string>(ExecuteAddQuesetion2));
  114. private DelegateCommand<string> _AddQuesetion3;
  115. public DelegateCommand<string> AddQuesetion3 =>
  116. _AddQuesetion3 ?? (_AddQuesetion3 = new DelegateCommand<string>(ExecuteAddQuesetion3));
  117. #endregion
  118. public AccountPageViewModel(IContainerProvider container)
  119. {
  120. _container = container;
  121. AddCmm = new DelegateCommand(AddProCmm);
  122. AcquireCmm = new DelegateCommand(AcquireProCmm);
  123. ModificationCmm = new DelegateCommand(ModificationProCmm);
  124. Products = _container.Resolve<MulProductModel>();
  125. if (Products == null)
  126. {
  127. Products = new MulProductModel();
  128. }
  129. }
  130. private void ModificationProCmm()
  131. {
  132. var currentProduct = Products.ProductCollection.FirstOrDefault(p => p.Name == PTName);
  133. if (currentProduct != null)
  134. {
  135. currentProduct.NameNumber = PTNameNumber;
  136. currentProduct.QuestionType1= new ObservableCollection<string>(Question1);
  137. currentProduct.QuestionType2= new ObservableCollection<string>(Question2);
  138. currentProduct.QuestionType3= new ObservableCollection<string>(Question3);
  139. }
  140. Products.SaveParame();
  141. MessageBox.Show("修改成功");
  142. }
  143. private void AcquireProCmm()
  144. {
  145. Products.LoadParame();
  146. var currentProduct = Products.ProductCollection.FirstOrDefault(p => p.Name == PTName);
  147. if (currentProduct != null)
  148. {
  149. PTNameNumber = currentProduct.NameNumber;
  150. Question1 = currentProduct.QuestionType1;
  151. Question2 = currentProduct.QuestionType2;
  152. Question3 = currentProduct.QuestionType3;
  153. }
  154. }
  155. private void AddProCmm()
  156. {
  157. if (Products.ProductCollection.Count != 0)
  158. {
  159. Products.LoadParame();
  160. }
  161. Products.ProductCollection.Add(new ProductModel() { Name = PTName, NameNumber=PTNameNumber,
  162. QuestionType1 = Question1, QuestionType2 = Question2, QuestionType3 = Question3 });
  163. Products.SaveParame();
  164. MessageBox.Show("添加成功");
  165. }
  166. void ExecuteRemoveQuestion3Command()
  167. {
  168. if (Selected3 < 0) return;
  169. Question3.RemoveAt(Selected3);
  170. }
  171. void ExecuteRemoveQuestion2Command()
  172. {
  173. if (Selected2 < 0) return;
  174. Question2.RemoveAt(Selected2);
  175. }
  176. void ExecuteRemoveQuestion1Command()
  177. {
  178. if (Selected1 < 0) return;
  179. Question1.RemoveAt(Selected1);
  180. }
  181. private void ExecuteAddQuesetion3(string str)
  182. {
  183. Question3.Add(str);
  184. Text3 = string.Empty;
  185. }
  186. private void ExecuteAddQuesetion2(string str)
  187. {
  188. Question2.Add(str);
  189. Text2 = string.Empty;
  190. }
  191. private void ExecuteAddQuesetion1(string str)
  192. {
  193. Question1.Add(str);
  194. Text1 = string.Empty;
  195. }
  196. }
  197. }