private int GetFactorId(ScoringFactorEnum factorName) { using (var context = new BillingContext()) { var factor = context.Set <ScoringFactor>().AsNoTracking().FirstOrDefault(f => f.Code == factorName.ToString()); return(factor?.Id ?? 0); } }
private Task RaiseScoringEvent(int scoringId, int factorId, Func <BillingContext, decimal> action) { return(Task.Run(() => { try { using (var context = new BillingContext(true)) { using (var dbContextTransaction = context.Database.BeginTransaction()) { var connection = context.Database.GetDbConnection(); var id = connection.QueryFirstOrDefault <int>($"SELECT id FROM scoring WHERE id = {scoringId} FOR UPDATE;");//block scoring for updates var start = DateTime.Now; var lifestyle = action(context); var factor = context.Set <ScoringFactor>().AsNoTracking().FirstOrDefault(f => f.Id == factorId); var category = context.Set <ScoringCategory>().AsNoTracking().FirstOrDefault(f => f.Id == factor.CategoryId); var scoring = context.Set <Scoring>().AsTracking().FirstOrDefault(s => s.Id == scoringId); var systemsettings = IocContainer.Get <ISettingsManager>(); var oldScoring = scoring.CurerentRelative + scoring.CurrentFix; var curCategory = context.Set <CurrentCategory>().Include(f => f.Category) .FirstOrDefault(c => c.ScoringId == scoringId && c.CategoryId == factor.CategoryId); if (curCategory == null) { curCategory = new CurrentCategory { ScoringId = scoringId, CategoryId = factor.CategoryId, Category = category, Value = 1 }; Add(curCategory, context); } var curFactor = context.Set <CurrentFactor>() .FirstOrDefault(s => s.CurrentCategoryId == curCategory.Id && s.ScoringFactorId == factorId); if (curFactor == null) { curFactor = new CurrentFactor { ScoringFactorId = factorId, CurrentCategoryId = curCategory.Id, Value = scoring.StartFactor ?? 1 }; Add(curFactor, context); } var oldFactorValue = curFactor.Value; var newValue = CalculateFactor((double)lifestyle, (double)curFactor.Value); curFactor.Value = newValue; Add(curFactor, context); var curFactors = context.Set <CurrentFactor>().AsNoTracking().Include(f => f.ScoringFactor) .Where(f => f.CurrentCategoryId == curCategory.Id).ToList(); var allCates = context.Set <CurrentCategory>() .Where(c => c.ScoringId == scoringId && c.Category.CategoryType == category.CategoryType && c.CurrentFactors.Count > 0).ToList(); var factorsCount = curFactors.Count; if (factorsCount == 0) { factorsCount = 1; } var oldCatValue = curCategory.Value; var curCatCount = allCates.Count; var k = (decimal)Math.Pow((curCatCount > 0 ? curCatCount : 2) * 2, -1); var averFactors = curFactors.Sum(f => f.Value) / factorsCount; var catWeight = curCategory?.Category?.Weight; curCategory.Value = (decimal)Math.Pow((double)averFactors, (double)GetCatWeight(catWeight ?? 0)); Add(curCategory, context); var newCatValue = curCategory.Value; if (category.CategoryType == (int)ScoringCategoryType.Fix) { scoring.CurrentFix = allCates.Sum(c => c.Value) * k; } else if (category.CategoryType == (int)ScoringCategoryType.Relative) { scoring.CurerentRelative = allCates.Sum(c => c.Value) * k; } Add(scoring, context); var end = DateTime.Now; var scoringEvent = new ScoringEvent { FinishTime = end, StartTime = start, CurrentFactor = curFactor, OldFactorValue = oldFactorValue, NewFactorValue = newValue, OldCategoryValue = oldCatValue, NewCategoryValue = newCatValue, OldScoring = oldScoring, NewScoring = scoring.CurerentRelative + scoring.CurrentFix, SaveK = k, AverFactors = averFactors }; Add(scoringEvent, context); dbContextTransaction.Commit(); } } } catch (Exception e) { Console.Error.WriteLine(e.ToString()); } })); }
public BillingRepository(BillingContext _context) { context = _context; dbSet = context.Set <Entity>(); }