Survey.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 List<KeyValuePair<int, SurveyItem>> SurveyItemsSorted { get; set; } = new(); /// wrap for sorting
  19. public bool IsNew { get; set; } = true;
  20. public bool IsDeleted { get; set; } = false;
  21. public async Task LoadByID(int id)
  22. {
  23. string sql = $"SELECT * FROM surveys WHERE id={id}";
  24. await Load(sql);
  25. }
  26. public async Task LoadByEventID(int eventID)
  27. {
  28. string sql = $"SELECT * FROM surveys WHERE eventid={eventID}";
  29. await Load(sql);
  30. }
  31. private async Task Load(string sql)
  32. {
  33. var surveys = await MySQLConnector.Instance().SQLSelectComplex(sql);
  34. if (surveys.Count > 0)
  35. {
  36. EvenID = Convert.ToInt32(surveys[0]["eventid"]);
  37. ID = Convert.ToInt32(surveys[0]["id"]);
  38. Name = Convert.ToString(surveys[0]["name"]);
  39. Description = Convert.ToString(surveys[0]["description"]);
  40. DateCreated = Convert.ToDateTime(surveys[0]["date_created"]);
  41. DateUpdated = Convert.ToDateTime(surveys[0]["date_updated"]);
  42. CreatorID = Convert.ToString(surveys[0]["creatorid"]);
  43. IsNew = false;
  44. Console.WriteLine($"Loading Survey, ID: {ID}, Name: {Name}");
  45. }
  46. SurveyItem item;
  47. SurveyItemOption option;
  48. int itemPosition = 1;
  49. int optionPosition = 1;
  50. var surveyItems = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitems WHERE surveyid={ID} AND deleted<>1 ORDER BY position");
  51. if (surveyItems.Count > 0)
  52. {
  53. foreach (var i in surveyItems)
  54. {
  55. item = new()
  56. {
  57. ID = Convert.ToInt32(i["id"]),
  58. SurveyID = Convert.ToInt32(i["surveyid"]),
  59. DateCreated = Convert.ToDateTime(i["date_created"]),
  60. DateUpdated = Convert.ToDateTime(i["date_updated"]),
  61. CreatorID = Convert.ToString(i["creatorid"]),
  62. Text = Convert.ToString(i["itemtext"]),
  63. Position = itemPosition++,
  64. Required = Convert.ToBoolean(i["required"]),
  65. IsNew = false
  66. };
  67. Console.WriteLine($"Loading SurveyItem, survey: {item.SurveyID}, item id: {item.ID}, position: {item.Position}, name: {item.Text}");
  68. var surveyItemOption = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitemoptions WHERE itemid={item.ID} AND deleted<>1 ORDER BY position");
  69. if (surveyItemOption.Count > 0)
  70. {
  71. foreach (var o in surveyItemOption)
  72. {
  73. option = new()
  74. {
  75. ID = Convert.ToInt32(o["id"]),
  76. ItemID = Convert.ToInt32(o["itemid"]),
  77. DateCreated = Convert.ToDateTime(o["date_created"]),
  78. DateUpdated = Convert.ToDateTime(o["date_updated"]),
  79. CreatorID = Convert.ToString(o["creatorid"]),
  80. Text = Convert.ToString(o["optiontext"]),
  81. Position = optionPosition++,
  82. Rate1 = Convert.ToInt32(o["rate1"]),
  83. Rate2 = Convert.ToInt32(o["rate2"]),
  84. Rate3 = Convert.ToInt32(o["rate3"]),
  85. Rate4 = Convert.ToInt32(o["rate4"]),
  86. IsNew = false
  87. };
  88. Console.WriteLine($"Loading SurveyItemOption, survey: {item.SurveyID}, item {item.ID}, option id: {option.ID}, position: {option.Position}, name: {option.Text}.");
  89. item.SurveyItemOptions.Add(option.ID, option);
  90. }
  91. }
  92. SurveyItems.Add(item.ID, item);
  93. item.SortOptions();
  94. }
  95. SortItems();
  96. }
  97. }
  98. public async Task<int> Save(string userID)
  99. {
  100. long newID;
  101. string stringSQL;
  102. if (IsNew)
  103. {
  104. stringSQL = $"INSERT INTO surveys (eventid, name, description, creatorid) " +
  105. $"VALUES ({EvenID}, '{Name}', '{Description}', '{userID}')";
  106. }
  107. else
  108. {
  109. stringSQL = $"UPDATE surveys " +
  110. $"SET eventid={EvenID}, name='{Name}', description='{Description}', deleted={Convert.ToInt32(IsDeleted)}, date_updated='{DateTime.Now:yyyy-MM-dd HH:mm:ss}' " +
  111. $"WHERE id={ID}";
  112. }
  113. newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
  114. IsNew = false;
  115. /// saving survey items
  116. int newItemID;
  117. Dictionary<int, SurveyItem> tmpSurveyItems = new();
  118. foreach (var item in SurveyItems)
  119. {
  120. if (newID != 0)
  121. item.Value.SurveyID = (int)newID;
  122. newItemID = await item.Value.Save(userID);
  123. if (newItemID != 0 && item.Key != newItemID)
  124. {
  125. Console.WriteLine($"Adding and updating item id:{item.Key} with new id:{newItemID}");
  126. tmpSurveyItems.Add(newItemID, item.Value);
  127. }
  128. else
  129. {
  130. Console.WriteLine($"Adding existed item id:{item.Key}");
  131. tmpSurveyItems.Add(item.Key, item.Value);
  132. }
  133. /// saving survey item options
  134. int newOptionID;
  135. Dictionary<int, SurveyItemOption> tmpSurveyItemOption = new();
  136. foreach (var option in item.Value.SurveyItemOptions)
  137. {
  138. if (newItemID != 0)
  139. option.Value.ItemID = newItemID;
  140. newOptionID = await option.Value.Save(userID);
  141. if (newOptionID != 0 && option.Key != newOptionID)
  142. {
  143. Console.WriteLine($"Adding and updating option id:{option.Key} with new id:{newOptionID}");
  144. tmpSurveyItemOption.Add(newOptionID, option.Value);
  145. }
  146. else
  147. {
  148. Console.WriteLine($"Adding existed option id:{option.Key}");
  149. tmpSurveyItemOption.Add(option.Key, option.Value);
  150. }
  151. }
  152. /// updating options collection with actual IDs from DB
  153. if (tmpSurveyItemOption.Count > 0)
  154. {
  155. item.Value.SurveyItemOptions.Clear();
  156. item.Value.SurveyItemOptions = tmpSurveyItemOption.ToDictionary(entry => entry.Key, entry => (SurveyItemOption)entry.Value.Clone());
  157. }
  158. }
  159. /// updating items collection with actual IDs from DB
  160. if (tmpSurveyItems.Count > 0)
  161. {
  162. SurveyItems.Clear();
  163. SurveyItems = tmpSurveyItems.ToDictionary(entry => entry.Key, entry => (SurveyItem)entry.Value.Clone());
  164. }
  165. return ID = (int)newID;
  166. }
  167. public void AddNewItem(string userID)
  168. {
  169. int surveyID = ID;
  170. Console.WriteLine($"Survey AddNewItem. survey id={surveyID}.");
  171. int newTempKey = (SurveyItems.Count > 0) ? SurveyItems.Keys.Max() + 1 : 1;
  172. int position = (SurveyItems.Count > 0) ? SurveyItems.Select(o => o.Value.Position).Max() + 1 : 1;
  173. Console.WriteLine($"Survey AddNewItem. item newTempKey={newTempKey}.");
  174. SurveyItem item = new()
  175. {
  176. ID = newTempKey,
  177. SurveyID = surveyID,
  178. Position = position,
  179. CreatorID = userID,
  180. DateCreated = DateTime.Now
  181. };
  182. SurveyItems.Add(newTempKey, item);
  183. }
  184. public void DeleteItem(int itemID)
  185. {
  186. if (SurveyItems.Count > 0 && SurveyItems.ContainsKey(itemID))
  187. {
  188. Console.WriteLine($"Survey [{ID}] DeleteItem id:{SurveyItems[itemID].ID} - isnew:{SurveyItems[itemID].IsNew}");
  189. if (SurveyItems[itemID].IsNew)
  190. SurveyItems.Remove(itemID);
  191. else
  192. SurveyItems[itemID].IsDeleted = true;
  193. }
  194. }
  195. public void MoveItem(int itemID, int step)
  196. {
  197. if (SurveyItems.Count > 0 && SurveyItems.ContainsKey(itemID))
  198. {
  199. Console.WriteLine($"Survey:[{ID}] MoveItem id: [{SurveyItems[itemID].ID}]");
  200. int min = SurveyItems.Select(o => o.Value.Position).Min();
  201. int max = SurveyItems.Select(o => o.Value.Position).Max();
  202. Console.WriteLine($"min:{min}, max:{max}.");
  203. int oldPosition, newPosition;
  204. oldPosition = SurveyItems[itemID].Position;
  205. int targetPosition = (oldPosition + step < min) ? min : oldPosition + step;
  206. targetPosition = (oldPosition + step > max) ? max : oldPosition + step;
  207. Console.WriteLine($"oldPosition:{oldPosition}, step:{step}, target:{targetPosition}.");
  208. int targetKey = SurveyItems.FirstOrDefault(x => x.Value.Position == targetPosition).Key;
  209. if (targetKey != 0)
  210. {
  211. newPosition = SurveyItems[targetKey].Position;
  212. Console.WriteLine($"newPosition:{newPosition}.");
  213. SurveyItems[itemID].Position = newPosition;
  214. SurveyItems[targetKey].Position = oldPosition;
  215. }
  216. }
  217. }
  218. public void SortItems()
  219. {
  220. int itemPosition = 1;
  221. SurveyItemsSorted = SurveyItems.OrderBy(i => i.Value.Position).ToList();
  222. foreach (var item in SurveyItemsSorted) { item.Value.Position = itemPosition++; }
  223. }
  224. }
  225. public class SurveyItem : ICloneable
  226. {
  227. public int ID { get; set; }
  228. public int SurveyID { get; set; }
  229. public string Text { get; set; }
  230. public int Position { get; set; }
  231. public string CreatorID { get; set; }
  232. public DateTime DateCreated { get; set; }
  233. public DateTime DateUpdated { get; set; }
  234. public bool Required { get; set; } = true;
  235. public Dictionary<int, SurveyItemOption> SurveyItemOptions { get; set; } = new();
  236. public List<KeyValuePair<int, SurveyItemOption>> SurveyItemOptionsSorted { get; set; } = new(); /// wrap for sorting
  237. public int AnswerID { get; set; } /// SurveyItemOption selected ID
  238. public bool IsNew { get; set; } = true;
  239. public bool IsDeleted { get; set; } = false;
  240. public async Task<int> Save(string userID)
  241. {
  242. long newID;
  243. string stringSQL;
  244. if (IsNew)
  245. {
  246. stringSQL = $"INSERT INTO surveyitems (surveyid, itemtext, position, required, creatorid) " +
  247. $"VALUES ({SurveyID}, '{Text}', {Position}, {Convert.ToInt32(Required)}, '{userID}')";
  248. }
  249. else
  250. {
  251. stringSQL = $"UPDATE surveyitems " +
  252. $"SET surveyid={SurveyID}, itemtext='{Text}', position={Position}, required={Convert.ToInt32(Required)}, deleted={Convert.ToInt32(IsDeleted)} " +
  253. $"WHERE id={ID}";
  254. }
  255. newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
  256. IsNew = false;
  257. return ID = (int)newID;
  258. }
  259. public void AddNewOption(string userID)
  260. {
  261. int itemID = ID;
  262. Console.WriteLine($"SurveyItem AddNewOption. item id={itemID}.");
  263. int newTempKey = (SurveyItemOptions.Count > 0) ? SurveyItemOptions.Keys.Max() + 1 : 1;
  264. int position = (SurveyItemOptions.Count > 0) ? SurveyItemOptions.Select(o => o.Value.Position).Max() + 1 : 1;
  265. Console.WriteLine($"SurveyItem AddNewOption. option newTempKey={newTempKey}.");
  266. SurveyItemOption option = new()
  267. {
  268. ID = newTempKey,
  269. ItemID = itemID,
  270. Position = position,
  271. CreatorID = userID,
  272. DateCreated = DateTime.Now
  273. };
  274. SurveyItemOptions.Add(newTempKey, option);
  275. }
  276. public void DeleteOption(int optionID)
  277. {
  278. if (SurveyItemOptions.Count > 0 && SurveyItemOptions.ContainsKey(optionID))
  279. {
  280. Console.WriteLine($"Survey [{SurveyID}] Item [{ID}] DeleteItem id:{SurveyItemOptions[optionID].ID} - isnew:{SurveyItemOptions[optionID].IsNew}");
  281. if (SurveyItemOptions[optionID].IsNew)
  282. SurveyItemOptions.Remove(optionID);
  283. else
  284. SurveyItemOptions[optionID].IsDeleted = true;
  285. }
  286. }
  287. public void MoveItem(int optionID, int step)
  288. {
  289. if (SurveyItemOptions.Count > 0 && SurveyItemOptions.ContainsKey(optionID))
  290. {
  291. Console.WriteLine($"Survey [{SurveyID}] Item [{ID}] MoveOption id:{SurveyItemOptions[optionID].ID}");
  292. int min = SurveyItemOptions.Select(o => o.Value.Position).Min();
  293. int max = SurveyItemOptions.Select(o => o.Value.Position).Max();
  294. Console.WriteLine($"min:{min}, max:{max}.");
  295. int oldPosition, newPosition;
  296. oldPosition = SurveyItemOptions[optionID].Position;
  297. int targetPosition = (oldPosition + step < min) ? min : oldPosition + step;
  298. targetPosition = (oldPosition + step > max) ? max : oldPosition + step;
  299. Console.WriteLine($"oldPosition:{oldPosition}, step:{step}, target:{targetPosition}.");
  300. int targetKey = SurveyItemOptions.FirstOrDefault(x => x.Value.Position == targetPosition).Key;
  301. if (targetKey != 0)
  302. {
  303. newPosition = SurveyItemOptions[targetKey].Position;
  304. Console.WriteLine($"newPosition:{newPosition}.");
  305. SurveyItemOptions[optionID].Position = newPosition;
  306. SurveyItemOptions[targetKey].Position = oldPosition;
  307. }
  308. }
  309. }
  310. public void SortOptions()
  311. {
  312. int optionPosition = 1;
  313. SurveyItemOptionsSorted = SurveyItemOptions.OrderBy(i => i.Value.Position).ToList();
  314. foreach (var item in SurveyItemOptionsSorted) { item.Value.Position = optionPosition++; }
  315. }
  316. public object Clone()
  317. {
  318. return MemberwiseClone();
  319. }
  320. }
  321. public class SurveyItemOption : ICloneable
  322. {
  323. public int ID { get; set; }
  324. public int ItemID { get; set; }
  325. public string Text { get; set; }
  326. public int Rate1 { get; set; }
  327. public int Rate2 { get; set; }
  328. public int Rate3 { get; set; }
  329. public int Rate4 { get; set; }
  330. public int Position { get; set; }
  331. public string CreatorID { get; set; }
  332. public DateTime DateCreated { get; set; }
  333. public DateTime DateUpdated { get; set; }
  334. public bool IsNew { get; set; } = true;
  335. public bool IsDeleted { get; set; } = false;
  336. public async Task<int> Save(string userID)
  337. {
  338. long newID;
  339. string stringSQL;
  340. if (IsNew)
  341. {
  342. stringSQL = $"INSERT INTO surveyitemoptions (itemid, optiontext, position, rate1, rate2, rate3, rate4, creatorid) " +
  343. $"VALUES ({ItemID}, '{Text}', {Position}, {Rate1}, {Rate2}, {Rate3}, {Rate4}, '{userID}')";
  344. }
  345. else
  346. {
  347. stringSQL = $"UPDATE surveyitemoptions " +
  348. $"SET itemid={ItemID}, optiontext='{Text}', position={Position}, rate1={Rate1}, rate2={Rate2}, rate3={Rate3}, rate4={Rate4}, deleted={Convert.ToInt32(IsDeleted)} " +
  349. $"WHERE id={ID}";
  350. }
  351. newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
  352. IsNew = false;
  353. return ID = (int)newID;
  354. }
  355. public object Clone()
  356. {
  357. return MemberwiseClone();
  358. }
  359. }
  360. }