From 7c80e1aaaf909c01d54575ce8aae08a1bb2d6599 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Fri, 3 Sep 2021 18:01:58 +0200 Subject: [PATCH] Prima versione caricamento dati NUOVI da filesystem --- MP.FileData/Controllers/FileController.cs | 197 +++++++++++++++++++--- MP.FileData/DatabaseModels/FileModel.cs | 2 +- MP.FileData/FileUtils.cs | 23 ++- MP.Prog/Data/FileArchDataService.cs | 18 ++ MP.Prog/Pages/Archive.razor | 2 +- MP.Prog/Pages/Archive.razor.cs | 4 +- 6 files changed, 216 insertions(+), 30 deletions(-) diff --git a/MP.FileData/Controllers/FileController.cs b/MP.FileData/Controllers/FileController.cs index 2f6956c2..d6fa58a7 100644 --- a/MP.FileData/Controllers/FileController.cs +++ b/MP.FileData/Controllers/FileController.cs @@ -2,6 +2,7 @@ using NLog; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -98,31 +99,79 @@ namespace MP.FileData.Controllers { maxNum = maxNum <= 0 ? 100 : maxNum; List dbResult = new List(); - - int totRecord = dbCtx + using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) + { + int totRecord = localDbCtx .DbSetArticoli .Where(x => x.CodArticolo.Contains(searchVal) || x.DescArticolo.Contains(searchVal) || x.Disegno.Contains(searchVal)) .Count(); - if (totRecord > maxNum) - { - dbResult = dbCtx - .DbSetArticoli - .Where(x => x.CodArticolo.Contains(searchVal) || x.DescArticolo.Contains(searchVal) || x.Disegno.Contains(searchVal)) - .OrderBy(x => x.DescArticolo) - .Take(maxNum) - .ToList(); - dbResult.Add(new DatabaseModels.ArticoloModel() { CodArticolo = "#####", DescArticolo = $"... +{totRecord - maxNum} rec ..." }); + if (totRecord > maxNum) + { + dbResult = localDbCtx + .DbSetArticoli + .Where(x => x.CodArticolo.Contains(searchVal) || x.DescArticolo.Contains(searchVal) || x.Disegno.Contains(searchVal)) + .OrderBy(x => x.DescArticolo) + .Take(maxNum) + .ToList(); + dbResult.Add(new DatabaseModels.ArticoloModel() { CodArticolo = "#####", DescArticolo = $"... +{totRecord - maxNum} rec ..." }); + } + else + { + dbResult = localDbCtx + .DbSetArticoli + .Where(x => x.CodArticolo.Contains(searchVal) || x.DescArticolo.Contains(searchVal) || x.Disegno.Contains(searchVal)) + .OrderBy(x => x.DescArticolo) + .ToList(); + } } - else + return dbResult; + } + + /// + /// Effettua la comparazione tra i file in archivio ed i file attuali e segna info LastCheck e Changed (se cambiati) + /// + /// + public bool CheckFileArchived(string idxMacchina, string path, string searchPattern) + { + bool answ = false; + DirectoryInfo dirInfo = new DirectoryInfo(path); + FileInfo[] fileList = dirInfo.GetFiles("*.*"); + List fileNew = new List(); + List fileMod = new List(); + + // recupera elenco file nel DB + var archivedFile = FileGetByPath(path, true); + + // verifica i file + foreach (var file in fileList) { - dbResult = dbCtx - .DbSetArticoli - .Where(x => x.CodArticolo.Contains(searchVal) || x.DescArticolo.Contains(searchVal) || x.Disegno.Contains(searchVal)) - .OrderBy(x => x.DescArticolo) - .ToList(); + // cerca nel DB... + var currRecord = archivedFile + .Where(x => x.Path == file.FullName) + .FirstOrDefault(); + // se NON trova lo crea + if (currRecord == null) + { + fileNew.Add(file); + } + else + { + fileMod.Add(file); + } } - return dbResult; + // salvo i NUOVI file + if (fileNew != null && fileNew.Count > 0) + { + FileInsert(idxMacchina, fileNew); + } + // aggiorno i file modificati + if (fileMod != null && fileMod.Count > 0) + { + FileUpdate(idxMacchina, fileMod); + } + + return answ; } public void Dispose() @@ -131,19 +180,116 @@ namespace MP.FileData.Controllers dbCtx.Dispose(); } + public List FileGetByPath(string path, bool onlyActive) + { + List dbResult = new List(); + using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) + { + dbResult = localDbCtx + .DbSetProgFile + .Where(x => x.Path.StartsWith(path) && ((onlyActive && x.Active) || !onlyActive)) + .OrderBy(x => x.Name) + .ToList(); + } + return dbResult; + } + public List FileGetFilt(string IdxMacchina, string CodArticolo, string SearchVal = "") { List dbResult = new List(); - - dbResult = dbCtx + using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) + { + dbResult = localDbCtx .DbSetProgFile - .Where(x => ((x.IdxMacchina == IdxMacchina || IdxMacchina == "0") || (x.CodArticolo == CodArticolo || CodArticolo == "ND")) && (x.Name.Contains(SearchVal) || string.IsNullOrEmpty(SearchVal))) + .Where(x => ((x.IdxMacchina == IdxMacchina || IdxMacchina == "0") || (x.CodArticolo == CodArticolo || CodArticolo == "ND" || CodArticolo == "")) && (x.Name.Contains(SearchVal) || string.IsNullOrEmpty(SearchVal))) .OrderBy(x => x.Name) .ToList(); - + } return dbResult; } + /// + /// effettua inserimento di un record in archivio come NUOVO documento tracciato + /// + /// + /// + /// + public bool FileInsert(string idxMacchina, List newFiles) + { + bool answ = false; + DateTime adesso = DateTime.Now; + using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) + { + // converto + List newRec = newFiles.Select(o => new DatabaseModels.FileModel() + { + Active = true, + CodArticolo = "ND", + Changed = false, + IdxMacchina = idxMacchina, + LastCheck = adesso, + LastMod = o.LastWriteTime, + MimeType = o.Extension, + Name = o.Name, + Path = o.FullName, + Rev = 0, + Size = o.Length, + FileContent = File.ReadAllBytes(o.FullName) + }).ToList(); + + // aggiungo in blocco + localDbCtx + .DbSetProgFile + .AddRange(newRec); + + // salvo + localDbCtx.SaveChanges(); + answ = true; + } + return answ; + } + + /// + /// effettua inserimento di un record in archivio come NUOVO documento tracciato + /// + /// + /// + /// + public bool FileUpdate(string idxMacchina, List updFiles) + { + bool answ = false; + DateTime adesso = DateTime.Now; + using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) + { + // converto + List newRec = updFiles.Select(o => new DatabaseModels.FileModel() + { + Active = true, + CodArticolo = "ND", + Changed = false, + IdxMacchina = idxMacchina, + LastCheck = adesso, + LastMod = o.LastWriteTime, + MimeType = o.Extension, + Name = o.Name, + Path = o.FullName, + Rev = 0, + Size = o.Length, + FileContent = File.ReadAllBytes(o.FullName) + }).ToList(); + + // aggiungo in blocco + localDbCtx + .DbSetProgFile + .UpdateRange(newRec); + + // salvo + localDbCtx.SaveChanges(); + answ = true; + } + return answ; + } + /// /// Elenco tabella Macchine /// @@ -151,12 +297,13 @@ namespace MP.FileData.Controllers public List MacchineGetAll() { List dbResult = new List(); - - dbResult = dbCtx + using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration)) + { + dbResult = localDbCtx .DbSetMacchine .OrderBy(x => x.IdxMacchina) .ToList(); - + } return dbResult; } diff --git a/MP.FileData/DatabaseModels/FileModel.cs b/MP.FileData/DatabaseModels/FileModel.cs index 3ccf4eb4..8ec845e6 100644 --- a/MP.FileData/DatabaseModels/FileModel.cs +++ b/MP.FileData/DatabaseModels/FileModel.cs @@ -27,7 +27,7 @@ namespace MP.FileData.DatabaseModels public string Name { get; set; } = ""; public int Rev { get; set; } = 0; public DateTime LastMod { get; set; } - public int Size { get; set; } = 0; + public long Size { get; set; } = 0; public string Path { get; set; } = ""; public string MimeType { get; set; } = ""; public bool Changed { get; set; } = false; diff --git a/MP.FileData/FileUtils.cs b/MP.FileData/FileUtils.cs index 58091b84..a33d69bb 100644 --- a/MP.FileData/FileUtils.cs +++ b/MP.FileData/FileUtils.cs @@ -8,8 +8,25 @@ using System.Threading.Tasks; namespace MP.FileData { - public class FileUtils + public class FileUtils { + #region Public Methods + + /// + /// Elenco dei file data la directory da controllare + /// + /// + /// + /// + public static FileInfo[] GetFileList(string path, string searchPattern) + { + //string[] answ = Directory.GetFiles(path, searchPattern); + DriveInfo di = new DriveInfo(path); + DirectoryInfo dirInfo = di.RootDirectory; + FileInfo[] answ = dirInfo.GetFiles("*.*"); + + return answ; + } public static async Task SaveToCsv(List reportData, string path) { @@ -21,5 +38,7 @@ namespace MP.FileData lines.AddRange(valueLines); await Task.Run(() => File.WriteAllLines(path, lines.ToArray())); } + + #endregion Public Methods } -} +} \ No newline at end of file diff --git a/MP.Prog/Data/FileArchDataService.cs b/MP.Prog/Data/FileArchDataService.cs index 204e4f72..6254e742 100644 --- a/MP.Prog/Data/FileArchDataService.cs +++ b/MP.Prog/Data/FileArchDataService.cs @@ -219,6 +219,24 @@ namespace MP.Prog.Data dbController.RollBackEntity(item); } + /// + /// Aggiorna intero archivio scansionando dati x tutte le macchine che hanno un path valido + /// + /// + public async Task updateAllArchive() + { + try + { + var listaMacchine = await MacchineGetAll(); + foreach (var item in listaMacchine.Where(x => !string.IsNullOrEmpty(x.BasePath)).ToList()) + { + dbController.CheckFileArchived(item.IdxMacchina, item.BasePath, "*.*"); + } + } + catch + { } + } + #endregion Public Methods #if false diff --git a/MP.Prog/Pages/Archive.razor b/MP.Prog/Pages/Archive.razor index 8326cbc8..cb76925d 100644 --- a/MP.Prog/Pages/Archive.razor +++ b/MP.Prog/Pages/Archive.razor @@ -40,7 +40,7 @@ - +