using YieldChain.Commons; using YLErp.BLL; using YLErp.Models; using YLErp.Modules.AppModule; namespace YLErp.Modules.SystemModule { /// /// /// public static class ClientEditConfigService { static DateTime _optDate; static ClientEditConfig _config; static readonly ValueWrap _updateTime; static ClientEditConfigService() { _updateTime = new ValueWrap(); } /// /// 重置配置 /// public static void ResetConfig() { lock (_updateTime) { _config = null; _updateTime.Value = _optDate = DateTime.MinValue; } } /// /// 获取配置json /// public static string GetConfigJson() { using (var db = DbContextFactory.GetYLDbContext()) { var query = db.AppConfig.Where(n => n.PGroup == "System" && n.PName == "ClientEditConfig"); var configJson = query.Select(n => n.PValue).FirstOrDefault(); return configJson.TrimToNull() ?? Properties.Resources.clientEditConfig; } } /// /// /// public static ClientEditConfig GetConfig(bool forceUpdateSelects = false, bool isView = false) { string configJson = null; if (_config == null || _updateTime.Value.AddSeconds(60) < DateTime.Now) { lock (_updateTime) { if (_config == null || _updateTime.Value.AddSeconds(60) < DateTime.Now) { using (var db = DbContextFactory.GetYLDbContext()) { var query = db.AppConfig.Where(n => n.PGroup == "System" && n.PName == "ClientEditConfig"); if (_config == null || query.Select(n => (DateTime?)n.OptDate).FirstOrDefault() != _optDate) { var data = query.Select(n => new { n.PValue, n.OptDate }).FirstOrDefault(); if (data != null) { _optDate = data.OptDate; configJson = data.PValue; } if (string.IsNullOrWhiteSpace(configJson)) { _config = null; configJson = Properties.Resources.clientEditConfig; } } } if (configJson != null) { var index = configJson.IndexOf('='); if (index > 0) { configJson = configJson.Substring(index + 1); } _updateTime.Value = DateTime.MinValue; _config = JsonHelper.Deserialize(configJson) ?? new ClientEditConfig(); //开户管理和客户列表列去重; _config.openList = _config.openList.DistinctBy(O => O.name); _config.clientList = _config.clientList.DistinctBy(O => O.name); } else if (_config == null) { throw new SystemException("系统错误"); } else if (!forceUpdateSelects) { return _config.Clone(); } } } } //防止服务器时间更改所以需要双重验证(比如服务器时间由15点改为12点) if (forceUpdateSelects || _updateTime.Value > DateTime.Now || _updateTime.Value.AddMinutes(1) < DateTime.Now) { lock (_updateTime) { if (forceUpdateSelects || _updateTime.Value > DateTime.Now || _updateTime.Value.AddMinutes(1) < DateTime.Now) { var allFields = _config.sections.SelectMany(n => n.fields ?? Enumerable.Empty()).ToArray(); foreach (var f in allFields) { if (f.name != null && f.name.StartsWith("meta.", StringComparison.OrdinalIgnoreCase)) { f.name = "meta_" + f.name.Substring(5); } } var fieldNames = allFields.Select(m => m.name ?? string.Empty).ToHashSet(); //客户等级 _config.selects[nameof(Client.LevelId)] = ClientDataModelV1.GetAllclientlevel(); //客户等级 _config.selects[nameof(Client.SettlementCurrency)] = ClientDataModelV1.GetAllCurrency(); //客服经理 if (fieldNames.Contains(nameof(Client.CustomerManagerId))) { _config.selects[nameof(Client.CustomerManagerId)] = ClientDataModelV1.GetAllCustomerManager(); } //销售 if (fieldNames.Contains(nameof(Client.Seller))) { _config.selects[nameof(Client.Seller)] = SalesDataModelV1.GetSalesmen(); } //所属机构 if (fieldNames.Contains(nameof(Client.ParentId))) { _config.selects[nameof(Client.ParentId)] = ClientDataModelV1.GetAllOpenaccountClient(null); } //归属营业部 if (fieldNames.Contains(nameof(Client.SalesDepartmentId))) { _config.selects[nameof(Client.SalesDepartmentId)] = SalesDataModelV1.GetAllSaleDepartment(); } //交易种类 if (!_config.selects.ContainsKey(nameof(Client.DerivativesInvestmentVarieties))) { _config.selects[nameof(Client.DerivativesInvestmentVarieties)] = ClientDataModelV1.GetAllDerivativesInvestmentVarieties(); } //不良诚信记录 if (!_config.selects.ContainsKey(nameof(Client.BadFaithRecord))) { _config.selects[nameof(Client.BadFaithRecord)] = ClientDataModelV1.GetAllBadFaithRecord(); } //可接受风险服务 _config.selects[nameof(Client.RiskServiceDegree)] = ClientDataModelV1.GetRiskServiceDegree(); //适当性评估人 _config.selects[nameof(Client.AppropriatenessAssessor)] = ClientDataModelV1.GetAppropriatenessAssessor(!isView); //适当性评级-默认值,如果配置了其他项,这里的设置会被覆盖掉 _config.selects[nameof(Client.AppropriatenessDegree)] = ClientDataModelV1.GetAppropriatenessList(); var dicInfos = _config.sections .Where(n => n.fields != null) .SelectMany(n => n.fields.Where(m => m.dictionaryKey != null && m.dictionaryKey != "") .Select(m => new KeyValuePair(m.name, m.dictionaryKey ?? string.Empty))) .Concat(new[] { new KeyValuePair("文件类型", "文件类型"), new KeyValuePair("权益类签署版本", "权益类签署版本"), new KeyValuePair("资金用途", "资金用途"), }).ToArray(); var yldic = DictionaryBLL.GetDictionary(dicInfos.Select(O => O.Value).ToArray()); foreach (var item in dicInfos) { if (item.Key == nameof(Client.AppropriatenessDegree)) { continue; } _config.selects[item.Key] = yldic.GetList(item.Value, false); if (item.Key == "meta_ClientTagDesc") { _config.selects[item.Key].ToList().ForEach(O => { O.Text = O.Value; }); } //行业字段比较特殊,KeyValue需要保持一致; if (item.Key == nameof(Client.BusinessType) || item.Key == nameof(Client.ProtocolSignVersion) || item.Key == "meta_ClientTag") { _config.selects[item.Key].ToList().ForEach(O => { O.Value = O.Text; }); } } _updateTime.Value = DateTime.Now; } } } return _config.Clone(); } /// /// 获取当前配置的所有表单字段 /// public static IEnumerable GetAllFormEditField() { var config = GetConfig(false); return config.sections.SelectMany(n => n.fields ?? Enumerable.Empty()); } public static IEnumerable GetAllFormEditFieldByTableId(string tableid) { var config = GetConfig(false); return config.sections.Where(l => l.tabId == tableid).SelectMany(n => n.fields ?? Enumerable.Empty()); } public static string GetSelectValue(string name, string text) { var config = GetConfig(true); var selectItem = _config.selects.Where(a => a.Key == name).FirstOrDefault(); var item = selectItem.Value.Where(a => a.Text == text).FirstOrDefault(); return item != null ? item.Value : string.Empty; } public static FormEditField GetFormEditFieldByName(string name) { var FormEditFields = GetAllFormEditField(); if (FormEditFields != null) { return FormEditFields.Where(a => a.name == name).FirstOrDefault(); } return null; } public static FormEditField GetFormEditFieldByLabel(string label) { var config = GetConfig(true); var FormEditFields = GetAllFormEditField(); if (FormEditFields != null) { return FormEditFields.Where(a => a.label == label).FirstOrDefault(); } return null; } public static FormEditField GetFormEditFieldByLabel(IEnumerable FormEditFields, string label) { if (FormEditFields != null) { return FormEditFields.Where(a => a.label == label).FirstOrDefault(); } return null; } /// /// 转换成横向流布局的字段列表 /// public static IEnumerable GetFlatList(IEnumerable fields, out int cols) { cols = 0; if (fields is null) { return Enumerable.Empty(); } var colIndex = 0; var list = new List(); var listList = new List> { list }; foreach (var f in fields) { if (f.type == "new-col") { if (list.Count > 0) { colIndex++; if (colIndex >= listList.Count) { listList.Add(list = new List()); } } } else if (f.type == "new-row") { colIndex = 0; JustifyListList(listList); list = listList[0]; } else if (f.visible) { if (f.layout == "row") { JustifyListList(listList); listList.ForEach(x => x.Add(f)); } else { list.Add(f); } } } var len = JustifyListList(listList); var results = new List(fields.Count()); for (var i = 0; i < len; i++) { results.AddRange(listList.Select(n => n[i])); } cols = listList.Count; return results; } private static int JustifyListList(List> listList) { if (listList.Count > 0) { var maxCount = listList.Max(n => n.Count); foreach (var list in listList) { if (list.Count < maxCount) { list.AddRange(new FormEditField[maxCount - list.Count]); } } return maxCount; } return 0; } /// /// 获取默认配置内容 /// public static string GetDefaultConfigJson() { return Properties.Resources.clientEditConfig; } /// /// 获取当前配置内容 /// public static string GetCurrentConfigJson() { using (var db = DbContextFactory.GetYLDbContext()) { return db.AppConfig.Where(n => n.PGroup == "System" && n.PName == "ClientEditConfig") .Select(n => n.PValue).FirstOrDefault().TrimToNull() ?? Properties.Resources.clientEditConfig; } } /// /// 保存配置 /// public static void SaveEditConfig(string config) { var arr = config.Split('='); if (arr.Length == 2) { var obj = JsonHelper.Deserialize(arr[1]); if (obj?.clientList?.Any() ?? false) { var nameList = obj.clientList.GroupBy(O => O.name).Where(O => O.ToArray().Length > 1).Select(O => O.Key); if (nameList.Any()) { throw new ServiceException("客户列表中出现列name信息重复,name为" + string.Join(",", nameList)); } } if (obj?.openList?.Any() ?? false) { var nameList = obj.openList.GroupBy(O => O.name).Where(O => O.ToArray().Length > 1).Select(O => O.Key); if (nameList.Any()) { throw new ServiceException("开户列表中出现列name信息重复,name为" + string.Join(",", nameList)); } } } new AppConfigService(OptUserInfo.SystemUser).SaveConfig("System", "ClientEditConfig", config, "string", ""); ResetConfig(); } public static List GetOpenList() { var config = GetConfig(); return config.openList.ToList(); } public static List GetClientList() { var config = GetConfig(); return config.clientList.ToList(); } } /// /// 客户编辑配置 /// public class ClientEditConfig { private IEnumerable _sections; private Dictionary> _selectItems; private Dictionary _defaults; private Dictionary> _listDatas; private IEnumerable _openList; private IEnumerable _clientList; /// /// 页面编辑数据组 /// public IEnumerable sections { get => _sections ?? (_sections = Enumerable.Empty()); set => _sections = value; } /// /// 页面编辑下拉选择数据 /// public Dictionary> selects { get => _selectItems ?? (_selectItems = new Dictionary>()); set => _selectItems = value; } /// /// 页面编辑数据默认值 /// public Dictionary defaults { get => _defaults ?? (_defaults = new Dictionary()); set => _defaults = value; } /// /// 页面编辑列表数据 /// public Dictionary> lists { get => _listDatas ?? (_listDatas = new Dictionary>()); set => _listDatas = value; } /// /// 开户编辑列表数据 /// public IEnumerable openList { get => _openList ?? (_openList = Enumerable.Empty()); set => _openList = value; } /// /// 客户编辑列表数据 /// public IEnumerable clientList { get => _clientList ?? (_clientList = Enumerable.Empty()); set => _clientList = value; } public ClientEditConfig Clone() { var clone = (ClientEditConfig)MemberwiseClone(); clone.lists = null; if (clone.sections != null) { clone.sections = clone.sections.Select(n => n.Clone()).ToArray(); } if (clone.defaults != null) { clone.defaults = new Dictionary(clone.defaults); } return clone; } } /// /// 编辑组 /// public class FormEditSection { /// /// 是否可见 /// public bool visible { get; set; } = true; /// /// tab ID /// public string tabId { get; set; } /// /// tab标题 /// public string title { get; set; } /// /// 表单样式类 /// public string className { get; set; } /// /// 视图路径(这个值存在时fields失效) /// public string viewPath { get; set; } /// /// 表单输入字段 /// public IEnumerable fields { get; set; } public FormEditSection Clone() { var clone = (FormEditSection)MemberwiseClone(); if (clone.fields != null) { clone.fields = clone.fields.Select(n => n.Clone()).ToArray(); } else { clone.fields = Enumerable.Empty(); } return clone; } } /// /// 表单字段 /// public class FormEditField { /// /// 是否可见 /// public bool visible { get; set; } = true; /// /// 字段名 /// public string name { get; set; } /// /// 字段说明 /// public string label { get; set; } /// /// 字段类型(特殊:none,new-col,new-row) /// public string type { get; set; } /// /// 字段样式类 /// public string className { get; set; } /// /// 是否必需 /// public bool required { get; set; } = false; /// /// 是否只读 /// public bool @readonly { get; set; } /// /// 当type等于select时是否增加空选项 /// public bool appendBlank { get; set; } /// /// placeholder /// public string placeholder { get; set; } /// /// 布局 /// public string layout { get; set; } /// /// 数据字典绑定关系 /// public string dictionaryKey { get; set; } public override string ToString() { return $"{name}-{label}-{type}"; } public FormEditField Clone() { return (FormEditField)MemberwiseClone(); } } public class ClientField { public string name { get; set; } public string label { get; set; } public bool hidden { get; set; } public bool sortable { get; set; } /// /// left, center, right. /// public string align { get; set; } public string width { get; set; } } }