123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using MongoDB.Bson;
- using MongoDB.Driver;
- using System;
- using System.Diagnostics;
- using System.Threading;
- using Vinno.IUS.Common.Log;
- using Mpeg4ConverterTool.AfterSales;
- using Mpeg4ConverterTool.Remedical;
- using Mpeg4ConverterTool.Teaching;
- using System.Text;
- using System.Text.RegularExpressions;
- using Mpeg4ConverterTool.RemoteAppointment;
- using Mpeg4ConverterTool.FrontPage;
- namespace Mpeg4ConverterTool
- {
- internal class MongoDbClient
- {
- private const int ConnnectTimeOut = 120;
- private const int WaitQueueTimeout = 120;
- private const int ServerSelectionTimeout = 300;
- private const int SocketTimeout = 120;
- private readonly string _users = "Users";
- private readonly string _terminals = "Terminals";
- private readonly string _chatMessages = "ChatMessages";
- private readonly string _storageFileInfoes = "StorageFileInfoes";
- //Remedical
- private readonly string _terminalDatas = "TerminalDatas";
- private readonly string _posterHistory = "PosterHistories";
- private readonly string _reportInfoResults = "ReportInfoResults";
- //AfterSale
- private readonly string _patches = "Patches";
- //Storage file
- private readonly string _uploadStorageFileInfo = "UploadStorageFileInfo";
- //carotid
- private readonly string _carotid3dModelDatas = "Carotid3dModelDatas";
- private IMongoDatabase _database;
- private MongoClient _mongoClient;
- //Teaching
- private readonly string _answerSheets = "AnswerSheets";
- private readonly string _teachingTerminalDatas = "TeachingTerminalDatas";
- public readonly string _shareLiveMessageRecords = "ShareLiveMessageRecords";
- private readonly string _terminalAIDataExcuteRecords = "TerminalAIDataExcuteRecords";
- private readonly string _banners = "Banners";
- private readonly string _consultationRecords = "ConsultationRecords";
- private readonly string _printerDrives = "PrinterDrives";
- private readonly string _homePagePublicitys = "HomePagePublicity";
- private readonly string _terminalRecord = "TerminalRecords";
- private readonly string _requestNotes = "RequestNotes";
- private readonly string _organizations = "Organizations";
- public MongoDbClient(string address, int port)
- {
- BuilderClient(address, port);
- RegisterEntities();
- }
- /// <summary>
- /// 构造MongoClient
- /// </summary>
- private void BuilderClient(string address, int port, int tryCount = 0)
- {
- try
- {
- CreateClient(address, port);
- }
- catch (Exception ex)
- {
- tryCount++;
- if (tryCount > 5)
- {
- Logger.WriteLineError($"BuilderClient fail {ex},tryCount:{tryCount} over 5 times,Server will closed");
- throw;
- }
- Logger.WriteLineError($"BuilderClient fail {ex},tryCount:{tryCount} wait 5 second to try again!");
- Thread.Sleep(5000);
- BuilderClient(address, port, tryCount);
- }
- }
- private void CreateClient(string address, int port)
- {
- var mongoSetting = new MongoClientSettings
- {
- ConnectTimeout = TimeSpan.FromSeconds(ConnnectTimeOut),
- Server = new MongoServerAddress(address, port),
- MaxConnectionPoolSize = 20000,
- WaitQueueTimeout = TimeSpan.FromSeconds(WaitQueueTimeout),
- ServerSelectionTimeout = TimeSpan.FromSeconds(ServerSelectionTimeout),
- SocketTimeout = TimeSpan.FromSeconds(SocketTimeout)
- };
- _mongoClient = new MongoClient(mongoSetting);
- _database = _mongoClient.GetDatabase("vCloudDb");
- }
- /// <summary>
- /// 获取数据集对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="collectionName"></param>
- /// <returns></returns>
- public IMongoCollection<T> GetCollection<T>(string collectionName)
- {
- return _database.GetCollection<T>(collectionName);
- }
- public void RunScript(string command)
- {
- _database.RunCommand(new JsonCommand<BsonDocument>(command));
- }
- private void RegisterEntities()
- {
- Users = GetCollection<User>(_users);
- Terminals = GetCollection<Terminal>(_terminals);
- ChatMessages = GetCollection<ChatMessage>(_chatMessages);
- TerminalDatas = GetCollection<TerminalData>(_terminalDatas);
- Patches = GetCollection<Patch>(_patches);
- PosterHistories = GetCollection<PosterHistory>(_posterHistory);
- StorageFileInfoes = GetCollection<StorageFileInfo>(_storageFileInfoes);
- ReportInfoResults = GetCollection<ReportInfoResult>(_reportInfoResults);
- UploadStorageFileInfoes= GetCollection<UploadStorageFileInfo>(_uploadStorageFileInfo);
- Carotid3dModelDatas = GetCollection<Carotid3dModelData>(_carotid3dModelDatas);
- AnswerSheets = GetCollection<AnswerSheet>(_answerSheets);
- TeachingTerminalDatas = GetCollection<TeachingTerminalData>(_teachingTerminalDatas);
- TerminalAIDataExcuteRecords = GetCollection<TerminalAIDataExcuteRecord>(_terminalAIDataExcuteRecords);
- Banners = GetCollection<Banner>(_banners);
- ConsultationRecords = GetCollection<ConsultationRecord>(_consultationRecords);
- PrinterDrives = GetCollection<PrinterDrive>(_printerDrives);
- HomePagePublicitys = GetCollection<HomePagePublicity>(_homePagePublicitys);
- TerminalRecords = GetCollection<TerminalRecord>(_terminalRecord);
- RequestNotes = GetCollection<RequestNote>(_requestNotes);
- Organizations = GetCollection<Organization>(_organizations);
- }
- public IMongoCollection<User> Users { get; private set; }
- public IMongoCollection<Terminal> Terminals { get; private set; }
- public IMongoCollection<ChatMessage> ChatMessages { get; private set; }
- public IMongoCollection<TerminalData> TerminalDatas { get; private set; }
- public IMongoCollection<PosterHistory> PosterHistories { get; private set; }
- //AfterSale
- public IMongoCollection<Patch> Patches { get; private set; }
- public IMongoCollection<StorageFileInfo> StorageFileInfoes { get; private set; }
- public IMongoCollection<ReportInfoResult> ReportInfoResults { get; private set; }
-
- public IMongoCollection<UploadStorageFileInfo> UploadStorageFileInfoes { get; private set; }
- //carotid
- public IMongoCollection<Carotid3dModelData> Carotid3dModelDatas { get; private set; }
-
- public IMongoCollection<AnswerSheet> AnswerSheets { get; private set; }
- public IMongoCollection<TeachingTerminalData> TeachingTerminalDatas { get; private set; }
- public IMongoCollection<TerminalAIDataExcuteRecord> TerminalAIDataExcuteRecords { get; private set; }
- public IMongoCollection<Banner> Banners { get; private set; }
- public IMongoCollection<ConsultationRecord> ConsultationRecords { get; private set; }
- public IMongoCollection<PrinterDrive> PrinterDrives { get; private set; }
- public IMongoCollection<HomePagePublicity> HomePagePublicitys { get; private set; }
- public IMongoCollection<TerminalRecord> TerminalRecords { get; private set; }
- public IMongoCollection<RequestNote> RequestNotes { get; private set; }
- public IMongoCollection<Organization> Organizations { get; private set; }
- }
- internal class MongoDbClientSingle
- {
- public static MongoDbClient Instance { get; set; }
- public static MongoDbClient CreateInstance(string address, int port) => Instance = new MongoDbClient(address, port);
- }
- internal static class MongoExtends
- {
- public static FilterDefinition<IDocument> BetweenTime<IDocument>(this FilterDefinitionBuilder<IDocument> filter, DateTime startTime, DateTime endTime) where IDocument : Entity
- {
- if (startTime > DateTime.MinValue && endTime > DateTime.MinValue)
- {
- var st = filter.Gt(f => f.CreateTime, startTime);
- var et = filter.Lt(f => f.CreateTime, endTime);
- return st & et;
- }
- else
- {
- return filter.Empty;
- }
- }
- public static string GetStorageUrl(this string token, string internalUrl)
- {
- if (string.IsNullOrWhiteSpace(internalUrl)) { return string.Empty; }
- var ufileType = 0;
- if (internalUrl.Contains("ufileos.com") || internalUrl.Contains("fis.plus") || internalUrl.Contains("ucloud"))
- {
- internalUrl = new StringBuilder(internalUrl)
- .Replace("flyinsono-cn.cn-bj.ufileos.com", "cn.fis.plus")
- .Replace("flyinsono-cn.ufile.cn-north-02.ucloud.cn", "cn.fis.plus")
- .Replace("flyinsono-ge.ge-fra.ufileos.com", "ge.fis.plus")
- .Replace("flyinsono-ge.internal-ge-fra.ufileos.com", "ge.fis.plus")
- .Replace("flyinsono-bra.bra-saopaulo.ufileos.com", "bra.fis.plus")
- .Replace("flyinsono-upgrade.cn-bj.ufileos.com", "upgrade.fis.plus")
- .Replace("flyinsono-upgrade.ufile.cn-north-02.ucloud.cn", "upgrade.fis.plus")
- .Replace("flyinsono-release.cn-sh2.ufileos.com", "release.fis.plus")
- .Replace("flyinsono-release.internal-cn-sh2-01.ufileos.com", "release.fis.plus")
- .Replace("flyinsono-download.cn-bj.ufileos.com", "download.fis.plus")
- .Replace("flyinsono-download.ufile.cn-north-02.ucloud.cn", "download.fis.plus")
- .ToString();
- ufileType = 1;
- }
- if (ufileType == 0)
- {
- return $"{ufileType}!U${token}";
- }
- else
- {
- return $"{ufileType}!U${internalUrl}";
- }
- }
- public static string ReplaceTokenHeader(Match match)
- {
- if (match.Success && match.Groups.Count > 1)
- {
- return match.Value.Replace(match.Groups[1].Value, "");
- }
- return match.Value;
- }
- public static string GetRealToken(this string token)
- {
- if (string.IsNullOrWhiteSpace(token)) { return token; }
- return Regex.Replace(token, "([.a-zA-Z0-9]+:[0-9]+/).+", ReplaceTokenHeader);
- }
- }
- }
|