电表查表更改

zw
tq 5 months ago
parent 2e3e1cd18d
commit 34a8995f15

@ -48,5 +48,23 @@ namespace Entity.Ammeter
[SugarColumn(ColumnName = "upload_flag")]
public int UploadFlag { get; set; }
/// <summary>
/// 统计sql时间接收
/// </summary>
[SugarColumn(IsIgnore = true)]
public virtual DateTime UploadTime { get; set; }
/// <summary>
/// 统计sql小时接收
/// </summary>
[SugarColumn(IsIgnore = true)]
public virtual string Hour { get; set; }
/// <summary>
/// 统计sql分钟接收
/// </summary>
[SugarColumn(IsIgnore = true)]
public virtual string Minute { get; set; }
}
}

@ -47,5 +47,24 @@ namespace Entity.Ammeter
[SugarColumn(ColumnName = "upload_flag")]
public int UploadFlag { get; set; }
/// <summary>
/// 统计sql时间接收
/// </summary>
[SugarColumn(IsIgnore = true)]
public virtual DateTime UploadTime { get; set; }
/// <summary>
/// 统计sql小时接收
/// </summary>
[SugarColumn(IsIgnore = true)]
public virtual string Hour { get; set; }
/// <summary>
/// 统计sql分钟接收
/// </summary>
[SugarColumn(IsIgnore = true)]
public virtual string Minute { get; set; }
}
}

@ -50,5 +50,24 @@ namespace Entity.Ammeter
public int UploadFlag { get; set; }
/// <summary>
/// 统计sql时间接收
/// </summary>
[SugarColumn(IsIgnore = true)]
public virtual DateTime UploadTime { get; set; }
/// <summary>
/// 统计sql小时接收
/// </summary>
[SugarColumn(IsIgnore = true)]
public virtual string Hour { get; set; }
/// <summary>
/// 统计sql分钟接收
/// </summary>
[SugarColumn(IsIgnore = true)]
public virtual string Minute { get; set; }
}
}

@ -29,5 +29,93 @@ namespace Service.Ammeter
else
return PageResult<EmeterDayEnergy>.ConvertPage(this.BaseDal.QueryIPageByCause(queryPageModel1, null));
}
/// <summary>
/// 获取七天用电量
/// </summary>
/// <returns></returns>
public async Task<List<EmeterDayEnergy>> SevenDaysElectrical(int type)
{
// 根据code分组,取最近7天数据,每天最大值-最小值=当日用电量,如果当天目前只有一条数据,取当天最大值-前一天最大值=当天耗电量
string sql = $@"
WITH daily_max_min AS (
SELECT
`code`,
DATE(`time`) AS `UploadTime`,
MAX(`real_time_value`) AS `MaxValue`,
MIN(`real_time_value`) AS `MinValue`,
COUNT(*) AS `Count`
FROM
`emeter_day_energy`
WHERE
`time` >= NOW() - INTERVAL 8 DAY
AND `type` = {type}
GROUP BY
`code`, DATE(`time`)
), previous_day_max AS (
SELECT
`code`,
`UploadTime`,
`MaxValue`,
`MinValue`,
`Count`,
LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `UploadTime`) AS `PreviousMaxValue`
FROM
daily_max_min
), daily_consumption AS (
SELECT
d.`code`,
d.`UploadTime`,
CASE
WHEN d.`Count` > 1 THEN d.`MaxValue` - d.`MinValue`
WHEN d.`Count` = 1 AND d.`PreviousMaxValue` IS NOT NULL THEN d.`MaxValue` - d.`PreviousMaxValue`
ELSE 0
END AS `DailyValue`
FROM
previous_day_max d
WHERE
d.`UploadTime` >= NOW() - INTERVAL 7 DAY
)
SELECT
`UploadTime`,
SUM(`DailyValue`) AS `Value`
FROM
daily_consumption
GROUP BY
`UploadTime`
ORDER BY
`UploadTime`;
";
List<EmeterDayEnergy> emeterEnergies = await this.BaseDal.SqlQueryable(sql);
return emeterEnergies;
}
/// <summary>
/// 获取全部用电量
/// </summary>
/// <returns></returns>
public async Task<List<EmeterDayEnergy>> GetAllElectricalData(int type)
{
string sql = $@"
SELECT
code,
MAX(real_time_value) - MIN(real_time_value) AS Value
FROM
emeter_day_energy
WHERE
time >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND type = {type}
GROUP BY
code
ORDER BY
code;
";
List<EmeterDayEnergy> emeterEnergies = await this.BaseDal.SqlQueryable(sql);
return emeterEnergies;
}
}
}

