399 lines
14 KiB
C#
399 lines
14 KiB
C#
using Autofac;
|
|
using DocumentFormat.OpenXml.Spreadsheet;
|
|
using Newtonsoft.Json;
|
|
using Org.BouncyCastle.Asn1.Ocsp;
|
|
using System.Reflection;
|
|
using System.Runtime.CompilerServices;
|
|
using YieldChain.Commons;
|
|
using YieldChain.Helpers;
|
|
using YLErp.Cache;
|
|
using YLErp.DBModels.Consts;
|
|
using YLErp.Models;
|
|
|
|
namespace YLErp.Modules.AppModule
|
|
{
|
|
partial class AppInitializer
|
|
{
|
|
readonly InnerAppConfigService configService;
|
|
|
|
public AppInitializer()
|
|
{
|
|
configService = new InnerAppConfigService();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化项目配置文件
|
|
/// </summary>
|
|
public void InitializePsConfig(SmtpConfigHandler smtpConfigHandler)
|
|
{
|
|
var items = configService.GetAllDatas().Where(a => a.PGroup == "ProjectConfig").ToArray();
|
|
var dic = new Dictionary<string, object>();
|
|
foreach (var item in items)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(item.PName) || string.IsNullOrWhiteSpace(item.PValue))
|
|
{
|
|
continue;
|
|
}
|
|
var pname = item.PName;
|
|
var index = pname.IndexOf('.');
|
|
pname = pname.Substring(index + 1).Trim();
|
|
if ("bool".Equals(item.PType, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
dic[pname] = "true".Equals(item.PValue, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
else
|
|
{
|
|
dic[pname] = item.PValue;
|
|
}
|
|
}
|
|
PS.ResetConfig(Newtonsoft.Json.JsonConvert.SerializeObject(dic));
|
|
|
|
//邮件发送配置
|
|
var smptConfig = configService.GetData("ProjectConfig", "Erp.SendMailConfig");
|
|
if (!string.IsNullOrWhiteSpace(smptConfig?.PValue) && smtpConfigHandler != null)
|
|
{
|
|
smtpConfigHandler.ResetConfig(smptConfig.PValue);
|
|
}
|
|
}
|
|
|
|
public void InitialRedisConfig()
|
|
{
|
|
var cacheConfig =JsonConvert.DeserializeObject<CacheConfig>(configService.GetData("ProjectConfig", "Erp.RedisConfig")?.PValue ?? string.Empty)??new CacheConfig();
|
|
YLServiceLocator.ServiceCollection.AddYLCache(opt =>
|
|
{
|
|
opt.Enable = cacheConfig.Enable;
|
|
opt.ConnectionString= cacheConfig.ConnectionString;
|
|
opt.DataBaseNum = cacheConfig.DataBaseNum;
|
|
opt.CustomPrefix = cacheConfig.CustomPrefix;
|
|
if (string.IsNullOrEmpty(opt.CustomPrefix))
|
|
{
|
|
opt.CustomPrefix = ((int)PS.Config.Company).ToString();
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string GetOtcFormatConfig()
|
|
{
|
|
return configService.GetData(ConsAppConfig.ProjectConfig, ConsAppConfig.OtcFormatConfig)?.PValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化插件
|
|
/// </summary>
|
|
public static PluginInfo InitializePlugin(out IContainer container)
|
|
{
|
|
container = null;
|
|
|
|
var pluginFolder = Environment.GetEnvironmentVariable("AppSettings:PluginFolder");
|
|
|
|
if (string.IsNullOrEmpty(pluginFolder))
|
|
{
|
|
if (OtcAppContext.MapPath != null)
|
|
{
|
|
pluginFolder = OtcAppContext.MapPath("~/App_Plugin");
|
|
|
|
if (!Directory.Exists(pluginFolder))
|
|
{
|
|
pluginFolder = Path.Combine(AppContext.BaseDirectory, "App_Plugin");
|
|
}
|
|
}
|
|
}
|
|
|
|
var info = new PluginInfo
|
|
{
|
|
PluginFolder = pluginFolder
|
|
};
|
|
|
|
if (!Directory.Exists(pluginFolder))
|
|
{
|
|
info.LoadMessage = "插件目录不存在!";
|
|
return info;
|
|
}
|
|
|
|
Environment.SetEnvironmentVariable("AppSettings:PluginFolder", pluginFolder);
|
|
|
|
//加载并注册插件
|
|
var iocBuilder = new ContainerBuilder();
|
|
var dllFiles = Directory.GetFiles(pluginFolder, "YLErp.Plugins.*.dll");
|
|
|
|
foreach (var dllFile in dllFiles)
|
|
{
|
|
if (dllFile.LastIndexOf(".Abstract.") < 0)
|
|
{
|
|
var assembly = Assembly.Load(File.ReadAllBytes(dllFile));
|
|
iocBuilder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces();
|
|
info.LoadMessage += assembly.FullName + ";";
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(info.LoadMessage))
|
|
{
|
|
info.LoadMessage = "未加载到任何插件!";
|
|
}
|
|
else
|
|
{
|
|
info.LoadMessage = "加载插件:" + info.LoadMessage;
|
|
}
|
|
|
|
container = iocBuilder.Build();
|
|
|
|
return info;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取 某个配置值
|
|
/// </summary>
|
|
/// <param name="groupName"></param>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public string GetConfigValue(string groupName,string key)
|
|
{
|
|
return configService.GetValue(groupName, key);
|
|
}
|
|
public void SetConfigValue(string groupName, string key,string value,string remark)
|
|
{
|
|
configService.AddDataIfNotExists(groupName, key, value,"string", remark,true);
|
|
configService.SaveChanges();
|
|
}
|
|
|
|
private static void CopyPluginFiles(string srcDirPath, string destDirPath, int depth = 0)
|
|
{
|
|
var srcDir = new DirectoryInfo(srcDirPath);
|
|
var srcDirs = srcDir.GetDirectories();
|
|
|
|
if (!Directory.Exists(destDirPath))
|
|
{
|
|
Directory.CreateDirectory(destDirPath);
|
|
}
|
|
|
|
var srcFiles = srcDir.GetFiles();
|
|
|
|
foreach (FileInfo file in srcFiles)
|
|
{
|
|
if (depth == 0 && !file.Name.StartsWith("YLErp.Plugins.", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
continue;
|
|
}
|
|
string destFile = Path.Combine(destDirPath, file.Name);
|
|
file.CopyTo(destFile, true);
|
|
}
|
|
|
|
foreach (DirectoryInfo subdir in srcDirs)
|
|
{
|
|
string destDir = Path.Combine(destDirPath, subdir.Name);
|
|
CopyPluginFiles(subdir.FullName, destDir, depth + 1);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 内部数据访问类
|
|
/// </summary>
|
|
class InnerAppConfigService : YLBaseService
|
|
{
|
|
Dictionary<string, AppConfig> configDic;
|
|
|
|
public InnerAppConfigService() : base(OptUserInfo.SystemUser)
|
|
{
|
|
|
|
}
|
|
|
|
public Dictionary<string, AppConfig> ConfigDic
|
|
{
|
|
get
|
|
{
|
|
configDic ??= DbContext.AppConfig.ToDictionary(n => GetDicKey(n.PGroup, n.PName), StringComparer.OrdinalIgnoreCase);
|
|
return configDic;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有数据
|
|
/// </summary>
|
|
public IEnumerable<AppConfig> GetAllDatas()
|
|
{
|
|
return ConfigDic.Values;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取数据
|
|
/// </summary>
|
|
public AppConfig GetData(string group, string name)
|
|
{
|
|
return ConfigDic.TryGetValue(GetDicKey(group, name), out AppConfig appConfig) ? appConfig : null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取数据
|
|
/// </summary>
|
|
public string GetValue(string group, string name)
|
|
{
|
|
return ConfigDic.TryGetValue(GetDicKey(group, name), out AppConfig appConfig) ? appConfig?.PValue : null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加配置数据
|
|
/// </summary>
|
|
public void AddData(AppConfig item)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(item.PGroup))
|
|
{
|
|
throw new ArgumentNullException("item.PGroup");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(item.PName))
|
|
{
|
|
throw new ArgumentNullException("item.PName");
|
|
}
|
|
|
|
item.PGroup = item.PGroup.Trim();
|
|
item.PName = item.PName.Trim();
|
|
var key = GetDicKey(item.PGroup, item.PName);
|
|
if (ConfigDic.ContainsKey(key))
|
|
{
|
|
throw new ArgumentException("已存在相同主键的数据");
|
|
}
|
|
DbContext.AppConfig.Add(item);
|
|
ConfigDic.Add(key, item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除配置数据
|
|
/// </summary>
|
|
public AppConfig RemoveData(string group, string name)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(group))
|
|
{
|
|
throw new ArgumentNullException(nameof(group));
|
|
}
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
{
|
|
throw new ArgumentNullException(nameof(name));
|
|
}
|
|
var key = GetDicKey(group.Trim(), name.Trim());
|
|
if (ConfigDic.TryGetValue(key, out AppConfig config))
|
|
{
|
|
DbContext.AppConfig.Remove(config);
|
|
ConfigDic.Remove(key);
|
|
return config;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新配置数据
|
|
/// </summary>
|
|
public void UpdateData(string group, string name, string newRemark)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(group))
|
|
{
|
|
throw new ArgumentNullException(nameof(group));
|
|
}
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
{
|
|
throw new ArgumentNullException(nameof(name));
|
|
}
|
|
var key = GetDicKey(group.Trim(), name.Trim());
|
|
if (ConfigDic.TryGetValue(key, out AppConfig config))
|
|
{
|
|
config.Remark = newRemark;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新配置数据(如果不存在则添加),
|
|
/// oldItem需要PGroup和PName
|
|
/// </summary>
|
|
public void UpdateData(AppConfig oldItem, AppConfig newItem)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(oldItem.PGroup))
|
|
{
|
|
throw new ArgumentNullException("oldItem.PGroup");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(oldItem.PName))
|
|
{
|
|
throw new ArgumentNullException("oldItem.PName");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(newItem.PGroup))
|
|
{
|
|
throw new ArgumentNullException("newItem.PGroup");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(newItem.PName))
|
|
{
|
|
throw new ArgumentNullException("newItem.PName");
|
|
}
|
|
|
|
ObjectHelper.TrimPropertyValues(oldItem);
|
|
ObjectHelper.TrimPropertyValues(newItem);
|
|
|
|
var oldKey = GetDicKey(oldItem.PGroup, oldItem.PName);
|
|
var newKey = GetDicKey(newItem.PGroup, newItem.PName);
|
|
if (oldKey.Equals(newKey, StringComparison.OrdinalIgnoreCase) && configDic.TryGetValue(oldKey, out oldItem))
|
|
{
|
|
UpdateChanges(oldItem, newItem);
|
|
}
|
|
else
|
|
{
|
|
configDic.Remove(oldKey);
|
|
configDic[newKey] = newItem;
|
|
DbContext.AppConfig.Add(newItem);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 如果不存在,则添加为新数据
|
|
/// </summary>
|
|
public bool AddDataIfNotExists(string pgroup, string pname, string pvalue, string ptype, string remark,bool modify=false)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(pgroup))
|
|
{
|
|
throw new ArgumentNullException(nameof(pgroup));
|
|
}
|
|
if (string.IsNullOrWhiteSpace(pname))
|
|
{
|
|
throw new ArgumentNullException(nameof(pname));
|
|
}
|
|
pgroup = pgroup.Trim();
|
|
pname = pname.Trim();
|
|
var key = GetDicKey(pgroup, pname);
|
|
var appConfig = new AppConfig
|
|
{
|
|
PGroup = pgroup,
|
|
PName = pname,
|
|
PValue = pvalue,
|
|
PType = ptype,
|
|
Remark = remark,
|
|
CreateTime = DateTime.Now
|
|
};
|
|
if (!ConfigDic.ContainsKey(key))
|
|
{
|
|
|
|
DbContext.AppConfig.Add(appConfig);
|
|
ConfigDic.Add(key, appConfig);
|
|
return true;
|
|
}
|
|
if (modify)
|
|
{
|
|
var oldAppconfig = ConfigDic[key];
|
|
UpdateChanges(oldAppconfig, appConfig);
|
|
ConfigDic[key] = appConfig;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
static string GetDicKey(string group, string name)
|
|
{
|
|
return string.Concat(group, "\t\f\t", name);
|
|
}
|
|
|
|
public void SaveChanges()
|
|
{
|
|
DbContext.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|