123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.RazorPages;
- using Microsoft.EntityFrameworkCore;
- using WZExport.Data;
- using WZExport.Models;
- using static WZExport.Utilitys.ExcelHelper;
- namespace WZExport.Pages.Lms
- {
- public class ExportModel : PageModel
- {
- private readonly WZContext _context;
- public ExportModel(WZContext context)
- {
- _context = context;
- }
- private IList<Lm> _lms;
- public IActionResult OnGet()
- {
- return Page();
- }
- public async Task<IActionResult> OnPostAsync()
- {
- _lms = await _context.Lm.ToListAsync();
- string webRootFolder = $"/temp/{nameof(Lm)}/{DateTime.Now:yyyyMMdd}";
- string fileName = $"栏目导出-{DateTime.Now:yyyyMMddHHmmss}";
- string zipFilePath = $"/temp/{nameof(Lm)}/{fileName}.zip";
- if (Directory.Exists(webRootFolder) is false)
- {
- Directory.CreateDirectory(webRootFolder);
- var treeData = CoverTreeData();
- string sexLeveFolder = $"{webRootFolder}/层级6级及以内的栏目";
- Directory.CreateDirectory(sexLeveFolder);
- var sexLevelData = treeData.Where(lm => lm.Siteid <= 6).ToList();
- const int onceNumber = 1000;
- var onceData = new List<Lm>();
- int index = 0;
- foreach (var data in sexLevelData)
- {
- index++;
- if (data.Siteid == 1 && index>= onceNumber)
- {
- ExportLmUniversal(onceData, $"{fileName}-{index-1}", sexLeveFolder);
- onceData.Clear();
- index = 0;
- }
- onceData.Add(data);
- }
- ExportLmUniversal(onceData, $"{fileName}-{sexLevelData.Count}", sexLeveFolder);
- string otherFolder = $"{webRootFolder}/层级大于6级的栏目";
- Directory.CreateDirectory(otherFolder);
- var otherData = treeData.Where(lm => lm.Siteid > 6)
- .Select(lm=> { lm.Siteid = lm.Siteid - 6;return lm; })
- .ToList();
- var otherGpData = otherData.GroupBy(lm => lm.Parentid);
- foreach (var data in otherGpData)
- {
- string parentName = _lms.FirstOrDefault(lm=>lm.Channelid==data.Key).Chnldesc;
- fileName = $"{parentName}-超出六级栏目";
- ExportLmUniversal(data, fileName, otherFolder);
- }
- }
- ZipFile.CreateFromDirectory(webRootFolder, zipFilePath);
- return File(System.IO.File.OpenRead(zipFilePath), "application/x-zip-compressed", fileName+ ".zip");
- }
- private IList<Lm> CoverTreeData()
- {
- var treeLms = new List<Lm>();
- var parentLms = _lms.Where(lm =>lm.Parentid==0).OrderByDescending(lm =>lm.Chnlorder);
- foreach (var parentLm in parentLms)
- {
- FindSon(parentLm,1);
- }
- return treeLms;
- void FindSon(Lm parentLm,int index)
- {
- parentLm.Siteid = index;
- treeLms.Add(parentLm);
- var sonLms = _lms.Where(lm => lm.Parentid == parentLm.Channelid).OrderByDescending(lm => lm.Chnlorder).ToList();
- if (sonLms.Count > 0)
- {
- foreach (var sonLm in sonLms)
- {
- FindSon(sonLm, index+1);
- }
- }
- }
- }
- }
- }
|