ArticleModel.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. public string Tags { get; set; }
  42. public int CharCount { get; set; }
  43. [Required]
  44. public string Annotation { get; set; }
  45. public string Text { get; set; }
  46. public ArticleStatus Status { get; set; } = ArticleStatus.New;
  47. /// fields for recognited text
  48. public string NounGroups { get; set; }
  49. public string Entities { get; set; }
  50. public string Morph { get; set; }
  51. public string Keywords1 { get; set; }
  52. public string Keywords2 { get; set; }
  53. public float[] weights = new float[] {1,1,1,1,1,1,1};
  54. /// collection for history of verification
  55. public Dictionary<int, VerificationPoint> VerificationHistory { get; set; } = new();
  56. public int CalcPValue()
  57. {
  58. var P1 = CharCount;
  59. var P2 = Keywords.Split(",").Length;
  60. if (Tags == null)
  61. Tags = "";
  62. var P3 = Tags.Split(",").Length;
  63. var P4 = 1; //параметр качества аннотации, измеряется семантическим алгоритмом Системы
  64. var P5 = 1; //параметр рейтинга издания, численно равен единице, делённой на номер квартиля издания, при отсутствии квартиля равен 0,2
  65. var P6 = 1; //параметр страны, численно равен единице, делённой на номер группы рейтинга страны
  66. var P7 = 1; //параметр ценности нарратива, измеряется семантическим алгоритмом Системы.
  67. return (int) Math.Floor(P1 * weights[0] + P2 * weights[1] + P3 * weights[2] + P4 * weights[3] + P5 * weights[4] + P6 * weights[5] + P7 * weights[6]);
  68. }
  69. public int? Rating
  70. {
  71. get { return rating; }
  72. set
  73. {
  74. rating = value;
  75. if (rating < 1) rating = 1;
  76. if (rating > 5) rating = 5;
  77. }
  78. }
  79. private int? rating;
  80. private string initiatorUUID = "";
  81. public static ArticleModel Find(int id)
  82. {
  83. if (id > 0) {
  84. if (AppData.Articles.ContainsKey(id))
  85. return AppData.Articles[id];
  86. }
  87. return null;
  88. }
  89. public async Task<int> GetEditsCount(string acc_id = "")
  90. {
  91. MySQLConnector dbCon = MySQLConnector.Instance();
  92. string stringSQL = $"SELECT COUNT(*) " +
  93. $"FROM articles_edit_log ";
  94. if (acc_id.Length < 36)
  95. stringSQL += $"WHERE article_id={this.ID}";
  96. else
  97. stringSQL += $"WHERE article_id={this.ID} AND acc_id='{acc_id}'";
  98. int count = await dbCon.SQLSelectCount(stringSQL);
  99. return count;
  100. }
  101. public async Task<string> GetInitiatorUUID(ArticleStatus status)
  102. {
  103. if (initiatorUUID.Length < 36)
  104. {
  105. Console.WriteLine($"initiatorUUID {initiatorUUID} is null, getting it from DB");
  106. MySQLConnector dbCon = MySQLConnector.Instance();
  107. string stringSQL = $"SELECT acc_id " +
  108. $"FROM articles " +
  109. $"JOIN actions_history ON actions_history.article_id = articles.id " +
  110. $"WHERE articles.id={this.ID} AND action_type={(int)status}";
  111. initiatorUUID = await dbCon.SQLSelectUUID(stringSQL);
  112. }
  113. else
  114. Console.WriteLine("initiatorUUID already set");
  115. Console.WriteLine($"initiatorUUID found {initiatorUUID}");
  116. return initiatorUUID;
  117. }
  118. public async Task<Dictionary<int, VerificationPoint>> GetVerificationHistory(string acc_uuid)
  119. {
  120. Console.WriteLine($"GetVerificationHistory for article [{this.ID}]");
  121. //Dictionary<int, VerificationPoint> verificationHistory = new();
  122. VerificationPoint verificationPoint;
  123. var vps = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM articles_verification WHERE article_id='{this.ID}'"); //AND acc_id='{acc_uuid}'
  124. if (vps.Count > 0)
  125. {
  126. foreach (var vp in vps)
  127. {
  128. verificationPoint = new()
  129. {
  130. ID = Convert.ToInt32(vp["id"]),
  131. RulesViolation = Convert.ToBoolean(vp["rules_violation"]),
  132. NonExpert = Convert.ToBoolean(vp["nonexpert"]),
  133. AdditionalVerificationRequired = Convert.ToBoolean(vp["additional_verification_required"]),
  134. Rejected = Convert.ToBoolean(vp["rejected"]),
  135. RejectReason = Convert.ToString(vp["reject_reason"]),
  136. Tags = Convert.ToString(vp["tags"]),
  137. DateAdd = Convert.ToDateTime(vp["date_add"])
  138. };
  139. Console.WriteLine($"add verificationPoint. id: {verificationPoint.ID}, rules_violation: {verificationPoint.RulesViolation}, date_add: {verificationPoint.DateAdd}");
  140. VerificationHistory.Add(verificationPoint.ID, verificationPoint);
  141. }
  142. }
  143. else
  144. Console.WriteLine($"Article [{this.ID}] has no history yet.");
  145. return VerificationHistory;
  146. }
  147. public async Task SaveLastVerificationPoint(VerificationPoint verificationPoint, string acc_uuid)
  148. {
  149. //Console.WriteLine($"SaveVerificationPoint");
  150. int rules_violation = Convert.ToInt32(verificationPoint.RulesViolation);
  151. int nonexpert = Convert.ToInt32(verificationPoint.NonExpert);
  152. int additional_verification_required = Convert.ToInt32(verificationPoint.AdditionalVerificationRequired);
  153. int rejected = Convert.ToInt32(verificationPoint.Rejected);
  154. //if (verificationPoint.ID == 0) ///new point
  155. //{
  156. Console.WriteLine($"SaveVerificationPoint. New point to save");
  157. long id = await MySQLConnector.Instance().SQLInsert($"INSERT INTO articles_verification " +
  158. $"(article_id, acc_id, rules_violation, nonexpert, additional_verification_required, rejected, reject_reason, tags) " +
  159. $"VALUES ('{this.ID}','{acc_uuid}','{rules_violation}', '{nonexpert}', '{additional_verification_required}', " +
  160. $"'{rejected}', '{verificationPoint.RejectReason}', '{verificationPoint.Tags}')");
  161. if (id > 0)
  162. {
  163. verificationPoint.ID = (int)id;
  164. VerificationHistory.Add(verificationPoint.ID, verificationPoint);
  165. Console.WriteLine($"SaveVerificationPoint. Point saved, id: {id}");
  166. }
  167. else
  168. Console.WriteLine($"SaveVerificationPoint. Something went wrong, id < 1");
  169. //}
  170. //else ///existed
  171. //{
  172. // Console.WriteLine($"SaveVerificationPoint. Existed point to save, id: {verificationPoint.ID}");
  173. // await MySQLConnector.Instance().SQLInsert($"UPDATE articles_verification SET " +
  174. // $"acc_id='{acc_uuid}', rules_violation='{rules_violation}', nonexpert='{nonexpert}', additional_verification_required={additional_verification_required}, " +
  175. // $"rejected={rejected}, reject_reason={verificationPoint.RejectReason}, tags={verificationPoint.Tags}) " +
  176. // $"WHERE id={verificationPoint.ID}");
  177. //}
  178. }
  179. public object Clone()
  180. {
  181. return MemberwiseClone();
  182. }
  183. }
  184. public class VerificationPoint
  185. {
  186. public int ID;
  187. public bool RulesViolation;
  188. public bool NonExpert;
  189. public bool AdditionalVerificationRequired;
  190. public bool Rejected;
  191. public string RejectReason;
  192. public string Tags;
  193. public DateTime DateAdd;
  194. public VerificationPoint()
  195. {
  196. }
  197. }
  198. }