ArticleModel.cs 8.5 KB

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