From 1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Mon, 30 Mar 2026 10:38:18 -0300 Subject: chore: location --- .../ViewModels/Ferramentas/ProdutoViewModel.cs | 303 +++++++++++++++++++++ 1 file changed, 303 insertions(+) create mode 100644 Codemerx/Gestor.Application/ViewModels/Ferramentas/ProdutoViewModel.cs (limited to 'Codemerx/Gestor.Application/ViewModels/Ferramentas/ProdutoViewModel.cs') diff --git a/Codemerx/Gestor.Application/ViewModels/Ferramentas/ProdutoViewModel.cs b/Codemerx/Gestor.Application/ViewModels/Ferramentas/ProdutoViewModel.cs new file mode 100644 index 0000000..40f14ae --- /dev/null +++ b/Codemerx/Gestor.Application/ViewModels/Ferramentas/ProdutoViewModel.cs @@ -0,0 +1,303 @@ +using Gestor.Application.Helpers; +using Gestor.Application.Servicos.Ferramentas; +using Gestor.Application.Servicos.Generic; +using Gestor.Application.ViewModels.Generic; +using Gestor.Common.Validation; +using Gestor.Model.Common; +using Gestor.Model.Domain.Generic; +using Gestor.Model.Domain.Seguros; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Diagnostics; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; + +namespace Gestor.Application.ViewModels.Ferramentas +{ + public class ProdutoViewModel : BaseSegurosViewModel + { + private readonly ProdutoServico _servico; + + private Produto _selectedProduto; + + public Produto CancelProduto; + + private ObservableCollection _produtosFiltrados = new ObservableCollection(); + + private bool _isExpanded; + + public bool IsExpanded + { + get + { + return this._isExpanded; + } + set + { + this._isExpanded = value; + base.OnPropertyChanged("IsExpanded"); + } + } + + public List Produtos + { + get; + set; + } + + public ObservableCollection ProdutosFiltrados + { + get + { + return this._produtosFiltrados; + } + set + { + this._produtosFiltrados = value; + this.IsExpanded = (value != null ? value.Count > 0 : false); + base.OnPropertyChanged("ProdutosFiltrados"); + } + } + + public Produto SelectedProduto + { + get + { + return this._selectedProduto; + } + set + { + long? nullable; + this._selectedProduto = value; + this.WorkOnSelectedProduto(value, true); + if (value != null) + { + nullable = new long?(value.get_Id()); + } + else + { + nullable = null; + } + base.VerificarEnables(nullable); + base.OnPropertyChanged("SelectedProduto"); + } + } + + public ProdutoViewModel() + { + this._servico = new ProdutoServico(); + base.EnableMenu = true; + this.Seleciona(); + } + + public void CancelarAlteracao() + { + if (this.CancelProduto == null || !this.Produtos.Any((Produto x) => x.get_Id() == this.CancelProduto.get_Id())) + { + this.Incluir(); + } + else + { + DomainBase.Copy(this.Produtos.First((Produto x) => x.get_Id() == this.CancelProduto.get_Id()), this.CancelProduto); + if (this.ProdutosFiltrados.Count <= 0 || !this.ProdutosFiltrados.Any((Produto x) => x.get_Id() == this.CancelProduto.get_Id())) + { + this.ProdutosFiltrados.Add(this.CancelProduto); + } + else + { + DomainBase.Copy(this.ProdutosFiltrados.First((Produto x) => x.get_Id() == this.CancelProduto.get_Id()), this.CancelProduto); + } + this.ProdutosFiltrados = new ObservableCollection(this.ProdutosFiltrados); + this.SelectedProduto = this.ProdutosFiltrados.First((Produto x) => x.get_Id() == this.CancelProduto.get_Id()); + } + base.Alterar(false); + } + + internal async Task> Filtrar(string value) + { + List produtos = await Task.Run>(() => this.FiltrarProduto(value)); + return produtos; + } + + public List FiltrarProduto(string filter) + { + this.ProdutosFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(this.Produtos) : new ObservableCollection( + from x in this.Produtos + where ValidationHelper.RemoveDiacritics(x.get_Nome().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)) + orderby x.get_Ativo() descending, x.get_Nome() + select x)); + this.SelectedProduto = this.ProdutosFiltrados.FirstOrDefault(); + return this.ProdutosFiltrados.ToList(); + } + + public void Incluir() + { + Produto produto = new Produto(); + produto.set_Ativo(true); + this.SelectedProduto = produto; + base.Alterar(true); + } + + public async Task>> Salvar() + { + List> keyValuePairs; + string str; + string str1; + List> keyValuePairs1 = this.SelectedProduto.Validate(); + List> keyValuePairs2 = keyValuePairs1; + keyValuePairs2.AddRange(await this.Validate()); + keyValuePairs2 = null; + if (keyValuePairs1.Count <= 0) + { + str = (this.SelectedProduto.get_Id() == 0 ? "INCLUIU" : "ALTEROU"); + str1 = str; + Produto produto = await this._servico.Save(this.SelectedProduto); + if (this._servico.Sucesso) + { + base.RegistrarAcao(string.Concat(str1, " PRODUTO \"", produto.get_Nome(), "\""), produto.get_Id(), new TipoTela?(10), string.Format("PRODUTO \"{0}\", ID: {1}", produto.get_Nome(), produto.get_Id())); + if (!this.Produtos.Any((Produto x) => x.get_Id() == produto.get_Id())) + { + this.Produtos.Add(produto); + } + else + { + DomainBase.Copy(this.Produtos.First((Produto x) => x.get_Id() == produto.get_Id()), produto); + } + if (!this.ProdutosFiltrados.Any((Produto x) => x.get_Id() == produto.get_Id())) + { + this.ProdutosFiltrados.Add(produto); + } + else + { + DomainBase.Copy(this.ProdutosFiltrados.First((Produto x) => x.get_Id() == produto.get_Id()), produto); + } + this.ProdutosFiltrados = new ObservableCollection(this.ProdutosFiltrados); + List produtos = this.Produtos; + Recursos.Produtos = ( + from x in produtos + orderby x.get_Nome() + select x).ToList(); + this.WorkOnSelectedProduto(produto, false); + base.Alterar(false); + base.ToggleSnackBar("PRODUTO SALVO COM SUCESSO", true); + keyValuePairs = null; + } + else + { + keyValuePairs = null; + } + } + else + { + keyValuePairs = keyValuePairs1; + } + keyValuePairs1 = null; + str1 = null; + return keyValuePairs; + } + + private async void Seleciona() + { + base.Loading(true); + await base.PermissaoTela(10); + await this.SelecionaProdutos(); + base.Loading(false); + } + + public void SelecionaProduto(Produto produto) + { + this.SelectedProduto = produto; + } + + private async Task SelecionaProdutos() + { + base.Loading(true); + List produtos = await (new BaseServico()).BuscarProdutosAsync(); + ProdutoViewModel list = this; + List produtos1 = produtos; + IOrderedEnumerable ativo = + from x in produtos1 + orderby x.get_Ativo() descending + select x; + list.Produtos = ativo.ThenBy((Produto x) => x.get_Nome()).ToList(); + this.ProdutosFiltrados = new ObservableCollection(this.Produtos); + this.SelectedProduto = this.ProdutosFiltrados.FirstOrDefault(); + List produtos2 = this.Produtos; + Recursos.Produtos = ( + from x in produtos2 + orderby x.get_Nome() + select x).ToList(); + base.Loading(false); + base.Loading(true); + } + + public async Task>> Validate() + { + List> keyValuePairs; + List> keyValuePairs1; + if (!string.IsNullOrWhiteSpace(this.SelectedProduto.get_Nome())) + { + keyValuePairs1 = new List>(); + bool flag = true; + List produtos = await (new BaseServico()).BuscarProduto(this.SelectedProduto.get_Nome()); + if (produtos.Count > 0) + { + produtos.ForEach((Produto x) => { + if (x.get_Id() == this.SelectedProduto.get_Id()) + { + return; + } + if (x.get_Nome() == this.SelectedProduto.get_Nome()) + { + flag = false; + } + }); + } + if (!flag) + { + keyValuePairs1.Add(new KeyValuePair("Nome", "UM PRODUTO COM O MESMO NOME JÁ EXISTE.")); + } + keyValuePairs = keyValuePairs1; + } + else + { + keyValuePairs = new List>(); + } + keyValuePairs1 = null; + return keyValuePairs; + } + + private void WorkOnSelectedProduto(Produto value, bool registrar = true) + { + long? nullable; + this.CancelProduto = (value == null || value.get_Id() == 0 ? this.CancelProduto : (Produto)value.Clone()); + if (value == null || value.get_Id() == 0 || this.LastAccessId == value.get_Id() && this.LastAccessTela == 10) + { + return; + } + if (registrar) + { + base.RegistrarAcao(string.Concat("ACESSOU PRODUTO \"", value.get_Nome(), "\""), value.get_Id(), new TipoTela?(10), string.Format("ID PRODUTO: {0}", value.get_Id())); + } + this.LastAccessId = value.get_Id(); + this.LastAccessTela = 10; + Produto selectedProduto = this.SelectedProduto; + if (selectedProduto != null) + { + nullable = new long?(selectedProduto.get_Id()); + } + else + { + nullable = null; + } + long? nullable1 = nullable; + long id = value.get_Id(); + if (nullable1.GetValueOrDefault() != id | !nullable1.HasValue) + { + this.SelectedProduto = this.ProdutosFiltrados.FirstOrDefault((Produto x) => x.get_Id() == value.get_Id()); + } + } + } +} \ No newline at end of file -- cgit v1.2.3