private long SaveNewPromotion(PromotionUi model) { var file = SaveFile(model.File); var regionMask = model.RegionList.Select(x => (ulong)Convert.ToInt64(x)).Aggregate((y, z) => y | z); if (file == null) { if (model.PromotionFileId != null) { file = DB2.MediaFiles.Find(model.PromotionFileId.Value); } } var promotion = new Promotion(CurrentUser) { Name = model.Name, Annotation = model.Annotation, Begin = Convert.ToDateTime(model.Begin), End = Convert.ToDateTime(model.End), Author = DB2.Users.Find(CurrentUser.Id), MediaFile = file, RegionMask = (long)regionMask, }; DB2.Promotions.Add(promotion); DB2.SaveChanges(); foreach (var item in model.DrugList) { var promotionToDrug = new PromotionToDrug() { DrugId = Convert.ToInt64(item), PromotionId = promotion.Id }; DB2.PromotionToDrugs.Add(promotionToDrug); } if (model.SuppierRegions.Contains("0")) { model.SuppierRegions = h.GetSupplierList(model.SuppierRegions.ToList().Select(x => (ulong)Convert.ToInt64(x)).ToList()).ToList().Select(x => x.Value).ToList(); promotion.AllSuppliers = true; } else { promotion.AllSuppliers = false; } foreach (var item in model.SuppierRegions) { var X = new PromotionsToSupplier() { PromotionId = promotion.Id, SupplierId = Convert.ToInt64(item) }; DB2.PromotionsToSuppliers.Add(X); } DB2.PromotionHistory.Add(new PromotionSnapshot(CurrentUser, promotion, DB, DB2) { SnapshotName = "Добавление промоакции", }); DB2.SaveChanges(); Mails.PromotionNotification(MailType.CreatePromotion, promotion); return(promotion.Id); }
private long ChangePromotion(PromotionUi model) { var promotion = DB2.Promotions.Find(model.Id); var promotionToDrug = promotion.PromotionToDrug.ToList(); foreach (var item in promotionToDrug) { var drugExsist = model.DrugList.Any(x => Convert.ToInt64(x) == item.DrugId); if (!drugExsist) { DB2.PromotionToDrugs.Remove(item); } } foreach (var item in model.DrugList) { var drugExsist = promotion.PromotionToDrug.Any(x => x.DrugId == Convert.ToInt64(item)); if (!drugExsist) { PromotionToDrug newAddDrug = new PromotionToDrug() { DrugId = Convert.ToInt64(item), PromotionId = promotion.Id }; DB2.PromotionToDrugs.Add(newAddDrug); } } var promoPromotionsToSupplier = promotion.PromotionsToSupplier.ToList(); foreach (var item in promoPromotionsToSupplier) { var supllierExsist = model.SuppierRegions.Any(x => (ulong)Convert.ToInt64(x) == (ulong)item.SupplierId); if (!supllierExsist) { DB2.PromotionsToSuppliers.Remove(item); } } if (model.SuppierRegions.Contains("0")) { model.SuppierRegions = h.GetSupplierList(model.SuppierRegions.ToList().Select(x => (ulong)Convert.ToInt64(x)).ToList()).ToList().Select(x => x.Value).ToList(); promotion.AllSuppliers = true; } else { promotion.AllSuppliers = false; } foreach (var item in model.SuppierRegions) { var supllierExsist = promotion.PromotionsToSupplier.Any(x => (ulong)x.SupplierId == (ulong)Convert.ToInt64(item)); if (!supllierExsist) { var addNew = new PromotionsToSupplier() { SupplierId = Convert.ToInt64(item), PromotionId = promotion.Id }; promotion.PromotionsToSupplier.Add(addNew); } } ulong regionMask = 0; if (model.RegionList.Count() == 1) { regionMask = (ulong)Convert.ToInt64(model.RegionList.First()); } else { regionMask = model.RegionList.Select(x => (ulong)Convert.ToInt64(x)).Aggregate((y, z) => y | z); } promotion.RegionMask = (long)regionMask; promotion.Name = model.Name; promotion.Annotation = model.Annotation; promotion.Begin = Convert.ToDateTime(model.Begin); promotion.End = Convert.ToDateTime(model.End); promotion.Enabled = true; promotion.Status = PromotionStatus.New; promotion.Author = DB2.Users.Find(CurrentUser.Id); var file = SaveFile(model.File); if (file != null) { promotion.MediaFile = file; } else if (model.PromotionFileId != null) { promotion.MediaFile = DB2.MediaFiles.Find(model.PromotionFileId.Value); } DB2.PromotionHistory.Add(new PromotionSnapshot(CurrentUser, promotion, DB, DB2)); DB2.SaveChanges(); Mails.PromotionNotification(MailType.EditPromotion, promotion); return(promotion.Id); }