@ -4,6 +4,7 @@ using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Entity;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Repository.Ammeter;
using SqlSugar;
namespace Service.Ammeter
{
@ -30,5 +31,107 @@ namespace Service.Ammeter
else
return Result<PageResult<EmeterHourEnergy>>.Success(PageResult<EmeterHourEnergy>.ConvertPage(this.BaseDal.QueryIPageByCause(queryPageModel1, null)));
}
/// <summary>
/// 根据小时统计电量
/// </summary>
/// <returns></returns>
public async Task<List<EmeterHourEnergy>> GetTodayHourlyElectricalData(int type)
{
string sql = $@"
WITH hourly_max_min AS (
SELECT
`code`,
DATE_FORMAT(`time`, '%Y-%m-%d %H:00:00') AS `Hour`,
MAX(`real_time_value`) AS `MaxValue`,
MIN(`real_time_value`) AS `MinValue`,
COUNT(*) AS `record_count`
FROM
`emeter_hour_energy`
WHERE
DATE(`time`) = CURDATE()
AND `type` = {type}
GROUP BY
`code`, DATE_FORMAT(`time`, '%Y-%m-%d %H:00:00')
), previous_hour_max AS (
SELECT
`code`,
`Hour`,
LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `Hour`) AS `PreviousMaxValue`
FROM
hourly_max_min
)
SELECT
h.`code`,
h.`Hour`,
CASE
WHEN h.`record_count` > 1 THEN h.`MaxValue` - h.`MinValue`
ELSE h.`MaxValue` - COALESCE(p.`PreviousMaxValue`, 0)
END AS `Value`
FROM
hourly_max_min h
LEFT JOIN
previous_hour_max p ON h.`code` = p.`code` AND h.`Hour` = p.`Hour`
ORDER BY
h.`code`, h.`Hour`;
";
List<EmeterHourEnergy> emeterEnergies = await this.BaseDal.SqlQueryable(sql);
return emeterEnergies;
}
/// <summary>
/// 获取直流电表实时数据
/// </summary>
/// <returns></returns>
public async Task<List<EmeterHourEnergy>> GetEnergyMeterRealTime(string endTime, int type)
{
// 将传入的时间参数转换为 DateTime 类型
DateTime endDateTime = DateTime.Parse(endTime);
// 根据传入的时间参数计算 1 天前的时间
DateTime startDateTime = endDateTime.AddDays(-1);
// 构建 SQL 查询字符串并包含参数
string sql = @"
WITH latest_data AS (
SELECT
code,
MAX(time) AS latest_upload_time
FROM emeter_hour_energy
WHERE time >= @StartDateTime AND time <= @EndDateTime
AND type = @Type
GROUP BY code
),
max_value_data AS (
SELECT
e.*,
ROW_NUMBER() OVER (PARTITION BY e.code ORDER BY e.real_time_value DESC) AS rn
FROM emeter_hour_energy e
JOIN latest_data ld ON e.code = ld.code AND e.time = ld.latest_upload_time
WHERE e.type = @Type
)
SELECT
*
FROM max_value_data
WHERE rn = 1;
";
// 使用参数化查询防止 SQL 注入
var parameters = new List<SugarParameter>
{
new SugarParameter("@StartDateTime", startDateTime),
new SugarParameter("@EndDateTime", endDateTime),
new SugarParameter("@Type", type)
};
// 调用现有的 SqlQuery 方法
List<EmeterHourEnergy> emeterEnergyChanges = this.BaseDal.SqlQuery(sql, parameters);
return emeterEnergyChanges;
}
}
}

