ReportController.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using Microsoft.AspNetCore.Cors;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.Logging;
  6. using MS.Component.Aop;
  7. using MS.Models;
  8. using MS.Services;
  9. using MS.Services.PosTrxService;
  10. using MS.WebCore.Core;
  11. using NPOI.SS.UserModel;
  12. using NPOI.XSSF.UserModel;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Net.Http;
  17. using System.Threading.Tasks;
  18. namespace MS.WebApi.Controllers
  19. {
  20. [Route("api/[controller]")]
  21. [ApiController]
  22. public class ReportController : ControllerBase
  23. {
  24. /// <summary>
  25. /// 报表的地址
  26. /// </summary>
  27. readonly static string ReportServerUrl= "ReportServerUrl";
  28. private readonly IPosTrxService _PosTrxService;
  29. private readonly IAccountService _AccountService;
  30. private readonly ILogger<ReportController> _logger;
  31. private readonly IConfiguration _configuration;
  32. public ReportController(IPosTrxService posTrxService, IAccountService accountService, ILogger<ReportController> logger, IConfiguration Configuration)
  33. {
  34. _PosTrxService = posTrxService;
  35. _AccountService = accountService;
  36. _logger = logger;
  37. _configuration = Configuration;
  38. }
  39. [Route("NozzleTotalizer")]
  40. [EnableCors("AllowSpecificOrigin")]
  41. [HttpPost]
  42. public async Task<ExecuteResult> NozzleTotalizer([FromBody] ReportRequest request)
  43. {
  44. return await _PosTrxService.NozzleTotalizerShiftReportAsync(request.startTime, request.endTime, request.currentBuId);
  45. }
  46. //[Route("ExportToExcel")]
  47. //[EnableCors("AllowSpecificOrigin")]
  48. //[HttpPost]
  49. //public async Task<IActionResult> ExportToExcel([FromBody] ReportRequest request)
  50. //{
  51. // var nozzleTotalizers = await _PosTrxService.GetListFromSessionAsync(request.startTime, request.endTime, request.currentBuId); // 异步获取NozzleTotalizers数据
  52. // var stream = await _PosTrxService.ExportToExcelAsync(nozzleTotalizers);
  53. // // 返回Excel文件
  54. // return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "NozzleTotalizers.xlsx");
  55. //}
  56. [Route("ExportToExcel")]
  57. [EnableCors("AllowSpecificOrigin")]
  58. [HttpGet]
  59. public async Task<FileContentResult> ExportToExcel(string startTime,
  60. string endTime,
  61. string currentBuId)
  62. {
  63. // 获取数据源
  64. List<NozzleTotalizerDto> nozzleTotalizers = await _PosTrxService.GetListFromSessionAsync(startTime, endTime, currentBuId); // 异步获取NozzleTotalizers数据
  65. // 创建Excel文档
  66. IWorkbook workbook = new XSSFWorkbook();
  67. ISheet sheet = workbook.CreateSheet("Nozzle Totalizers");
  68. // 创建表头
  69. IRow headerRow = sheet.CreateRow(0);
  70. headerRow.CreateCell(0).SetCellValue("枪号");
  71. headerRow.CreateCell(1).SetCellValue("油品名称");
  72. headerRow.CreateCell(2).SetCellValue("单价");
  73. headerRow.CreateCell(3).SetCellValue("起泵数");
  74. headerRow.CreateCell(4).SetCellValue("止泵数");
  75. headerRow.CreateCell(5).SetCellValue("销售升数");
  76. headerRow.CreateCell(6).SetCellValue("回罐");
  77. headerRow.CreateCell(7).SetCellValue("自用");
  78. headerRow.CreateCell(8).SetCellValue("流水升数");
  79. headerRow.CreateCell(9).SetCellValue("应收款");
  80. headerRow.CreateCell(10).SetCellValue("折扣");
  81. headerRow.CreateCell(11).SetCellValue("实收款");
  82. // 填充数据
  83. for (int i = 0; i < nozzleTotalizers.Count; i++)
  84. {
  85. NozzleTotalizerDto nozzleTotalizer = nozzleTotalizers[i];
  86. IRow dataRow = sheet.CreateRow(i + 1);
  87. dataRow.CreateCell(0).SetCellValue((int)nozzleTotalizer.NozzleId);
  88. dataRow.CreateCell(1).SetCellValue(nozzleTotalizer.GradeName);
  89. dataRow.CreateCell(2).SetCellValue((double)nozzleTotalizer.CurrentPrice);
  90. dataRow.CreateCell(3).SetCellValue((double)nozzleTotalizer.VolumeOnPumpStart);
  91. dataRow.CreateCell(4).SetCellValue((double)nozzleTotalizer.VolumeOnPumpEnd);
  92. dataRow.CreateCell(5).SetCellValue((double)nozzleTotalizer.VolumeOnPos);
  93. dataRow.CreateCell(6).SetCellValue((double)nozzleTotalizer.PumpTestVolume);
  94. dataRow.CreateCell(7).SetCellValue((double)nozzleTotalizer.SelfUseVolume);
  95. dataRow.CreateCell(8).SetCellValue((double)nozzleTotalizer.TotalVolume);
  96. dataRow.CreateCell(9).SetCellValue((double)nozzleTotalizer.TotalPaid);
  97. dataRow.CreateCell(10).SetCellValue((double)nozzleTotalizer.TotalDiscount);
  98. dataRow.CreateCell(11).SetCellValue((double)nozzleTotalizer.TotalNetAmount);
  99. }
  100. // 设置列宽
  101. sheet.SetColumnWidth(0, 10 * 256);
  102. sheet.SetColumnWidth(1, 15 * 256);
  103. sheet.SetColumnWidth(2, 15 * 256);
  104. sheet.SetColumnWidth(3, 20 * 256);
  105. sheet.SetColumnWidth(4, 20 * 256);
  106. sheet.SetColumnWidth(5, 15 * 256);
  107. sheet.SetColumnWidth(6, 15 * 256);
  108. sheet.SetColumnWidth(7, 15 * 256);
  109. sheet.SetColumnWidth(8, 15 * 256);
  110. sheet.SetColumnWidth(9, 15 * 256);
  111. sheet.SetColumnWidth(10, 15 * 256);
  112. sheet.SetColumnWidth(11, 15 * 256);
  113. sheet.SetColumnWidth(12, 20 * 256);
  114. sheet.SetColumnWidth(13, 15 * 256);
  115. // 将Excel文档写入内存流
  116. MemoryStream stream = new MemoryStream();
  117. workbook.Write(stream);
  118. byte[] bytes = stream.ToArray();
  119. // 返回Excel文件
  120. return new FileContentResult(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  121. }
  122. /// <summary>
  123. /// 执行统计报表,日,周,月
  124. /// 9999默认初始化全部数据
  125. /// </summary>
  126. /// <param name="type"></param>
  127. /// <returns></returns>
  128. [Route("ExportToExcel")]
  129. [EnableCors("AllowSpecificOrigin")]
  130. [HttpGet]
  131. public async Task<object> BusinessScopeReportTimeJob(int type = 1,string time="",int pagesize=13)
  132. {
  133. _logger.LogInformation($"{type}--BusinessScopeReportTimeJob start {DateTime.Now}");
  134. var buids =await _AccountService.GetBuids();
  135. _logger.LogInformation($"buids 个数 {buids} time:{DateTime.Now}");
  136. var bassAddress = _configuration.GetSection(ReportServerUrl).Value;
  137. HttpClient httpClient = new HttpClient();
  138. httpClient.BaseAddress=new Uri(bassAddress);
  139. foreach (var buid in buids)
  140. {
  141. //日执行
  142. if (type == 1 || type == 9999)
  143. {
  144. var url = $"/api/Report/BusinessScopeReportTimeJob?pageNumber=0&pageCount=13&reportType=day";
  145. if (type == 9999)
  146. {
  147. url = $"/api/Report/BusinessScopeReportTimeJob?pageNumber=0&pageCount=80&reportType=day";
  148. }
  149. var result=await httpClient.GetAsync(url);
  150. _logger.LogInformation($"{buid}日报表 统计执行完成 {result.StatusCode}-{result.Content.ToString()} time:{DateTime.Now}");
  151. }
  152. //周执行
  153. if (type == 2 || type == 9999)
  154. {
  155. var url = $"/api/Report/BusinessScopeReportTimeJob?pageNumber=0&pageCount=5&reportType=week";
  156. if (type == 9999)
  157. {
  158. url = $"/api/Report/BusinessScopeReportTimeJob?pageNumber=0&pageCount=20&reportType=week";
  159. }
  160. var result = await httpClient.GetAsync(url);
  161. _logger.LogInformation($"{buid}周报表 统计执行完成 {result.StatusCode}-{result.Content.ToString()} time:{DateTime.Now}");
  162. }
  163. //月执行
  164. if (type == 3 || type == 9999)
  165. {
  166. var url = $"/api/Report/BusinessScopeReportTimeJob?pageNumber=0&pageCount=4&reportType=month";
  167. if (type == 9999)
  168. {
  169. url = $"/api/Report/BusinessScopeReportTimeJob?pageNumber=0&pageCount=6&reportType=month";
  170. }
  171. var result = await httpClient.GetAsync(url);
  172. _logger.LogInformation($"{buid}月报表 统计执行完成 {result.StatusCode}-{result.Content.ToString()} time:{DateTime.Now}");
  173. }
  174. }
  175. _logger.LogInformation($"{type}--BusinessScopeReportTimeJob end {DateTime.Now}");
  176. return new { message=$"执行完成{DateTime.Now}" };
  177. }
  178. }
  179. }