Waste/Waste.Web.Entry/Pages/Result/ResultColumnConfigService.cs

239 lines
9.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Formats.Asn1;
using System.Linq;
using System.Threading.Tasks;
using Furion;
using Furion.DependencyInjection;
using Furion.JsonSerialization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Waste.Application;
using Waste.Domain;
using static Google.Protobuf.WellKnownTypes.Field.Types;
namespace Waste.Web.Entry.Pages.Result
{
public class ResultColumnConfigService : ITransient
{
public const string AccountSettingDefinitionName = "AccountResultColumnConfig";
public const string GlobalSettingDefinitionName = "GlobalResultColumnConfig";
private readonly ILogger<ResultColumnConfigService> logger;
private readonly SettingProvider settingProvider;
private readonly UserSettingValueProvider userSettingValueProvider;
private readonly GlobalSettingValueProvider globalSettingValueProvider;
public static readonly Dictionary<string, string> SystemColumn = new Dictionary<string, string>()
{
{nameof(ResultList.DeviceFacEcode).ToLower(),"<22><EFBFBD><E8B1B8><EFBFBD><EFBFBD>"},
{nameof(ResultList.DeviceEcode).ToLower(),"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"},
{nameof(ResultList.DeviceName).ToLower(),"<22><EFBFBD><E8B1B8><EFBFBD><EFBFBD>"},
{nameof(ResultList.BusinessName).ToLower(),"<22><><EFBFBD><EFBFBD><EFBFBD>̻<EFBFBD>"},
{nameof(ResultList.PostStatus).ToLower(),"״̬"},
{nameof(ResultList.CreateTime).ToLower(),"<22><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>"},
{nameof(ResultList.WasteType).ToLower(),"<22><>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>"},
{nameof(ResultList.Registration).ToLower(),"<22><><EFBFBD><EFBFBD>Ͱ"},
{nameof(ResultList.GrossWeight).ToLower(),<><C3AB>(KG)"},
{nameof(ResultList.Tare).ToLower(),<><C6A4>(KG)"},
{nameof(ResultList.NetWeight).ToLower(),"<22><><EFBFBD><EFBFBD>(KG)"},
{nameof(ResultList.DeviceAddress).ToLower(),"<22><>ַ"},
{nameof(ResultList.Measure_Price).ToLower(),"<22><><EFBFBD><EFBFBD>"},
{nameof(ResultList.Measure_Amount).ToLower(),"<22><><EFBFBD><EFBFBD>"},
{nameof(ResultList.Measure_OpUser).ToLower(),"<22><><EFBFBD><EFBFBD>Ա"},
{nameof(ResultList.Measure_UUID).ToLower(),"<22><>ϢID"},
{nameof(ResultList.Measure_WasteSType).ToLower(),"<22><>ƷС<C6B7><D0A1>"},
{nameof(ResultList.ID1).ToLower(),"ID1"},
{nameof(ResultList.ID2).ToLower(),"ID2"},
{nameof(ResultList.ID3).ToLower(),"ID3"},
{nameof(ResultList.ID4).ToLower(),"ID4"},
{nameof(ResultList.ID5).ToLower(),"ID5"},
{nameof(ResultList.ID6).ToLower(),"ID6"},
{nameof(ResultList.ID7).ToLower(),"ID7"},
{nameof(ResultList.ID8).ToLower(),"ID8"},
};
public ResultColumnConfigService(ILogger<ResultColumnConfigService> logger, SettingProvider settingProvider, UserSettingValueProvider userSettingValueProvider, GlobalSettingValueProvider globalSettingValueProvider)
{
this.logger = logger;
this.settingProvider = settingProvider;
this.userSettingValueProvider = userSettingValueProvider;
this.globalSettingValueProvider = globalSettingValueProvider;
}
private Task<Dictionary<string, string>> GetDefaultColumn()
{
return Task.FromResult(SystemColumn);
}
public async Task<Dictionary<string, string>> GetAccountDefaultColumn()
{
return (await GetGlobalColumn()).Where(x => x.IsShow).ToDictionary(x => x.Name, x => x.Title);
}
public async Task<List<GlobalColumnConfig>> GetGlobalColumn()
{
var def = await GetDefaultColumn();
var value = await settingProvider.GetOrNullAsync(GlobalSettingDefinitionName);
if (string.IsNullOrWhiteSpace(value)) return DefaultList();
try
{
var config = JSON.Deserialize<GlobalConfig>(value);
if (config.ResultColumn != null && config.ResultColumn.Any())
{
#region <EFBFBD><EFBFBD>ϵͳ˳<EFBFBD><EFBFBD>Ϊ׼
var dic = config.ResultColumn.GroupBy(x => x.Name).ToDictionary(x => x.Key, x => x.First());
return def
.Select(x =>
{
if (dic.TryGetValue(x.Key, out var find))
{
return new GlobalColumnConfig
{ Name = x.Key, Title = find.Title, IsShow = find.IsShow };
}
return new GlobalColumnConfig { Name = x.Key, Title = string.Empty, IsShow = false };
}).ToList();
#endregion
#region <EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD>
/* return config.ResultColumn.GroupBy(x => x.Name)
.Where(x => def.ContainsKey(x.Key))
.Select(x =>
new GlobalColumnConfig { Name = x.Key, Title = x.First().Title, IsShow = x.First().IsShow }).ToList();
*/
#endregion
}
}
catch (Exception e)
{
logger.LogError(e, "<22><><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>ʧ<EFBFBD><CAA7>:{value}", value);
}
return DefaultList();
List<GlobalColumnConfig> DefaultList()
{
return def.Select(x =>
new GlobalColumnConfig { Name = x.Key, Title = x.Value, IsShow = true }).ToList();
}
}
public async Task<Dictionary<string, string>> GetAccountColumn()
{
var _GlobalColumn = (await GetGlobalColumn()).Where(x => SystemColumn.ContainsKey(x.Name) && x.IsShow).ToDictionary(x => x.Name, x => string.IsNullOrWhiteSpace(x.Title) ? SystemColumn[x.Name] : x.Title);
var value = await settingProvider.GetOrNullAsync(AccountSettingDefinitionName);
if (string.IsNullOrWhiteSpace(value)) return _GlobalColumn;
try
{
var config = JSON.Deserialize<AccountConfig>(value);
if (config.ResultColumn != null && config.ResultColumn.Any())
{
return config.ResultColumn.GroupBy(x => x.Name)
.Where(x => _GlobalColumn.ContainsKey(x.Key))
.ToDictionary(x => x.Key,
//x => x.First().Title
x => _GlobalColumn[x.Key]
);
}
}
catch (Exception e)
{
logger.LogError(e, "<22><><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>ʧ<EFBFBD><CAA7>:{value}", value);
}
return _GlobalColumn;
}
public async Task WriteAccountConfig(List<ColumnConfig> ResultColumn)
{
var value = SerializeConfig(ResultColumn, SystemColumn);
await userSettingValueProvider.SetAsync(AccountSettingDefinitionName, value);
}
public static string SerializeConfig(List<ColumnConfig> ResultColumn, Dictionary<string, string> DefaultColumn)
{
if (ResultColumn != null && ResultColumn.Any())
{
return JSON.Serialize(new AccountConfig
{ ResultColumn = ResultColumn.GroupBy(x => x.Name).Where(x => DefaultColumn.ContainsKey(x.Key) && x.Any()).Select(x => new ColumnConfig { Name = x.Key/*, Title = x.First().Title*/ }).ToList() });
}
return null;
}
public async Task WriteGlobalConfig(List<GlobalColumnConfig> ResultColumn)
{
var value = SerializeConfig(ResultColumn, SystemColumn);
await globalSettingValueProvider.SetAsync(GlobalSettingDefinitionName, value);
}
public static string SerializeConfig(List<GlobalColumnConfig> ResultColumn, Dictionary<string, string> DefaultColumn)
{
if (ResultColumn != null && ResultColumn.Any())
{
return JSON.Serialize(new GlobalConfig
{ ResultColumn = ResultColumn.Where(x => DefaultColumn.ContainsKey(x.Name)).ToList() });
}
return null;
}
}
public sealed class ResultColumnConfigStartup : AppStartup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.ApplicationServices.GetRequiredService<SettingDefinitionManager>()
.AddSettingDefinition(new SettingDefinition()
{
Name = ResultColumnConfigService.GlobalSettingDefinitionName,
Providers = { GlobalSettingValueProvider.ProviderName }
})
.AddSettingDefinition(new SettingDefinition()
{
Name = ResultColumnConfigService.AccountSettingDefinitionName,
Providers = { UserSettingValueProvider.ProviderName }
});
}
}
public class AccountConfig
{
public List<ColumnConfig> ResultColumn { get; set; }
}
public class GlobalConfig
{
public List<GlobalColumnConfig> ResultColumn { get; set; }
}
public class ColumnConfig
{
public string Name { get; set; }
}
public class GlobalColumnConfig : ColumnConfig
{
//public string SystemTitle { get; set; }
public string Title { get; set; }
public bool IsShow { get; set; }
}
}