@ -31,5 +31,55 @@ namespace Service.Ammeter
else
return PageResult<EmeterMinutesEnergy>.ConvertPage(_emeterMinutesEnergyRepository1.QueryIPageByCause(queryPageModel1, null));
}
/// <summary>
/// 根据分钟统计电量
/// </summary>
/// <returns></returns>
public async Task<List<EmeterMinutesEnergy>> GetTodayMinutelyElectricalData(int type)
{
string sql = $@"
WITH minute_max_min AS (
SELECT
`code`,
DATE_FORMAT(`time`, '%Y-%m-%d %H:%i:00') AS `Minute`,
MAX(`real_time_value`) AS `MaxValue`,
MIN(`real_time_value`) AS `MinValue`,
COUNT(*) AS `record_count`
FROM
`emeter_minutes_energy`
WHERE
`time` >= NOW() - INTERVAL 1 HOUR
AND `type` = {type}
GROUP BY
`code`, DATE_FORMAT(`time`, '%Y-%m-%d %H:%i:00')
), previous_minute_max AS (
SELECT
`code`,
`Minute`,
LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `Minute`) AS `PreviousMaxValue`
FROM
minute_max_min
)
SELECT
m.`code`,
m.`Minute`,
CASE
WHEN m.`record_count` > 1 THEN m.`MaxValue` - m.`MinValue`
WHEN p.`PreviousMaxValue` IS NULL THEN 0
ELSE m.`MaxValue` - p.`PreviousMaxValue`
END AS `Value`
FROM
minute_max_min m
LEFT JOIN
previous_minute_max p ON m.`code` = p.`code` AND m.`Minute` = p.`Minute`
ORDER BY
m.`code`, m.`Minute`;
";
List<EmeterMinutesEnergy> emeterEnergies = await this.BaseDal.SqlQueryable(sql);
return emeterEnergies;
}
}
}

