Export.cshtml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.Mvc.RazorPages;
  9. using Microsoft.EntityFrameworkCore;
  10. using WZExport.Data;
  11. using WZExport.Models;
  12. using static WZExport.Utilitys.ExcelHelper;
  13. namespace WZExport.Pages.Lms
  14. {
  15. public class ExportModel : PageModel
  16. {
  17. private readonly WZContext _context;
  18. public ExportModel(WZContext context)
  19. {
  20. _context = context;
  21. }
  22. private IList<Lm> _lms;
  23. public IActionResult OnGet()
  24. {
  25. return Page();
  26. }
  27. public async Task<IActionResult> OnPostAsync()
  28. {
  29. _lms = await _context.Lm.ToListAsync();
  30. string webRootFolder = $"/temp/{nameof(Lm)}/{DateTime.Now:yyyyMMdd}";
  31. string fileName = $"栏目导出-{DateTime.Now:yyyyMMddHHmmss}";
  32. string zipFilePath = $"/temp/{nameof(Lm)}/{fileName}.zip";
  33. if (Directory.Exists(webRootFolder) is false)
  34. {
  35. Directory.CreateDirectory(webRootFolder);
  36. var treeData = CoverTreeData();
  37. string sexLeveFolder = $"{webRootFolder}/层级6级及以内的栏目";
  38. Directory.CreateDirectory(sexLeveFolder);
  39. var sexLevelData = treeData.Where(lm => lm.Siteid <= 6).ToList();
  40. const int onceNumber = 1000;
  41. var onceData = new List<Lm>();
  42. int index = 0;
  43. foreach (var data in sexLevelData)
  44. {
  45. index++;
  46. if (data.Siteid == 1 && index>= onceNumber)
  47. {
  48. ExportLmUniversal(onceData, $"{fileName}-{index-1}", sexLeveFolder);
  49. onceData.Clear();
  50. index = 0;
  51. }
  52. onceData.Add(data);
  53. }
  54. ExportLmUniversal(onceData, $"{fileName}-{sexLevelData.Count}", sexLeveFolder);
  55. string otherFolder = $"{webRootFolder}/层级大于6级的栏目";
  56. Directory.CreateDirectory(otherFolder);
  57. var otherData = treeData.Where(lm => lm.Siteid > 6)
  58. .Select(lm=> { lm.Siteid = lm.Siteid - 6;return lm; })
  59. .ToList();
  60. var otherGpData = otherData.GroupBy(lm => lm.Parentid);
  61. foreach (var data in otherGpData)
  62. {
  63. string parentName = _lms.FirstOrDefault(lm=>lm.Channelid==data.Key).Chnldesc;
  64. fileName = $"{parentName}-超出六级栏目";
  65. ExportLmUniversal(data, fileName, otherFolder);
  66. }
  67. }
  68. ZipFile.CreateFromDirectory(webRootFolder, zipFilePath);
  69. return File(System.IO.File.OpenRead(zipFilePath), "application/x-zip-compressed", fileName+ ".zip");
  70. }
  71. private IList<Lm> CoverTreeData()
  72. {
  73. var treeLms = new List<Lm>();
  74. var parentLms = _lms.Where(lm =>lm.Parentid==0).OrderByDescending(lm =>lm.Chnlorder);
  75. foreach (var parentLm in parentLms)
  76. {
  77. FindSon(parentLm,1);
  78. }
  79. return treeLms;
  80. void FindSon(Lm parentLm,int index)
  81. {
  82. parentLm.Siteid = index;
  83. treeLms.Add(parentLm);
  84. var sonLms = _lms.Where(lm => lm.Parentid == parentLm.Channelid).OrderByDescending(lm => lm.Chnlorder).ToList();
  85. if (sonLms.Count > 0)
  86. {
  87. foreach (var sonLm in sonLms)
  88. {
  89. FindSon(sonLm, index+1);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }