You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.0 KiB

using System.Data;
using System.Text;
using System.Threading.Tasks;
using Monitor.Models;
using Monitor.IRepositories;
using Monitor.Repositories;
namespace DataBase.MySql.Tool
{
/// <summary>
/// 电表每天能耗操作类
/// </summary>
public class SEgEMeterDayEnergyValue
{
private TEgEmeterDayEnergyValueRepository _dalEgEMeterDayEnergyValueRepository;
public SEgEMeterDayEnergyValue()
{
_dalEgEMeterDayEnergyValueRepository = new TEgEmeterDayEnergyValueRepository(DBTool.Instance.MyDbContext());
}
/// <summary>
/// 添加电表每天能耗值
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public async Task<int> AddEMeterDayEnergyValue(TEgEmeterDayEnergyValue val)
{
await _dalEgEMeterDayEnergyValueRepository.DelBy(a => a.FEmeterNo == val.FEmeterNo && a.FStartTime == val.FStartTime);
return await _dalEgEMeterDayEnergyValueRepository.Insert(val);
}
/// <summary>
/// 更新电表每天能耗值
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public async Task<int> UpdateEMeterDayEnergyValue(TEgEmeterDayEnergyValue val)
{
return await _dalEgEMeterDayEnergyValueRepository.ModifyBy(val,a=>a.FStartTime==val.FStartTime&&a.FEmeterNo==val.FEmeterNo,new string[] { "FDayValue" });
}
/// <summary>
/// 统计每日电表能耗
/// </summary>
/// <returns></returns>
public async Task<List<TEgEmeterDayEnergyValue>> AddEMeterDayEnergyValue()
{
List<TEgEmeterDayEnergyValue> lstEMeterDayValue = new List<TEgEmeterDayEnergyValue>();
List<string> lstMeterNo = new List<string>();
DateTime startTime = DateTime.Now.Date;
DateTime endTime = DateTime.Now.Date.AddDays(1);
List<TEgEmeterHourEnergyValue> result = new List<TEgEmeterHourEnergyValue>();
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;
}
}
}