ImportConfig.cshtml.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.Http;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.AspNetCore.Mvc.RazorPages;
  10. using Microsoft.EntityFrameworkCore;
  11. using Microsoft.Extensions.FileProviders;
  12. using WZExport.Data;
  13. using WZExport.Models;
  14. using WZExport.Utilitys;
  15. using static WZExport.Utilitys.ExcelHelper;
  16. namespace WZExport.Pages.Config
  17. {
  18. public class ImportConfig : PageModel
  19. {
  20. private readonly WZContext _context;
  21. public ImportConfig(WZContext context)
  22. {
  23. _context = context;
  24. }
  25. private IList<Lm> _lms;
  26. public IActionResult OnGet()
  27. {
  28. return Page();
  29. }
  30. [BindProperty]
  31. public string Type { get; set; }
  32. public readonly string _type1 = "导入组配(Excel)";
  33. public readonly string _type2 = "导入节点(Excel)";
  34. [BindProperty]
  35. public IFormFile FormFile { get; set; }
  36. public async Task OnPostAsync()
  37. {
  38. _lms = await _context.Lm.ToListAsync();
  39. var dics = ExcelHelper.ImportConfig(FormFile.OpenReadStream());
  40. var sonDicTemp = new List<Dictionary>();
  41. foreach (var dic in dics)
  42. {
  43. dic.Key = _lms.FirstOrDefault(lm =>lm.Chnldesc==dic.KeyLable)?.Channelid??1;
  44. if (Type == _type1) dic.Type = 1;
  45. else if (Type == _type2)
  46. {
  47. dic.Type = 2;
  48. sonDicTemp.AddRange(GetAllSon(dic));
  49. }
  50. }
  51. dics.AddRange(sonDicTemp);
  52. var c = dics.Where(dic => dic.Value ==null);
  53. using var context = DatabaseService.Current.GetDbContext();
  54. context.InsertRange(dics.Where(dic =>dic.Key!=1).ToList());
  55. List<Dictionary> GetAllSon(Dictionary dic)
  56. {
  57. var dicTemp = new List<Dictionary>();
  58. FindSon(dic.Key);
  59. return dicTemp;
  60. void FindSon(int patentId)
  61. {
  62. var sonLms = _lms.Where(lm => lm.Parentid == patentId).ToList();
  63. var sonDics = sonLms.Select(lm => new Dictionary {Key=lm.Channelid,KeyLable=lm.Chnldesc,Value=dic.Value,Type=2 }).ToList();
  64. dicTemp.AddRange(sonDics);
  65. foreach (var lm in sonLms)
  66. {
  67. FindSon(lm.Channelid);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }