public PriceExtra AddPriceExtra(int productId, int priceExtraCategoryId) { Argument.Require(productId > 0, "Product identifier is required."); Argument.Require(priceExtraCategoryId > 0, "Price extra category id is required."); var priceExtra = new PriceExtra { PriceExtraCategoryId = priceExtraCategoryId, ProductId = productId, }; using (var storage = new Storage()) { var existingExtra = storage .PriceExtras .SingleOrDefault(pe => pe.ProductId == productId && pe.PriceExtraCategoryId == priceExtraCategoryId); if (null == existingExtra) { priceExtra.PriceExtraId = Convert.ToInt32(storage.InsertWithIdentity(priceExtra)); } else { priceExtra.PriceExtraId = existingExtra.PriceExtraId; } } return priceExtra; }
public IEnumerable<PriceExtraCategory> Get() { using (var storage = new Storage()) { return storage.PriceExtraCategories.ToList(); } }
public IEnumerable<RawMaterialType> Get() { using (var storage = new Storage()) { return storage.RawMaterialTypes.ToList(); } }
public Manufacturer Get(int id) { using (var storage = new Storage()) { return storage .Manufacturers .SingleOrDefault(m => m.ManufacturerId == id); } }
public void Delete(int id) { using (var storage = new Storage()) { storage .PriceExtraCategories .Delete(pc => pc.PriceExtraCategoryId == id); } }
public IEnumerable<Manufacturer> Get() { using (var storage = new Storage()) { return storage .Manufacturers .ToList(); } }
public PriceExtraCategory Get(int id) { using (var storage = new Storage()) { return storage .PriceExtraCategories .SingleOrDefault(pc => pc.PriceExtraCategoryId == id); } }
public PriceExtra Get(int id) { using (var storage = new Storage()) { return storage .PriceExtras .SingleOrDefault(pe => pe.PriceExtraId == id); } }
public RawMaterialType Get(int id) { using (var storage = new Storage()) { return storage .RawMaterialTypes .SingleOrDefault(pc => pc.RawMaterialTypeId == id); } }
public void Delete(int id) { using (var storage = new Storage()) { storage .RawMaterialTypes .Delete(pc => pc.RawMaterialTypeId == id); } }
//[Route("~/api/raw-material")] //[HttpOptions] //[HttpDelete] public IHttpActionResult Delete(int id) { using (var storage = new Storage()) { storage .RawMaterials .Delete(rm => rm.RawMaterialId == id); } return Ok(); }
public void ClearStorage() { using (var storage = new Storage()) { storage.DropTable<Manufacturer>(); storage.DropTable<RawMaterialType>(); storage.DropTable<RawMaterial>(); storage.DropTable<PriceExtraCategory>(); storage.DropTable<PriceExtra>(); storage.DropTable<PriceItem>(); storage.DropTable<Product>(); storage.DropTable<PriceCache>(); } }
public IEnumerable<RawMaterialModel> Get() { using (var storage = new Storage()) { return storage .RawMaterials .LoadWith(rm => rm.RawMaterialType) .ToList() .Select(rm => new RawMaterialModel { ManufacturerId = rm.ManufacturerId, Name = rm.RawMaterialType.Name, RawMaterialId = rm.RawMaterialId, RawMaterialTypeId = rm.RawMaterialTypeId, }); } }
public void RemovePriceExtra(int productId, int priceExtraId, DateTime? date = null) { Argument.Require(productId > 0, "Product id is required."); Argument.Require(priceExtraId > 0, "Price extra id is required."); using (var storage = new Storage()) { var existingPriceExtra = storage .PriceExtras .SingleOrDefault(pe => pe.PriceExtraId == priceExtraId); if(null == existingPriceExtra) return; storage.Delete(existingPriceExtra); UpdatePriceCache(storage, existingPriceExtra.ProductId, date ?? DateTime.Today); } }
public RawMaterialModel Get(int id) { using (var storage = new Storage()) { return storage .RawMaterials .LoadWith(rm => rm.RawMaterialType) .Where(m => m.RawMaterialId == id) .ToList() .Select(rm => new RawMaterialModel { ManufacturerId = rm.ManufacturerId, Name = rm.RawMaterialType.Name, RawMaterialId = rm.RawMaterialId, RawMaterialTypeId = rm.RawMaterialTypeId, }) .SingleOrDefault(); } }
public void Post([FromBody] RawMaterial material) { Argument.NotNull(material, "Material is required."); using (var storage = new Storage()) { var existingMaterial = storage .RawMaterials .SingleOrDefault(rm => rm.RawMaterialId == material.RawMaterialId); if (null != existingMaterial) { storage.Update(material); } else { storage.Insert(material); } } }
public ProductModel Get(int id) { using (var storage = new Storage()) { return storage .Products .LoadWith(p => p.Manufacturer) .LoadWith(p => p.RawMaterial.RawMaterialType) .Where(p => p.ProductId == id) .Select(p => new ProductModel { ProductId = p.ProductId, ManufacturerId = p.ManufacturerId, ManufacturerName = p.Manufacturer.Name, RawMaterialTypeId = p.RawMaterial.RawMaterialTypeId, RawMaterialTypeName = p.RawMaterial.RawMaterialType.Name, Thickness = p.Thickness, }) .SingleOrDefault(); } }
public RawMaterialType Post([FromBody] RawMaterialType rawMaterialType) { Argument.NotNull(rawMaterialType, "Price extra category is required."); using (var storage = new Storage()) { var existingMaterial = storage .RawMaterialTypes .SingleOrDefault(rmt => rmt.RawMaterialTypeId == rawMaterialType.RawMaterialTypeId); if (null != existingMaterial) { storage.Update(rawMaterialType); } else { storage.Insert(rawMaterialType); } return rawMaterialType; } }
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 void StorageShouldCreateManufacturers() { using (var storage = new Storage()) { var result = storage.InsertWithIdentity(new Manufacturer { IsPrimary = true, Name = "Severstahl", }); var result2 = storage.InsertWithIdentity(new Manufacturer { IsPrimary = true, Name = "Severstahl2", }); Debug.WriteLine(result); Debug.WriteLine(result2); Assert.AreEqual(2, storage.Manufacturers.ToList().Count); } }
public void SetupStorage() { try { ClearStorage(); } catch { } using (var storage = new Storage()) { storage.CreateTable<Manufacturer>(); storage.CreateTable<RawMaterialType>(); storage.CreateTable<RawMaterial>(); storage.CreateTable<PriceExtraCategory>(); storage.CreateTable<PriceExtra>(); storage.CreateTable<PriceItem>(); storage.CreateTable<Product>(); storage.CreateTable<PriceCache>(); } }
public PriceExtraCategory Post([FromBody] PriceExtraCategory priceExtraCategory) { Argument.NotNull(priceExtraCategory, "Price extra category is required."); using (var storage = new Storage()) { var existingCategory = storage .PriceExtraCategories .SingleOrDefault(pc => pc.PriceExtraCategoryId == priceExtraCategory.PriceExtraCategoryId); if (null != existingCategory) { storage.Update(priceExtraCategory); } else { storage.Insert(priceExtraCategory); } return priceExtraCategory; } }
public IEnumerable<PriceExtraModel> GetPriceExtras(int id) { var date = DateTime.Today; using (var storage = new Storage()) { var product = storage .Products .LoadWith(p => p.PriceExtras) .LoadWith(p => p.PriceExtras[0].PriceExtraCategory) .SingleOrDefault(p => p.ProductId == id); if (null == product) yield break; foreach (var priceExtra in product.PriceExtras) { var lastPrice = storage .PriceItems .Where(pi => pi.PriceExtraId == priceExtra.PriceExtraId && pi.Date <= date) .OrderByDescending(pi => pi.Date) .FirstOrDefault(); yield return new PriceExtraModel { ProductId = id, PriceExtraId = priceExtra.PriceExtraId, PriceCategoryId = priceExtra.PriceExtraCategoryId, PriceCategoryName = priceExtra.PriceExtraCategory.Name, LastDate = lastPrice?.Date ?? date, LastPrice = lastPrice?.Price }; } } }
public void Post([FromBody] Manufacturer manufacturer) { Argument.NotNull(manufacturer, "Manufacturer is required."); if (manufacturer.IsPrimary) return; using (var storage = new Storage()) { var existingManufacturer = storage .Manufacturers .SingleOrDefault(m => m.ManufacturerId == manufacturer.ManufacturerId); if (null != existingManufacturer) { storage.Update(manufacturer); } else { storage.Insert(manufacturer); } } }
public void Seed(Storage storage, int plantCode) { storage.Insert(new Manufacturer { Name = "ЧерМК", IsPrimary = true }); storage.Insert(new Manufacturer { Name = "НЛМК", IsPrimary = true }); storage.Insert(new Manufacturer { Name = "ММК", IsPrimary = true }); //storage.Insert(new Manufacturer { Name = "(1) КТЗ", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(1) РязТЗ", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(1) БТЗ", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(1) ОМК", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(1) СЗТЗ", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(1) ТПК Союз", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(1) Машпрофиль", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(1) Профиль А", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(1) БИС", IsPrimary = false }); storage.Insert(new Manufacturer { Name = "(2) ВестМет", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(2) Промизделия", IsPrimary = false }); storage.Insert(new Manufacturer { Name = "(2) ТЭМПО", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(2) ОМК", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(2) ТМК", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(2) Уралтрубпром", IsPrimary = false }); //storage.Insert(new Manufacturer { Name = "(2) Исаевский ТЗ", IsPrimary = false }); storage.Insert(new PriceExtraCategory { Name = "Себестоимость" }); storage.Insert(new PriceExtraCategory { Name = "Доставка материала" }); storage.Insert(new PriceExtraCategory { Name = "Передел" }); storage.Insert(new PriceExtraCategory { Name = "Доставка до Москвы" }); var allManufacturers = storage .Manufacturers .ToList(); var primaryManufacturers = allManufacturers.Where(m => m.IsPrimary); var nonPrimaryManufacturers = allManufacturers.Where(m => !m.IsPrimary); var materialThickness = EnumerateThickness(plantCode); foreach (var thickness in materialThickness) { foreach (var rollType in new [] {RollType.Cold, RollType.Hot }) { foreach (var alloyType in new[] {AlloyType.LowAlloy, AlloyType.Regular }) { var materialType = new RawMaterialType { AlloyType = alloyType, RollType = rollType, Thickness = thickness, }; SetMaterialTypeName(materialType); var materialTypeId = storage.InsertWithIdentity(materialType); foreach (var primaryManufacturer in primaryManufacturers) { var rawMaterialId = storage.InsertWithIdentity( new RawMaterial { ManufacturerId = primaryManufacturer.ManufacturerId, RawMaterialTypeId = Convert.ToInt32(materialTypeId), }); foreach (var productName in EnumerateProductNames(plantCode, rollType, alloyType)) { storage.Insert( new Product { ManufacturerId = primaryManufacturer.ManufacturerId, RawMaterialId = Convert.ToInt32(rawMaterialId), Name = productName, Thickness = thickness, }); foreach (var nonPrimaryManufacturer in nonPrimaryManufacturers) { storage.Insert( new Product { ManufacturerId = nonPrimaryManufacturer.ManufacturerId, RawMaterialId = Convert.ToInt32(rawMaterialId), Name = productName, Thickness = thickness, }); } } } } } } }
public void Post([FromBody] Product product) { Argument.NotNull(product, "Product is required."); using (var storage = new Storage()) { var existingProduct = storage .Products .SingleOrDefault(p => p.ProductId == product.ProductId); if (null != existingProduct) { storage.Update(product); } else { storage.Insert(product); } } }
public BulkMaterialParseResult ParseMaterialPricesBulk( int manufacturerId, int supplierId, DateTime date, AlloyType alloyType, RollType rollType, bool remove, string raw) { var materials = new List<RawMaterial>(); var errors = new List<string>(); try { using (var storage = new Storage()) { if (remove) { var allThickness = storage.RawMaterials .Where( rm => rm.ManufacturerId == supplierId && (alloyType == AlloyType.Undefined || rm.RawMaterialType.AlloyType == alloyType) && (rollType == RollType.Undefined || rm.RawMaterialType.RollType == rollType) ) .Select(rm => rm.RawMaterialType.Thickness.ToString(CultureInfo.InvariantCulture)) .ToList(); raw = string.Join("\t", allThickness) + "\n" + string.Join("\t", allThickness.Select(p => "")); } var lines = raw.Split('\n'); var headers = lines[0].Split('\t'); var prices = lines[1].Split('\t'); for (int i = 0; i < headers.Length; i++) { var thickness = Convert.ToDecimal(headers[i].FixDecimalSeparator()); if (!string.IsNullOrEmpty(prices[i])) { var rawMaterial = storage .RawMaterials .Where( rm => rm.ManufacturerId == supplierId && rm.RawMaterialType.AlloyType == alloyType && rm.RawMaterialType.RollType == rollType && rm.RawMaterialType.Thickness == thickness) .ToList(); if (!rawMaterial.Any()) { errors.Add($"Выбранный поставщик не производит материала с такими параметрами и толщиной {thickness}"); continue; } if (rawMaterial.Count > 1) { errors.Add( $"Выбранный поставщик производит более 1 материала с такими параметрами и толщиной {thickness}, невозможно определить нужный."); continue; } var priceItem = new PriceItem { RawMaterialId = rawMaterial[0].RawMaterialId, OwnerId = manufacturerId, Date = date, Price = remove ? null : (decimal?)Convert.ToDecimal(prices[i].FixDecimalSeparator()) }; rawMaterial[0].PriceItems = new[] { priceItem }; materials.Add(rawMaterial[0]); } } } } catch (Exception ex) { _logger.Error(ex); errors.Add("Ошибка синтаксического разбора строки."); } return new BulkMaterialParseResult { Errors = errors.ToArray(), Materials = errors.Any() ? new RawMaterial[0] : materials.ToArray() }; }
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() }; }
public IEnumerable<ProductModel> Get() { using (var storage = new Storage()) { return storage .Products .LoadWith(p => p.Manufacturer) .LoadWith(p => p.RawMaterial.RawMaterialType) .Select(p => new ProductModel { ProductId = p.ProductId, ManufacturerId = p.ManufacturerId, ManufacturerName = p.Manufacturer.Name, RawMaterialTypeId = p.RawMaterial.RawMaterialTypeId, RawMaterialTypeName = p.RawMaterial.RawMaterialType.Name, Thickness = p.Thickness, }) .ToList(); } }
public IHttpActionResult Delete(int id) { Argument.Require(id > 0, "Product id is required."); using (var storage = new Storage()) { storage .Products .Delete(p => p.ProductId == id); } return Ok(); }