Ejemplo n.º 1
0
            public async Task <ResponseResult> Handle(Command request, CancellationToken cancellationToken)
            {
                if (string.IsNullOrWhiteSpace(request.Id))
                {
                    return(ResponseResult.Error("分类Id不能为空."));
                }

                request.Id = request.Id.Trim();

                QueryStack.Models.ProducType pt = new QueryStack.Models.ProducType();
                pt.Id      = request.Id;
                pt.Deleted = true;

                using (var scope = _databaseScopeFactory.CreateReadOnly())
                {
                    if (await _producTypeRepository.HasChildren(pt.Id))
                    {
                        return(ResponseResult.Error("存在子级数据 , 本级不允许删除."));
                    }

                    if (await _productInfoRepository.HasProductOfThisTypeAsync(pt.Id))
                    {
                        return(ResponseResult.Error("此分类下存在产品数据 , 不允许删除."));
                    }
                }

                using (var scope = _databaseScopeFactory.CreateWithTransaction())
                {
                    await _producTypeRepository.UpdateAsync(pt, request.UserId);

                    scope.SaveChanges();
                }

                return(ResponseResult.Ok());
            }
            public async Task <ResponseResult> Handle(Command request, CancellationToken cancellationToken)
            {
                if (string.IsNullOrWhiteSpace(request.TypeName))
                {
                    return(ResponseResult.Error("分类名称不能为空."));
                }

                if (string.IsNullOrWhiteSpace(request.ParentId))
                {
                    return(ResponseResult.Error("父级分类不能为空."));
                }

                if (request.CurrentLevelCode == 1 && !string.IsNullOrWhiteSpace(request.ParentId))
                {
                    return(ResponseResult.Error("此操作暂无顶级分类创建权限."));
                }

                request.TypeName = request.TypeName.Trim();
                request.ParentId = request.ParentId.Trim();

                int    maxSortOrder = 0;
                string maxCode      = string.Empty;

                using (var scope = _databaseScopeFactory.CreateReadOnly())
                {
                    maxSortOrder = await _producTypeRepository.GetMaxSortOrderValue(request.ParentId);

                    if (maxSortOrder > 99)
                    {
                        return(ResponseResult.Error("分类级别已达到上限."));
                    }

                    bool hasSameName = await _producTypeRepository.CheckTheSameTypeName(request.TypeName, request.ParentId);

                    if (hasSameName)
                    {
                        return(ResponseResult.Error($"同级分类: {request.TypeName} 已存在."));
                    }

                    maxCode = await _producTypeRepository.GetMaxCode(request.ParentId);
                }

                using (var scope = _databaseScopeFactory.CreateWithTransaction())
                {
                    QueryStack.Models.ProducType pt = new QueryStack.Models.ProducType
                    {
                        TypeCode  = Common.FormatDataCode.GetNewMaxCode(maxCode, request.ParentCode),
                        ParentId  = request.ParentId,
                        TypeName  = request.TypeName,
                        LevelCode = request.CurrentLevelCode,
                        SortOrder = maxSortOrder + 1,
                    };

                    await _producTypeRepository.AddAsync(pt, request.UserId);

                    scope.SaveChanges();
                }

                return(ResponseResult.Ok());
            }
Ejemplo n.º 3
0
            public async Task <ResponseResult> Handle(Command request, CancellationToken cancellationToken)
            {
                QueryStack.Models.ProducType pt = new QueryStack.Models.ProducType
                {
                    TypeName = request.TypeName,
                    ParentId = request.ParentId,
                    Id       = request.Id,
                };

                using (var scope = _databaseScopeFactory.CreateWithTransaction())
                {
                    bool hasSameName = false;
                    if (string.IsNullOrWhiteSpace(request.ParentId))
                    {// 顶级分类查重
                        hasSameName = await _producTypeRepository.CheckTheTopTypeName(pt.TypeName, pt.Id);
                    }
                    else
                    {// 分类查重
                        hasSameName = await _producTypeRepository.CheckTheSameTypeName(pt.TypeName, pt.ParentId, pt.Id);
                    }

                    if (hasSameName)
                    {
                        return(ResponseResult.Error($"分类: {pt.TypeName} 已存在."));
                    }
                }

                using (var scope = _databaseScopeFactory.CreateWithTransaction())
                {
                    await _producTypeRepository.UpdateAsync(pt, request.UserId);

                    scope.SaveChanges();
                }

                return(ResponseResult.Ok());
            }