public async Task <JsonResult> GetReportMonthByType(string day = "", int?deviceId = null, string type = "")
        {
            double   total1 = 0, total2 = 0, total3 = 0;
            int      count1 = 0, count2 = 0, count3 = 0;
            DateTime date = DateTime.Today;

            if (day != "")
            {
                try
                {
                    date = Convert.ToDateTime(day);
                }
                catch { }
            }
            DateTime distance1 = new DateTime(date.Year, date.Month, 1);
            DateTime distance2 = new DateTime(date.Year, date.Month, 10, 23, 59, 59);
            DateTime distance3 = new DateTime(date.Year, date.Month, 11);
            DateTime distance4 = new DateTime(date.Year, date.Month, 20, 23, 59, 59);
            DateTime distance5 = new DateTime(date.Year, date.Month, 21);
            DateTime distance6 = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month), 23, 59, 59);
            var      result    = await Task.WhenAll(dataObservationMongoService.GetDatayDeviceId(distance1, distance2, deviceId.Value)
                                                    , dataObservationMongoService.GetDatayDeviceId(distance3, distance4, deviceId.Value)
                                                    , dataObservationMongoService.GetDatayDeviceId(distance5, distance6, deviceId.Value));

            foreach (var item in result[0].ToList())
            {
                total1 = Total(item, type.Trim(), total1);
                count1++;
            }
            double tb1 = total1 / count1;

            foreach (var item in result[1].ToList())
            {
                total2 = Total(item, type.Trim(), total2);
                count2++;
            }
            double tb2 = total2 / count2;

            foreach (var item in result[2].ToList())
            {
                total3 = Total(item, type.Trim(), total3);
                count3++;
            }
            double             tb3        = total3 / count3;
            ReportType         reportType = reportTypeService.GetByCode(type).FirstOrDefault();
            Site               site       = sitesService.GetByDeviceId(deviceId).FirstOrDefault();
            ReportMonthlyModel model      = new ReportMonthlyModel();

            model.Distance1  = tb1;
            model.Distance2  = tb2;
            model.Distance3  = tb3;
            model.Title      = reportType == null ? "" : reportType.Title;
            model.Measure    = reportType == null ? "" : reportType.Measure;
            model.Name       = reportType == null ? "" : reportType.Description + " " + (site == null ? "" : site.Name);
            model.dayInMonth = DateTime.DaysInMonth(date.Year, date.Month);
            return(Json(new { model }, JsonRequestBehavior.AllowGet));
        }
Esempio n. 2
0
    //lấy tên đơn vị theo user ID

    public async Task <ReportMonthlyModel> GetReportByMonth(int deviceId, string type, string Date = "")
    {
        var                QICache    = new Cache_Provider();
        string             strSession = "GetReportByMonth_" + deviceId + type + Date;
        ReportMonthlyModel model      = new ReportMonthlyModel();

        if (!QICache.IsSet(strSession))
        {
            double   total1 = 0, total2 = 0, total3 = 0;
            int      count1 = 0, count2 = 0, count3 = 0;
            DateTime date = DateTime.Today;
            if (Date != "")
            {
                try
                {
                    date = Convert.ToDateTime(Date);
                }
                catch { }
            }
            DateTime distance1 = new DateTime(date.Year, date.Month, 1);
            DateTime distance2 = new DateTime(date.Year, date.Month, 10, 23, 59, 59);
            DateTime distance3 = new DateTime(date.Year, date.Month, 11);
            DateTime distance4 = new DateTime(date.Year, date.Month, 20, 23, 59, 59);
            DateTime distance5 = new DateTime(date.Year, date.Month, 21);
            DateTime distance6 = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month), 23, 59, 59);
            var      result    = await Task.WhenAll(dataObservationMongoService.GetDatayDeviceId(distance1, distance2, deviceId)
                                                    , dataObservationMongoService.GetDatayDeviceId(distance3, distance4, deviceId)
                                                    , dataObservationMongoService.GetDatayDeviceId(distance5, distance6, deviceId));

            foreach (var item in result[0].ToList())
            {
                total1 = CMSHelper.Total(item, type.Trim(), total1);
                count1++;
            }
            double tb1 = total1 / count1;
            foreach (var item in result[1].ToList())
            {
                total2 = CMSHelper.Total(item, type.Trim(), total2);
                count2++;
            }
            double tb2 = total2 / count2;
            foreach (var item in result[2].ToList())
            {
                total3 = CMSHelper.Total(item, type.Trim(), total3);
                count3++;
            }
            double     tb3        = total3 / count3;
            ReportType reportType = reportTypeService.GetByCode(type).FirstOrDefault();
            Site       site       = sitesService.GetByDeviceId(deviceId).FirstOrDefault();
            model.Distance1  = tb1;
            model.Distance2  = tb2;
            model.Distance3  = tb3;
            model.Title      = reportType == null ? "" : reportType.Title;
            model.Measure    = reportType == null ? "" : reportType.Measure;
            model.Name       = reportType == null ? "" : reportType.Description + " " + (site == null ? "" : site.Name);
            model.dayInMonth = DateTime.DaysInMonth(date.Year, date.Month);
            QICache.Set(strSession, model, int.Parse(ConfigurationManager.AppSettings["timeout_cacheserver"]));
        }
        else
        {
            try
            {
                model = QICache.Get(strSession) as ReportMonthlyModel;
            }
            catch
            {
                QICache.Invalidate(strSession);
            }
        }
        return(model);
    }