运营模型接口修改,增加电表统计

master
tq 6 months ago
parent 9cd82fb7f1
commit e31017f2f3

@ -0,0 +1,39 @@
using SqlSugar;
namespace Entity.Ammeter;
[SugarTable("emeter_energy")]
public class EmeterEnergy
{
/// <summary>
/// 主键ID 与 EmeterEnergyChange ID相同
/// </summary>
[SugarColumn(ColumnName = "id", IsPrimaryKey = true)]
public virtual string Id { get; set; }
/// <summary>
/// 充电机电表编码 (充电机Sn)
/// </summary>
[SugarColumn(ColumnName = "code")]
public virtual string Code { get; set; }
/// <summary>
/// 电表当前读数
/// </summary>
[SugarColumn(ColumnName = "value")]
public virtual string Value { get; set; }
/// <summary>
/// 充电机上报时间
/// </summary>
[SugarColumn(ColumnName = "upload_time")]
public virtual string UploadTime { get; set; }
/// <summary>
/// 上报标识0未上传 1上传
/// </summary>
[SugarColumn(ColumnName = "upload_flag")]
public virtual string UploadFlag { get; set; }
[SugarColumn(IsIgnore = true)] public virtual string Hour { get; set; }
}

@ -0,0 +1,39 @@
using SqlSugar;
namespace Entity.Ammeter;
[SugarTable("emeter_energy_change")]
public class EmeterEnergyChange
{
/// <summary>
/// 主键ID 与EmeterEnergy id 相同
/// </summary>
[SugarColumn(ColumnName = "id", IsPrimaryKey = true)]
public virtual string Id { get; set; }
/// <summary>
/// 充电机电表编码 (充电机Sn)
/// </summary>
[SugarColumn(ColumnName = "code")]
public virtual string Code { get; set; }
/// <summary>
/// 电表当前读数
/// </summary>
[SugarColumn(ColumnName = "value")]
public virtual float Value { get; set; }
/// <summary>
/// 充电机上报时间
/// </summary>
[SugarColumn(ColumnName = "upload_time")]
public virtual DateTime UploadTime { get; set; }
/// <summary>
/// 上报标识0未上传 1上传
/// </summary>
[SugarColumn(ColumnName = "upload_flag")]
public virtual int UploadFlag { get; set; }
[SugarColumn(IsIgnore = true)] public virtual string Hour { get; set; }
}

@ -4,6 +4,7 @@ namespace Entity.Api.Resp;
/// </summary>
public class BinBatteryResp
{
/// <summary>
///电池总数
/// </summary>

@ -0,0 +1,13 @@
namespace Entity.Api.Resp;
public class EmeterEnergyResp
{
/// <summary>
/// 直流电
/// </summary>
public List<EmeterResp> emeterEnergy { get; set; }
/// <summary>
/// 交流电
/// </summary>
public List<EmeterResp> emeterEnergyChange{ get; set; }
}

@ -0,0 +1,17 @@
namespace Entity.Api.Resp;
public class EmeterResp
{
/// <summary>
/// 充电机电表编码 (充电机Sn)
/// </summary>
public string Code { get; set; }
/// <summary>
/// 电表当前读数
/// </summary>
public float Value { get; set; }
public DateTime UploadTime { get; set; }
public string Hour { get; set; }
}

@ -0,0 +1,16 @@
using Entity.Ammeter;
using HybirdFrameworkCore.Autofac.Attribute;
using SqlSugar;
namespace Repository.Ammeter;
[Scope("SingleInstance")]
public class EmeterEnergyChangeRepository : BaseRepository<EmeterEnergyChange>
{
private ISqlSugarClient DbBaseClient;
public EmeterEnergyChangeRepository(ISqlSugarClient sqlSugar) : base(sqlSugar)
{
DbBaseClient = sqlSugar;
}
}

@ -0,0 +1,16 @@
using Entity.Ammeter;
using HybirdFrameworkCore.Autofac.Attribute;
using SqlSugar;
namespace Repository.Ammeter;
[Scope("SingleInstance")]
public class EmeterEnergyRepository : BaseRepository<EmeterEnergy>
{
private ISqlSugarClient DbBaseClient;
public EmeterEnergyRepository(ISqlSugarClient sqlSugar) : base(sqlSugar)
{
DbBaseClient = sqlSugar;
}
}

@ -29,23 +29,5 @@ namespace Service.Ammeter
else
return PageResult<EmeterDayEnergy>.ConvertPage(this.BaseDal.QueryIPageByCause(queryPageModel1, null));
}
/// <summary>
/// 七天电能统计
/// </summary>
/// <returns></returns>
public async Task<List<EmeterDayEnergy>> SevenDaysElectricalEnergy()
{
DateTime sevenDaysAgo = DateTime.Today.AddDays(-7);
List<EmeterDayEnergy> result = this.BaseDal.Queryable()
.Where(e => e.St >= sevenDaysAgo)
.GroupBy(e => e.Code)
.Select(e => new EmeterDayEnergy
{
Code = e.Code,
Value = SqlFunc.AggregateSum(e.Value)
}).ToList();
return result;
}
}
}

@ -0,0 +1,74 @@
using Entity.Ammeter;
using HybirdFrameworkCore.Autofac.Attribute;
using Repository.Ammeter;
namespace Service.Ammeter;
[Scope("SingleInstance")]
public class EmeterEnergyChangeService : BaseServices<EmeterEnergyChange>
{
private readonly EmeterEnergyChangeRepository _emeterEnergyChangeRep;
public EmeterEnergyChangeService(EmeterEnergyChangeRepository emeterEnergyChangeRep)
{
_emeterEnergyChangeRep = emeterEnergyChangeRep;
}
public async Task<List<EmeterEnergyChange>> SevenDaysElectrical()
{
string sql = @"
SELECT
code,
date(upload_time) AS UploadTime,
MAX(value) - MIN(value) AS Value
FROM
emeter_energy_change
WHERE
upload_time >= NOW() - INTERVAL 7 DAY
GROUP BY
code, date(upload_time)
ORDER BY
code, date(upload_time);
";
List<EmeterEnergyChange> emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql);
return emeterEnergies;
}
public async Task<List<EmeterEnergyChange>> GetAllElectricalData()
{
string sql = @"
SELECT
code,
MAX(value) - MIN(value) AS Value
FROM
emeter_energy_change
GROUP BY
code
ORDER BY
code;
";
List<EmeterEnergyChange> emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql);
return emeterEnergies;
}
public async Task<List<EmeterEnergyChange>> GetTodayHourlyElectricalData()
{
string sql = @"
SELECT
code,
DATE_FORMAT(upload_time, '%Y-%m-%d %H:00:00') AS Hour,
MAX(value) - MIN(value) AS Value
FROM
emeter_energy_change
WHERE
DATE(upload_time) = CURDATE()
GROUP BY
code, DATE_FORMAT(upload_time, '%Y-%m-%d %H:00:00')
ORDER BY
code, Hour;
";
List<EmeterEnergyChange> emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql);
return emeterEnergies;
}
}

@ -0,0 +1,87 @@
using Entity.Ammeter;
using Entity.Api.Resp;
using HybirdFrameworkCore.Autofac.Attribute;
using Repository.Ammeter;
using SqlSugar;
namespace Service.Ammeter;
[Scope("SingleInstance")]
public class EmeterEnergyService : BaseServices<EmeterEnergy>
{
public EmeterEnergyService(EmeterEnergyRepository service)
{
this.BaseDal = service;
}
/// <summary>
/// 获取七天用电量
/// </summary>
/// <returns></returns>
public async Task<List<EmeterEnergy>> SevenDaysElectrical()
{
string sql = @"
SELECT
code,
date(upload_time) AS UploadTime,
MAX(value) - MIN(value) AS Value
FROM
emeter_energy
WHERE
upload_time >= NOW() - INTERVAL 7 DAY
GROUP BY
code, date(upload_time)
ORDER BY
code, date(upload_time);
";
List<EmeterEnergy> emeterEnergies = await this.BaseDal.SqlQueryable(sql);
return emeterEnergies;
}
/// <summary>
/// 获取全部用电量
/// </summary>
/// <returns></returns>
public async Task<List<EmeterEnergy>> GetAllElectricalData()
{
string sql = @"
SELECT
code,
MAX(value) - MIN(value) AS Value
FROM
emeter_energy
GROUP BY
code
ORDER BY
code;
";
List<EmeterEnergy> emeterEnergies = await this.BaseDal.SqlQueryable(sql);
return emeterEnergies;
}
/// <summary>
/// 根据小时统计电量
/// </summary>
/// <returns></returns>
public async Task<List<EmeterEnergy>> GetTodayHourlyElectricalData()
{
string sql = @"
SELECT
code,
DATE_FORMAT(upload_time, '%Y-%m-%d %H:00:00') AS Hour,
MAX(value) - MIN(value) AS Value
FROM
emeter_energy
WHERE
DATE(upload_time) = CURDATE()
GROUP BY
code, DATE_FORMAT(upload_time, '%Y-%m-%d %H:00:00')
ORDER BY
code, Hour;
";
List<EmeterEnergy> emeterEnergies = await this.BaseDal.SqlQueryable(sql);
return emeterEnergies;
}
}

