Example #1
0
        /// <summary>
        /// 添加数据字典信息信息
        /// </summary>
        /// <param name="dtos">要添加的数据字典信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public OperationResult AddDictionarys(params DictionaryDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            OperationResult result = DictionaryRepository.Insert(dtos,
                                                                 dto =>
            {
                if (DictionaryRepository.CheckExists(m => m.Value == dto.Value))
                {
                    throw new Exception("值为“{0}”的数据字典已存在,不能重复添加。".FormatWith(dto.Value));
                }
            },
                                                                 (dto, entity) =>
            {
                if (dto.ParentId.HasValue && dto.ParentId.Value > 0)
                {
                    Dictionary parent = DictionaryRepository.GetByKey(dto.ParentId.Value);
                    if (parent == null)
                    {
                        throw new Exception("指定父数据字典不存在。");
                    }
                    entity.Parent = parent;
                }
                return(entity);
            });

            return(result);
        }
Example #2
0
        /// <summary>
        /// 更新数据字典信息信息
        /// </summary>
        /// <param name="dtos">包含更新信息的数据字典信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public OperationResult EditDictionarys(params DictionaryDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            OperationResult result = DictionaryRepository.Update(dtos,
                                                                 dto =>
            {
                if (DictionaryRepository.CheckExists(m => m.Value == dto.Value))
                {
                    throw new Exception("值为“{0}”的数据字典已存在,不能重复添加。".FormatWith(dto.Value));
                }
            },
                                                                 (dto, entity) =>
            {
                if (!dto.ParentId.HasValue || dto.ParentId == 0)
                {
                    entity.Parent = null;
                }
                else if (entity.Parent != null && entity.Parent.Id != dto.ParentId)
                {
                    Dictionary parent = DictionaryRepository.GetByKey(dto.ParentId.Value);
                    if (parent == null)
                    {
                        throw new Exception("指定父数据字典不存在。");
                    }
                    entity.Parent = parent;
                }
                return(entity);
            });

            return(result);
        }