/// <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.WriteLog("系统参数配置保存异常", ex); result.Message = ex.Message; return(result); } //对当前配置项进行赋值 SetValue(curConfigOption, value.ListOptions); result.HasError = false; return(result); }
/// <summary> /// 保存时 对当前配置项进行赋值 /// </summary> /// <param name="item">当前配置项</param> /// <param name="ListOptions">配置项值</param> public void SetValue(ConfigOption item, List <Options> ListOptions) { //调用保存后处理事件 item.AfterSave(ListOptions); var desc = ConfigDescriptionCache.GetTypeDiscription(item.GetType()); Options option = null; foreach (PropertyInfo prop in desc.StaticPropertyInfo) { option = ListOptions.First(e => e.Key.Equals(prop.Name, StringComparison.OrdinalIgnoreCase)); if (option == null) { //不存在该配置项,则清空当前值 prop.SetValue(null, Convert.ChangeType(null, prop.PropertyType), null); } else { prop.SetValue(null, Convert.ChangeType(option.Value, prop.PropertyType), null); } } }