1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.RazorPages;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.FileProviders;
- using WZExport.Data;
- using WZExport.Models;
- using WZExport.Utilitys;
- using static WZExport.Utilitys.ExcelHelper;
- namespace WZExport.Pages.Config
- {
- public class ImportConfig : PageModel
- {
- private readonly WZContext _context;
- public ImportConfig(WZContext context)
- {
- _context = context;
- }
- private IList<Lm> _lms;
- public IActionResult OnGet()
- {
- return Page();
- }
- [BindProperty]
- public string Type { get; set; }
- public readonly string _type1 = "导入组配(Excel)";
- public readonly string _type2 = "导入节点(Excel)";
- [BindProperty]
- public IFormFile FormFile { get; set; }
- public async Task OnPostAsync()
- {
- _lms = await _context.Lm.ToListAsync();
- var dics = ExcelHelper.ImportConfig(FormFile.OpenReadStream());
- var sonDicTemp = new List<Dictionary>();
- foreach (var dic in dics)
- {
- dic.Key = _lms.FirstOrDefault(lm =>lm.Chnldesc==dic.KeyLable)?.Channelid??1;
- if (Type == _type1) dic.Type = 1;
- else if (Type == _type2)
- {
- dic.Type = 2;
- sonDicTemp.AddRange(GetAllSon(dic));
- }
- }
- dics.AddRange(sonDicTemp);
- var c = dics.Where(dic => dic.Value ==null);
- using var context = DatabaseService.Current.GetDbContext();
- context.InsertRange(dics.Where(dic =>dic.Key!=1).ToList());
- List<Dictionary> GetAllSon(Dictionary dic)
- {
- var dicTemp = new List<Dictionary>();
- FindSon(dic.Key);
- return dicTemp;
- void FindSon(int patentId)
- {
- var sonLms = _lms.Where(lm => lm.Parentid == patentId).ToList();
- var sonDics = sonLms.Select(lm => new Dictionary {Key=lm.Channelid,KeyLable=lm.Chnldesc,Value=dic.Value,Type=2 }).ToList();
- dicTemp.AddRange(sonDics);
- foreach (var lm in sonLms)
- {
- FindSon(lm.Channelid);
- }
- }
- }
- }
- }
- }
|