SurveyEditor.razor.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using Microsoft.AspNetCore.Components;
  2. using Microsoft.AspNetCore.Components.Authorization;
  3. using Microsoft.AspNetCore.Identity;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using HyperCube.Models;
  9. using Console = HyperCube.Utils.AdvConsole;
  10. namespace HyperCube.Pages
  11. {
  12. public partial class SurveyEditor : ComponentBase
  13. {
  14. [Inject]
  15. NavigationManager NavigationManager { get; set; }
  16. [Inject]
  17. AuthenticationStateProvider AuthenticationStateProvider { get; set; }
  18. [Inject]
  19. UserManager<IdentityUser> UserManager { get; set; }
  20. [Parameter]
  21. public int ID { get; set; }
  22. AccountModel _currentAccount;
  23. Survey _survey = new();
  24. List<KeyValuePair<int, SurveyItem>> _sortedItems = new(); /// wrap for items sorting
  25. List<KeyValuePair<int, SurveyItemOption>> _sortedOptions = new(); /// wrap for options sorting
  26. ModalInfo _modalInfo;
  27. protected override async Task OnInitializedAsync()
  28. {
  29. _currentAccount = await GetCurrentAcc();
  30. if (ID > 0)
  31. {
  32. var surveys = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveys WHERE id={ID}");
  33. if (surveys.Count > 0)
  34. {
  35. _survey = new()
  36. {
  37. EvenID = Convert.ToInt32(surveys[0]["eventid"]),
  38. ID = Convert.ToInt32(surveys[0]["id"]),
  39. Name = Convert.ToString(surveys[0]["name"]),
  40. Description = Convert.ToString(surveys[0]["description"]),
  41. DateCreated = Convert.ToDateTime(surveys[0]["date_created"]),
  42. DateUpdated = Convert.ToDateTime(surveys[0]["date_updated"]),
  43. CreatorID = Convert.ToString(surveys[0]["creatorid"]),
  44. IsNew = false
  45. };
  46. Console.WriteLine($"SurveyEditor. load survey id: {_survey.ID}, name: {_survey.Name}");
  47. }
  48. SurveyItem item;
  49. SurveyItemOption option;
  50. int itemPosition = 1;
  51. int optionPosition = 1;
  52. var surveyItems = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitems WHERE surveyid={_survey.ID} AND deleted<>1 ORDER BY position");
  53. if (surveyItems.Count > 0)
  54. {
  55. Console.WriteLine($"surveyItems.Count={surveyItems.Count}");
  56. foreach (var i in surveyItems)
  57. {
  58. item = new()
  59. {
  60. ID = Convert.ToInt32(i["id"]),
  61. SurveyID = Convert.ToInt32(i["surveyid"]),
  62. DateCreated = Convert.ToDateTime(i["date_created"]),
  63. DateUpdated = Convert.ToDateTime(i["date_updated"]),
  64. CreatorID = Convert.ToString(i["creatorid"]),
  65. Text = Convert.ToString(i["itemtext"]),
  66. //Position = Convert.ToInt32(i["position"]),
  67. Position = itemPosition++,
  68. Required = Convert.ToBoolean(i["required"]),
  69. IsNew = false
  70. };
  71. Console.WriteLine($"SurveyEditor. load survey [{item.SurveyID}] item id: {item.ID}, position: {item.Position}, name: {item.Text}");
  72. var surveyItemOption = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitemoptions WHERE itemid={item.ID} AND deleted<>1 ORDER BY position");
  73. if (surveyItemOption.Count > 0)
  74. {
  75. Console.WriteLine($"surveyItemOption.Count={surveyItemOption.Count}");
  76. foreach (var o in surveyItemOption)
  77. {
  78. option = new()
  79. {
  80. ID = Convert.ToInt32(o["id"]),
  81. ItemID = Convert.ToInt32(o["itemid"]),
  82. DateCreated = Convert.ToDateTime(o["date_created"]),
  83. DateUpdated = Convert.ToDateTime(o["date_updated"]),
  84. CreatorID = Convert.ToString(o["creatorid"]),
  85. Text = Convert.ToString(o["optiontext"]),
  86. //Position = Convert.ToInt32(o["position"]),
  87. Position = optionPosition++,
  88. Rate1 = Convert.ToInt32(o["rate1"]),
  89. Rate2 = Convert.ToInt32(o["rate2"]),
  90. Rate3 = Convert.ToInt32(o["rate3"]),
  91. Rate4 = Convert.ToInt32(o["rate4"]),
  92. IsNew = false
  93. };
  94. Console.WriteLine($"SurveyEditor. load survey [{item.SurveyID}] item [{item.ID}] option id: {option.ID}, position: {option.Position}, name: {option.Text}.");
  95. item.SurveyItemOptions.Add(option.ID, option);
  96. }
  97. }
  98. _survey.SurveyItems.Add(item.ID, item);
  99. SortOptions(item.ID);
  100. }
  101. SortItems();
  102. }
  103. }
  104. }
  105. void AddNewItem() => _survey.AddNewItem(_currentAccount.UUID);
  106. void AddNewOption(int ItemID) => _survey.SurveyItems[ItemID].AddNewOption(_currentAccount.UUID);
  107. void NewSurvey() => NavigationManager.NavigateTo("surveyeditor", true);
  108. void ItemMove(int itemID, int step)
  109. {
  110. _survey.MoveItem(itemID, step);
  111. SortItems();
  112. }
  113. void ItemDelete(int itemID)
  114. {
  115. _survey.DeleteItem(itemID);
  116. SortItems();
  117. }
  118. void OptionMove(int itemID, int optionID, int step)
  119. {
  120. _survey.SurveyItems[itemID].MoveItem(optionID, step);
  121. SortOptions(itemID);
  122. }
  123. void OptionDelete(int itemID, int optionID)
  124. {
  125. _survey.SurveyItems[itemID].DeleteOption(optionID);
  126. SortOptions(itemID);
  127. }
  128. async Task SaveSurvey()
  129. {
  130. await _survey.Save(_currentAccount.UUID);
  131. _modalInfo.Open("Опрос сохранен.");
  132. }
  133. void SortItems()
  134. {
  135. int itemPosition = 1;
  136. _sortedItems = _survey.SurveyItems.OrderBy(i => i.Value.Position).ToList();
  137. foreach (var item in _sortedItems) { item.Value.Position = itemPosition++; }
  138. }
  139. void SortOptions(int itemID)
  140. {
  141. //Console.WriteLine($"SortOptions, itemID:{itemID}, SurveyItems.Count:{_survey.SurveyItems.Count}.");
  142. //Console.WriteLine($"SurveyItemOptions.Count:{_survey.SurveyItems[itemID].SurveyItemOptions.Count}.");
  143. int optionPosition = 1;
  144. _sortedOptions = _survey.SurveyItems[itemID].SurveyItemOptions.OrderBy(i => i.Value.Position).ToList();
  145. foreach (var item in _sortedOptions) { item.Value.Position = optionPosition++; }
  146. }
  147. async Task<AccountModel> GetCurrentAcc()
  148. {
  149. Console.WriteLine($"SurveyEditor GetCurrentAcc");
  150. if (_currentAccount == null)
  151. {
  152. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  153. var user = authState.User;
  154. if (user.Identity.IsAuthenticated)
  155. {
  156. var currentUser = await UserManager.GetUserAsync(user);
  157. var acc = AccountModel.Find(currentUser.Id);
  158. if (acc != null)
  159. {
  160. _currentAccount = AccountModel.Loaded[currentUser.Id];
  161. }
  162. else
  163. {
  164. Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
  165. if (accounts.ContainsKey(currentUser.Id))
  166. _currentAccount = accounts[currentUser.Id];
  167. }
  168. }
  169. }
  170. return _currentAccount;
  171. }
  172. }
  173. }