MongoDbClient.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using MongoDB.Bson;
  2. using MongoDB.Driver;
  3. using System;
  4. using System.Diagnostics;
  5. using System.Threading;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using ReportDataResume.Remedical;
  9. using ReportDataResume.AfterSales;
  10. using ReportDataResume.Teaching;
  11. using ReportDataResume.RemoteAppointment;
  12. using ReportDataResume.FrontPage;
  13. namespace ReportDataResume
  14. {
  15. internal class MongoDbClient
  16. {
  17. private const int ConnnectTimeOut = 120;
  18. private const int WaitQueueTimeout = 120;
  19. private const int ServerSelectionTimeout = 300;
  20. private const int SocketTimeout = 120;
  21. private readonly string _users = "Users";
  22. private readonly string _terminals = "Terminals";
  23. private readonly string _chatMessages = "ChatMessages";
  24. private readonly string _storageFileInfoes = "StorageFileInfoes";
  25. //Remedical
  26. private readonly string _terminalDatas = "TerminalDatas";
  27. private readonly string _posterHistory = "PosterHistories";
  28. private readonly string _reportInfoResults = "ReportInfoResults";
  29. //AfterSale
  30. private readonly string _patches = "Patches";
  31. //Storage file
  32. private readonly string _uploadStorageFileInfo = "UploadStorageFileInfo";
  33. //carotid
  34. private readonly string _carotid3dModelDatas = "Carotid3dModelDatas";
  35. private IMongoDatabase _database;
  36. private MongoClient _mongoClient;
  37. //Teaching
  38. private readonly string _answerSheets = "AnswerSheets";
  39. private readonly string _teachingTerminalDatas = "TeachingTerminalDatas";
  40. public readonly string _shareLiveMessageRecords = "ShareLiveMessageRecords";
  41. private readonly string _terminalAIDataExcuteRecords = "TerminalAIDataExcuteRecords";
  42. private readonly string _banners = "Banners";
  43. private readonly string _consultationRecords = "ConsultationRecords";
  44. private readonly string _printerDrives = "PrinterDrives";
  45. private readonly string _homePagePublicitys = "HomePagePublicity";
  46. private readonly string _terminalRecord = "TerminalRecords";
  47. private readonly string _requestNotes = "RequestNotes";
  48. private readonly string _organizations = "Organizations";
  49. public MongoDbClient(string address, int port)
  50. {
  51. BuilderClient(address, port);
  52. RegisterEntities();
  53. }
  54. /// <summary>
  55. /// 构造MongoClient
  56. /// </summary>
  57. private void BuilderClient(string address, int port, int tryCount = 0)
  58. {
  59. try
  60. {
  61. CreateClient(address, port);
  62. }
  63. catch (Exception ex)
  64. {
  65. tryCount++;
  66. if (tryCount > 5)
  67. {
  68. //Logger.WriteLineError($"BuilderClient fail {ex},tryCount:{tryCount} over 5 times,Server will closed");
  69. throw;
  70. }
  71. //Logger.WriteLineError($"BuilderClient fail {ex},tryCount:{tryCount} wait 5 second to try again!");
  72. Thread.Sleep(5000);
  73. BuilderClient(address, port, tryCount);
  74. }
  75. }
  76. private void CreateClient(string address, int port)
  77. {
  78. var mongoSetting = new MongoClientSettings
  79. {
  80. ConnectTimeout = TimeSpan.FromSeconds(ConnnectTimeOut),
  81. Server = new MongoServerAddress(address, port),
  82. MaxConnectionPoolSize = 20000,
  83. WaitQueueTimeout = TimeSpan.FromSeconds(WaitQueueTimeout),
  84. ServerSelectionTimeout = TimeSpan.FromSeconds(ServerSelectionTimeout),
  85. SocketTimeout = TimeSpan.FromSeconds(SocketTimeout)
  86. };
  87. _mongoClient = new MongoClient(mongoSetting);
  88. _database = _mongoClient.GetDatabase("vCloudDb");
  89. }
  90. /// <summary>
  91. /// 获取数据集对象
  92. /// </summary>
  93. /// <typeparam name="T"></typeparam>
  94. /// <param name="collectionName"></param>
  95. /// <returns></returns>
  96. public IMongoCollection<T> GetCollection<T>(string collectionName)
  97. {
  98. return _database.GetCollection<T>(collectionName);
  99. }
  100. public void RunScript(string command)
  101. {
  102. _database.RunCommand(new JsonCommand<BsonDocument>(command));
  103. }
  104. private void RegisterEntities()
  105. {
  106. Users = GetCollection<User>(_users);
  107. Terminals = GetCollection<Terminal>(_terminals);
  108. ChatMessages = GetCollection<ChatMessage>(_chatMessages);
  109. TerminalDatas = GetCollection<TerminalData>(_terminalDatas);
  110. Patches = GetCollection<Patch>(_patches);
  111. PosterHistories = GetCollection<PosterHistory>(_posterHistory);
  112. StorageFileInfoes = GetCollection<StorageFileInfo>(_storageFileInfoes);
  113. ReportInfoResults = GetCollection<ReportInfoResult>(_reportInfoResults);
  114. UploadStorageFileInfoes= GetCollection<UploadStorageFileInfo>(_uploadStorageFileInfo);
  115. Carotid3dModelDatas = GetCollection<Carotid3dModelData>(_carotid3dModelDatas);
  116. AnswerSheets = GetCollection<AnswerSheet>(_answerSheets);
  117. TeachingTerminalDatas = GetCollection<TeachingTerminalData>(_teachingTerminalDatas);
  118. TerminalAIDataExcuteRecords = GetCollection<TerminalAIDataExcuteRecord>(_terminalAIDataExcuteRecords);
  119. Banners = GetCollection<Banner>(_banners);
  120. ConsultationRecords = GetCollection<ConsultationRecord>(_consultationRecords);
  121. PrinterDrives = GetCollection<PrinterDrive>(_printerDrives);
  122. HomePagePublicitys = GetCollection<HomePagePublicity>(_homePagePublicitys);
  123. TerminalRecords = GetCollection<TerminalRecord>(_terminalRecord);
  124. RequestNotes = GetCollection<RequestNote>(_requestNotes);
  125. Organizations = GetCollection<Organization>(_organizations);
  126. }
  127. public IMongoCollection<User> Users { get; private set; }
  128. public IMongoCollection<Terminal> Terminals { get; private set; }
  129. public IMongoCollection<ChatMessage> ChatMessages { get; private set; }
  130. public IMongoCollection<TerminalData> TerminalDatas { get; private set; }
  131. public IMongoCollection<PosterHistory> PosterHistories { get; private set; }
  132. //AfterSale
  133. public IMongoCollection<Patch> Patches { get; private set; }
  134. public IMongoCollection<StorageFileInfo> StorageFileInfoes { get; private set; }
  135. public IMongoCollection<ReportInfoResult> ReportInfoResults { get; private set; }
  136. public IMongoCollection<UploadStorageFileInfo> UploadStorageFileInfoes { get; private set; }
  137. //carotid
  138. public IMongoCollection<Carotid3dModelData> Carotid3dModelDatas { get; private set; }
  139. public IMongoCollection<AnswerSheet> AnswerSheets { get; private set; }
  140. public IMongoCollection<TeachingTerminalData> TeachingTerminalDatas { get; private set; }
  141. public IMongoCollection<TerminalAIDataExcuteRecord> TerminalAIDataExcuteRecords { get; private set; }
  142. public IMongoCollection<Banner> Banners { get; private set; }
  143. public IMongoCollection<ConsultationRecord> ConsultationRecords { get; private set; }
  144. public IMongoCollection<PrinterDrive> PrinterDrives { get; private set; }
  145. public IMongoCollection<HomePagePublicity> HomePagePublicitys { get; private set; }
  146. public IMongoCollection<TerminalRecord> TerminalRecords { get; private set; }
  147. public IMongoCollection<RequestNote> RequestNotes { get; private set; }
  148. public IMongoCollection<Organization> Organizations { get; private set; }
  149. }
  150. internal class MongoDbClientSingle
  151. {
  152. public static MongoDbClient Instance { get; set; }
  153. public static MongoDbClient CreateInstance(string address, int port) => Instance = new MongoDbClient(address, port);
  154. }
  155. internal static class MongoExtends
  156. {
  157. public static FilterDefinition<IDocument> BetweenTime<IDocument>(this FilterDefinitionBuilder<IDocument> filter, DateTime startTime, DateTime endTime) where IDocument : Entity
  158. {
  159. if (startTime > DateTime.MinValue && endTime > DateTime.MinValue)
  160. {
  161. var st = filter.Gt(f => f.CreateTime, startTime);
  162. var et = filter.Lt(f => f.CreateTime, endTime);
  163. return st & et;
  164. }
  165. else
  166. {
  167. return filter.Empty;
  168. }
  169. }
  170. public static string GetStorageUrl(this string token, string internalUrl)
  171. {
  172. if (string.IsNullOrWhiteSpace(internalUrl)) { return string.Empty; }
  173. var ufileType = 0;
  174. if (internalUrl.Contains("ufileos.com") || internalUrl.Contains("fis.plus") || internalUrl.Contains("ucloud"))
  175. {
  176. internalUrl = new StringBuilder(internalUrl)
  177. .Replace("flyinsono-cn.cn-bj.ufileos.com", "cn.fis.plus")
  178. .Replace("flyinsono-cn.ufile.cn-north-02.ucloud.cn", "cn.fis.plus")
  179. .Replace("flyinsono-ge.ge-fra.ufileos.com", "ge.fis.plus")
  180. .Replace("flyinsono-ge.internal-ge-fra.ufileos.com", "ge.fis.plus")
  181. .Replace("flyinsono-bra.bra-saopaulo.ufileos.com", "bra.fis.plus")
  182. .Replace("flyinsono-upgrade.cn-bj.ufileos.com", "upgrade.fis.plus")
  183. .Replace("flyinsono-upgrade.ufile.cn-north-02.ucloud.cn", "upgrade.fis.plus")
  184. .Replace("flyinsono-release.cn-sh2.ufileos.com", "release.fis.plus")
  185. .Replace("flyinsono-release.internal-cn-sh2-01.ufileos.com", "release.fis.plus")
  186. .Replace("flyinsono-download.cn-bj.ufileos.com", "download.fis.plus")
  187. .Replace("flyinsono-download.ufile.cn-north-02.ucloud.cn", "download.fis.plus")
  188. .ToString();
  189. ufileType = 1;
  190. }
  191. if (ufileType == 0)
  192. {
  193. return $"{ufileType}!U${token}";
  194. }
  195. else
  196. {
  197. return $"{ufileType}!U${internalUrl}";
  198. }
  199. }
  200. public static string ReplaceTokenHeader(Match match)
  201. {
  202. if (match.Success && match.Groups.Count > 1)
  203. {
  204. return match.Value.Replace(match.Groups[1].Value, "");
  205. }
  206. return match.Value;
  207. }
  208. public static string GetRealToken(this string token)
  209. {
  210. if (string.IsNullOrWhiteSpace(token)) { return token; }
  211. return Regex.Replace(token, "([.a-zA-Z0-9]+:[0-9]+/).+", ReplaceTokenHeader);
  212. }
  213. }
  214. }