From 0dcd74388f2fdb1e9d05cdeb8c620e51f2e5d494 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Wed, 28 Jun 2023 16:04:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=89=8D=E7=AB=AF=E5=9B=BD?= =?UTF-8?q?=E9=99=85=E5=8C=96=E5=88=B7=E6=96=B0=E6=9F=A5=E8=AF=A2=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.API/Program.cs | 60 ++++---------- .../Helper/InternationalizationHelper.cs | 82 +++++++++++++++++-- .../IRaCIS.Core.Application.xml | 7 ++ .../DTO/InternationalizationViewModel.cs | 9 +- .../Common/InternationalizationService.cs | 35 ++++++-- IRaCIS.Core.Application/TestService.cs | 2 +- 6 files changed, 133 insertions(+), 62 deletions(-) diff --git a/IRaCIS.Core.API/Program.cs b/IRaCIS.Core.API/Program.cs index 24f9703c9..b771b7a5f 100644 --- a/IRaCIS.Core.API/Program.cs +++ b/IRaCIS.Core.API/Program.cs @@ -11,6 +11,8 @@ using MassTransit; using MassTransit.NewIdProviders; using System.IO; using IRaCIS.Core.Domain.Share; +using IRaCIS.Core.Infra.EFCore; +using IRaCIS.Core.Application.Helper; namespace IRaCIS.Core.API { @@ -64,13 +66,11 @@ namespace IRaCIS.Core.API //缓存项目的状态 匿名化数据 await InitCache(host); - WatchJsonFile(Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources, StaticData.En_US_Json) ); - - WatchJsonFile(Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources, StaticData.Zh_CN_Json)); + + - host.Run(); @@ -100,58 +100,28 @@ namespace IRaCIS.Core.API webBuilder.UseStartup(); }).UseSerilog() .UseServiceProviderFactory(new AutofacServiceProviderFactory()); - + private static async Task InitCache(IHost host) { + var _repository = host.Services.GetService(typeof(IRepository)) as IRepository; + + //初始化 国际化数据,并且监测国际化文件变更 + await InternationalizationHelper.InitInternationlizationDataAndWatchJsonFileAsync(_repository); + var _mediator = host.Services.GetService(typeof(IMediator)) as IMediator; await _mediator.Send(new AnonymizeCacheRequest()); await _mediator.Send(new TrialStateCacheRequest()); + + + } - private static void LoadJsonFile(string filePath) - { - - IConfigurationBuilder builder = new ConfigurationBuilder() - .AddJsonFile(filePath); - - IConfigurationRoot enConfiguration = builder.Build(); - - - - foreach (IConfigurationSection section in enConfiguration.GetChildren()) - { - if (filePath.Contains(StaticData.En_US_Json) ) - { - StaticData.En_US_Dic[section.Key] = section.Value; - - } - else - { - StaticData.Zh_CN_Dic[section.Key] = section.Value; - } - } - - - - - - - } - public static void WatchJsonFile(string filePath) - { - LoadJsonFile(filePath); - - - FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(filePath), Path.GetFileName(filePath)); - watcher.Changed += (sender, e) => LoadJsonFile(filePath); - watcher.EnableRaisingEvents = true; - - } - + + } } diff --git a/IRaCIS.Core.Application/Helper/InternationalizationHelper.cs b/IRaCIS.Core.Application/Helper/InternationalizationHelper.cs index e1feefcea..ddf14b32d 100644 --- a/IRaCIS.Core.Application/Helper/InternationalizationHelper.cs +++ b/IRaCIS.Core.Application/Helper/InternationalizationHelper.cs @@ -1,5 +1,7 @@ 锘縰sing IRaCIS.Core.Domain.Share; using IRaCIS.Core.Infrastructure; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; @@ -13,20 +15,21 @@ namespace IRaCIS.Core.Application.Helper { public static class InternationalizationHelper { + public static string JsonFileFolder = Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources); - public static async void AddOrUpdateJsonKeyValueAsync(string key, string value) + public static async Task AddOrUpdateJsonKeyValueAsync(string key, string value) { - var jsonFileFolder = Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources); - if (!Directory.Exists(jsonFileFolder) || - Directory.GetFiles(jsonFileFolder).Any(filePath => Path.GetExtension(filePath).Equals(".json", StringComparison.OrdinalIgnoreCase))) + + if (!Directory.Exists(JsonFileFolder) || + Directory.GetFiles(JsonFileFolder).Any(filePath => Path.GetExtension(filePath).Equals(".json", StringComparison.OrdinalIgnoreCase))) { throw new BusinessValidationFailedException("鍥介檯鍖朖son鏂囦欢鐩綍鏈夎"); } - // - foreach (var filePath in Directory.GetFiles(jsonFileFolder).Where(filePath => Path.GetExtension(filePath).Equals(".json", StringComparison.OrdinalIgnoreCase))) + //灏嗚鐩綍涓嬬殑鍥介檯鍖栨枃浠跺叏閮ㄤ慨鏀 + foreach (var filePath in Directory.GetFiles(JsonFileFolder).Where(filePath => Path.GetExtension(filePath).Equals(".json", StringComparison.OrdinalIgnoreCase))) { var json = await File.ReadAllTextAsync(filePath); @@ -40,5 +43,72 @@ namespace IRaCIS.Core.Application.Helper } } + + public static async Task InitInternationlizationDataAndWatchJsonFileAsync(IRepository _repository) + { + //鏌ヨ鏁版嵁搴撶殑鏁版嵁 + var toJsonList = await _repository.Where(t => t.InternationalizationType == 1).Select(t => new + { + t.Code, + t.Value, + t.ValueCN + }).ToListAsync(); + + //缁勭粐鎴恓son 鏂囦欢 + + var usJsonPath = Path.Combine(JsonFileFolder, StaticData.En_US_Json); + var cnJsonPath = Path.Combine(JsonFileFolder, StaticData.Zh_CN_Json); + + + + + //鏈湴闈欐佹枃浠跺浗闄呭寲闇瑕 + foreach (var tojsonItem in toJsonList) + { + StaticData.En_US_Dic[tojsonItem.Code] = tojsonItem.Value; + StaticData.Zh_CN_Dic[tojsonItem.Code] = tojsonItem.ValueCN; + } + + File.WriteAllText(usJsonPath, JsonConvert.SerializeObject(StaticData.En_US_Dic)); + File.WriteAllText(cnJsonPath, JsonConvert.SerializeObject(StaticData.Zh_CN_Dic)); + + + //鐩戞祴Json鏂囦欢鍙樻洿 瀹炴椂鍒锋柊鏁版嵁 + + WatchJsonFile(usJsonPath); + WatchJsonFile(cnJsonPath); + + } + + public static void WatchJsonFile(string filePath) + { + + FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(filePath), Path.GetFileName(filePath)); + watcher.Changed += (sender, e) => LoadJsonFile(filePath); + watcher.EnableRaisingEvents = true; + + } + + + private static void LoadJsonFile(string filePath) + { + + IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile(filePath); + + IConfigurationRoot enConfiguration = builder.Build(); + + foreach (IConfigurationSection section in enConfiguration.GetChildren()) + { + if (filePath.Contains(StaticData.En_US_Json)) + { + StaticData.En_US_Dic[section.Key] = section.Value; + + } + else + { + StaticData.Zh_CN_Dic[section.Key] = section.Value; + } + } + } } } diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index e9b41531c..9b20bf2e4 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -2494,6 +2494,13 @@ 涓村簥绛旀 + + + 鑷姩娣诲姞CRC涓村簥鏁版嵁 + + + + 鑾峰彇CRC鍙楄瘯鑰呬复搴婃暟鎹 diff --git a/IRaCIS.Core.Application/Service/Common/DTO/InternationalizationViewModel.cs b/IRaCIS.Core.Application/Service/Common/DTO/InternationalizationViewModel.cs index c4f2f3fc9..5f2e25a59 100644 --- a/IRaCIS.Core.Application/Service/Common/DTO/InternationalizationViewModel.cs +++ b/IRaCIS.Core.Application/Service/Common/DTO/InternationalizationViewModel.cs @@ -38,7 +38,7 @@ namespace IRaCIS.Core.Application.ViewModel public Guid? Id { get; set; } public int State { get; set; } public string Description { get; set; } = string.Empty; - public string Code { get; set; } + public string Code { get; set; } = string.Empty; public string Value { get; set; } = string.Empty; public string ValueCN { get; set; } = string.Empty; @@ -46,6 +46,13 @@ namespace IRaCIS.Core.Application.ViewModel } + public class InternationalizationSimpleDto + { + public string Code { get; set; } = string.Empty; + public string Value { get; set; } = string.Empty; + public string ValueCN { get; set; } = string.Empty; + } + } diff --git a/IRaCIS.Core.Application/Service/Common/InternationalizationService.cs b/IRaCIS.Core.Application/Service/Common/InternationalizationService.cs index ebbcc3f58..0fa7d246c 100644 --- a/IRaCIS.Core.Application/Service/Common/InternationalizationService.cs +++ b/IRaCIS.Core.Application/Service/Common/InternationalizationService.cs @@ -8,6 +8,8 @@ using IRaCIS.Core.Domain.Models; using Microsoft.AspNetCore.Mvc; using IRaCIS.Core.Application.Interfaces; using IRaCIS.Core.Application.ViewModel; +using Microsoft.AspNetCore.Authorization; + namespace IRaCIS.Core.Application.Service { /// @@ -24,22 +26,37 @@ namespace IRaCIS.Core.Application.Service _internationalizationRepository = internationalizationRepository; } + + [AllowAnonymous] + + public async Task> GetFrontInternationalizationList() + { + + var list = await _internationalizationRepository.Where(t => t.InternationalizationType == 0).Select(t => new InternationalizationSimpleDto() + { + Code = t.Code, + Value = t.Value, + ValueCN = t.ValueCN + }).ToListAsync(); + + return list; + } + [HttpPost] public async Task> GetInternationalizationList(InternationalizationQuery inQuery) { - var internationalizationQueryable =_internationalizationRepository - .WhereIf(inQuery.Description!=null,t=>t.Description.Contains(inQuery.Description)) + var internationalizationQueryable = _internationalizationRepository + .WhereIf(inQuery.Description != null, t => t.Description.Contains(inQuery.Description)) .WhereIf(inQuery.Code != null, t => t.Code.Contains(inQuery.Code)) - .WhereIf(inQuery.State != null, t => t.State==inQuery.State) - .WhereIf(inQuery.InternationalizationType != null, t => t.InternationalizationType == inQuery.InternationalizationType) - + .WhereIf(inQuery.State != null, t => t.State == inQuery.State) + .WhereIf(inQuery.InternationalizationType != null, t => t.InternationalizationType == inQuery.InternationalizationType) .WhereIf(inQuery.Value != null, t => t.Value.Contains(inQuery.Value)) - .WhereIf(inQuery.ValueCN != null, t => t.ValueCN.Contains(inQuery.ValueCN)) - .ProjectTo(_mapper.ConfigurationProvider); + .WhereIf(inQuery.ValueCN != null, t => t.ValueCN.Contains(inQuery.ValueCN)) + .ProjectTo(_mapper.ConfigurationProvider); var pageList = await internationalizationQueryable - .ToPagedListAsync(inQuery.PageIndex, inQuery.PageSize, string.IsNullOrWhiteSpace(inQuery.SortField) ? "Id" : inQuery.SortField, + .ToPagedListAsync(inQuery.PageIndex, inQuery.PageSize, string.IsNullOrWhiteSpace(inQuery.SortField) ? nameof(InternationalizationView.State) : inQuery.SortField, inQuery.Asc); return pageList; @@ -56,7 +73,7 @@ namespace IRaCIS.Core.Application.Service IsVerify = true }; - var entity = await _internationalizationRepository.InsertOrUpdateAsync(addOrEditInternationalization, true,verifyExp1); + var entity = await _internationalizationRepository.InsertOrUpdateAsync(addOrEditInternationalization, true, verifyExp1); return ResponseOutput.Ok(entity.Id.ToString()); diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index 78b43a002..0bdd15b12 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -53,7 +53,7 @@ namespace IRaCIS.Application.Services { - var rows = await MiniExcel.QueryAsync(@"C:\Users\Administrator\Desktop\Export\NetCore.xlsx"); + var rows = await MiniExcel.QueryAsync(@"C:\Users\Administrator\Desktop\Export\vue.xlsx"); foreach (var row in rows) {