AdminStorehouseController.cs 3.7 KB

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