private async Task CheckAndSetParentIds(ProductClassify classify) { if (_classifyRepository.CheckExists(p => p.ParentId == classify.ParentId && p.Name == classify.Name, classify.Id)) { throw new UserFriendlyException("该分类已存在"); } if (classify.ParentId.HasValue) { if (classify.ParentId.Value == 0) { classify.ParentId = null; } else { var parent = await _classifyRepository.GetAsync(classify.ParentId.Value); if (parent == null) { throw new UserFriendlyException("指定的父级分类不存在"); } classify.ParentIds = parent.ParentIds + parent.Id.ToString() + ","; } } }
/// <summary> /// 更新 分类 /// </summary> /// <param name="classify">分类</param> /// <returns></returns> public async Task UpdateClassifyAsync(ProductClassify classify) { classify.CheckNotNull("classify"); await CheckAndSetParentIds(classify); await _classifyRepository.UpdateAsync(classify); }
/// <summary> /// 删除 分类,同时删除所有子分类 /// </summary> /// <param name="classify">分类</param> /// <returns></returns> public async Task DeleteClassifyAsync(ProductClassify classify) { classify.CheckNotNull("classify"); await _classifyRepository.DeleteAsync(classify); //删除所有子分类 string parentIds = classify.ParentIds + classify.Id + ","; await _classifyRepository.DeleteAsync(p => p.ParentIds.StartsWith(parentIds)); }
/// <summary> /// 创建 分类 /// </summary> /// <param name="classify">分类</param> /// <returns></returns> public async Task CreateClassifyAsync(ProductClassify classify) { classify.CheckNotNull("classify"); await CheckAndSetParentIds(classify); var orderNos = _classifyRepository.GetAll().Where(p => (p.ParentId) == classify.ParentId).Select(p => p.OrderNo).ToList(); classify.OrderNo = orderNos.Any() ? orderNos.Max() + 1 : 1; await _classifyRepository.InsertAsync(classify); }
/// <summary> /// 分类降序 /// </summary> /// <param name="classify">分类</param> /// <returns></returns> public async Task ClassifyDownAsync(ProductClassify classify) { classify.CheckNotNull("classify"); var nextClassify = _classifyRepository.GetAll().Where(p => p.OrderNo > classify.OrderNo && p.ParentId == classify.ParentId) .OrderBy(p => p.OrderNo).Take(1).FirstOrDefault(); if (nextClassify != null) { int orderNo = classify.OrderNo; classify.OrderNo = nextClassify.OrderNo; nextClassify.OrderNo = orderNo; await _classifyRepository.UpdateAsync(classify); await _classifyRepository.UpdateAsync(nextClassify); } }
/// <summary> /// 分类升序 /// </summary> /// <param name="classify">分类</param> /// <returns></returns> public async Task ClassifyUpAsync(ProductClassify classify) { classify.CheckNotNull("classify"); var preClassify = _classifyRepository.GetAll().Where(p => p.OrderNo < classify.OrderNo && p.ParentId == classify.ParentId) .OrderByDescending(p => p.OrderNo).Take(1).FirstOrDefault(); if (preClassify != null) { int orderNo = classify.OrderNo; classify.OrderNo = preClassify.OrderNo; preClassify.OrderNo = orderNo; await _classifyRepository.UpdateAsync(classify); await _classifyRepository.UpdateAsync(preClassify); } }
/// <summary> /// 获取 分类所有的属性(包含父级和公共属性) /// </summary> /// <param name="classify">分类</param> /// <returns></returns> public ICollection <ProductAttribute> GetAttributeByClassify(ProductClassify classify) { classify.CheckNotNull("Classify"); var attributes = new List <ProductAttribute>(); var classifyIds = classify.ParentIds.IsNullOrWhiteSpace() ? new List <int>() :classify.ParentIds.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList(); classifyIds.Add(classify.Id); classifyIds.Reverse(); //将id集合倒序 classifyIds.Add(0); //在集合末尾添加公共属性的ClassifyId foreach (var classifyId in classifyIds) { var existAttributeNames = attributes.Select(p => p.Name); attributes.AddRange(_attributeRepository.GetAllList(p => p.ProductClassifyId == classifyId && !existAttributeNames.Contains(p.Name))); } return(attributes); }