@ -7,20 +7,22 @@ using Repository.Station;
using SqlSugar;
namespace Service.Station;
[Scope("SingleInstance")]
public class BatteryOpModelDetailService: BaseServices<BatteryOpModelDetail>
[Scope("SingleInstance")]
public class BatteryOpModelDetailService : BaseServices<BatteryOpModelDetail>
{
private BatteryOpModelDetailRepository _batteryOpModelDetailRepository;
private BatteryOpModelRepository _batteryOpModelRepository;
public BatteryOpModelDetailService(BatteryOpModelDetailRepository dal,BatteryOpModelRepository dalBatteryOpModelRepository)
public BatteryOpModelDetailService(BatteryOpModelDetailRepository dal,
BatteryOpModelRepository dalBatteryOpModelRepository)
{
_batteryOpModelDetailRepository = dal;
BaseDal = dal;
_batteryOpModelRepository= dalBatteryOpModelRepository;
_batteryOpModelRepository = dalBatteryOpModelRepository;
}
/// <summary>
/// 电池运营模型详情分页列表 🔖
/// </summary>
@ -44,6 +46,7 @@ public class BatteryOpModelDetailService: BaseServices<BatteryOpModelDetail>
Rows = items,
};
}
/// <summary>
/// 删除电池运营模型 🔖
/// </summary>
@ -57,7 +60,7 @@ public class BatteryOpModelDetailService: BaseServices<BatteryOpModelDetail>
// 删除模型
var isExistModel = await _batteryOpModelRepository.QueryByClauseAsync(u => u.ModelId == user.ModelId);
bool batteay = true;
if (isExistModel!=null)
if (isExistModel != null)
{
batteay = await _batteryOpModelRepository.DeleteAsync(isExistModel);
}
@ -67,12 +70,11 @@ public class BatteryOpModelDetailService: BaseServices<BatteryOpModelDetail>
if (batteay && batteryOpModelDetail)
{
return true;
}
return false;
}
/// <summary>
/// 增加电池运营模型 🔖
/// </summary>
@ -83,6 +85,15 @@ public class BatteryOpModelDetailService: BaseServices<BatteryOpModelDetail>
// 事务
using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
if (DateTime.TryParse(input.StartTime, out DateTime startTime) &&
DateTime.TryParse(input.EndTime, out DateTime endTime))
{
if (startTime > endTime)
{
return Result<string>.Fail("开始时间不能大于结束时间");
}
}
List<BatteryOpModelDetail> batteryOpModelDetails = await _batteryOpModelDetailRepository.QueryAsync();
foreach (var detail in batteryOpModelDetails)
{
@ -91,12 +102,13 @@ public class BatteryOpModelDetailService: BaseServices<BatteryOpModelDetail>
return Result<string>.Fail("模型id在模型详情表已存在");
}
if (detail.StartTime == input.StartTime || detail.EndTime == input.EndTime)
if (detail.StartTime == input.StartTime && detail.Id != input.Id ||
detail.EndTime == input.EndTime && detail.Id != input.Id)
{
return Result<string>.Fail("相同时间已经存在");
}
}
// 检查两个表模型id是否已存在
var isExistModel = await _batteryOpModelRepository.QueryByClauseAsync(u => u.ModelId == input.ModelId);
if (isExistModel != null)
@ -118,32 +130,36 @@ public class BatteryOpModelDetailService: BaseServices<BatteryOpModelDetail>
// 新增
transactionScope.Complete();
return Result<string>.Success("新增id" + insertedDetail.Id);
}
}
public virtual async Task<bool> UpdateBatteryOpModelDetail(UpdateBatteryOpModelDetailReq batteryOpReq)
public virtual async Task<Result<string>> UpdateBatteryOpModelDetail(UpdateBatteryOpModelDetailReq batteryOpReq)
{
if (DateTime.TryParse(batteryOpReq.StartTime, out DateTime startTime) &&
DateTime.TryParse(batteryOpReq.EndTime, out DateTime endTime))
{
if (startTime > endTime)
{
return Result<string>.Fail("开始时间不能大于结束时间");
}
}
List<BatteryOpModelDetail> batteryOpModelDetails = await _batteryOpModelDetailRepository.QueryAsync();
foreach (var detail in batteryOpModelDetails)
{
if (detail.ModelId == batteryOpReq.ModelId)
if (detail.ModelId == batteryOpReq.ModelId&& detail.Id != batteryOpReq.Id)
{
throw new ArgumentException($"模型id在模型详情表已存在");
return Result<string>.Fail("模型id在模型详情表已存在");
}
if (detail.StartTime == batteryOpReq.StartTime || detail.EndTime == batteryOpReq.EndTime)
if (detail.StartTime == batteryOpReq.StartTime && detail.Id != batteryOpReq.Id ||
detail.EndTime == batteryOpReq.EndTime && detail.Id != batteryOpReq.Id)
{
throw new ArgumentException($"已存在相同时间");
return Result<string>.Fail("已存在相同时间");
}
}
var isExistModel = await _batteryOpModelRepository.QueryByClauseAsync(u => u.ModelId == batteryOpReq.ModelId);
if (isExistModel != null)
{
return false;
}
// 通过id查找修改之前的模型id
var batteryOpModelDetail =
@ -153,16 +169,15 @@ public class BatteryOpModelDetailService: BaseServices<BatteryOpModelDetail>
await _batteryOpModelRepository.QueryByClauseAsync(u => u.ModelId == batteryOpModelDetail.ModelId);
batteryOpModel.ModelId = batteryOpReq.ModelId;
bool updateModelDetail = await _batteryOpModelDetailRepository.UpdateAsync(batteryOpReq);
bool updateMode = await _batteryOpModelRepository.UpdateAsync(batteryOpModel);
if (updateModelDetail && updateMode)
{
return true;
return Result<string>.Success("修改成功");
}
return false;
return Result<string>.Success("修改失败");
}
}

