public Product CreateProduct(int manufacturerId, int rawMaterialId, string size, decimal thickness) { Argument.Require(manufacturerId > 0, "Manufacturer id is required."); Argument.Require(rawMaterialId > 0, "Raw material id is required."); Argument.NotNullOrEmpty(size, "Product size is required."); Argument.Require(thickness > 0, "Product thickness is required."); var product = new Product { ManufacturerId = manufacturerId, RawMaterialId = rawMaterialId, Name = size, Thickness = thickness, }; using (var storage = new Storage()) { product.ProductId = Convert.ToInt32(storage.InsertWithIdentity(product)); } return product; }
public BulkProductParseResult ParseExtraPricesBulk( int manufacturerId, int supplierId, DateTime date, AlloyType alloyType, RollType rollType, int priceExtraCategoryId, bool remove, string raw) { var products = new List<Product>(); var errors = new List<string>(); if (remove) { raw = "0"; } try { var lines = raw.Split(new [] {'\n'}, StringSplitOptions.RemoveEmptyEntries); var headers = lines[0].Split('\t'); using (var storage = new Storage()) { if (lines.Length == 1 && headers.Length == 1) { var price = Convert.ToDecimal(raw.Trim().FixDecimalSeparator()); return new BulkProductParseResult { Errors = new string[0], Products = storage .Products .Where( p => p.ManufacturerId == manufacturerId && p.RawMaterial.ManufacturerId == supplierId && (alloyType == AlloyType.Undefined || p.RawMaterial.RawMaterialType.AlloyType == alloyType) && (rollType == RollType.Undefined || p.RawMaterial.RawMaterialType.RollType == rollType) ) .ToList() .Select( p => { p.PriceExtras = new[] { new PriceExtra { PriceExtraCategoryId = priceExtraCategoryId, PriceItems = new[] { new PriceItem { Date = date, OwnerId = manufacturerId, Price = price, } } } }; return p; }) .ToArray() }; } for (int lineId = 1; lineId < lines.Length; lineId++) { var prices = lines[lineId].Split('\t'); var productName = prices[0].Trim(); for (int colId = 1; colId < headers.Length; colId++) { var thickness = Convert.ToDecimal(headers[colId].FixDecimalSeparator()); var query = storage .RawMaterials .LoadWith(rm => rm.RawMaterialType) .Where( rm => rm.ManufacturerId == supplierId && rm.RawMaterialType.Thickness == thickness); if (alloyType != AlloyType.Undefined) { query = query.Where(rm => rm.RawMaterialType.AlloyType == alloyType); } if (rollType != RollType.Undefined) { query = query.Where(rm => rm.RawMaterialType.RollType == rollType); } var rawMaterials = query.ToList(); if (!rawMaterials.Any()) { errors.Add($"Выбранный поставщик не производит материала с такими параметрами и толщиной {thickness}"); continue; } foreach (var rawMaterial in rawMaterials) { var product = storage .Products .SingleOrDefault( p => p.ManufacturerId == manufacturerId && p.RawMaterialId == rawMaterial.RawMaterialId && p.Name == productName); if (null == product) { product = new Product { ManufacturerId = manufacturerId, RawMaterialId = rawMaterial.RawMaterialId, Thickness = rawMaterial.RawMaterialType.Thickness, Name = productName, }; } var priceExtra = new PriceExtra { PriceExtraCategoryId = priceExtraCategoryId, ProductId = product.ProductId, }; var price = string.IsNullOrEmpty(prices[colId]) ? null : (decimal?)Convert.ToDecimal(prices[colId].FixDecimalSeparator()); var priceItem = new PriceItem { OwnerId = manufacturerId, Date = date, Price = remove ? null : price, }; priceExtra.PriceItems = new[] { priceItem }; product.PriceExtras = new[] { priceExtra }; products.Add(product); if (!string.IsNullOrEmpty(prices[colId])) { } } } } } } catch (Exception ex) { _logger.Error(ex); errors.Add("Ошибка синтаксического разбора строки."); } return new BulkProductParseResult { Errors = errors.ToArray(), Products = errors.Any() ? new Product[0] : products.ToArray() }; }