private void Validate(Create.Command message, CustomContext context)
        {
            using (var scope = _databaseScopeFactory.CreateReadOnly())
            {
                // 采购主体
                if (!string.IsNullOrEmpty(message.MateTypeCode) && message.MateTypeCode.StartsWith(_tradCode))
                {
                    if (message.Trades.Count == 0)
                    {
                        context.AddBadRequestError("采购主体不能为空");
                    }
                }

                var nameValidate = _materialRepository.ExistNameAsync(message.MateName, message.MateTypeId).GetAwaiter().GetResult();
                if (nameValidate.Any())
                {
                    context.AddBadRequestError("物料名称不能重复");
                }

                var list = _materialRepository.BasicExistAsync(message).GetAwaiter().GetResult().ToList();
                if (list.Any())
                {
                    var listQuery = list.Select(x => new
                    {
                        x.Id,
                        Tradestr = string.Join(",", x.Trades.OrderBy(t => t.TradeId).Select(y => y.TradeId).ToArray()),
                        ProStr   = string.Join(",", x.Properties.OrderBy(y => y.PropId)
                                               .Select(z => string.Concat(z.PropId, z.OptionId)).ToArray()),
                    }).ToList();

                    var trades = message.Trades.Where(x => !string.IsNullOrWhiteSpace(x.TradeId))
                                 .Select(x => x.TradeId).Distinct().OrderBy(y => y);
                    var tradestr = string.Join(",", trades);

                    // 自定义属性
                    var properties = message.Properties.Where(x => (!string.IsNullOrWhiteSpace(x.PropId)) &&
                                                              (!string.IsNullOrWhiteSpace(x.OptionId))).OrderBy(y => y.PropId);
                    var proStr = string.Join(",", properties.Select(z => string.Concat(z.PropId, z.OptionId)).Distinct());
                    if (listQuery.Any(y => y.Tradestr.Equals(tradestr) && y.ProStr.Equals(proStr)))
                    {
                        context.AddBadRequestError("该物料已存在。");
                    }
                }
            }
        }
Example #2
0
            private bool MaterialValidation(QueryStack.Models.Material material)
            {
                if (material == null)
                {
                    return(false);
                }

                using (var scope = _databaseScopeFactory.CreateReadOnly())
                {
                    var materialName = _repository.ExistNameAsync(material.MateName, material.MateTypeId).GetAwaiter().GetResult();
                    if (materialName?.Any() ?? false)
                    {
                        return(false);
                    }

                    // 物料判重
                    var list = _repository.BasicExistAsync(material).GetAwaiter().GetResult().ToList();
                    if (list?.Any() ?? false)
                    {
                        var listQuery = list.Select(x => new
                        {
                            x.Id,
                            TradeStr = string.Join(",", x.Trades.OrderBy(t => t.TradeId).Select(y => y.TradeId).ToList()),
                            ProStr   = string.Join(",", x.Properties.OrderBy(y => y.PropId)
                                                   .Select(z => string.Concat(z.PropId, z.OptionId)).ToList()),
                        }).ToList();

                        // 采购主体
                        var tradeList = material.Trades.Where(x => !string.IsNullOrWhiteSpace(x.TradeId))
                                        .Select(x => x.TradeId).Distinct().OrderBy(y => y);
                        var tradeStr = string.Join(",", tradeList);

                        // 自定义属性
                        var propertyList = material.Properties.Where(x => (!string.IsNullOrWhiteSpace(x.PropId)) &&
                                                                     (!string.IsNullOrWhiteSpace(x.OptionId))).OrderBy(y => y.PropId);
                        var proStr = string.Join(",", propertyList.Select(z => string.Concat(z.PropId, z.OptionId)).Distinct());

                        // 该物料已存在
                        if (listQuery.Any(y => y.TradeStr.Equals(tradeStr) && y.ProStr.Equals(proStr)))
                        {
                            return(false);
                        }
                    }

                    // Id有效性验证

                    // 物料规格 Id
                    var basicDataresult = _basicDataRepository.FindAsync(material.MaterialSpecId).GetAwaiter().GetResult();
                    if (basicDataresult == null)
                    {
                        return(false);
                    }
                    else
                    {
                        material.MaterialSpec = basicDataresult.Name;
                    }

                    // 物料单位 Id
                    basicDataresult = _basicDataRepository.FindAsync(material.MaterialUnitId).GetAwaiter().GetResult();
                    if (basicDataresult == null)
                    {
                        return(false);
                    }
                    else
                    {
                        material.MaterialUnits = basicDataresult.Name;
                    }

                    // 属性 Id
                    foreach (var property in material.Properties)
                    {
                        var propertyResult = _propertyRepository.FindPropertyAsync(property.PropId).GetAwaiter().GetResult();
                        if (propertyResult == null)
                        {
                            return(false);
                        }
                        else
                        {
                            property.PropName = propertyResult.PropName;

                            // 属性值 Id
                            var optionResult = _propertyOptionRepository.FindAsync(property.OptionId).GetAwaiter().GetResult();
                            if (optionResult == null)
                            {
                                return(false);
                            }
                        }
                    }

                    // 采购主体 Id
                    foreach (var trade in material.Trades)
                    {
                        basicDataresult = _basicDataRepository.FindAsync(trade.TradeId).GetAwaiter().GetResult();
                        if (basicDataresult == null)
                        {
                            return(false);
                        }
                        else
                        {
                            trade.TradeName = basicDataresult.Name;
                        }
                    }
                }

                return(true);
            }