Exemplo n.º 1
0
        /// <summary>
        /// 计算分控能耗数据
        /// </summary>
        /// <param name="SubId">分控主键</param>
        /// <param name="OrganizeId">分控所属的组织机构</param>
        /// <param name="value">能耗值</param>
        private void AddEnergyConsumption(Guid SubId, Guid OrganizeId, double value)
        {
            try
            {
                /*===================================================
                * 备注:1、分控上传的能耗信息将一直累计
                *       2、分控如果坏了,需更换则能耗清空
                *  ===================================================*/

                // 获取能耗类型主键=='分控能耗类型'
                var ditem = DataItemDetailRepository.Entities.FirstOrDefault(m => m.QueryCoding == "ECType" && m.Index == 1);

                // 获取去年的能耗记录是否存在
                var _LastYearEct = AnnualElectricityRepository.TrackEntities
                                   .FirstOrDefault(m => m.ObjectId == SubId && m.DataItemDetail_Id == ditem.Id && m.Year == (DateTime.Now.Year - 1));
                // 获取当年的能耗记录是否存在
                var _CurrentYearEct = AnnualElectricityRepository.TrackEntities
                                      .FirstOrDefault(m => m.ObjectId == SubId && m.DataItemDetail_Id == ditem.Id && m.Year == DateTime.Now.Year);

                if (_CurrentYearEct == null /*当年的能耗记录为空,创建新的能耗记录*/)
                {
                    // 创建当前年的能耗记录
                    _CurrentYearEct = new AnnualElectricity
                    {
                        CreatedTime       = DateTime.Now,
                        UpdateTime        = DateTime.Now,
                        DataItemDetail_Id = ditem.Id,
                        Year        = DateTime.Now.Year,
                        ObjectId    = SubId,
                        Organzie_Id = OrganizeId,
                    };

                    // 创建当前月的能耗记录
                    MonthElectricity monthEct = new MonthElectricity
                    {
                        AnnualElectricityOne = _CurrentYearEct,
                        CreatedTime          = DateTime.Now,
                        Month      = DateTime.Now.Month,
                        UpdateTime = DateTime.Now
                    };

                    // 创建当前天的能耗记录
                    DayElectricity dayEct = new DayElectricity
                    {
                        UpdateTime          = DateTime.Now,
                        CreatedTime         = DateTime.Now,
                        MonthElectricityOne = monthEct,
                        Today = DateTime.Now.Day,
                    };

                    if (_LastYearEct == null /*去年能耗不存在,表示是全新的分控*/)
                    {
                        _CurrentYearEct.Cumulative          = value;
                        _CurrentYearEct.YearTotal           = value;
                        _CurrentYearEct[DateTime.Now.Month] = value;

                        monthEct.MonthTotal        = value;
                        monthEct[DateTime.Now.Day] = value;

                        dayEct.DayTotal           = value;
                        dayEct[DateTime.Now.Hour] = value;
                    }
                    else /*去年能耗存在,表示这是一个老分控,已经经过一年了,进行数据运算*/
                    {
                        if ((_LastYearEct.TagCumulative == -1 && _LastYearEct.Cumulative > value) || (_LastYearEct.TagCumulative != -1 && _LastYearEct.TagCumulative > value))
                        {   // 分控上传的累计能耗比存储数据的能耗记录小_CurrentYearEct
                            // 1.可能分控坏了,刚好过完一年更换了新的分控
                            _CurrentYearEct.YearTotal           = value;
                            _CurrentYearEct.Cumulative         += value;
                            _CurrentYearEct.TagCumulative       = value; //分控被更换,计算新的累计值
                            _CurrentYearEct[DateTime.Now.Month] = value;

                            monthEct.MonthTotal        = value;
                            monthEct[DateTime.Now.Day] = value;

                            dayEct.DayTotal           = value;
                            dayEct[DateTime.Now.Hour] = value;
                        }
                        else
                        {
                            double xj = value - _LastYearEct.Cumulative;
                            if (_LastYearEct.TagCumulative != -1)
                            {
                                xj = value - _LastYearEct.TagCumulative;
                            }
                            xj = xj > 0 ? xj : 0;
                            _CurrentYearEct.YearTotal           = xj;
                            _CurrentYearEct.Cumulative          = _LastYearEct.Cumulative + xj;
                            _CurrentYearEct.TagCumulative       = _LastYearEct.TagCumulative += xj;
                            _CurrentYearEct[DateTime.Now.Month] = xj;

                            monthEct.MonthTotal        = xj;
                            monthEct[DateTime.Now.Day] = xj;

                            dayEct.DayTotal           = xj;
                            dayEct[DateTime.Now.Hour] = xj;
                        }
                    }

                    // 关联能耗数据
                    monthEct.DayElectricityMany.Add(dayEct);
                    _CurrentYearEct.MonthElectricityMany.Add(monthEct);
                    AnnualElectricityRepository.Insert(_CurrentYearEct);
                }
                else /*==今年能耗存在,计算更新==*/
                {
                    var monthEct = _CurrentYearEct.MonthElectricityMany.FirstOrDefault(m => m.Month == DateTime.Now.Month);
                    if ((_CurrentYearEct.TagCumulative == -1 && _CurrentYearEct.Cumulative > value) || (_CurrentYearEct.TagCumulative != -1 && _CurrentYearEct.TagCumulative > value))
                    {
                        _CurrentYearEct.TagCumulative        = value;
                        _CurrentYearEct.YearTotal           += value;
                        _CurrentYearEct.Cumulative          += value;
                        _CurrentYearEct[DateTime.Now.Month] += value;
                        _CurrentYearEct.UpdateTime           = DateTime.Now;
                        if (monthEct == null)
                        {
                            monthEct = new MonthElectricity
                            {
                                Month                = DateTime.Now.Month,
                                CreatedTime          = DateTime.Now,
                                AnnualElectricityOne = _CurrentYearEct,
                                UpdateTime           = DateTime.Now,
                                MonthTotal           = value,
                            };
                            monthEct[DateTime.Now.Day] = value;

                            DayElectricity dayEct = new DayElectricity
                            {
                                UpdateTime          = DateTime.Now,
                                CreatedTime         = DateTime.Now,
                                DayTotal            = value,
                                MonthElectricityOne = monthEct,
                                Today = DateTime.Now.Day,
                            };
                            dayEct[DateTime.Now.Hour] = value;

                            monthEct.DayElectricityMany.Add(dayEct);
                            _CurrentYearEct.MonthElectricityMany.Add(monthEct);
                            AnnualElectricityRepository.Update(_CurrentYearEct);
                        }
                        else
                        {
                            monthEct.Tag                = DateTime.Now.Day;
                            monthEct.LastTagValue       = monthEct.MonthTotal;
                            monthEct.MonthTotal        += value;
                            monthEct[DateTime.Now.Day] += value;
                            monthEct.UpdateTime         = DateTime.Now;

                            var dayEct = monthEct.DayElectricityMany.FirstOrDefault(m => m.Today == DateTime.Now.Day);
                            if (dayEct == null)
                            {
                                dayEct = new DayElectricity
                                {
                                    DayTotal            = value,
                                    CreatedTime         = DateTime.Now,
                                    MonthElectricityOne = monthEct,
                                    Today      = DateTime.Now.Day,
                                    UpdateTime = DateTime.Now
                                };
                                dayEct[DateTime.Now.Hour] = value;
                                monthEct.DayElectricityMany.Add(dayEct);
                                AnnualElectricityRepository.Update(_CurrentYearEct);
                            }
                            else
                            {
                                dayEct.DayTotal           += value;
                                dayEct.UpdateTime          = DateTime.Now;
                                dayEct[DateTime.Now.Hour] += value;
                                AnnualElectricityRepository.Update(_CurrentYearEct);
                            }
                        }
                    }
                    else
                    {
                        double ob = value - _CurrentYearEct.Cumulative;
                        if (_CurrentYearEct.TagCumulative != -1)
                        {
                            ob = value - _CurrentYearEct.TagCumulative;
                            _CurrentYearEct.TagCumulative = value;
                        }
                        ob = ob > 0 ? ob : 0;
                        _CurrentYearEct[DateTime.Now.Month] += ob;
                        _CurrentYearEct.YearTotal           += ob;
                        _CurrentYearEct.Cumulative          += ob;
                        _CurrentYearEct.UpdateTime           = DateTime.Now;

                        if (monthEct == null)
                        {
                            monthEct = new MonthElectricity
                            {
                                AnnualElectricityOne = _CurrentYearEct,
                                CreatedTime          = DateTime.Now,
                                UpdateTime           = DateTime.Now,
                                Month      = DateTime.Now.Month,
                                MonthTotal = ob,
                            };
                            monthEct[DateTime.Now.Day] = ob;

                            DayElectricity dayEct = new DayElectricity
                            {
                                UpdateTime          = DateTime.Now,
                                CreatedTime         = DateTime.Now,
                                MonthElectricityOne = monthEct,
                                Today    = DateTime.Now.Day,
                                DayTotal = ob,
                            };
                            dayEct[DateTime.Now.Hour] = ob;

                            monthEct.DayElectricityMany.Add(dayEct);
                            _CurrentYearEct.MonthElectricityMany.Add(monthEct);
                            AnnualElectricityRepository.Update(_CurrentYearEct);
                        }
                        else
                        {
                            monthEct[DateTime.Now.Day] += ob;
                            monthEct.MonthTotal        += ob;
                            monthEct.UpdateTime         = DateTime.Now;

                            var dayEct = monthEct.DayElectricityMany.FirstOrDefault(m => m.Today == DateTime.Now.Day);

                            if (dayEct == null)
                            {
                                dayEct = new DayElectricity
                                {
                                    DayTotal            = ob,
                                    CreatedTime         = DateTime.Now,
                                    UpdateTime          = DateTime.Now,
                                    MonthElectricityOne = monthEct,
                                    Today = DateTime.Now.Day
                                };
                                dayEct[DateTime.Now.Hour] = ob;
                                monthEct.DayElectricityMany.Add(dayEct);
                                AnnualElectricityRepository.Update(_CurrentYearEct);
                            }
                            else
                            {
                                dayEct[DateTime.Now.Hour] += ob;
                                dayEct.DayTotal           += ob;
                                dayEct.UpdateTime          = DateTime.Now;
                                AnnualElectricityRepository.Update(_CurrentYearEct);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // 如果报错,保存错误日志
                LogManager.GetLogger("AddEnergyConsumption_Sub").Error(ex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 添加或更新主机的能耗值
        /// </summary>
        /// <param name="HostId">主机主键</param>
        /// <param name="OrganizeId">主机所属的组织机构Id</param>
        /// <param name="value">指定能耗值</param>
        private void AddEnergyConsumption(Guid HostId, Guid OrganizeId, double value)
        {
            try
            {
                /*===================================================
                * 备注:1、主机上传的能耗信息按月统计,每月清零
                *       2、主机上传的能耗值小于记录中的值,代表主机能耗可能被清零或更换
                *  ===================================================*/
                // 获取主机能耗类型主键==‘主机能耗类型’
                var ditem = DataItemDetailRepository.Entities.FirstOrDefault(m => m.QueryCoding == "ECType" && m.Index == 0);
                // 获取查询年能耗的查询仓储
                var _AnnualIQ = AnnualElectricityRepository.TrackEntities.Where(m => m.ObjectId == HostId && m.DataItemDetail_Id == ditem.Id);
                // 获取去年的年能耗记录
                var _LastYearEct = _AnnualIQ.FirstOrDefault(m => m.Year == (DateTime.Now.Year - 1));
                // 获取今年的能耗记录
                var _CurrentYeartEct = _AnnualIQ.FirstOrDefault(m => m.Year == DateTime.Now.Year);

                if (_CurrentYeartEct == null /*今年的能耗记录不存在,我们需要创建*/)
                {
                    // 按年统计,每月的能耗
                    AnnualElectricity annualElectricity = new AnnualElectricity
                    {
                        ObjectId          = HostId,
                        DataItemDetail_Id = ditem.Id,
                        Year        = DateTime.Now.Year,
                        UpdateTime  = DateTime.Now,
                        CreatedTime = DateTime.Now,
                        Organzie_Id = OrganizeId,
                        YearTotal   = value,
                    };
                    annualElectricity.Cumulative = value;
                    if (_LastYearEct != null /*去年能耗存在,则计算历史能耗累计数值*/)
                    {
                        annualElectricity.Cumulative += _LastYearEct.Cumulative;
                    }
                    annualElectricity[DateTime.Now.Month] = value; // 设置当前月的能耗

                    // 按月统计,每天的能耗
                    MonthElectricity monthElectricity = new MonthElectricity()
                    {
                        Month                = DateTime.Now.Month,
                        CreatedTime          = DateTime.Now,
                        AnnualElectricityOne = annualElectricity,
                        MonthTotal           = value,
                        UpdateTime           = DateTime.Now
                    };
                    monthElectricity[DateTime.Now.Day] = value;

                    // 按天统计,每时的能耗
                    DayElectricity dayElectricity = new DayElectricity()
                    {
                        UpdateTime          = DateTime.Now,
                        CreatedTime         = DateTime.Now,
                        DayTotal            = value,
                        MonthElectricityOne = monthElectricity,
                        Today = DateTime.Now.Day
                    };
                    dayElectricity[DateTime.Now.Hour] = value;

                    // 关联统计
                    monthElectricity.DayElectricityMany.Add(dayElectricity);
                    annualElectricity.MonthElectricityMany.Add(monthElectricity);
                    AnnualElectricityRepository.Insert(annualElectricity);
                }
                else/*今年的能耗记录存在,则直接计算能耗差值*/
                {
                    // 获取本月的能耗记录
                    var _CurrentMonthEct = _CurrentYeartEct.MonthElectricityMany.FirstOrDefault(m => m.Month == DateTime.Now.Month);
                    if (_CurrentMonthEct == null /*当月能耗记录不存在*/)
                    {
                        _CurrentYeartEct.Cumulative += value; // 历史累计能耗计算
                        _CurrentYeartEct.YearTotal  += value; // 年总能耗计算

                        // 创建当前月的能耗统计
                        _CurrentMonthEct = new MonthElectricity
                        {
                            UpdateTime           = DateTime.Now,
                            CreatedTime          = DateTime.Now,
                            MonthTotal           = value,
                            AnnualElectricityOne = _CurrentYeartEct,
                            Month = DateTime.Now.Month,
                        };
                        _CurrentMonthEct[DateTime.Now.Day] = value;

                        // 创建当前天的能耗统计
                        var _CurrentDayEct = new DayElectricity
                        {
                            UpdateTime          = DateTime.Now,
                            CreatedTime         = DateTime.Now,
                            DayTotal            = value,
                            MonthElectricityOne = _CurrentMonthEct,
                            Today = DateTime.Now.Day
                        };
                        _CurrentDayEct[DateTime.Now.Hour] = value;

                        // 关联能耗统计记录
                        _CurrentMonthEct.DayElectricityMany.Add(_CurrentDayEct);
                        _CurrentYeartEct.MonthElectricityMany.Add(_CurrentMonthEct);
                        AnnualElectricityRepository.Update(_CurrentYeartEct);
                    }
                    else/*当月能耗存在*/
                    {
                        // 获取当天的能耗记录
                        var _CurrentDayEct = _CurrentMonthEct.DayElectricityMany.FirstOrDefault(m => m.Today == DateTime.Now.Day);

                        // 上传的能耗数据比记录中的小,则主机能耗被清零操作或主机被更换
                        if (_CurrentYeartEct[DateTime.Now.Month] > value)
                        {
                            double overs = Math.Abs(value - (_CurrentMonthEct.MonthTotal - _CurrentMonthEct.LastTagValue));
                            if (_CurrentMonthEct.Tag == 0)
                            {
                                _CurrentMonthEct.Tag          = DateTime.Now.Day;
                                _CurrentMonthEct.LastTagValue = _CurrentMonthEct.MonthTotal;
                                overs = value;
                            }
                            else
                            {
                                if ((_CurrentMonthEct.MonthTotal - _CurrentMonthEct.LastTagValue) > value)
                                {
                                    _CurrentMonthEct.Tag          = DateTime.Now.Day;
                                    _CurrentMonthEct.LastTagValue = _CurrentMonthEct.MonthTotal;
                                    overs = value;
                                }
                            }
                            if (_CurrentDayEct == null)
                            {
                                // 创建当前天的能耗统计
                                _CurrentDayEct = new DayElectricity
                                {
                                    UpdateTime          = DateTime.Now,
                                    CreatedTime         = DateTime.Now,
                                    MonthElectricityOne = _CurrentMonthEct,
                                    Today = DateTime.Now.Day
                                };
                                _CurrentMonthEct.DayElectricityMany.Add(_CurrentDayEct);
                                //if (boolRp) { overs = value; }
                                _CurrentMonthEct[DateTime.Now.Day] = overs;
                                _CurrentDayEct[DateTime.Now.Hour]  = overs;
                            }
                            else
                            {
                                _CurrentMonthEct[DateTime.Now.Day] += overs;
                                double xc = _CurrentMonthEct[DateTime.Now.Day];
                                for (int n = 0; n < DateTime.Now.Hour; n++)
                                {
                                    xc -= _CurrentDayEct[n];
                                }
                                _CurrentDayEct[DateTime.Now.Hour] = Math.Abs(xc);
                            }
                            _CurrentDayEct.DayTotal               = _CurrentMonthEct[DateTime.Now.Day];
                            _CurrentMonthEct.MonthTotal          += overs;
                            _CurrentYeartEct[DateTime.Now.Month] += overs;
                            _CurrentYeartEct.YearTotal           += overs;
                            _CurrentYeartEct.Cumulative          += overs;
                            AnnualElectricityRepository.Update(_CurrentYeartEct);
                        }
                        else
                        {
                            _CurrentYeartEct.YearTotal          += (value - _CurrentYeartEct[DateTime.Now.Month]);
                            _CurrentYeartEct.Cumulative         += (value - _CurrentYeartEct[DateTime.Now.Month]);
                            _CurrentMonthEct.MonthTotal          = value;
                            _CurrentYeartEct[DateTime.Now.Month] = value;
                            Double xf = value;
                            for (int n = 1; n < DateTime.Now.Day; n++)
                            {
                                xf -= _CurrentMonthEct[n];
                            }
                            _CurrentMonthEct[DateTime.Now.Day] = Math.Abs(xf);
                            _CurrentDayEct.DayTotal            = Math.Abs(xf);
                            double xc = Math.Abs(xf);
                            for (int n = 0; n < DateTime.Now.Hour; n++)
                            {
                                xc -= _CurrentDayEct[n];
                            }
                            _CurrentDayEct[DateTime.Now.Hour] = Math.Abs(xc);
                            AnnualElectricityRepository.Update(_CurrentYeartEct);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // 如果报错,保存错误日志
                LogManager.GetLogger("AddEnergyConsumption_Host").Error(ex);
            }
        }