fix: 更改保证金模板名称配置的取值 由admin-> 数据字典
This commit is contained in:
@@ -1,73 +1,32 @@
|
||||
using YLErp.Modules.AppModule;
|
||||
using BaseOUDAL;
|
||||
using YLErp.Models;
|
||||
|
||||
namespace YLErp.Modules.SwapModule
|
||||
{
|
||||
public static class SwapMarginTemplateConfigService
|
||||
{
|
||||
public const string ConfigName = "Trade.SwapMarginTemplateConfig";
|
||||
public const string DictionaryName = "保证金模板名称";
|
||||
|
||||
public const string DefaultConfigJson = @"{
|
||||
""options"": [
|
||||
{ ""text"": ""授信保证金"", ""value"": ""授信保证金"" },
|
||||
{ ""text"": ""现金保证金"", ""value"": ""现金保证金"" }
|
||||
],
|
||||
""defaultValue"": ""现金保证金""
|
||||
}";
|
||||
public static readonly string[] InitialTemplateNames = { "现金保证金", "授信保证金" };
|
||||
|
||||
public static SwapMarginTemplateConfig GetConfig()
|
||||
{
|
||||
var json = AppManager.GetAppConfigValue("ProjectConfig", ConfigName);
|
||||
return TryParse(json, out var config, out _) ? config : GetDefaultConfig();
|
||||
}
|
||||
|
||||
public static bool TryValidate(string json, out string error)
|
||||
{
|
||||
return TryParse(json, out _, out error);
|
||||
}
|
||||
|
||||
private static SwapMarginTemplateConfig GetDefaultConfig()
|
||||
{
|
||||
return JsonHelper.Deserialize<SwapMarginTemplateConfig>(DefaultConfigJson);
|
||||
}
|
||||
|
||||
private static bool TryParse(string json, out SwapMarginTemplateConfig config, out string error)
|
||||
{
|
||||
config = null;
|
||||
error = null;
|
||||
|
||||
try
|
||||
{
|
||||
config = JsonHelper.Deserialize<SwapMarginTemplateConfig>(json);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
error = "保证金模板配置不是有效的 JSON";
|
||||
return false;
|
||||
}
|
||||
|
||||
var items = config?.options?
|
||||
.Where(item => item != null && !string.IsNullOrWhiteSpace(item.Value))
|
||||
using var db = new ErpBaseContext();
|
||||
var dictionaryId = db.Dictionaries
|
||||
.Where(item => item.Name == DictionaryName)
|
||||
.Select(item => item.Id)
|
||||
.FirstOrDefault();
|
||||
var items = db.DictionaryItems
|
||||
.Where(item => item.DictId == dictionaryId && !string.IsNullOrWhiteSpace(item.Name))
|
||||
.OrderBy(item => item.IndexNum)
|
||||
.Select(item => new SelectItem { Text = item.Name, Value = item.Name })
|
||||
.ToArray();
|
||||
if (items == null || items.Length == 0)
|
||||
{
|
||||
error = "保证金模板配置至少需要一个有效选项";
|
||||
return false;
|
||||
}
|
||||
if (items.Select(item => item.Value).Distinct(StringComparer.OrdinalIgnoreCase).Count() != items.Length)
|
||||
{
|
||||
error = "保证金模板配置的选项值不能重复";
|
||||
return false;
|
||||
}
|
||||
var defaultValue = config.defaultValue;
|
||||
if (!items.Any(item => item.Value == defaultValue))
|
||||
{
|
||||
error = "保证金模板默认值必须在选项中";
|
||||
return false;
|
||||
}
|
||||
|
||||
config.options = items;
|
||||
return true;
|
||||
return new SwapMarginTemplateConfig
|
||||
{
|
||||
options = items,
|
||||
defaultValue = items.FirstOrDefault()?.Value
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user