AdminFinanceController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using ExcelDataReader;
  7. using GxPress.Common.Exceptions;
  8. using GxPress.Common.Page;
  9. using GxPress.Entity;
  10. using GxPress.Repository.Interface;
  11. using GxPress.Request.Finance;
  12. using GxPress.Request.User;
  13. using GxPress.Result.Finance;
  14. using Microsoft.AspNetCore.Authorization;
  15. using Microsoft.AspNetCore.Mvc;
  16. using Microsoft.Extensions.Logging;
  17. namespace GxPress.Api.AdminControllers
  18. {
  19. /// <summary>
  20. /// 财务数据
  21. /// </summary>
  22. [Route("api/admin/finance")]
  23. [ApiController]
  24. [Authorize]
  25. public class AdminFinanceController : ControllerBase
  26. {
  27. private readonly ILogger<AdminFinanceController> _logger;
  28. private readonly IFinanceRepository _repository;
  29. public AdminFinanceController(ILogger<AdminFinanceController> logger, IFinanceRepository repository)
  30. {
  31. _logger = logger;
  32. _repository = repository;
  33. }
  34. /// <summary>
  35. /// 解析财务数据Excel数据
  36. /// </summary>
  37. /// <param name="request"></param>
  38. /// <returns></returns>
  39. [HttpPost("upload")]
  40. public async Task<bool> FinanceExcelUpload([FromBody] UserExcelUploadRequest request)
  41. {
  42. var relativePath = request.ExcelUrl;
  43. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  44. //获取excel分析
  45. var stream = System.IO.File.Open("wwwroot/" + relativePath, FileMode.Open, FileAccess.Read);
  46. IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream);
  47. var result = reader.AsDataSet();
  48. var sheet = result.Tables["Sheet1"];
  49. var error = string.Empty;
  50. var listFinance = new List<Finance>();
  51. for (int i = 1; i < sheet.Rows.Count; i++) //行
  52. {
  53. var finance = new Finance();
  54. if (i == 1)
  55. continue;
  56. var amount = sheet.Rows[i][0].ToString().Trim(); //金额
  57. if (string.IsNullOrEmpty(amount))
  58. error = "金额必填";
  59. finance.Amount = int.Parse(amount);
  60. var financeType = sheet.Rows[i][1].ToString().Trim(); //类型
  61. if (string.IsNullOrEmpty(financeType))
  62. error = "类型必须";
  63. finance.FinanceType = int.Parse(financeType);
  64. var enteringDateTime = sheet.Rows[i][2].ToString().Trim(); //录入时间
  65. if (string.IsNullOrEmpty(enteringDateTime))
  66. error = "录入时间必填";
  67. finance.EnteringDateTime = DateTime.Parse(enteringDateTime);
  68. if (!string.IsNullOrEmpty(error))
  69. break;
  70. listFinance.Add(finance);
  71. }
  72. if (!string.IsNullOrEmpty(error)) throw new BusinessException(error);
  73. //计算
  74. var success = await _repository.InsertAsync(listFinance);
  75. if (success == false)
  76. throw new BusinessException("服务器异常");
  77. return true;
  78. }
  79. /// <summary>
  80. /// 财务列表
  81. /// </summary>
  82. /// <param name="request"></param>
  83. /// <returns></returns>
  84. [HttpPost("list")]
  85. public async Task<PagedList<Finance>> GetPagedList(PageParameter request)
  86. {
  87. return await _repository.GetPageListAsync(request);
  88. }
  89. /// <summary>
  90. /// 财务图表
  91. /// </summary>
  92. /// <param name="request"></param>
  93. /// <returns></returns>
  94. [HttpPost("chart")]
  95. public async Task<FinanceResult> GetFinanceChart(FinanceRequest request)
  96. {
  97. return await _repository.GetFinanceChartAsync(request);
  98. }
  99. }
  100. }