/// <summary> /// 保存配置信息 /// </summary> /// <param name="value">配置信息</param> public ApiResult <string> Save(OptionViewModel value) { ApiResult <string> result = new ApiResult <string>(); result.HasError = true; string GroupType = value.Group.GroupType; if (value.Group == null || string.IsNullOrEmpty(GroupType) || value.ListOptions == null) { result.Message = "保存参数配置时发生参数空异常"; return(result); } //调用保存前处理事件 ConfigOption curConfigOption = AllConfig.First(e => e.GroupType.Equals(GroupType, StringComparison.OrdinalIgnoreCase)); if (curConfigOption == null) { //如果没有找到匹配项 result.Message = string.Format("当前保存配置信息{0}不对应后台的任务配置类", GroupType); return(result); } VerifyResult vr = curConfigOption.BeforeSave(value); if (!vr.IsSusscess) { result.Message = vr.ErrorMessage; return(result); } //保存数据 try { //删除原有数据 SQLHelper.ExecuteNonQuery("Delete from Configuration_Options WHERE OptionType=@OptionType", new { OptionType = GroupType }); //保存数据 foreach (var item in value.ListOptions) { item.OptionId = Guid.NewGuid().ToString("N"); SQLHelper.ExecuteNonQuery(@"INSERT INTO Configuration_Options(OptionId,OptionType,OptionName,[Key],Value,ValueType) select @OptionId,@OptionType,@OptionName,@Key,@Value,@ValueType", item); } } catch (Exception ex) { LogHelper.Error("系统参数配置保存异常", ex); result.Message = ex.Message; return(result); } //对当前配置项进行赋值 SetValue(curConfigOption, value.ListOptions); result.HasError = false; return(result); }
/// <summary> /// 参数配置项保存前处理逻辑 /// </summary> /// <param name="value">当前保存的参数</param> public virtual VerifyResult BeforeSave(OptionViewModel value) { return(new VerifyResult()); }
/// <summary> /// 获取所有配置信息 /// </summary> /// <returns>所有配置信息</returns> public List <OptionViewModel> GetAllOption(string GroupType = "") { //所有选项值 List <Options> listOption = ConfigService.GetAllOptions(GroupType); //所有分组 List <OptionGroup> listOptionGroup = new List <OptionGroup>(); IEnumerable <ConfigOption> listConfigs = AllConfig; if (!string.IsNullOrEmpty(GroupType)) { listConfigs = AllConfig.Where(e => e.GroupType.Equals(GroupType, StringComparison.OrdinalIgnoreCase)); } ConfigDescription desc = null; //分组信息 OptionGroup optionGroup = null; Options op = null; ConfigAttribute ca = null; List <OptionViewModel> result = new List <OptionViewModel>(); OptionViewModel itemOptionViewModel = null; //代码现有配置项 foreach (ConfigOption item in listConfigs) { //反射读取配置项ConfigTypeAttribute ConfigAttribute 信息 desc = ConfigDescriptionCache.GetTypeDiscription(item.GetType()); itemOptionViewModel = new OptionViewModel(); optionGroup = new OptionGroup { GroupType = desc.Group, GroupName = desc.GroupCn, ImmediateUpdate = desc.ImmediateUpdate, CustomPage = desc.CustomPage }; listOptionGroup.Add(optionGroup); itemOptionViewModel.Group = optionGroup; itemOptionViewModel.ListOptions = new List <Options>(); //每项值信息 List <Options> itemOptions = listOption.Where(e => e.OptionType.Equals(desc.Group, StringComparison.OrdinalIgnoreCase)).ToList(); foreach (PropertyInfo prop in desc.StaticPropertyInfo) { op = itemOptions.FirstOrDefault(e1 => e1.Key.Equals(prop.Name, StringComparison.OrdinalIgnoreCase)); ca = desc.MemberDict[prop.Name]; if (op == null) { op = new Options { OptionType = desc.Group, OptionName = ca.Name, Key = prop.Name, Value = Convert.ToString(ca.DefaultValue), ValueType = Convert.ToInt32(ca.ValueType).ToString() }; } //必填设置 op.Required = ca.Required; //校验规则 op.ValidateRule = ca.ValidateRule; //悬浮title op.Title = ca.Title; itemOptionViewModel.ListOptions.Add(op); } result.Add(itemOptionViewModel); } return(result.OrderBy(e => e.Group.GroupType).ToList()); }