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