@ -58,20 +58,20 @@ namespace WebStarter.Controllers.Ammeter
[HttpGet("SevenDaysElectrical")]
public async Task<Result<EmeterEnergyResp>> SevenDaysElectrical()
{
// 获取直流和交流表数据
List<EmeterEnergy> sevenDaysElectrical = await _emeterEnergyService.SevenDaysElectrical();
List<EmeterEnergyChange> emeterEnergyChanges = await _emeterEnergyChangeService.SevenDaysElectrical();
EmeterEnergyResp resp = new EmeterEnergyResp();
List<EmeterDayEnergy> sevenDaysElectrical = await _emeterDayEnergyService.SevenDaysElectrical(1);
List<EmeterDayEnergy> emeterEnergyChanges = await _emeterDayEnergyService.SevenDaysElectrical(2);
ChargeAssignment(sevenDaysElectrical, emeterEnergyChanges, resp);
EmeterEnergyResp resp = new EmeterEnergyResp();
ElectricEnergyDay(sevenDaysElectrical, emeterEnergyChanges, resp);
return Result<EmeterEnergyResp>.Success(resp, "成功");
}
/// <summary>
/// 30天累计能耗
/// </summary>
@ -79,13 +79,13 @@ namespace WebStarter.Controllers.Ammeter
[HttpGet("GetAllElectricalData")]
public async Task<Result<EmeterEnergyResp>> GetAllElectricalData()
{
List<EmeterEnergy> emeterEnergy = await _emeterEnergyService.GetAllElectricalData();
List<EmeterEnergyChange> emeterEnergyChanges = await _emeterEnergyChangeService.GetAllElectricalData();
EmeterEnergyResp resp = new EmeterEnergyResp();
List<EmeterDayEnergy> emeterEnergy = await _emeterDayEnergyService.GetAllElectricalData(1);
List<EmeterDayEnergy> emeterEnergyChanges = await _emeterDayEnergyService.GetAllElectricalData(2);
ChargeAssignment(emeterEnergy, emeterEnergyChanges, resp);
EmeterEnergyResp resp = new EmeterEnergyResp();
ElectricEnergyDay(emeterEnergy, emeterEnergyChanges, resp);
return Result<EmeterEnergyResp>.Success(resp, "成功");
}
@ -98,14 +98,28 @@ namespace WebStarter.Controllers.Ammeter
public async Task<Result<EmeterEnergyResp>> GetLastDayMeterData()
{
// 获取最近一天直流和交流表数据
List<EmeterEnergy> daysElectrical = await _emeterEnergyService.GetTodayHourlyElectricalData();
List<EmeterEnergyChange> daysElectricalChange =
await _emeterEnergyChangeService.GetTodayHourlyElectricalData();
List<EmeterHourEnergy> daysElectrical = await _emeterHourEnergyService.GetTodayHourlyElectricalData(1);
List<EmeterHourEnergy> daysElectricalChange =
await _emeterHourEnergyService.GetTodayHourlyElectricalData(2);
EmeterEnergyResp resp = new EmeterEnergyResp();
ChargeAssignment(daysElectrical, daysElectricalChange, resp);
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EmeterResp, EmeterHourEnergy>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List<EmeterResp> EnergyList = mapper.Map<List<EmeterResp>>(daysElectrical);
// 类型赋值直流
EnergyList?.ForEach(emeterResp => emeterResp.Type = 0);
List<EmeterResp> EnergyChangeList = mapper.Map<List<EmeterResp>>(daysElectricalChange);
// 类型赋值交流
EnergyChangeList?.ForEach(emeterResp => emeterResp.Type = 1);
resp.emeterEnergy = EnergyList.OrderBy(i => i.Code).ToList();
resp.emeterEnergyChange = EnergyChangeList.OrderBy(i => i.Code).ToList();
return Result<EmeterEnergyResp>.Success(resp, "成功");
}
@ -115,10 +129,10 @@ namespace WebStarter.Controllers.Ammeter
/// </summary>
/// <returns></returns>
[HttpGet("GetTodayHourlyEnergy")]
public async Task<Result<List<EmeterEnergy>>> GetTodayHourlyElectricalData()
public async Task<Result<List<EmeterHourEnergy>>> GetTodayHourlyElectricalData()
{
// 直流表数据
return Result<List<EmeterEnergy>>.Success(await _emeterEnergyService.GetTodayHourlyElectricalData(), "成功");
return Result<List<EmeterHourEnergy>>.Success(await _emeterHourEnergyService.GetTodayHourlyElectricalData(1), "成功");
}
@ -127,11 +141,11 @@ namespace WebStarter.Controllers.Ammeter
/// </summary>
/// <returns></returns>
[HttpGet("GetTodayHourlyEnergyChange")]
public async Task<Result<List<EmeterEnergyChange>>> GetTodayHourlyElectricalChangeData()
public async Task<Result<List<EmeterHourEnergy>>> GetTodayHourlyElectricalChangeData()
{
// 交流表数据
return Result<List<EmeterEnergyChange>>.Success(
await _emeterEnergyChangeService.GetTodayHourlyElectricalData(), "成功");
return Result<List<EmeterHourEnergy>>.Success(
await _emeterHourEnergyService.GetTodayHourlyElectricalData(2), "成功");
}
/// <summary>
@ -142,13 +156,28 @@ namespace WebStarter.Controllers.Ammeter
public async Task<Result<EmeterEnergyResp>> GetTodayMinutelyElectricalData()
{
// 获取最近一个小时直流和交流表数据
List<EmeterEnergy> minutesElectrical = await _emeterEnergyService.GetTodayMinutelyElectricalData();
List<EmeterEnergyChange> minutesElectricalChange =
await _emeterEnergyChangeService.GetTodayMinutelyElectricalData();
List<EmeterMinutesEnergy> minutesElectrical = await _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(1);
List<EmeterMinutesEnergy> minutesElectricalChange =
await _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(2);
EmeterEnergyResp resp = new EmeterEnergyResp();
ChargeAssignment(minutesElectrical, minutesElectricalChange, resp);
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EmeterResp, EmeterMinutesEnergy>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List<EmeterResp> EnergyList = mapper.Map<List<EmeterResp>>(minutesElectrical);
// 类型赋值直流
EnergyList?.ForEach(emeterResp => emeterResp.Type = 0);
List<EmeterResp> EnergyChangeList = mapper.Map<List<EmeterResp>>(minutesElectricalChange);
// 类型赋值交流
EnergyChangeList?.ForEach(emeterResp => emeterResp.Type = 1);
resp.emeterEnergy = EnergyList.OrderBy(i => i.Code).ToList();
resp.emeterEnergyChange = EnergyChangeList.OrderBy(i => i.Code).ToList();
return Result<EmeterEnergyResp>.Success(resp, "成功");
@ -170,30 +199,42 @@ namespace WebStarter.Controllers.Ammeter
endTime = parsedEndTime.ToString("yyyy-MM-dd HH:mm:ss");
// 获取实时直流和交流表数据
List<EmeterEnergy> minutesElectrical = await _emeterEnergyService.GetEnergyMeterRealTime(endTime);
List<EmeterEnergyChange> minutesElectricalChange =
await _emeterEnergyChangeService.GetEnergyMeterRealTime(endTime);
EmeterEnergyResp resp = new EmeterEnergyResp();
ChargeAssignment(minutesElectrical, minutesElectricalChange, resp);
List<EmeterHourEnergy> minutesElectrical = await _emeterHourEnergyService.GetEnergyMeterRealTime(endTime, 1);
List<EmeterHourEnergy> minutesElectricalChange = await _emeterHourEnergyService.GetEnergyMeterRealTime(endTime, 2);
EmeterEnergyResp resp = new EmeterEnergyResp
{
emeterEnergy = TransformEnergyData(minutesElectrical, 0),
emeterEnergyChange = TransformEnergyData(minutesElectricalChange, 1)
};
return Result<EmeterEnergyResp>.Success(resp, "成功");
}
private List<EmeterResp> TransformEnergyData(List<EmeterHourEnergy> energyData, int type)
{
return energyData.Select(hourEnergy =>
{
DateTime? uploadTime = DateTime.TryParse(hourEnergy.Time, out DateTime parsedTime) ? parsedTime : (DateTime?)null;
return new EmeterResp
{
Code = hourEnergy.Code,
Value = hourEnergy.RealTimeValue,
UploadTime = uploadTime,
Type = type
};
}).OrderBy(e => e.Code).ToList();
}
private static void ChargeAssignment(List<EmeterEnergy> emeterEnergy,
List<EmeterEnergyChange> emeterEnergyChanges, EmeterEnergyResp resp)
private static void ElectricEnergyDay(List<EmeterDayEnergy> daysElectrical, List<EmeterDayEnergy> emeterEnergyChanges, EmeterEnergyResp resp)
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EmeterResp, EmeterEnergy>().ReverseMap();
cfg.CreateMap<EmeterResp, EmeterEnergyChange>().ReverseMap();
cfg.CreateMap<EmeterResp, EmeterDayEnergy>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List<EmeterResp> EnergyList = mapper.Map<List<EmeterResp>>(emeterEnergy);
List<EmeterResp> EnergyList = mapper.Map<List<EmeterResp>>(daysElectrical);
// 类型赋值直流
EnergyList?.ForEach(emeterResp => emeterResp.Type = 0);

Loading…
Cancel
Save