summaryrefslogtreecommitdiff
path: root/Gestor.Application/ViewModels/ImportViewModel.cs
blob: 48249e7f14da245c0f50d904f187521f837d2404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Gestor.Application.Helpers;
using Gestor.Application.ViewModels.Generic;
using Gestor.Model.Domain.Generic;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace Gestor.Application.ViewModels
{
	public class ImportViewModel : BaseViewModel
	{
		private ObservableCollection<Arquivo> _arquivos = new ObservableCollection<Arquivo>();

		public ObservableCollection<Arquivo> Arquivos
		{
			get
			{
				return this._arquivos;
			}
			set
			{
				this._arquivos = value;
				base.OnPropertyChanged("Arquivos");
			}
		}

		public ImportViewModel()
		{
		}

		public void CarregarArquivos()
		{
			OpenFileDialog openFileDialog = new OpenFileDialog()
			{
				Title = "Selecione o PDF",
				Filter = "Arquivos PDF|*.pdf",
				Multiselect = true
			};
			if (!openFileDialog.ShowDialog().GetValueOrDefault())
			{
				return;
			}
			this.Arquivos = new ObservableCollection<Arquivo>();
			openFileDialog.FileNames.ToList<string>().ForEach((string x) => this.Arquivos.Add(new Arquivo()
			{
				Nome = x,
				Processo = 0
			}));
		}

		public async Task Import(Arquivo arquivo)
		{
			await Task.Run(() => {
				try
				{
					Process process = new Process()
					{
						StartInfo = new ProcessStartInfo()
						{
							FileName = "C:\\AggerSeguros\\Agger.Import201911261630\\Agger.ImportPDF.exe",
							Arguments = string.Format(" {0} {1} NOVOGESTOR {2}", Recursos.Empresa.get_Id(), Recursos.Usuario.get_Id(), arquivo.Nome),
							UseShellExecute = false,
							RedirectStandardOutput = true,
							CreateNoWindow = true
						}
					};
					process.Start();
					process.WaitForExit();
					string end = process.StandardError.ReadToEnd();
					string str = process.StandardOutput.ReadToEnd();
					if (process.ExitCode != 0)
					{
						string[] strArrays = new string[] { "netsh exit code: ", null, null, null, null, null };
						strArrays[1] = process.ExitCode.ToString();
						strArrays[2] = " ";
						strArrays[3] = (!string.IsNullOrEmpty(end) ? string.Concat(" ", end) : "");
						strArrays[4] = " ";
						strArrays[5] = (!string.IsNullOrEmpty(str) ? string.Concat(" ", str) : "");
						throw new Exception(string.Concat(strArrays));
					}
				}
				catch (Exception exception)
				{
				}
			});
		}

		public void Importar()
		{
			(new TaskFactory()).StartNew<Task<bool>>(() => this.Importar(this.Arquivos.ToList<Arquivo>()));
		}

		public async Task<bool> Importar(List<Arquivo> arquivos)
		{
			arquivos.ForEach(async (Arquivo x) => await this.Import(x));
			return true;
		}
	}
}