public static ResultObject GetMostProfitableList(int?month) { try { if (month == null) { throw new BusinessException(Errors.Codes.DateEmpty); } ABCEntities context = new ABCEntities(); List <MODEL_DETAILS> modelsDetails = context.MODEL_DETAILS.AsNoTracking().ToList(); List <ModelWithCostAndProfit> modelsWithCostAndProfit = GeneralWrapper.CalculateCostsAndProfits(month.Value, modelsDetails); ModelWithCostAndProfit modelWithMostProfit = modelsWithCostAndProfit.OrderByDescending(m => m.Profit).FirstOrDefault(); List <ModelWithCostAndProfit> result = new List <ModelWithCostAndProfit>(); if (modelWithMostProfit != null) { result = modelsWithCostAndProfit.Where(m => m.Profit == modelWithMostProfit.Profit).ToList(); } return(BasicWrapper.SuccessResult(result)); } catch (BusinessException exp) { return(BasicWrapper.ErrorResult(exp.Code, exp.Message)); } catch (Exception exp) { return(BasicWrapper.GeneralErrorResult()); } }
public static List <ModelWithCostAndProfit> CalculateCostsAndProfits(int month, List <MODEL_DETAILS> models) { List <ModelWithCostAndProfit> result = new List <ModelWithCostAndProfit>(); if (models == null) { return(result); } foreach (var m in models) { var modelWithCostAndProfit = new ModelWithCostAndProfit() { ColorId = m.COLOR_ID, ColorName = m.COLOR_NAME, Id = m.MODEL_ID, IsConvertible = m.MODEL_CONVERTIBLE, TypeId = m.TYPE_ID, TypeName = m.TYPE_NAME, Cost = m.TYPE_COST, Price = m.TYPE_COST * (1 + Constants.DefaultProfitRatio) }; decimal offersAndPremiums = 1; if (Constants.OfferRedCarsMonths.Contains(month) && modelWithCostAndProfit.ColorId == Constants.ColorRedId) { offersAndPremiums -= Constants.OfferRedCarsRatio; } if (Constants.PremiumConvertiblesMonths.Contains(month) && modelWithCostAndProfit.IsConvertible > 0) { offersAndPremiums += Constants.PremiumConvertiblesRatio; } if (Constants.PremiumSuvMonths.Contains(month) && modelWithCostAndProfit.TypeId == Constants.TypeSuvId) { offersAndPremiums += Constants.PremiumSuvRatio; } modelWithCostAndProfit.Price *= offersAndPremiums; modelWithCostAndProfit.Profit = modelWithCostAndProfit.Price - modelWithCostAndProfit.Cost; result.Add(modelWithCostAndProfit); } return(result); }