ArticleModel.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.Threading.Tasks;
  6. using Console = HyperCube.Utils.AdvConsole;
  7. namespace HyperCube.Models
  8. {
  9. public enum ArticleStatus {
  10. [Display(Name = "Новый")]
  11. New = 0,
  12. [Display(Name = "Сохранен")]
  13. Saved,
  14. [Display(Name = "Ожидает верификации")]
  15. AwatingVerify,
  16. [Display(Name = "Верифицируется")]
  17. Verifying,
  18. [Display(Name = "Верифицирован")]
  19. Verified,
  20. [Display(Name = "Отклонен")]
  21. Rejected,
  22. [Display(Name = "Удален")]
  23. Deleted }
  24. public class ArticleModel : ICloneable
  25. {
  26. public int ID { get; set; }
  27. [Required]
  28. public string Filename { get; set; }
  29. [Required]
  30. public string HashSum { get; set; }
  31. public string FilenameReal { get { return ID + "_" + Filename; } }
  32. [Required]
  33. public string Name { get; set; }
  34. [Required]
  35. public DateTime PublishDate { get; set; } = DateTime.Now.Date;
  36. [Required]
  37. public string Authors { get; set; }
  38. [Required]
  39. public string Keywords { get; set; }
  40. [Required]
  41. public string Annotation { get; set; }
  42. public string Text { get; set; }
  43. public ArticleStatus Status { get; set; } = ArticleStatus.New;
  44. /// fields for recognited text
  45. public string NounGroups { get; set; }
  46. public string Entities { get; set; }
  47. public string Morph { get; set; }
  48. public string Keywords1 { get; set; }
  49. public string Keywords2 { get; set; }
  50. /// collection for history of verification
  51. public Dictionary<int, VerificationPoint> VerificationHistory { get; set; }
  52. public int? Rating
  53. {
  54. get { return rating; }
  55. set
  56. {
  57. rating = value;
  58. if (rating < 1) rating = 1;
  59. if (rating > 5) rating = 5;
  60. }
  61. }
  62. private int? rating;
  63. private string initiatorUUID = "";
  64. public static ArticleModel Find(int id)
  65. {
  66. if (id > 0) {
  67. if (AppData.Articles.ContainsKey(id))
  68. return AppData.Articles[id];
  69. }
  70. return null;
  71. }
  72. public async Task<int> GetEditsCount(string acc_id = "")
  73. {
  74. MySQLConnector dbCon = MySQLConnector.Instance();
  75. string stringSQL = $"SELECT COUNT(*) " +
  76. $"FROM articles_edit_log ";
  77. if (acc_id.Length < 36)
  78. stringSQL += $"WHERE article_id={this.ID}";
  79. else
  80. stringSQL += $"WHERE article_id={this.ID} AND acc_id='{acc_id}'";
  81. int count = await dbCon.SQLSelectCount(stringSQL);
  82. return count;
  83. }
  84. public async Task<string> GetInitiatorUUID()
  85. {
  86. if (initiatorUUID.Length < 36)
  87. {
  88. Console.WriteLine("initiatorUUID is null, getting it from DB");
  89. MySQLConnector dbCon = MySQLConnector.Instance();
  90. string stringSQL = $"SELECT acc_id " +
  91. $"FROM articles " +
  92. $"JOIN actions_history ON actions_history.article_id = articles.id " +
  93. $"WHERE articles.id={this.ID} AND action_type={(int)ArticleStatus.Saved}";
  94. initiatorUUID = await dbCon.SQLSelectUUID(stringSQL);
  95. }
  96. else
  97. Console.WriteLine("initiatorUUID already set");
  98. return initiatorUUID;
  99. }
  100. public async Task<Dictionary<int, VerificationPoint>> GetVerificationHistory(int articleid, string acc_uuid)
  101. {
  102. Console.WriteLine($"GetVerificationHistory for article [{articleid}]");
  103. Dictionary<int, VerificationPoint> verificationHistory = new();
  104. VerificationPoint verificationPoint;
  105. var vps = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FFROM articles_verification WHERE article_id='{articleid}'"); //AND acc_id='{acc_uuid}'
  106. if (vps.Count > 0)
  107. {
  108. foreach (var vp in vps)
  109. {
  110. verificationPoint = new()
  111. {
  112. ID = Convert.ToInt32(vp["id"]),
  113. RulesViolation = Convert.ToBoolean(vp["rules_violation"]),
  114. NonExpert = Convert.ToBoolean(vp["nonexpert"]),
  115. AdditionalVerificationRequired = Convert.ToBoolean(vp["additional_verification_required"]),
  116. Rejected = Convert.ToBoolean(vp["rejected"]),
  117. RejectReason = Convert.ToString(vp["reject_reason"]),
  118. Tags = Convert.ToString(vp["tags"]),
  119. DateAdd = Convert.ToDateTime(vp["date_add"])
  120. };
  121. Console.WriteLine($"add verificationPoint. id: {verificationPoint.ID}, rules_violation: {verificationPoint.RulesViolation}, date_add: {verificationPoint.DateAdd}");
  122. verificationHistory.Add(verificationPoint.ID, verificationPoint);
  123. }
  124. }
  125. else
  126. Console.WriteLine($"Article [{articleid}] has no history yet.");
  127. return verificationHistory;
  128. }
  129. public async Task SaveVerificationPoint(VerificationPoint verificationPoint, string acc_uuid)
  130. {
  131. //Console.WriteLine($"SaveVerificationPoint");
  132. int rules_violation = Convert.ToInt32(verificationPoint.RulesViolation);
  133. int nonexpert = Convert.ToInt32(verificationPoint.RulesViolation);
  134. int additional_verification_required = Convert.ToInt32(verificationPoint.RulesViolation);
  135. int rejected = Convert.ToInt32(verificationPoint.RulesViolation);
  136. if (verificationPoint.ID == 0) ///new point
  137. {
  138. Console.WriteLine($"SaveVerificationPoint. New point to save");
  139. long id = await MySQLConnector.Instance().SQLInsert($"INSERT INTO articles_verification " +
  140. $"(article_id, acc_id, rules_violation, nonexpert, additional_verification_required, rejected, reject_reason, tags) " +
  141. $"VALUES ('{this.ID}','{acc_uuid}','{rules_violation}', '{nonexpert}', '{additional_verification_required}', " +
  142. $"'{rejected}', '{verificationPoint.RejectReason}', '{verificationPoint.Tags}')");
  143. if (id > 0)
  144. {
  145. verificationPoint.ID = (int)id;
  146. VerificationHistory.Add(verificationPoint.ID, verificationPoint);
  147. Console.WriteLine($"SaveVerificationPoint. Point saved, id: {id}");
  148. }
  149. else
  150. Console.WriteLine($"SaveVerificationPoint. Something went wrong, id < 1");
  151. }
  152. else ///existed
  153. {
  154. Console.WriteLine($"SaveVerificationPoint. Existed point to save, id: {verificationPoint.ID}");
  155. await MySQLConnector.Instance().SQLInsert($"UPDATE articles_verification SET " +
  156. $"acc_id='{acc_uuid}', rules_violation='{rules_violation}', nonexpert='{nonexpert}', additional_verification_required={additional_verification_required}, " +
  157. $"rejected={rejected}, reject_reason={verificationPoint.RejectReason}, tags={verificationPoint.Tags}) " +
  158. $"WHERE id={verificationPoint.ID}");
  159. }
  160. }
  161. public object Clone()
  162. {
  163. return MemberwiseClone();
  164. }
  165. }
  166. public class VerificationPoint
  167. {
  168. public int ID;
  169. public bool RulesViolation;
  170. public bool NonExpert;
  171. public bool AdditionalVerificationRequired;
  172. public bool Rejected;
  173. public string RejectReason;
  174. public string Tags;
  175. public DateTime DateAdd;
  176. public VerificationPoint()
  177. {
  178. }
  179. }
  180. }