예제 #1
0
        /// <summary>
        /// 保存时 对当前配置项进行赋值
        /// </summary>
        /// <param name="item">当前配置项</param>
        /// <param name="ListOptions">配置项值</param>
        /// <param name="AfterSave">是否调用保存后方法</param>
        public void SetValue(ConfigOption item, List <Options> ListOptions, bool AfterSave = true)
        {
            if (AfterSave)
            {
                //调用保存后处理事件
                item.AfterSave(ListOptions);
            }
            //获取本地化配置
            var     localConfigList = ConfigService.GetAllLocalOptions(item.GroupType);
            var     desc            = ConfigDescriptionCache.GetTypeDiscription(item.GetType());
            Options option          = null;

            foreach (PropertyInfo prop in desc.StaticPropertyInfo)
            {
                option = ListOptions.FirstOrDefault(e => e.Key.Equals(prop.Name, StringComparison.OrdinalIgnoreCase));
                if (option == null)
                {
                    //不存在该配置项,则清空当前值
                    prop.SetValue(null, ConvertScalar(null, prop.PropertyType), null);
                }
                else
                {
                    var    localConfig = localConfigList.FirstOrDefault(e => e.Key.Equals(prop.Name, StringComparison.OrdinalIgnoreCase));
                    string optionValue = option.Value;
                    if (localConfig != null)
                    {
                        optionValue = localConfig.Value;
                    }
                    prop.SetValue(null, ConvertScalar(optionValue, prop.PropertyType), null);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 保存配置信息
        /// </summary>
        /// <param name="value">配置信息</param>
        /// <param name="AfterSave">是否调用保存后方法</param>
        public ApiResult <string> Save(OptionViewModel value, bool AfterSave = true)
        {
            //保存标签信息
            if (value.TagList == null || value.TagList.Count == 0)
            {
                //删除标签
                TagService.DeleteTags(ConfigHandler, value.Group.GroupType);
            }
            else
            {
                //保存标签
                TagService.SaveTags(value.TagList, ConfigHandler, value.Group.GroupType, "");
            }

            ApiResult <string> result = new ApiResult <string>();

            result.Code = ResultCode.Parameter_Error;
            string GroupType = value.Group.GroupType;

            if (value.Group == null || string.IsNullOrEmpty(GroupType) || value.ListOptions == null)
            {
                result.Message = "保存参数配置时发生参数空异常";
                return(result);
            }
            //调用保存前处理事件
            ConfigOption curConfigOption = AllConfig.FirstOrDefault(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);
            }

            //保存数据
            using (CommonDbContext db = new CommonDbContext())
            {
                using (var trans = db.BeginTransaction())
                {
                    try
                    {
                        //先删除后插入
                        //删除原有数据
                        db.Set <Options>().Delete(e => e.OptionType == GroupType, trans);
                        foreach (var item in value.ListOptions)
                        {
                            item.OptionId = GuidHelper.GetSeqGUID();
                        }
                        //保存数据
                        db.Set <Options>().BulkInsert(value.ListOptions, trans);
                        db.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception e)
                    {
                        trans.Rollback();
                        throw e;
                    }
                }
            }

            //对当前配置项进行赋值
            SetValue(curConfigOption, value.ListOptions, AfterSave);

            ////MQ消息发送
            //RabbitMQClient.SendMessage(MQRoutingKey.ConfigHandler, GroupType, ChangeType.Update);

            result.Code = ResultCode.Success;
            return(result);
        }