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.

160 lines
6.0 KiB

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;
using Entity.Dto.Resp;
namespace Service.Station
{
[Scope("SingleInstance")]
public class ExStationDayRunResultService : BaseServices<ExchangeStationDayRunResult>
{
ExStationDayRunResultRepository _exStationDayRunResultRepository;
public ExStationDayRunResultService(ExStationDayRunResultRepository dal)
{
_exStationDayRunResultRepository = dal;
BaseDal = dal;
}
/// <summary>
/// 展示换电站日运行统计结果
/// </summary>
/// <returns></returns>
public async Task<PageResult<ExchangeStationDayRunResult>> ExStationDaySwapRunRes(QueryPageModel queryPageModel)
{
return PageResult<ExchangeStationDayRunResult>.ConvertPage(_exStationDayRunResultRepository.QueryIPageByCause(queryPageModel, null));
}
/// <summary>
/// 根据id 删除换电站日运行结果
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<bool> DeleteDaySwapRunRes(int id)
{
return await _exStationDayRunResultRepository.DeleteByIdAsync(id);
}
/// <summary>
/// 根据id 批量删除换电站日运行结果
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
public async Task<bool> BatchDeleteDaySwapRunRes(List<int> ids)
{
return await _exStationDayRunResultRepository.DeleteByIdsAsync(ids);
}
/// <summary>
/// 导出换电站日运行统计结果 🔖
/// </summary>
/// <returns></returns>
public async Task<IActionResult> ExportExchangeStationDayRunResult()
{
List<ExchangeStationDayRunResult> exchangeStationDayRunResults = await _exStationDayRunResultRepository.QueryAsync();
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ExchangeStationDayRunDto, ExchangeStationDayRunResult>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List<ExchangeStationDayRunDto> logExList = mapper.Map<List<ExchangeStationDayRunDto>>(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" };
}
/// <summary>
/// 获取一个月的每日数据
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public async Task<List<ExchangeStationDayRunResult>> GetMonthRunResult()
{
var monthTime1 = DateTimeOffset.UtcNow.AddMonths(-1).ToString("yyyy/MM/01 00:00:00");//上月1号
var monthTime2 = DateTimeOffset.UtcNow.AddMonths(0).ToString("yyyy/MM/01 00:00:00");//本月1号
var runresult = await _exStationDayRunResultRepository.QueryListByClauseAsync(u => u.CreatedTime >= Convert.ToDateTime(monthTime1) && u.CreatedTime < Convert.ToDateTime(monthTime2));
return runresult;
}
/// <summary>
/// 获取一年内每月的数据和
/// </summary>
/// <returns></returns>
public async Task<List<ExchangeStationDayRunResultResp>> GetYearRunResult()
{
var monthTime1 = DateTimeOffset.UtcNow.AddYears(0).ToString("yyyy/01/01 00:00:00");//今年1月1号
var monthTime2 = DateTimeOffset.UtcNow.AddMonths(0).ToString("yyyy/MM/01 00:00:00");//本月1号
var runresult = _exStationDayRunResultRepository.QueryListByClause(u => u.CreatedTime >= Convert.ToDateTime(monthTime1) && u.CreatedTime < Convert.ToDateTime(monthTime2))
.GroupBy(d => d.CreatedTime.Value.Month);
// 对每个月的数据求和
Dictionary<int, ExchangeStationDayRunResultResp> monthlyTotals = new Dictionary<int, ExchangeStationDayRunResultResp>();
foreach (var group in runresult)
{
var month = group.Key;
DateTime CreatedTime = (DateTime)group.First().CreatedTime;
//CreatedTime=Convert.ToDateTime(CreatedTime.ToString("yyyy/MM"));
DateTime UpdatedTime = (DateTime)group.First().UpdatedTime;
//UpdatedTime = Convert.ToDateTime(CreatedTime.ToString("yyyy/MM"));
ExchangeStationDayRunResultResp runResult = new ExchangeStationDayRunResultResp
{
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= CreatedTime.ToString("yyyy/MM"),
UpdatedTime = UpdatedTime.ToString("yyyy/MM"),
};
monthlyTotals[month] = runResult;
}
List<ExchangeStationDayRunResultResp> resultList = monthlyTotals
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
.Select(pair => pair.Value)
.ToList();
return resultList;
}
}
}