@ -1,4 +1,6 @@
using Entity.Ammeter;
using AutoMapper;
using Entity.Ammeter;
using Entity.Api.Resp;
using Entity.Dto.Req;
using HybirdFrameworkCore.Entity;
using Microsoft.AspNetCore.Mvc;
@ -21,6 +23,11 @@ namespace WebStarter.Controllers.Ammeter
private EmeterMinutesEnergyChangeService _emeterMinutesEnergyChangeService;
private EquipInfoService _equipInfoService;
/// <summary>
/// 直流电
/// </summary>
private EmeterEnergyService _emeterEnergyService;
private EmeterEnergyChangeService _emeterEnergyChangeService;
/// <summary>
/// 查询电表信息
/// </summary>
/// <param name="emeterDayEnergyService"></param>
@ -30,27 +37,104 @@ namespace WebStarter.Controllers.Ammeter
/// <param name="equipInfoService"></param>
public AmmeterController(EmeterDayEnergyService emeterDayEnergyService, EmeterHourEnergyService emeterHourEnergyService,
EmeterMinutesEnergyService emeterMinutesEnergyService, EmeterMinutesEnergyChangeService emeterMinutesEnergyChangeService,
EquipInfoService equipInfoService)
EquipInfoService equipInfoService,
EmeterEnergyService emeterEnergyService,
EmeterEnergyChangeService emeterEnergyChangeService)
{
_emeterDayEnergyService = emeterDayEnergyService;
_emeterHourEnergyService = emeterHourEnergyService;
_emeterMinutesEnergyService = emeterMinutesEnergyService;
_emeterMinutesEnergyChangeService = emeterMinutesEnergyChangeService;
_equipInfoService = equipInfoService;
_emeterEnergyService = emeterEnergyService;
_emeterEnergyChangeService = emeterEnergyChangeService;
}
/// <summary>
/// 七天电表用量
/// </summary>
/// <param name="Code"></param>
/// <returns></returns>
[HttpGet("SevenDaysElectricalEnergy")]
public async Task<Result<List<EmeterDayEnergy>>> SevenDaysElectricalEnergy()
[HttpGet("SevenDaysElectrical")]
public async Task<Result<EmeterEnergyResp>> SevenDaysElectrical()
{
return Result<List<EmeterDayEnergy>>.Success(await _emeterDayEnergyService.SevenDaysElectricalEnergy());
}
EmeterEnergyResp resp = new EmeterEnergyResp();
// 获取直流和交流表数据
List<EmeterEnergy> sevenDaysElectrical = await _emeterEnergyService.SevenDaysElectrical();
List<EmeterEnergyChange> emeterEnergyChanges = await _emeterEnergyChangeService.SevenDaysElectrical();
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EmeterResp, EmeterEnergy>().ReverseMap();
cfg.CreateMap<EmeterResp, EmeterEnergyChange>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List<EmeterResp> emeterEnergyList = mapper.Map<List<EmeterResp>>(sevenDaysElectrical);
List<EmeterResp> sevenEmeterResps = mapper.Map<List<EmeterResp>>(emeterEnergyChanges);
resp.emeterEnergy = emeterEnergyList;
resp.emeterEnergyChange = sevenEmeterResps;
return Result<EmeterEnergyResp>.Success(resp,"成功");
}
#region 统计分析/电能表累计值信息
/// <summary>
/// 电能表累计值
/// </summary>
/// <returns></returns>
[HttpGet("GetAllElectricalData")]
public async Task<Result<EmeterEnergyResp>> GetAllElectricalData()
{
EmeterEnergyResp resp = new EmeterEnergyResp();
// 获取直流和交流表数据
List<EmeterEnergy> Electrical = await _emeterEnergyService.GetAllElectricalData();
List<EmeterEnergyChange> EnergyChanges = await _emeterEnergyChangeService.GetAllElectricalData();
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EmeterResp, EmeterEnergy>().ReverseMap();
cfg.CreateMap<EmeterResp, EmeterEnergyChange>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List<EmeterResp> EnergyList = mapper.Map<List<EmeterResp>>(Electrical);
List<EmeterResp> EmeterResps = mapper.Map<List<EmeterResp>>(EnergyChanges);
resp.emeterEnergy = EnergyList;
resp.emeterEnergyChange = EmeterResps;
return Result<EmeterEnergyResp>.Success(resp,"成功");
}
/// <summary>
/// 电能根据小时统计
/// </summary>
/// <returns></returns>
[HttpGet("GetTodayHourlyElectricalData")]
public async Task<Result<EmeterEnergyResp>> GetTodayHourlyElectricalData()
{
EmeterEnergyResp resp = new EmeterEnergyResp();
// 获取直流和交流表数据
List<EmeterEnergy> Electrical = await _emeterEnergyService.GetTodayHourlyElectricalData();
List<EmeterEnergyChange> EnergyChanges = await _emeterEnergyChangeService.GetTodayHourlyElectricalData();
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EmeterResp, EmeterEnergy>().ReverseMap();
cfg.CreateMap<EmeterResp, EmeterEnergyChange>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List<EmeterResp> EnergyList = mapper.Map<List<EmeterResp>>(Electrical);
List<EmeterResp> EmeterResps = mapper.Map<List<EmeterResp>>(EnergyChanges);
resp.emeterEnergy = EnergyList;
resp.emeterEnergyChange = EmeterResps;
return Result<EmeterEnergyResp>.Success(resp,"成功");
}
/// <summary>
/// 统计分析/电能表累计值信息
/// </summary>

@ -45,6 +45,7 @@ public class BatteryOpModelDetailController
{
return await _batteryOpModelDetailService.AddBatteryOpModelDetail(input);
}
/// <summary>
/// 更新电池运营模型
/// </summary>
@ -52,13 +53,9 @@ public class BatteryOpModelDetailController
/// <returns></returns>
[HttpPost]
[Route("update")]
public async Task<Result<bool>> UpdateBatteryOpModelDetail([FromBody] UpdateBatteryOpModelDetailReq batteryOpReq)
public async Task<Result<string>> UpdateBatteryOpModelDetail([FromBody] UpdateBatteryOpModelDetailReq batteryOpReq)
{
var data = await _batteryOpModelDetailService.UpdateBatteryOpModelDetail(batteryOpReq);
if (data)
return Result<bool>.Success(data);
else
return Result<bool>.Fail(data);
return await _batteryOpModelDetailService.UpdateBatteryOpModelDetail(batteryOpReq);
}
/// <summary>
/// 删除电池运营模型

Loading…
Cancel
Save