Survey.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Console = HyperCube.Utils.AdvConsole;
  6. namespace HyperCube.Models
  7. {
  8. public class Survey
  9. {
  10. public int ID { get; set; }
  11. public int EvenID { get; set; }
  12. public string Name { get; set; }
  13. public string Description { get; set; }
  14. public DateTime DateCreated { get; set; }
  15. public DateTime DateUpdated { get; set; }
  16. public string CreatorID { get; set; }
  17. public Dictionary<int, SurveyItem> SurveyItems { get; set; } = new();
  18. public bool IsNew { get; set; } = true;
  19. public bool IsDeleted { get; set; } = false;
  20. public async Task<int> Save(string userID)
  21. {
  22. long newID;
  23. string stringSQL;
  24. if (IsNew)
  25. {
  26. stringSQL = $"INSERT INTO surveys (eventid, name, description, creatorid) " +
  27. $"VALUES ({EvenID}, '{Name}', '{Description}', '{userID}')";
  28. }
  29. else
  30. {
  31. stringSQL = $"UPDATE surveys " +
  32. $"SET eventid={EvenID}, name='{Name}', description='{Description}', deleted={Convert.ToInt32(IsDeleted)} " +
  33. $"WHERE id={ID}";
  34. }
  35. newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
  36. IsNew = false;
  37. int newItemID;
  38. Dictionary<int, SurveyItem> tmpSurveyItems = new();
  39. foreach (var item in SurveyItems)
  40. {
  41. if (newID != 0)
  42. item.Value.SurveyID = (int)newID;
  43. newItemID = await item.Value.Save(userID);
  44. if (newItemID != 0 && item.Key != newItemID)
  45. {
  46. Console.WriteLine($"Adding and updating item id: [{item.Key}] with new id: [{newItemID}]");
  47. tmpSurveyItems.Add(newItemID, item.Value);
  48. }
  49. else
  50. {
  51. Console.WriteLine($"Adding existed item id: [{item.Key}]");
  52. tmpSurveyItems.Add(item.Key, item.Value);
  53. }
  54. }
  55. if (tmpSurveyItems.Count > 0)
  56. {
  57. SurveyItems.Clear();
  58. SurveyItems = tmpSurveyItems.ToDictionary(entry => entry.Key, entry => (SurveyItem)entry.Value.Clone());
  59. }
  60. return ID = (int)newID;
  61. }
  62. public void AddNewItem(string userID)
  63. {
  64. int surveyID = ID;
  65. Console.WriteLine($"Survey AddNewItem. survey id={surveyID}.");
  66. int newTempKey = (SurveyItems.Count > 0) ? SurveyItems.Keys.Max() + 1 : 1;
  67. int position = (SurveyItems.Count > 0) ? SurveyItems.Select(o => o.Value.Position).Max() + 1 : 1;
  68. Console.WriteLine($"Survey AddNewItem. item newTempKey={newTempKey}.");
  69. SurveyItem item = new()
  70. {
  71. ID = newTempKey,
  72. SurveyID = surveyID,
  73. Position = position,
  74. CreatorID = userID,
  75. DateCreated = DateTime.Now
  76. };
  77. SurveyItems.Add(newTempKey, item);
  78. }
  79. public void DeleteItem(int itemID)
  80. {
  81. if (SurveyItems.Count > 0 && SurveyItems.ContainsKey(itemID))
  82. {
  83. Console.WriteLine($"Survey:[{ID}] DeleteItem id: [{SurveyItems[itemID].ID}] - isnew: {SurveyItems[itemID].IsNew}");
  84. if (SurveyItems[itemID].IsNew)
  85. SurveyItems.Remove(itemID);
  86. else
  87. SurveyItems[itemID].IsDeleted = true;
  88. }
  89. }
  90. public void MoveItem(int itemID, int step)
  91. {
  92. if (SurveyItems.Count > 0 && SurveyItems.ContainsKey(itemID))
  93. {
  94. Console.WriteLine($"Survey:[{ID}] MoveItem id: [{SurveyItems[itemID].ID}]");
  95. int min = SurveyItems.Select(o => o.Value.Position).Min();
  96. int max = SurveyItems.Select(o => o.Value.Position).Max();
  97. Console.WriteLine($"min:{min}, max:{max}.");
  98. int oldPosition, newPosition;
  99. oldPosition = SurveyItems[itemID].Position;
  100. int targetPosition = (oldPosition + step < min) ? min : oldPosition + step;
  101. targetPosition = (oldPosition + step > max) ? max : oldPosition + step;
  102. Console.WriteLine($"oldPosition:{oldPosition}, step:{step}, target:{targetPosition}.");
  103. int targetKey = SurveyItems.FirstOrDefault(x => x.Value.Position == targetPosition).Key;
  104. //SurveyItems.TryGetValue(targetPosition, out SurveyItem targetItem);
  105. if (targetKey != 0)
  106. {
  107. newPosition = SurveyItems[targetKey].Position;
  108. Console.WriteLine($"newPosition:{newPosition}.");
  109. SurveyItems[itemID].Position = newPosition;
  110. SurveyItems[targetKey].Position = oldPosition;
  111. }
  112. }
  113. }
  114. }
  115. public class SurveyItem : ICloneable
  116. {
  117. public int ID { get; set; }
  118. public int SurveyID { get; set; }
  119. public string Text { get; set; }
  120. public int Position { get; set; }
  121. public string CreatorID { get; set; }
  122. public DateTime DateCreated { get; set; }
  123. public DateTime DateUpdated { get; set; }
  124. public bool Required { get; set; } = true;
  125. public Dictionary<int, SurveyItemOption> SurveyItemOptions { get; set; } = new();
  126. public int AnswerID { get; set; } /// SurveyItemOption selected ID
  127. public bool IsNew { get; set; } = true;
  128. public bool IsDeleted { get; set; } = false;
  129. public async Task<int> Save(string userID)
  130. {
  131. long newID;
  132. string stringSQL;
  133. if (IsNew)
  134. {
  135. stringSQL = $"INSERT INTO surveyitems (surveyid, itemtext, position, required, creatorid) " +
  136. $"VALUES ({SurveyID}, '{Text}', {Position}, {Convert.ToInt32(Required)}, '{userID}')";
  137. }
  138. else
  139. {
  140. stringSQL = $"UPDATE surveyitems " +
  141. $"SET surveyid={SurveyID}, itemtext='{Text}', position={Position}, required={Convert.ToInt32(Required)}, deleted={Convert.ToInt32(IsDeleted)} " +
  142. $"WHERE id={ID}";
  143. }
  144. newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
  145. IsNew = false;
  146. return ID = (int)newID;
  147. }
  148. public void AddNewOption(string userID)
  149. {
  150. int itemID = ID;
  151. Console.WriteLine($"SurveyItem AddNewOption. item id={itemID}.");
  152. int newTempKey = (SurveyItemOptions.Count > 0) ? SurveyItemOptions.Keys.Max() + 1 : 1;
  153. int position = (SurveyItemOptions.Count > 0) ? SurveyItemOptions.Select(o => o.Value.Position).Max() + 1 : 1;
  154. Console.WriteLine($"SurveyItem AddNewOption. option newTempKey={newTempKey}.");
  155. SurveyItemOption option = new()
  156. {
  157. ID = newTempKey,
  158. ItemID = itemID,
  159. Position = position,
  160. CreatorID = userID,
  161. DateCreated = DateTime.Now
  162. };
  163. SurveyItemOptions.Add(newTempKey, option);
  164. }
  165. public object Clone()
  166. {
  167. return MemberwiseClone();
  168. }
  169. }
  170. public class SurveyItemOption : ICloneable
  171. {
  172. public int ID { get; set; }
  173. public int ItemID { get; set; }
  174. public string Text { get; set; }
  175. public int Rate1 { get; set; }
  176. public int Rate2 { get; set; }
  177. public int Rate3 { get; set; }
  178. public int Rate4 { get; set; }
  179. public int Position { get; set; }
  180. public string CreatorID { get; set; }
  181. public DateTime DateCreated { get; set; }
  182. public DateTime DateUpdated { get; set; }
  183. public bool IsNew { get; set; } = true;
  184. public bool IsDeleted { get; set; } = false;
  185. public object Clone()
  186. {
  187. return MemberwiseClone();
  188. }
  189. }
  190. }