diff options
| author | Lucas Faria Mendes <lucas.fariamo08@gmail.com> | 2026-03-30 13:38:18 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.fariamo08@gmail.com> | 2026-03-30 13:38:18 +0000 |
| commit | 1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1 (patch) | |
| tree | e1c3b20ea08f0cf71122a1e73f0d395f8fd83874 /Codemerx/Gestor.Application/ViewModels/Drawer/VinculoVendedorViewModel.cs | |
| parent | 674ca83ba9243a9e95a7568c797668dab6aee26a (diff) | |
| download | gestor-1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1.tar.gz gestor-1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1.zip | |
chore: location
Diffstat (limited to 'Codemerx/Gestor.Application/ViewModels/Drawer/VinculoVendedorViewModel.cs')
| -rw-r--r-- | Codemerx/Gestor.Application/ViewModels/Drawer/VinculoVendedorViewModel.cs | 307 |
1 files changed, 307 insertions, 0 deletions
diff --git a/Codemerx/Gestor.Application/ViewModels/Drawer/VinculoVendedorViewModel.cs b/Codemerx/Gestor.Application/ViewModels/Drawer/VinculoVendedorViewModel.cs new file mode 100644 index 0000000..bba206b --- /dev/null +++ b/Codemerx/Gestor.Application/ViewModels/Drawer/VinculoVendedorViewModel.cs @@ -0,0 +1,307 @@ +using Gestor.Application.Helpers;
+using Gestor.Application.Servicos;
+using Gestor.Application.Servicos.Ferramentas;
+using Gestor.Application.Servicos.Generic;
+using Gestor.Application.ViewModels;
+using Gestor.Application.ViewModels.Generic;
+using Gestor.Common.Validation;
+using Gestor.Infrastructure.UnitOfWork.Generic;
+using Gestor.Infrastructure.UnitOfWork.Logic;
+using Gestor.Model.Common;
+using Gestor.Model.Domain.Common;
+using Gestor.Model.Domain.Generic;
+using Gestor.Model.Domain.Seguros;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Net;
+using System.Net.Sockets;
+using System.Runtime.CompilerServices;
+using System.Threading.Tasks;
+
+namespace Gestor.Application.ViewModels.Drawer
+{
+ public class VinculoVendedorViewModel : BaseSegurosViewModel
+ {
+ private readonly VendedorUsuarioServico _servicoVendedorUsuario;
+
+ private readonly VendedorServico _servicoVendedor;
+
+ public readonly Usuario SelectedUsuario;
+
+ private bool _isVisibleVinculado;
+
+ private bool _enableAlterarVinculo;
+
+ private bool _isVisibleVendedor = true;
+
+ private List<VinculoVendedor> _vinculado;
+
+ private List<VinculoVendedor> _vinculadoFiltro;
+
+ private List<VinculoVendedor> _vendedor;
+
+ private List<VinculoVendedor> _vendedorFiltro;
+
+ public bool EnableAlterarVinculo
+ {
+ get
+ {
+ return this._enableAlterarVinculo;
+ }
+ set
+ {
+ this._enableAlterarVinculo = value;
+ base.OnPropertyChanged("EnableAlterarVinculo");
+ }
+ }
+
+ public bool IsVisibleVendedor
+ {
+ get
+ {
+ return this._isVisibleVendedor;
+ }
+ set
+ {
+ this._isVisibleVendedor = value;
+ base.OnPropertyChanged("IsVisibleVendedor");
+ }
+ }
+
+ public bool IsVisibleVinculado
+ {
+ get
+ {
+ return this._isVisibleVinculado;
+ }
+ set
+ {
+ this._isVisibleVinculado = value;
+ base.OnPropertyChanged("IsVisibleVinculado");
+ }
+ }
+
+ public List<VinculoVendedor> Vendedor
+ {
+ get
+ {
+ return this._vendedor;
+ }
+ set
+ {
+ this._vendedor = value;
+ this.IsVisibleVendedor = (value == null ? false : value.Count > 0);
+ base.OnPropertyChanged("Vendedor");
+ }
+ }
+
+ public List<VinculoVendedor> VendedorFiltro
+ {
+ get
+ {
+ return this._vendedorFiltro;
+ }
+ set
+ {
+ this._vendedorFiltro = value;
+ base.OnPropertyChanged("VendedorFiltro");
+ }
+ }
+
+ public List<VinculoVendedor> Vinculado
+ {
+ get
+ {
+ return this._vinculado;
+ }
+ set
+ {
+ this._vinculado = value;
+ this.IsVisibleVinculado = (value == null ? false : value.Count > 0);
+ base.OnPropertyChanged("Vinculado");
+ }
+ }
+
+ public List<VinculoVendedor> VinculadoFiltro
+ {
+ get
+ {
+ return this._vinculadoFiltro;
+ }
+ set
+ {
+ this._vinculadoFiltro = value;
+ base.OnPropertyChanged("VinculadoFiltro");
+ }
+ }
+
+ public VinculoVendedorViewModel(Usuario usuario)
+ {
+ this.SelectedUsuario = usuario;
+ this._servicoVendedorUsuario = new VendedorUsuarioServico();
+ this._servicoVendedor = new VendedorServico();
+ this.Load(usuario);
+ this.EnableAlterarVinculo = Recursos.Usuario.get_Id() > (long)0;
+ base.EnableMenu = true;
+ }
+
+ private async Task AwaitLoad(Usuario usuario)
+ {
+ this.Vinculado = await this.GetVinculo(usuario);
+ this.Vendedor = await this.GetVendedor(usuario);
+ this.VinculadoFiltro = this.Vinculado;
+ this.VendedorFiltro = this.Vendedor;
+ }
+
+ internal async Task BuscarVinculoVendedor(string value)
+ {
+ await Task.Run(() => this.FiltrarVinculoVendedor(value));
+ }
+
+ internal void FiltrarVinculoVendedor(string filter)
+ {
+ this.Vinculado = (string.IsNullOrEmpty(filter) ? this.VinculadoFiltro : new List<VinculoVendedor>(
+ from x in this.VinculadoFiltro
+ where Gestor.Common.Validation.ValidationHelper.RemoveDiacritics(x.get_Vendedor().get_Nome().ToUpper().Trim()).Contains(filter.ToUpper())
+ select x));
+ this.Vendedor = (string.IsNullOrEmpty(filter) ? this.VendedorFiltro : new List<VinculoVendedor>(
+ from x in this.VendedorFiltro
+ where Gestor.Common.Validation.ValidationHelper.RemoveDiacritics(x.get_Vendedor().get_Nome().ToUpper().Trim()).Contains(filter.ToUpper())
+ select x));
+ }
+
+ private async Task<List<VinculoVendedor>> GetVendedor(Usuario usuario)
+ {
+ List<Gestor.Model.Domain.Seguros.Vendedor> list;
+ List<VinculoVendedor> vinculoVendedors;
+ List<Gestor.Model.Domain.Seguros.Vendedor> vendedors = await this._servicoVendedor.BuscarVendedoresAtivosAsync();
+ if (vendedors != null)
+ {
+ list = (
+ from x in vendedors
+ where x.get_IdEmpresa() == usuario.get_IdEmpresa()
+ select x).ToList<Gestor.Model.Domain.Seguros.Vendedor>();
+ }
+ else
+ {
+ list = null;
+ }
+ vendedors = list;
+ if (vendedors == null || vendedors.Count == 0)
+ {
+ vinculoVendedors = null;
+ }
+ else
+ {
+ IEnumerable<Gestor.Model.Domain.Seguros.Vendedor> vendedors1 =
+ from vendedor in vendedors
+ where this.Vinculado.All<VinculoVendedor>((VinculoVendedor x) => x.get_Vendedor().get_Id() != vendedor.get_Id())
+ select vendedor;
+ vinculoVendedors = vendedors1.Select<Gestor.Model.Domain.Seguros.Vendedor, VinculoVendedor>((Gestor.Model.Domain.Seguros.Vendedor vendedor) => {
+ VinculoVendedor vinculoVendedor = new VinculoVendedor();
+ vinculoVendedor.set_Id((long)0);
+ vinculoVendedor.set_Vendedor(vendedor);
+ vinculoVendedor.set_Vinculado(false);
+ return vinculoVendedor;
+ }).ToList<VinculoVendedor>();
+ }
+ List<VinculoVendedor> vinculoVendedors1 = vinculoVendedors;
+ return vinculoVendedors1;
+ }
+
+ private async Task<List<VinculoVendedor>> GetVinculo(Usuario usuario)
+ {
+ List<VinculoVendedor> vinculoVendedors = new List<VinculoVendedor>();
+ await this._servicoVendedorUsuario.FindByVinculo(usuario).ForEach((VendedorUsuario x) => {
+ VinculoVendedor vinculoVendedor = new VinculoVendedor();
+ vinculoVendedor.set_Id(x.get_Id());
+ vinculoVendedor.set_Vendedor(x.get_Vendedor());
+ vinculoVendedor.set_Vinculado(true);
+ vinculoVendedors.Add(vinculoVendedor);
+ });
+ List<VinculoVendedor> vinculoVendedors1 = vinculoVendedors;
+ return vinculoVendedors1;
+ }
+
+ private async void Load(Usuario usuario)
+ {
+ await this.AwaitLoad(usuario);
+ }
+
+ public async Task Salvar()
+ {
+ string str;
+ List<VinculoVendedor> vinculado = this.Vinculado;
+ List<VendedorUsuario> list = (
+ from x in vinculado
+ where !x.get_Vinculado()
+ select x).Select<VinculoVendedor, VendedorUsuario>((VinculoVendedor x) => {
+ VendedorUsuario vendedorUsuario = new VendedorUsuario();
+ vendedorUsuario.set_Id(x.get_Id());
+ vendedorUsuario.set_Vendedor(x.get_Vendedor());
+ vendedorUsuario.set_Usuario(this.SelectedUsuario);
+ return vendedorUsuario;
+ }).ToList<VendedorUsuario>();
+ if (list.Count > 0)
+ {
+ if (!await this._servicoVendedorUsuario.Delete(list))
+ {
+ return;
+ }
+ }
+ List<VinculoVendedor> vendedor = this.Vendedor;
+ List<VendedorUsuario> vendedorUsuarios = (
+ from x in vendedor
+ where x.get_Vinculado()
+ select x).Select<VinculoVendedor, VendedorUsuario>((VinculoVendedor x) => {
+ VendedorUsuario vendedorUsuario = new VendedorUsuario();
+ vendedorUsuario.set_Id(x.get_Id());
+ vendedorUsuario.set_Vendedor(x.get_Vendedor());
+ vendedorUsuario.set_Usuario(this.SelectedUsuario);
+ return vendedorUsuario;
+ }).ToList<VendedorUsuario>();
+ if (vendedorUsuarios.Count > 0)
+ {
+ await this._servicoVendedorUsuario.SaveOrUpdate(vendedorUsuarios);
+ }
+ await this.AwaitLoad(this.SelectedUsuario);
+ using (UnitOfWork commited = Instancia.Commited)
+ {
+ IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
+ RegistroLog registroLog = new RegistroLog();
+ registroLog.set_Acao(0);
+ registroLog.set_Usuario(Recursos.Usuario);
+ registroLog.set_DataHora(Funcoes.GetNetworkTime());
+ List<VinculoVendedor> vinculoVendedors = this.Vinculado;
+ List<Gestor.Model.Domain.Seguros.Vendedor> vendedors = (
+ from x in vinculoVendedors
+ select x.get_Vendedor()).ToList<Gestor.Model.Domain.Seguros.Vendedor>();
+ JsonSerializerSettings jsonSerializerSetting = new JsonSerializerSettings();
+ jsonSerializerSetting.set_ReferenceLoopHandling(1);
+ registroLog.set_Descricao(JsonConvert.SerializeObject(vendedors, jsonSerializerSetting));
+ registroLog.set_EntidadeId(this.SelectedUsuario.get_Id());
+ registroLog.set_Tela(45);
+ registroLog.set_Versao(LoginViewModel.VersaoAtual);
+ registroLog.set_NomeMaquina(Environment.MachineName);
+ registroLog.set_UsuarioMaquina(Environment.UserName);
+ IPAddress[] addressList = hostEntry.AddressList;
+ IPAddress pAddress = ((IEnumerable<IPAddress>)addressList).FirstOrDefault<IPAddress>((IPAddress ip) => ip.AddressFamily == AddressFamily.InterNetwork);
+ if (pAddress != null)
+ {
+ str = pAddress.ToString();
+ }
+ else
+ {
+ str = null;
+ }
+ registroLog.set_Ip(str);
+ this._servicoVendedorUsuario.SaveLog(registroLog, commited);
+ commited.Commit();
+ }
+ base.RegistrarAcao(string.Concat("ALTEROU VÍNCULOS DE VENDEDOR DO USUÁRIO \"", this.SelectedUsuario.get_Nome(), "\""), this.SelectedUsuario.get_Id(), new TipoTela?(45), string.Format("ID: {0}\n\nACESSE O LOG DE ALTERAÇÕES NA TELA DE VÍNCULO DE VENDEDOR PARA VER EXATAMENTE O QUE FOI ALTERADO.", this.SelectedUsuario.get_Id()));
+ }
+ }
+}
\ No newline at end of file |