diff --git a/Entity/Ammeter/EmeterEnergy.cs b/Entity/Ammeter/EmeterEnergy.cs new file mode 100644 index 0000000..1c7549a --- /dev/null +++ b/Entity/Ammeter/EmeterEnergy.cs @@ -0,0 +1,39 @@ +using SqlSugar; + +namespace Entity.Ammeter; + +[SugarTable("emeter_energy")] +public class EmeterEnergy +{ + /// + /// 主键ID 与 EmeterEnergyChange ID相同 + /// + [SugarColumn(ColumnName = "id", IsPrimaryKey = true)] + public virtual string Id { get; set; } + + /// + /// 充电机电表编码 (充电机Sn) + /// + [SugarColumn(ColumnName = "code")] + public virtual string Code { get; set; } + + /// + /// 电表当前读数 + /// + [SugarColumn(ColumnName = "value")] + public virtual string Value { get; set; } + + /// + /// 充电机上报时间 + /// + [SugarColumn(ColumnName = "upload_time")] + public virtual string UploadTime { get; set; } + + /// + /// 上报标识:0未上传 1上传 + /// + [SugarColumn(ColumnName = "upload_flag")] + public virtual string UploadFlag { get; set; } + + [SugarColumn(IsIgnore = true)] public virtual string Hour { get; set; } +} \ No newline at end of file diff --git a/Entity/Ammeter/EmeterEnergyChange.cs b/Entity/Ammeter/EmeterEnergyChange.cs new file mode 100644 index 0000000..076daa0 --- /dev/null +++ b/Entity/Ammeter/EmeterEnergyChange.cs @@ -0,0 +1,39 @@ +using SqlSugar; + +namespace Entity.Ammeter; + +[SugarTable("emeter_energy_change")] +public class EmeterEnergyChange +{ + /// + /// 主键ID 与EmeterEnergy id 相同 + /// + [SugarColumn(ColumnName = "id", IsPrimaryKey = true)] + public virtual string Id { get; set; } + + /// + /// 充电机电表编码 (充电机Sn) + /// + [SugarColumn(ColumnName = "code")] + public virtual string Code { get; set; } + + /// + /// 电表当前读数 + /// + [SugarColumn(ColumnName = "value")] + public virtual float Value { get; set; } + + /// + /// 充电机上报时间 + /// + [SugarColumn(ColumnName = "upload_time")] + public virtual DateTime UploadTime { get; set; } + + /// + /// 上报标识:0未上传 1上传 + /// + [SugarColumn(ColumnName = "upload_flag")] + public virtual int UploadFlag { get; set; } + + [SugarColumn(IsIgnore = true)] public virtual string Hour { get; set; } +} \ No newline at end of file diff --git a/Entity/Api/Resp/BinBatteryResp.cs b/Entity/Api/Resp/BinBatteryResp.cs index 3c18695..39cbbb5 100644 --- a/Entity/Api/Resp/BinBatteryResp.cs +++ b/Entity/Api/Resp/BinBatteryResp.cs @@ -4,6 +4,7 @@ namespace Entity.Api.Resp; /// public class BinBatteryResp { + /// ///电池总数 /// diff --git a/Entity/Api/Resp/EmeterEnergyResp.cs b/Entity/Api/Resp/EmeterEnergyResp.cs new file mode 100644 index 0000000..434cd09 --- /dev/null +++ b/Entity/Api/Resp/EmeterEnergyResp.cs @@ -0,0 +1,13 @@ +namespace Entity.Api.Resp; + +public class EmeterEnergyResp +{ + /// + /// 直流电 + /// + public List emeterEnergy { get; set; } + /// + /// 交流电 + /// + public List emeterEnergyChange{ get; set; } +} \ No newline at end of file diff --git a/Entity/Api/Resp/EmeterResp.cs b/Entity/Api/Resp/EmeterResp.cs new file mode 100644 index 0000000..d1e0ba5 --- /dev/null +++ b/Entity/Api/Resp/EmeterResp.cs @@ -0,0 +1,17 @@ +namespace Entity.Api.Resp; + +public class EmeterResp +{ + /// + /// 充电机电表编码 (充电机Sn) + /// + public string Code { get; set; } + + /// + /// 电表当前读数 + /// + public float Value { get; set; } + + public DateTime UploadTime { get; set; } + public string Hour { get; set; } +} \ No newline at end of file diff --git a/Repository/Ammeter/EmeterEnergyChangeRepository.cs b/Repository/Ammeter/EmeterEnergyChangeRepository.cs new file mode 100644 index 0000000..70a84b0 --- /dev/null +++ b/Repository/Ammeter/EmeterEnergyChangeRepository.cs @@ -0,0 +1,16 @@ +using Entity.Ammeter; +using HybirdFrameworkCore.Autofac.Attribute; +using SqlSugar; + +namespace Repository.Ammeter; + +[Scope("SingleInstance")] +public class EmeterEnergyChangeRepository : BaseRepository +{ + private ISqlSugarClient DbBaseClient; + + public EmeterEnergyChangeRepository(ISqlSugarClient sqlSugar) : base(sqlSugar) + { + DbBaseClient = sqlSugar; + } +} \ No newline at end of file diff --git a/Repository/Ammeter/EmeterEnergyRepository.cs b/Repository/Ammeter/EmeterEnergyRepository.cs new file mode 100644 index 0000000..2753c03 --- /dev/null +++ b/Repository/Ammeter/EmeterEnergyRepository.cs @@ -0,0 +1,16 @@ +using Entity.Ammeter; +using HybirdFrameworkCore.Autofac.Attribute; +using SqlSugar; + +namespace Repository.Ammeter; + +[Scope("SingleInstance")] +public class EmeterEnergyRepository : BaseRepository +{ + private ISqlSugarClient DbBaseClient; + + public EmeterEnergyRepository(ISqlSugarClient sqlSugar) : base(sqlSugar) + { + DbBaseClient = sqlSugar; + } +} \ No newline at end of file diff --git a/Service/Ammeter/EmeterDayEnergyService.cs b/Service/Ammeter/EmeterDayEnergyService.cs index b6c5577..aca3677 100644 --- a/Service/Ammeter/EmeterDayEnergyService.cs +++ b/Service/Ammeter/EmeterDayEnergyService.cs @@ -29,23 +29,5 @@ namespace Service.Ammeter else return PageResult.ConvertPage(this.BaseDal.QueryIPageByCause(queryPageModel1, null)); } - /// - /// 七天电能统计 - /// - /// - public async Task> SevenDaysElectricalEnergy() - { - DateTime sevenDaysAgo = DateTime.Today.AddDays(-7); - - List 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; - } } } diff --git a/Service/Ammeter/EmeterEnergyChangeService.cs b/Service/Ammeter/EmeterEnergyChangeService.cs new file mode 100644 index 0000000..6cc4527 --- /dev/null +++ b/Service/Ammeter/EmeterEnergyChangeService.cs @@ -0,0 +1,74 @@ +using Entity.Ammeter; +using HybirdFrameworkCore.Autofac.Attribute; +using Repository.Ammeter; + +namespace Service.Ammeter; + +[Scope("SingleInstance")] +public class EmeterEnergyChangeService : BaseServices +{ + private readonly EmeterEnergyChangeRepository _emeterEnergyChangeRep; + + public EmeterEnergyChangeService(EmeterEnergyChangeRepository emeterEnergyChangeRep) + { + _emeterEnergyChangeRep = emeterEnergyChangeRep; + } + + public async Task> 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 emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql); + return emeterEnergies; + } + + public async Task> GetAllElectricalData() + { + string sql = @" + SELECT + code, + MAX(value) - MIN(value) AS Value + FROM + emeter_energy_change + GROUP BY + code + ORDER BY + code; + "; + List emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql); + return emeterEnergies; + } + + public async Task> 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 emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql); + return emeterEnergies; + } +} \ No newline at end of file diff --git a/Service/Ammeter/EmeterEnergyService.cs b/Service/Ammeter/EmeterEnergyService.cs new file mode 100644 index 0000000..37f0578 --- /dev/null +++ b/Service/Ammeter/EmeterEnergyService.cs @@ -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 +{ + public EmeterEnergyService(EmeterEnergyRepository service) + { + this.BaseDal = service; + } + + /// + /// 获取七天用电量 + /// + /// + public async Task> 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 emeterEnergies = await this.BaseDal.SqlQueryable(sql); + return emeterEnergies; + } + + /// + /// 获取全部用电量 + /// + /// + public async Task> GetAllElectricalData() + { + string sql = @" + SELECT + code, + MAX(value) - MIN(value) AS Value + FROM + emeter_energy + GROUP BY + code + ORDER BY + code; + "; + + List emeterEnergies = await this.BaseDal.SqlQueryable(sql); + return emeterEnergies; + } + + /// + /// 根据小时统计电量 + /// + /// + public async Task> 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 emeterEnergies = await this.BaseDal.SqlQueryable(sql); + return emeterEnergies; + } +} \ No newline at end of file diff --git a/Service/Station/BatteryOpModelDetailService.cs b/Service/Station/BatteryOpModelDetailService.cs index 1eefa4c..a67b193 100644 --- a/Service/Station/BatteryOpModelDetailService.cs +++ b/Service/Station/BatteryOpModelDetailService.cs @@ -7,20 +7,22 @@ using Repository.Station; using SqlSugar; namespace Service.Station; -[Scope("SingleInstance")] -public class BatteryOpModelDetailService: BaseServices +[Scope("SingleInstance")] +public class BatteryOpModelDetailService : BaseServices { 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; } + /// /// 电池运营模型详情分页列表 🔖 /// @@ -44,6 +46,7 @@ public class BatteryOpModelDetailService: BaseServices Rows = items, }; } + /// /// 删除电池运营模型 🔖 /// @@ -57,7 +60,7 @@ public class BatteryOpModelDetailService: BaseServices // 删除模型 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 if (batteay && batteryOpModelDetail) { return true; - } return false; } - + /// /// 增加电池运营模型 🔖 /// @@ -83,6 +85,15 @@ public class BatteryOpModelDetailService: BaseServices // 事务 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.Fail("开始时间不能大于结束时间"); + } + } + List batteryOpModelDetails = await _batteryOpModelDetailRepository.QueryAsync(); foreach (var detail in batteryOpModelDetails) { @@ -91,12 +102,13 @@ public class BatteryOpModelDetailService: BaseServices return Result.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.Fail("相同时间已经存在"); } } - + // 检查两个表模型id是否已存在 var isExistModel = await _batteryOpModelRepository.QueryByClauseAsync(u => u.ModelId == input.ModelId); if (isExistModel != null) @@ -118,32 +130,36 @@ public class BatteryOpModelDetailService: BaseServices // 新增 transactionScope.Complete(); return Result.Success("新增id:" + insertedDetail.Id); - } } - public virtual async Task UpdateBatteryOpModelDetail(UpdateBatteryOpModelDetailReq batteryOpReq) + public virtual async Task> UpdateBatteryOpModelDetail(UpdateBatteryOpModelDetailReq batteryOpReq) { + if (DateTime.TryParse(batteryOpReq.StartTime, out DateTime startTime) && + DateTime.TryParse(batteryOpReq.EndTime, out DateTime endTime)) + { + if (startTime > endTime) + { + return Result.Fail("开始时间不能大于结束时间"); + } + } + List 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.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.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 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.Success("修改成功"); } - return false; + return Result.Success("修改失败"); } - } \ No newline at end of file diff --git a/WebStarter/Controllers/Ammeter/AmmeterController.cs b/WebStarter/Controllers/Ammeter/AmmeterController.cs index 9d76f6a..e919ea8 100644 --- a/WebStarter/Controllers/Ammeter/AmmeterController.cs +++ b/WebStarter/Controllers/Ammeter/AmmeterController.cs @@ -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; /// + /// 直流电 + /// + private EmeterEnergyService _emeterEnergyService; + private EmeterEnergyChangeService _emeterEnergyChangeService; + /// /// 查询电表信息 /// /// @@ -30,27 +37,104 @@ namespace WebStarter.Controllers.Ammeter /// 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; } /// /// 七天电表用量 /// /// /// - [HttpGet("SevenDaysElectricalEnergy")] - public async Task>> SevenDaysElectricalEnergy() + [HttpGet("SevenDaysElectrical")] + public async Task> SevenDaysElectrical() { - return Result>.Success(await _emeterDayEnergyService.SevenDaysElectricalEnergy()); - } + EmeterEnergyResp resp = new EmeterEnergyResp(); + // 获取直流和交流表数据 + List sevenDaysElectrical = await _emeterEnergyService.SevenDaysElectrical(); + List emeterEnergyChanges = await _emeterEnergyChangeService.SevenDaysElectrical(); + + var config = new MapperConfiguration(cfg => + { + cfg.CreateMap().ReverseMap(); + cfg.CreateMap().ReverseMap(); + }); + IMapper mapper = config.CreateMapper(); + + List emeterEnergyList = mapper.Map>(sevenDaysElectrical); + List sevenEmeterResps = mapper.Map>(emeterEnergyChanges); + resp.emeterEnergy = emeterEnergyList; + resp.emeterEnergyChange = sevenEmeterResps; + + return Result.Success(resp,"成功"); + } #region 统计分析/电能表累计值信息 + /// + /// 电能表累计值 + /// + /// + [HttpGet("GetAllElectricalData")] + public async Task> GetAllElectricalData() + { + EmeterEnergyResp resp = new EmeterEnergyResp(); + // 获取直流和交流表数据 + List Electrical = await _emeterEnergyService.GetAllElectricalData(); + List EnergyChanges = await _emeterEnergyChangeService.GetAllElectricalData(); + + var config = new MapperConfiguration(cfg => + { + cfg.CreateMap().ReverseMap(); + cfg.CreateMap().ReverseMap(); + }); + IMapper mapper = config.CreateMapper(); + + List EnergyList = mapper.Map>(Electrical); + List EmeterResps = mapper.Map>(EnergyChanges); + + resp.emeterEnergy = EnergyList; + resp.emeterEnergyChange = EmeterResps; + + return Result.Success(resp,"成功"); + } + + /// + /// 电能根据小时统计 + /// + /// + [HttpGet("GetTodayHourlyElectricalData")] + public async Task> GetTodayHourlyElectricalData() + { + EmeterEnergyResp resp = new EmeterEnergyResp(); + // 获取直流和交流表数据 + + List Electrical = await _emeterEnergyService.GetTodayHourlyElectricalData(); + List EnergyChanges = await _emeterEnergyChangeService.GetTodayHourlyElectricalData(); + + var config = new MapperConfiguration(cfg => + { + cfg.CreateMap().ReverseMap(); + cfg.CreateMap().ReverseMap(); + }); + IMapper mapper = config.CreateMapper(); + + List EnergyList = mapper.Map>(Electrical); + List EmeterResps = mapper.Map>(EnergyChanges); + + resp.emeterEnergy = EnergyList; + resp.emeterEnergyChange = EmeterResps; + + return Result.Success(resp,"成功"); + } /// /// 统计分析/电能表累计值信息 /// diff --git a/WebStarter/Controllers/BatteryOpModelDetailController.cs b/WebStarter/Controllers/BatteryOpModelDetailController.cs index d363620..ea21c9a 100644 --- a/WebStarter/Controllers/BatteryOpModelDetailController.cs +++ b/WebStarter/Controllers/BatteryOpModelDetailController.cs @@ -45,6 +45,7 @@ public class BatteryOpModelDetailController { return await _batteryOpModelDetailService.AddBatteryOpModelDetail(input); } + /// /// 更新电池运营模型 /// @@ -52,13 +53,9 @@ public class BatteryOpModelDetailController /// [HttpPost] [Route("update")] - public async Task> UpdateBatteryOpModelDetail([FromBody] UpdateBatteryOpModelDetailReq batteryOpReq) + public async Task> UpdateBatteryOpModelDetail([FromBody] UpdateBatteryOpModelDetailReq batteryOpReq) { - var data = await _batteryOpModelDetailService.UpdateBatteryOpModelDetail(batteryOpReq); - if (data) - return Result.Success(data); - else - return Result.Fail(data); + return await _batteryOpModelDetailService.UpdateBatteryOpModelDetail(batteryOpReq); } /// /// 删除电池运营模型