public Guid AddNewProduct(Guid parentProduct, bool withDictNomen, Guid dictNomenID, Guid UserID) { Product parent = Products.SingleOrDefault(p => p.ID == parentProduct); List<ClassificationTreeProduct> trees = parent.ClassificationTreeProducts.ToList(); Guid id = Guid.NewGuid(); if (parent != null) { //insert product entity string sql; if (withDictNomen) { sql = "INSERT INTO Product(ID,Name,CreatedDate,_dictNomenID)VALUES('{0}','{1}',CONVERT(datetime, '{2}', 120),'{3}')"; sql = string.Format(sql, id, parent.PublicName.Trim(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), Guid.Empty.Equals(dictNomenID) ? parent._dictNomenID : dictNomenID); } else { sql = "INSERT INTO Product(ID,Name,CreatedDate)VALUES('{0}','{1}',CONVERT(datetime, '{2}', 120))"; sql = string.Format(sql, id, parent.PublicName.Trim(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); } using (CommonDataProvider provider = new CommonDataProvider()) { provider.ExecuteNonQuery(sql); } //add to global classification tree List<ClassificationTreeProduct> classif = new List<ClassificationTreeProduct>(); foreach (ClassificationTreeProduct item in trees) { ClassificationTreeProduct entity = new ClassificationTreeProduct() { ClassificationTreeID = item.ClassificationTreeID, ID = Guid.NewGuid(), ProductID = id }; classif.Add(entity); } this.ClassificationTreeProducts.InsertAllOnSubmit(classif); this.SubmitChanges(); return id; } return Guid.Empty; }
/// <summary> /// Перемещение продукта из класса (МатРесурсы) в заданный класс. /// </summary> /// <param name="dictNomenID"></param> /// <param name="newClassificationTreeID"></param> public void ChangeProductClassification(Guid dictNomenID, Guid newClassificationTreeID) { // находим единственный продукт, соответствующий данной номенклатуре // если продуктов несколько вырабатается исключение Guid productID = Products.Where(p => p._dictNomenID == dictNomenID).Select(p => p.ID).Single(); // удалям продукт из класса (МатРесурсы) ClassificationTreeProducts.DeleteOnSubmit( ClassificationTreeProducts.Where(cls => cls.ProductID == productID && cls.ClassificationTreeID == new Guid("55C7B455-0638-4ACB-AC2E-5B4992E48462")).Single()); // вставляем продукт в новый класс ClassificationTreeProduct newClassification = new ClassificationTreeProduct(); newClassification.ID = Guid.NewGuid(); newClassification.ProductID = productID; newClassification.ClassificationTreeID = newClassificationTreeID; ClassificationTreeProducts.InsertOnSubmit(newClassification); // подтверждаем изменения SubmitChanges(); }