Exemplo n.º 1
0
        internal MetersDataForChartDTO GetMeterDataHistoryForChart(int addressId, int bill_Id)
        {
            using (var context = new TownUtilityBillSystemV2Entities())
            {
                var metersDB = context.METERs.Where(m => m.ADDRESS_ID == addressId).ToList();
                var billDB   = context.BILLs.FirstOrDefault(b => b.ID == bill_Id);

                MetersDataForChartDTO metersDataDTO = new MetersDataForChartDTO();
                List <MeterItem>      meterItemList = new List <MeterItem>();

                float  valueDifference;
                string monthName;

                DateTime startDate          = new DateTime();
                DateTime startDateForChart  = new DateTime();
                DateTime finishDateForChart = new DateTime();

                startDate = Convert.ToDateTime(billDB.PERIOD + "-01");

                int monthDifferenceBillPeriodAndStartUsageChart = 1;
                int presChartYear  = startDate.Year;
                int startChartDay  = 1;
                int prevChartYear  = presChartYear - 1;
                int presChartMonth = startDate.Month;

                foreach (var m in metersDB)
                {
                    List <ChartData> chartData = new List <ChartData>();
                    var     meterTypeDB        = context.METER_TYPEs.Where(mt => mt.ID == m.METER_TYPE_ID).FirstOrDefault();
                    var     utilityDB          = context.UTILITYs.Where(u => u.ID == meterTypeDB.UTILITY_ID).FirstOrDefault();
                    Utility utility;

                    if (utilityDB.USAGEFORSTANDARTPRICE != null && utilityDB.BIGUSAGEPRICE != null)
                    {
                        utility = Utility.GetUtilityWithBigUsagePrice(utilityDB);
                    }
                    else
                    {
                        utility = Utility.GetUtilityWithOutBigUsagePrice(utilityDB);
                    }

                    meterItemList.Clear();

                    startDateForChart  = new DateTime(prevChartYear, presChartMonth + monthDifferenceBillPeriodAndStartUsageChart, startChartDay);
                    finishDateForChart = startDate.AddMonths(1);

                    meterItemList = context.METER_ITEMs.Where(mi => mi.METER_ID == m.ID).Select(MeterItem.GetMeterItemWithOutMeter).ToList();

                    if (meterItemList.Count != 0)
                    {
                        for (; startDateForChart <= finishDateForChart; startDateForChart = startDateForChart.AddMonths(1))
                        {
                            var startElValue  = meterItemList.FirstOrDefault(ml => ml.Date == startDateForChart.AddMonths(-1)).Value;
                            var finishElValue = meterItemList.FirstOrDefault(ml => ml.Date == startDateForChart).Value;
                            valueDifference = (float)Math.Round(finishElValue - startElValue, 2);

                            monthName = HelperMethod.GetMonthNameAndYear(startDateForChart);

                            chartData.Add(new ChartData()
                            {
                                MonthName = monthName, Value = valueDifference
                            });
                        }
                    }

                    metersDataDTO.MetersChartData.Add(chartData);
                    metersDataDTO.UtilityResourceNames.Add(utility.ResourceName);
                    metersDataDTO.UnitNames.Add(utility.Unit.Name);
                }

                return(metersDataDTO);
            }
        }