using System.Data; using System.Text; using System.Threading.Tasks; using Monitor.Models; using Monitor.IRepositories; using Monitor.Repositories; namespace DataBase.MySql.Tool { /// /// 电表每天能耗操作类 /// public class SEgEMeterDayEnergyValue { private TEgEmeterDayEnergyValueRepository _dalEgEMeterDayEnergyValueRepository; public SEgEMeterDayEnergyValue() { _dalEgEMeterDayEnergyValueRepository = new TEgEmeterDayEnergyValueRepository(DBTool.Instance.MyDbContext()); } /// /// 添加电表每天能耗值 /// /// /// public async Task AddEMeterDayEnergyValue(TEgEmeterDayEnergyValue val) { await _dalEgEMeterDayEnergyValueRepository.DelBy(a => a.FEmeterNo == val.FEmeterNo && a.FStartTime == val.FStartTime); return await _dalEgEMeterDayEnergyValueRepository.Insert(val); } /// /// 更新电表每天能耗值 /// /// /// public async Task UpdateEMeterDayEnergyValue(TEgEmeterDayEnergyValue val) { return await _dalEgEMeterDayEnergyValueRepository.ModifyBy(val,a=>a.FStartTime==val.FStartTime&&a.FEmeterNo==val.FEmeterNo,new string[] { "FDayValue" }); } /// /// 统计每日电表能耗 /// /// public async Task> AddEMeterDayEnergyValue() { List lstEMeterDayValue = new List(); List lstMeterNo = new List(); DateTime startTime = DateTime.Now.Date; DateTime endTime = DateTime.Now.Date.AddDays(1); List result = new List(); if(DateTime.Now.Hour==0|| DateTime.Now.Hour == 24) { result = await new SEgEMeterHourEnergyValue().GetEMeterPreDayEnergyValueList(); startTime = DateTime.Now.Date.AddDays(-1); endTime = DateTime.Now.Date; } else { result = await new SEgEMeterHourEnergyValue().GetEMeterCurrDayEnergyValueList(); } foreach (var q in result) { lstMeterNo.Add(q.FEmeterNo); } lstMeterNo = lstMeterNo.Distinct().ToList(); if (lstMeterNo != null) { int meterNum = lstMeterNo.Count; if (meterNum > 0) { for (int i = 0; i < meterNum; i++) { string meterNo = lstMeterNo[i]; float value = 0; for (int j = 0; j < result.Count; j++) { if (result[j].FEmeterNo == meterNo) { value += result[j].FHourValue; } } TEgEmeterDayEnergyValue dayResult = new TEgEmeterDayEnergyValue() { FEmeterNo = meterNo, FDayValue = value, FStartTime = startTime, FEndTime = endTime }; lstEMeterDayValue.Add(dayResult); } if (lstEMeterDayValue.Count > 0) { for (int k = 0; k < lstEMeterDayValue.Count; k++) { await AddEMeterDayEnergyValue(lstEMeterDayValue[k]); } } } } return lstEMeterDayValue; } } }