using Common.Enum; using Entity.Api.Resp; using Entity.DbModel.Station; using Entity.Dto.Req; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Entity; using Repository.Station; using Repository.System; using Service.Mgr; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AutoMapper; using Entity.Dto; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.Mvc; using SqlSugar; namespace Service.Station { [Scope("SingleInstance")] public class ExStationDayRunResultService : BaseServices { ExStationDayRunResultRepository _exStationDayRunResultRepository; public ExStationDayRunResultService(ExStationDayRunResultRepository dal) { _exStationDayRunResultRepository = dal; BaseDal = dal; } /// /// 展示换电站日运行统计结果 /// /// public async Task> ExStationDaySwapRunRes(QueryPageModel queryPageModel) { return PageResult.ConvertPage(_exStationDayRunResultRepository.QueryIPageByCause(queryPageModel, null)); } /// /// 根据id 删除换电站日运行结果 /// /// /// public async Task DeleteDaySwapRunRes(int id) { return await _exStationDayRunResultRepository.DeleteByIdAsync(id); } /// /// 根据id 批量删除换电站日运行结果 /// /// /// public async Task BatchDeleteDaySwapRunRes(List ids) { return await _exStationDayRunResultRepository.DeleteByIdsAsync(ids); } /// /// 导出换电站日运行统计结果 🔖 /// /// public async Task ExportExchangeStationDayRunResult() { List exchangeStationDayRunResults = await _exStationDayRunResultRepository.QueryAsync(); var config = new MapperConfiguration(cfg => { cfg.CreateMap().ReverseMap(); }); IMapper mapper = config.CreateMapper(); List logExList = mapper.Map>(exchangeStationDayRunResults); IExcelExporter excelExporter = new ExcelExporter(); var res = await excelExporter.ExportAsByteArray(logExList); return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "换电站日运行统计.xlsx" }; } /// /// 获取一个月的每日数据 /// /// /// public async Task> GetMonthRunResult() { var runresult = await _exStationDayRunResultRepository.QueryListByClauseAsync(u => u.CreatedTime >= DateTimeOffset.UtcNow.AddDays(-30)); return runresult; } /// /// 获取一年内每月的数据和 /// /// public async Task> GetYearRunResult() { var runresult = _exStationDayRunResultRepository.QueryListByClause(u => u.CreatedTime >= DateTimeOffset.UtcNow.AddMonths(-12)) .GroupBy(d => d.CreatedTime.Value.Month); // 对每个月的数据求和 Dictionary monthlyTotals = new Dictionary(); foreach (var group in runresult) { var month = group.Key; ExchangeStationDayRunResult runResult = new ExchangeStationDayRunResult { AvgChgTime = group.Sum(d => Convert.ToDouble(d.AvgChgTime)).ToString("F2"), AvgRepTime = group.Sum(d => Convert.ToDouble(d.AvgRepTime)).ToString("F2"), ChgCount = group.Sum(d => d.ChgCount), ToltalSwapCount = group.Sum(d => d.ToltalSwapCount), ToltalSwapAllTime = group.Sum(d => d.ToltalSwapAllTime), ToltalTimeCount = group.Sum(d => d.ToltalTimeCount), CreatedTime= group.First().CreatedTime, UpdatedTime = group.First().UpdatedTime, }; monthlyTotals[month] = runResult; } List resultList = monthlyTotals .ToDictionary(kvp => kvp.Key, kvp => kvp.Value) .Select(pair => pair.Value) .ToList(); return resultList; } } }