/// <summary> /// Performs the extraction and returns the result. /// </summary> /// <param name="info">Specifics of the information required for the extraction.</param> /// <param name="user_id">ID of the user requesting the extraction.</param> /// <returns></returns> public EndResultTichur Execute(TichurInfo info, string user_id) { Cache.gen_lock.WaitOne(); TablePullResult TRE; // Attempting extraction try { PullSuppList pull = new ExtractSuppList(); TRE = pull.TichurExtract(info); } // Extraction failed, catching cause of error catch (Exception e) { EndResultTichur res = new EndResultTichur(); res.Status = "error"; res.data = new List <string[]>(); res.data.Add(new string[] { e.Message }); Cache.gen_lock.ReleaseMutex(); return(res); } // Assuming extraction completed successfully, // Captures the data and updates the database entity accordingly before returning. using (Database.TimchurDatabaseEntities ent = new Database.TimchurDatabaseEntities()) { DateTime tic_date = DateTime.Now; try { foreach (string sup in TRE.table) { int sid = Int32.Parse(sup); Suppliers supa = ent.Suppliers.Where(x => x.ID == sid).First(); SuppliersClusters scl = supa.SuppliersClusters.Where(x => x.ClusterID == info.ClusterID).First(); scl.FormarLastTimeInList = scl.LastTimeInList; scl.LastTimeInList = tic_date; } Users user = ent.Users.Where(x => x.IDCardNumber == user_id).First(); Tichurim tichur = new Tichurim(); tichur.UnitID = info.UnitID; tichur.ClusterID = info.ClusterID; tichur.TichurNumber = info.TichurNumber; tichur.StatusID = 1; tichur.DateTimeCreated = DateTime.Now; tichur.DateTimeSelected = DateTime.Now; tichur.DateTimeUpdated = DateTime.Now; tichur.CreatedUserID = user.ID; tichur.UpdatedUserID = user.ID; tichur.UpdatedComment = "Created"; ent.Tichurim.Add(tichur); ent.SaveChanges(); } catch (Exception e) { Trace.Write(e.Message); } } EndResultTichur res2 = new EndResultTichur(); res2.Status = "K"; res2.data = new List <string[]>(); using (Database.TimchurDatabaseEntities ent2 = new Database.TimchurDatabaseEntities()) { try { int i = 0; Tichurim tich = ent2.Tichurim.Where(x => x.TichurNumber == info.TichurNumber && x.StatusID == 1).First(); foreach (string supp in TRE.table) { int sup_id = Int32.Parse(supp); SuppliersTichurim suptic = new SuppliersTichurim(); suptic.TichurID = tich.ID; suptic.SupplierID = sup_id; suptic.PositionInList = byte.Parse(i.ToString()); Suppliers supa = ent2.Suppliers.Where(x => x.ID == sup_id).First(); res2.data.Add(new string[] { i.ToString(), suptic.PositionInList.Value.ToString(), tich.Units.Name, tich.Clusters.Auctions.AuctionNumber, tich.Clusters.Auctions.Name, tich.TichurNumber, tich.Clusters.DisplayNumber.Value.ToString(), tich.Clusters.Name, supa.Name, supa.CompanyNumber, supa.ContactName, supa.EmailAddress, supa.PhoneNumber, tich.DateTimeCreated.Value.ToString("yyyy:MM:dd:HH:mm:ss") }); ent2.SuppliersTichurim.Add(suptic); i++; } ent2.SaveChanges(); } catch (Exception e) { Trace.Write(e.Message); } } Cache.gen_lock.ReleaseMutex(); return(res2); }
public TablePullResult TichurAlgorithem(TichurInfo input) { using (TimchurDatabaseEntities ent = new TimchurDatabaseEntities()) { // Checking for duplicate tichurs Database.Tichurim check = ent.Tichurim.Where(x => x.TichurNumber == input.TichurNumber).FirstOrDefault(); if (ent.Tichurim.Where(x => x.TichurNumber == input.TichurNumber).Count() != 0 && check.UnitID == input.UnitID && check.StatusID == 1) { throw new DuplicateKeyException(check); } TablePullResult res = new TablePullResult(); int suppliersRemaining = 0; if (ent.Clusetrs.Where(x => x.ID == input.CluestrID).FirstOrDefault() != null) { suppliersRemaining = ent.Clusetrs.Where(x => x.ID == input.CluestrID).FirstOrDefault().SuppliersInTichur.Value; } var sortByUsagePre = (from sup in ent.Suppliers join supcluc in ent.SuppliersClusetrs on sup.ID equals supcluc.SupplierID where supcluc.ClusetrID == input.CluestrID && sup.StatusID == 1 orderby supcluc.LastTimeInList select new { Sup = sup, SupCl = supcluc }); var sortByUsage = sortByUsagePre.AsExpandable().GroupBy(x => DbFunctions.AddMilliseconds(x.SupCl.LastTimeInList, -x.SupCl.LastTimeInList.Value.Millisecond)).OrderBy(x => x.Key); //Date-grouped iterator var itr = sortByUsage.GetEnumerator(); itr.MoveNext(); var rnd = new Random(); while (suppliersRemaining > 0) { //oldest group of suppliers var dateGroup = itr.Current; //Need to pick a random subset of the suppliers if (dateGroup.Count() < suppliersRemaining) { var shuffled = dateGroup.OrderBy(i => rnd.Next()); // Going over the shuffled list until enough are extracted foreach (var supplier in shuffled) { res.table.Add(supplier.Sup.ID.ToString()); if (--suppliersRemaining == 0) { break; } } itr.MoveNext(); } else { //Need at least one entire dategroup - adding all suppliers in current set. foreach (var supplier in dateGroup) { res.table.Add(supplier.Sup.ID.ToString()); } suppliersRemaining -= dateGroup.Count(); itr.MoveNext(); } } return(res); } }
public EndResultTichur Execute(TichurInfo info, string user_id) { Cache.gen_lock.WaitOne(); TablePullResult TRE; try { PullSuppList pull = new ExtractSuppList(); TRE = pull.TichurAlgorithem(info); } catch (Exception e) { EndResultTichur res = new EndResultTichur(); res.Status = "error"; res.data = new List <string[]>(); res.data.Add(new string[] { "תיחור כבר קיים בתוך המערכת" }); Cache.gen_lock.ReleaseMutex(); return(res); } using (Database.TimchurDatabaseEntities ent = new Database.TimchurDatabaseEntities()) { DateTime tic_date = DateTime.Now; try { foreach (string sup in TRE.table) { int sid = Int32.Parse(sup); Suppliers supa = ent.Suppliers.Where(x => x.ID == sid).First(); SuppliersClusetrs scl = supa.SuppliersClusetrs.Where(x => x.ClusetrID == info.CluestrID).First(); scl.FormarLastTimeInList = scl.LastTimeInList; scl.LastTimeInList = tic_date; } Users user = ent.Users.Where(x => x.IDCardNumber == user_id).First(); Tichurim tichur = new Tichurim(); tichur.UnitID = info.UnitID; tichur.ClusterID = info.CluestrID; tichur.TichurNumber = info.TichurNumber; tichur.StatusID = 1; tichur.DateTimeCreated = DateTime.Now; tichur.DateTimeSelected = DateTime.Now; tichur.DateTimeUpdated = DateTime.Now; tichur.CreatedUserID = user.ID; tichur.UpdatedUserID = user.ID; tichur.UpdatedComment = "Created"; ent.Tichurim.Add(tichur); ent.SaveChanges(); } catch (Exception e) { } } EndResultTichur res2 = new EndResultTichur(); res2.Status = "K"; res2.data = new List <string[]>(); using (Database.TimchurDatabaseEntities ent2 = new Database.TimchurDatabaseEntities()) { try { int i = 0; Tichurim tich = ent2.Tichurim.Where(x => x.TichurNumber == info.TichurNumber && x.StatusID == 1).First(); foreach (string supp in TRE.table) { int sup_id = Int32.Parse(supp); SuppliersTichurim suptic = new SuppliersTichurim(); suptic.TichurID = tich.ID; suptic.SupplierID = sup_id; suptic.PositionInList = byte.Parse(i.ToString()); Suppliers supa = ent2.Suppliers.Where(x => x.ID == sup_id).First(); res2.data.Add(new string[] { i.ToString(), suptic.PositionInList.Value.ToString(), tich.Units.Name, tich.Clusetrs.Auctions.AuctionNumber, tich.Clusetrs.Auctions.Name, tich.TichurNumber, tich.Clusetrs.DisplayNumber.Value.ToString(), tich.Clusetrs.Name, supa.Name, supa.CompanyNumber, supa.ContactName, supa.EmailAddress, supa.PhoneNumber, tich.DateTimeCreated.ToString() }); ent2.SuppliersTichurim.Add(suptic); i++; } ent2.SaveChanges(); } catch (Exception e) { } } Cache.gen_lock.ReleaseMutex(); return(res2); }