using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using MS.Component.Aop; using MS.Models; using MS.Services; using MS.Services.PosTrxService; using MS.WebCore.Core; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Threading.Tasks; namespace MS.WebApi.Controllers { [Route("api/[controller]")] [ApiController] public class ReportController : ControllerBase { /// /// 报表的地址 /// readonly static string ReportServerUrl= "ReportServerUrl"; private readonly IPosTrxService _PosTrxService; private readonly IAccountService _AccountService; private readonly ILogger _logger; private readonly IConfiguration _configuration; public ReportController(IPosTrxService posTrxService, IAccountService accountService, ILogger logger, IConfiguration Configuration) { _PosTrxService = posTrxService; _AccountService = accountService; _logger = logger; _configuration = Configuration; } [Route("NozzleTotalizer")] [EnableCors("AllowSpecificOrigin")] [HttpPost] public async Task NozzleTotalizer([FromBody] ReportRequest request) { return await _PosTrxService.NozzleTotalizerShiftReportAsync(request.startTime, request.endTime, request.currentBuId); } //[Route("ExportToExcel")] //[EnableCors("AllowSpecificOrigin")] //[HttpPost] //public async Task ExportToExcel([FromBody] ReportRequest request) //{ // var nozzleTotalizers = await _PosTrxService.GetListFromSessionAsync(request.startTime, request.endTime, request.currentBuId); // 异步获取NozzleTotalizers数据 // var stream = await _PosTrxService.ExportToExcelAsync(nozzleTotalizers); // // 返回Excel文件 // return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "NozzleTotalizers.xlsx"); //} [Route("ExportToExcel")] [EnableCors("AllowSpecificOrigin")] [HttpGet] public async Task ExportToExcel(string startTime, string endTime, string currentBuId) { // 获取数据源 List nozzleTotalizers = await _PosTrxService.GetListFromSessionAsync(startTime, endTime, currentBuId); // 异步获取NozzleTotalizers数据 // 创建Excel文档 IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet("Nozzle Totalizers"); // 创建表头 IRow headerRow = sheet.CreateRow(0); headerRow.CreateCell(0).SetCellValue("枪号"); headerRow.CreateCell(1).SetCellValue("油品名称"); headerRow.CreateCell(2).SetCellValue("单价"); headerRow.CreateCell(3).SetCellValue("起泵数"); headerRow.CreateCell(4).SetCellValue("止泵数"); headerRow.CreateCell(5).SetCellValue("销售升数"); headerRow.CreateCell(6).SetCellValue("回罐"); headerRow.CreateCell(7).SetCellValue("自用"); headerRow.CreateCell(8).SetCellValue("流水升数"); headerRow.CreateCell(9).SetCellValue("应收款"); headerRow.CreateCell(10).SetCellValue("折扣"); headerRow.CreateCell(11).SetCellValue("实收款"); // 填充数据 for (int i = 0; i < nozzleTotalizers.Count; i++) { NozzleTotalizerDto nozzleTotalizer = nozzleTotalizers[i]; IRow dataRow = sheet.CreateRow(i + 1); dataRow.CreateCell(0).SetCellValue((int)nozzleTotalizer.NozzleId); dataRow.CreateCell(1).SetCellValue(nozzleTotalizer.GradeName); dataRow.CreateCell(2).SetCellValue((double)nozzleTotalizer.CurrentPrice); dataRow.CreateCell(3).SetCellValue((double)nozzleTotalizer.VolumeOnPumpStart); dataRow.CreateCell(4).SetCellValue((double)nozzleTotalizer.VolumeOnPumpEnd); dataRow.CreateCell(5).SetCellValue((double)nozzleTotalizer.VolumeOnPos); dataRow.CreateCell(6).SetCellValue((double)nozzleTotalizer.PumpTestVolume); dataRow.CreateCell(7).SetCellValue((double)nozzleTotalizer.SelfUseVolume); dataRow.CreateCell(8).SetCellValue((double)nozzleTotalizer.TotalVolume); dataRow.CreateCell(9).SetCellValue((double)nozzleTotalizer.TotalPaid); dataRow.CreateCell(10).SetCellValue((double)nozzleTotalizer.TotalDiscount); dataRow.CreateCell(11).SetCellValue((double)nozzleTotalizer.TotalNetAmount); } // 设置列宽 sheet.SetColumnWidth(0, 10 * 256); sheet.SetColumnWidth(1, 15 * 256); sheet.SetColumnWidth(2, 15 * 256); sheet.SetColumnWidth(3, 20 * 256); sheet.SetColumnWidth(4, 20 * 256); sheet.SetColumnWidth(5, 15 * 256); sheet.SetColumnWidth(6, 15 * 256); sheet.SetColumnWidth(7, 15 * 256); sheet.SetColumnWidth(8, 15 * 256); sheet.SetColumnWidth(9, 15 * 256); sheet.SetColumnWidth(10, 15 * 256); sheet.SetColumnWidth(11, 15 * 256); sheet.SetColumnWidth(12, 20 * 256); sheet.SetColumnWidth(13, 15 * 256); // 将Excel文档写入内存流 MemoryStream stream = new MemoryStream(); workbook.Write(stream); byte[] bytes = stream.ToArray(); // 返回Excel文件 return new FileContentResult(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); } /// /// 执行统计报表,日,周,月 /// 9999默认初始化全部数据 /// /// /// [Route("ExportToExcel")] [EnableCors("AllowSpecificOrigin")] [HttpGet] public async Task BusinessScopeReportTimeJob(int type = 1,string time="",int pagesize=13) { _logger.LogInformation($"{type}--BusinessScopeReportTimeJob start {DateTime.Now}"); var buids =await _AccountService.GetBuids(); _logger.LogInformation($"buids 个数 {buids} time:{DateTime.Now}"); var bassAddress = _configuration.GetSection(ReportServerUrl).Value; HttpClient httpClient = new HttpClient(); httpClient.BaseAddress=new Uri(bassAddress); foreach (var buid in buids) { //日执行 if (type == 1 || type == 9999) { var url = $"/api/Report/BusinessScopeReportTimeJob?pageNumber=0&pageCount=13&reportType=day"; if (type == 9999) { url = $"/api/Report/BusinessScopeReportTimeJob?pageNumber=0&pageCount=80&reportType=day"; } var result=await httpClient.GetAsync(url); _logger.LogInformation($"{buid}日报表 统计执行完成 {result.StatusCode}-{result.Content.ToString()} time:{DateTime.Now}"); } //周执行 if (type == 2 || type == 9999) { var url = $"/api/Report/BusinessScopeReportTimeJob?pageNumber=0&pageCount=5&reportType=week"; if (type == 9999) { url = $"/api/Report/BusinessScopeReportTimeJob?pageNumber=0&pageCount=20&reportType=week"; } var result = await httpClient.GetAsync(url); _logger.LogInformation($"{buid}周报表 统计执行完成 {result.StatusCode}-{result.Content.ToString()} time:{DateTime.Now}"); } //月执行 if (type == 3 || type == 9999) { var url = $"/api/Report/BusinessScopeReportTimeJob?pageNumber=0&pageCount=4&reportType=month"; if (type == 9999) { url = $"/api/Report/BusinessScopeReportTimeJob?pageNumber=0&pageCount=6&reportType=month"; } var result = await httpClient.GetAsync(url); _logger.LogInformation($"{buid}月报表 统计执行完成 {result.StatusCode}-{result.Content.ToString()} time:{DateTime.Now}"); } } _logger.LogInformation($"{type}--BusinessScopeReportTimeJob end {DateTime.Now}"); return new { message=$"执行完成{DateTime.Now}" }; } } }