예제 #1
0
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <UserPlantaDto>(command.Json);

                if (!string.IsNullOrEmpty(dto.UserId) && dto.PlantaId > 0)
                {
                    var userPlantas = await MyRepository.Get(t => t.PlantaId == dto.PlantaId &&
                                                             t.UserId.Equals(dto.UserId));

                    var userDbSet = Context.Set <UserAccount>();
                    var user      = await userDbSet.FindAsync(dto.UserId);

                    var plantaRep = new Repository <Planta>(Context);
                    var planta    = await plantaRep.GetById(dto.PlantaId);

                    if (userPlantas != null && !userPlantas.Any() && user != null && planta != null)
                    {
                        var entity = new UserPlanta();
                        entity.UpdateEntity(dto);
                        entity.Codigo = dto.UserId + dto.PlantaId + DateTime.Now.ToString();
                        entity.Planta = planta;

                        var insertEntity = await MyRepository.Insert(entity);

                        if (insertEntity != null)
                        {
                            cmd.Cmd  = ServerCommands.LogResultOk;
                            cmd.Json = await SerializerAsync.SerializeJson(true);

                            await MyRepository.Save();

                            cmd.EntityId = entity.Id;
                        }
                        else
                        {
                            cmd.Cmd = ServerCommands.RepeatedHumanCode;
                            ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                        }
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.LogResultDeny;
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #2
0
        public override async Task <Command> Edit(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <TipoEntradaDto>(command.Json);

                var tipoEntrada = await MyRepository.GetById(cmd.EntityId);

                if (tipoEntrada != null)
                {
                    tipoEntrada.Descricao = dto.Description;
                    cmd.Cmd  = ServerCommands.LogResultOk;
                    cmd.Json = await SerializerAsync.SerializeJson(true);

                    await MyRepository.Save();
                }
                else
                {
                    cmd.Cmd  = ServerCommands.LogResultDeny;
                    cmd.Json = await SerializerAsync.SerializeJson(false);
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #3
0
        public static async void ReturnLoginRequest(Object[] server, Object[] local)
        {
            var dataJson = SerializerAsync.DeserializeJson <Command>(server[0].ToString()).Result;

            try
            {
                if (dataJson != null)
                {
                    if (dataJson.Cmd.Equals(ServerCommands.LogResultOk))
                    {
                        var username = await SerializerAsync.DeserializeJson <string>(dataJson.Json);

                        if (UsersConnections.TryGetValue(username, out var conn))
                        {
                            await conn.Zz.WriteServer(new Command { Cmd = ServerCommands.IsUser, Json = await SerializerAsync.SerializeJson(username) });
                        }
                    }
                    else if (dataJson.Cmd.Equals(ServerCommands.LogResultDeny))
                    {
                        var username = await SerializerAsync.DeserializeJson <string>(dataJson.Json);
                        await RemoveUserConnection(username);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #4
0
        public static async void ReturnLogoutRequest(Object[] server, Object[] local)
        {
            var dataJson = SerializerAsync.DeserializeJson <Command>(server[0].ToString()).Result;

            try
            {
                if (dataJson != null)
                {
                    if (dataJson.Cmd.Equals(ServerCommands.LogResultOk))
                    {
                        var username = await SerializerAsync.DeserializeJson <string>(dataJson.Json);

                        UsersConnections.Remove(username);
                        ConsoleEx.WriteLine("Usuario removido da lista de autorizados com sucesso");
                    }
                    else if (dataJson.Cmd.Equals(ServerCommands.LogResultDeny))
                    {
                        var username = await SerializerAsync.DeserializeJson <string>(dataJson.Json);

                        UsersConnections.Remove(username);
                        ConsoleEx.WriteLine("Deu merda ao remover o usuario da lista de autorizados");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #5
0
        public void ReturnServer(Object[] server, Object[] local)
        {
            var dataJson = SerializerAsync.DeserializeJson <Command>(server[0].ToString()).Result;

            try
            {
                if (dataJson != null)
                {
                    if (!dataJson.Cmd.Equals(ServerCommands.Exit))
                    {
                        if (dataJson.Cmd.Equals(ServerCommands.LogResultOk))
                        {
                            ConsoleEx.WriteLine("Uhulll, consegui logar");
                            _expirationTimer =
                                ZZApiMain.SetTimer(
                                    (LoginResultDto.ExpirationDate - LoginResultDto.CreatedDate).TotalMilliseconds,
                                    ExpirationTokenTimer);
                        }
                        else if (dataJson.Cmd.Equals(ServerCommands.LogResultDeny))
                        {
                            ConsoleEx.WriteLine("Algo deu errado");
                            ZZApiMain.RemoveUserConnection(LoginResultDto.Username);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #6
0
        public async Task <ActionResult <UserDadosDto> > GetByHumanCode(UserDadosDto dto)
        {
            try
            {
                var myUsername = User.Identity.Name;
                if (ZZApiMain.VerifyUserAuthorize(myUsername))
                {
                    if (ZZApiMain.UsersConnections.TryGetValue(myUsername, out var myConn))
                    {
                        var myId = await myConn.Zz.ApiWriteServer(myUsername, new Command { Cmd = ServerCommands.GetById, EntityId = dto.Id, Tela = Tela, Json = await SerializerAsync.SerializeJson(dto) });

                        var responseCommand = await myConn.Zz.GetApiWaitCommand(myId);

                        if (responseCommand != null && responseCommand.Cmd.Equals(ServerCommands.LogResultOk))
                        {
                            return(await SerializerAsync.DeserializeJson <UserDadosDto>(responseCommand.Json));
                        }
                    }
                }
                return(NotFound());
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
                return(NotFound());
            }
        }
예제 #7
0
        public async Task <Command> GetByHumanCode(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <EntityDto>(cmd.Json);

                var entity = await MyRepository.GetByHumanCode(dto.Codigo);

                if (entity != null)
                {
                    cmd.Cmd  = ServerCommands.LogResultOk;
                    cmd.Json = await SerializerAsync.SerializeJson(entity.ConvertDto());
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #8
0
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <ServicoDto>(command.Json);

                var servicos = await MyRepository.Get(t => t.Codigo.Equals(dto.Codigo));

                if (servicos != null && !servicos.Any())
                {
                    var unidadeMedidaRep = new Repository <UnidadeMedida>(Context);
                    var unidadeMedida    = await unidadeMedidaRep.GetById(dto.UnidadeMedidaId);

                    var tipoServicoRep = new Repository <TipoServico>(Context);
                    var tipoServico    = await tipoServicoRep.GetById(dto.TipoServicoId);

                    var centroCustoRep = new Repository <CentroCustoSintetico>(Context);
                    var centroCusto    = await centroCustoRep.GetById(dto.CentroCustoId);

                    if (unidadeMedida != null && tipoServico != null && centroCusto != null)
                    {
                        var entity = new Servico();
                        entity.UpdateEntity(dto);
                        entity.UnidadeMedida = unidadeMedida;
                        entity.TipoServico   = tipoServico;
                        entity.CentroCusto   = centroCusto;

                        var insertEntity = await MyRepository.Insert(entity);

                        if (insertEntity != null)
                        {
                            cmd.Cmd  = ServerCommands.LogResultOk;
                            cmd.Json = await SerializerAsync.SerializeJson(true);

                            await MyRepository.Save();

                            cmd.EntityId = entity.Id;
                        }
                        else
                        {
                            cmd.Cmd = ServerCommands.RepeatedHumanCode;
                            ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                        }
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #9
0
        public static async Task <Command> GetAddress(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                using (var context = new ZZContext())
                {
                    var dto = await SerializerAsync.DeserializeJson <EnderecoDto>(cmd.Json);

                    var rep       = new Repository <Endereco>(context);
                    var enderecos = await rep.Get(e => e.Logradouro.Equals(dto.Logradouro) && e.Numero == dto.Numero);

                    var endList = enderecos.ToList();
                    if (endList.Any())
                    {
                        cmd.Cmd = ServerCommands.LogResultOk;
                        var dtosEnd = endList.Select(e => e.ConvertDto()).ToList();
                        cmd.Json = await SerializerAsync.SerializeJsonList(dtosEnd);
                    }
                    else
                    {
                        var addressList = await SearchAddressAsync(dto.Estado, dto.Cidade, dto.Logradouro,
                                                                   CancellationToken.None);

                        var viaCepResults = addressList.ToList();
                        if (viaCepResults.Any())
                        {
                            cmd.Cmd = ServerCommands.LogResultOk;
                            var dtos = viaCepResults.Select(d => new EnderecoDto
                            {
                                Cep         = d.ZipCode,
                                Bairro      = d.Neighborhood,
                                Cidade      = d.City,
                                Estado      = d.StateInitials,
                                Logradouro  = d.Street,
                                Complemento = d.Complement,
                                Ibge        = d.IBGECode,
                                GIACode     = d.GIACode,
                                Numero      = dto.Numero,
                                Codigo      = d.IBGECode.ToString() + d.GIACode.ToString() + d.ZipCode + dto.Numero.ToString()
                            });
                            cmd.Json = await SerializerAsync.SerializeJsonList(dtos.ToList());
                        }
                        else
                        {
                            cmd.Cmd = ServerCommands.LogResultDeny;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #10
0
        public static async Task <Command> GetAddressByZipCode(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                using (var context = new ZZContext())
                {
                    var dto = await SerializerAsync.DeserializeJson <EnderecoDto>(cmd.Json);

                    var rep       = new Repository <Endereco>(context);
                    var enderecos = await rep.Get(e => e.Cep.Equals(dto.Cep) && e.Numero == dto.Numero);


                    if (enderecos != null && enderecos.Any())
                    {
                        var endList = enderecos.FirstOrDefault();
                        cmd.Cmd = ServerCommands.LogResultOk;
                        var dtosEnd = endList.ConvertDto();
                        cmd.Json = await SerializerAsync.SerializeJson(dtosEnd);
                    }
                    else
                    {
                        var address = await SearchAddressAsync(dto.Cep, CancellationToken.None);

                        if (address != null)
                        {
                            cmd.Cmd = ServerCommands.LogResultOk;
                            var entity = new EnderecoDto
                            {
                                Cep         = address.ZipCode,
                                Bairro      = address.Neighborhood,
                                Cidade      = address.City,
                                Estado      = address.StateInitials,
                                Logradouro  = address.Street,
                                Complemento = address.Complement,
                                Ibge        = address.IBGECode,
                                GIACode     = address.GIACode,
                                Numero      = dto.Numero,
                                Codigo      = address.IBGECode.ToString() + address.GIACode.ToString() + address.ZipCode + dto.Numero.ToString()
                            };
                            cmd.Json = await SerializerAsync.SerializeJson(entity);
                        }
                        else
                        {
                            cmd.Cmd = ServerCommands.LogResultDeny;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #11
0
        public override async Task <Command> Edit(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <ServicoDto>(command.Json);

                var servico = await MyRepository.GetById(cmd.EntityId);

                if (servico != null)
                {
                    servico.UpdateEntity(dto);
                    var unidadeMedidaRep = new Repository <UnidadeMedida>(Context);
                    var unidadeMedida    = await unidadeMedidaRep.GetById(dto.UnidadeMedidaId);

                    if (unidadeMedida != null)
                    {
                        servico.UnidadeMedida = unidadeMedida;
                    }
                    var tipoServicoRep = new Repository <TipoServico>(Context);
                    var tipoServico    = await tipoServicoRep.GetById(dto.TipoServicoId);

                    if (tipoServico != null)
                    {
                        servico.TipoServico = tipoServico;
                    }

                    var centroCustoRep = new Repository <CentroCustoSintetico>(Context);
                    var centroCusto    = await centroCustoRep.GetById(dto.CentroCustoId);

                    if (centroCusto != null)
                    {
                        servico.CentroCusto = centroCusto;
                    }

                    cmd.Cmd  = ServerCommands.LogResultOk;
                    cmd.Json = await SerializerAsync.SerializeJson(true);

                    await MyRepository.Save();
                }
                else
                {
                    cmd.Cmd  = ServerCommands.LogResultDeny;
                    cmd.Json = await SerializerAsync.SerializeJson(false);
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #12
0
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <TabelaDto>(command.Json);

                var tabelas = await MyRepository.Get(t => t.ServicoId == dto.ServicoId && t.Descricao.Equals(dto.Description));

                if (tabelas != null && !tabelas.Any())
                {
                    var servicoRep = new Repository <Servico>(Context);
                    var servico    = await servicoRep.GetById(dto.ServicoId);

                    if (servico != null)
                    {
                        var entity = new TabelaCusto();
                        entity.UpdateEntity(dto);
                        entity.DataTabela = DateTime.Now;
                        entity.Servico    = servico;

                        var insertEntity = await MyRepository.Insert(entity);

                        if (insertEntity != null)
                        {
                            cmd.Cmd  = ServerCommands.LogResultOk;
                            cmd.Json = await SerializerAsync.SerializeJson(true);

                            await MyRepository.Save();

                            cmd.EntityId = entity.Id;
                        }
                        else
                        {
                            cmd.Cmd = ServerCommands.RepeatedHumanCode;
                            ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                        }
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #13
0
        /// <summary>
        /// Searches the asynchronous.
        /// </summary>
        /// <param name="zipCode">The zip code.</param>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public static async Task <ViaCEPResult> SearchAddressAsync(String zipCode, CancellationToken token)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(BaseUrl);
                var response = await client.GetAsync($"/ws/{zipCode}/json", token).ConfigureAwait(false);

                response.EnsureSuccessStatusCode();
                var jsonContent = await response.Content.ReadAsStringAsync();

                var json = await SerializerAsync.DeserializeJson <ViaCEPResult>(jsonContent);

                json.ZipCode = new String(json.ZipCode.Where(Char.IsDigit).ToArray());
                return(json);
            }
        }
예제 #14
0
        public void ProcessLogin(Object[] server, Object[] local)
        {
            var dataJson = SerializerAsync.DeserializeJson <Command>(server[0].ToString()).Result;

            try
            {
                if (dataJson != null)
                {
                    if (!dataJson.Cmd.Equals(ServerCommands.Exit))
                    {
                        if (dataJson.Cmd.Equals(ServerCommands.IsController))
                        {
                            ConsoleEx.WriteLine("Oi Controller fofo *3* ");
                            _client = new ControllerClient();
                            MyId    = ServerCommands.IsController;
                            _client.Login(this, connection);
                        }
                        else if (dataJson.Cmd.Equals(ServerCommands.IsUser))
                        {
                            if (Server.VerifyUserAuthorization(
                                    SerializerAsync.DeserializeJson <string>(dataJson.Json).Result, this))
                            {
                                ConsoleEx.WriteLine("Oi Cli fofo *3* ");
                                MyId    = SerializerAsync.DeserializeJson <string>(dataJson.Json).Result;
                                _client = new UserClient();
                                _client.Login(this, connection);
                            }
                            else
                            {
                                ConsoleEx.WriteLine("Cliente não autorizado");
                                connection.WriteServer("", 542, ServerCommands.LogResultDeny, "");
                            }
                        }
                        else
                        {
                            _client.Command(dataJson);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteLine("Erro na criação do client : " + " | " + _client + " | " +
                                    dataJson + " | " + e.Message);
            }
        }
예제 #15
0
        /// <summary>
        /// Converte um array de object recebido como entrada de um DelegateAction em Command
        /// </summary>
        /// <param name="s"></param>
        /// <returns><typeparam name="dataJson"></typeparam></returns>
        public static async Task <Command> ObjectToCommand(Object[] s)
        {
            try
            {
                var     data     = s[0].ToString();
                Command dataJson = new Command();

                dataJson = await SerializerAsync.DeserializeJson <Command>(data);

                return(dataJson);
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
                return(null);
            }
        }
예제 #16
0
        public override async Task Command(Command command)
        {
            try
            {
                if (Manager != null && Connection != null)
                {
                    if (command.Cmd != null)
                    {
                        if (command.Cmd.Equals(ServerCommands.AddClientAuthorized))
                        {
                            if (Manager.Server.AddAuthorizedUser(
                                    await SerializerAsync.DeserializeJson <string>(command.Json)))
                            {
                                command.Cmd = ServerCommands.LogResultOk;
                            }
                            else
                            {
                                command.Cmd = ServerCommands.LogResultDeny;
                            }

                            await Connection.WriteServer(command);
                        }
                        else if (command.Cmd.Equals(ServerCommands.RemoveClientAuthorized))
                        {
                            if (Manager.Server.RemoveAuthorizedUser(
                                    await SerializerAsync.DeserializeJson <string>(command.Json)))
                            {
                                command.Cmd = ServerCommands.LogResultOk;
                            }
                            else
                            {
                                command.Cmd = ServerCommands.LogResultDeny;
                            }

                            await Connection.WriteServer(command);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError("Erro ao receber command do controller ", e);
                throw;
            }
        }
예제 #17
0
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <TipoEntradaDto>(command.Json);

                var tipos = await MyRepository.Get(t => t.Descricao.Equals(dto.Description));

                if (tipos != null && !tipos.Any())
                {
                    var entity = new TipoEntrada();
                    entity.UpdateEntity(dto);
                    var insertEntity = await MyRepository.Insert(entity);

                    if (insertEntity != null)
                    {
                        cmd.Cmd  = ServerCommands.LogResultOk;
                        cmd.Json = await SerializerAsync.SerializeJson(true);

                        await MyRepository.Save();

                        cmd.EntityId = entity.Id;
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.RepeatedHumanCode;
                        ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #18
0
        public override async Task <Command> Edit(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <CompraManualDto>(command.Json);

                var compras = await MyRepository.Get(c => c.Id == cmd.EntityId, null, "TipoEntrada,Fornecedor,Itens,Itens.Servico,Itens.Unidade");

                var compraManual = compras.FirstOrDefault();

                if (compraManual != null)
                {
                    compraManual.UpdateEntity(dto);

                    if (compraManual.Itens != null && compraManual.Itens.Count > 0)
                    {
                        compraManual.ValorTotal = 0;
                        foreach (var item in compraManual.Itens)
                        {
                            compraManual.ValorTotal += item.ValorTotal;
                        }
                    }

                    cmd.Cmd  = ServerCommands.LogResultOk;
                    cmd.Json = await SerializerAsync.SerializeJson(true);

                    await MyRepository.Save();
                }
                else
                {
                    cmd.Cmd  = ServerCommands.LogResultDeny;
                    cmd.Json = await SerializerAsync.SerializeJson(false);
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #19
0
        public override async Task <Command> Edit(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <UserDadosDto>(command.Json);

                var cliente = await MyRepository.GetById(cmd.EntityId);

                if (cliente != null)
                {
                    cliente.UpdateEntity(dto);
                    var enderecoRep = new Repository <Endereco>(Context);
                    var endereco    = await enderecoRep.GetById(dto.Endereco.Id);

                    if (endereco != null)
                    {
                        endereco.UpdateEntity(dto.Endereco);
                        cliente.Endereco = endereco;
                    }

                    cmd.Cmd  = ServerCommands.LogResultOk;
                    cmd.Json = await SerializerAsync.SerializeJson(true);

                    await MyRepository.Save();
                }
                else
                {
                    cmd.Cmd  = ServerCommands.LogResultDeny;
                    cmd.Json = await SerializerAsync.SerializeJson(false);
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #20
0
        public async Task <Command> GetApiWaitCommand(string id)
        {
            Command cmd = null;

            _isWait = true;
            int timeCount = 0;

            //while (cmd == null && timeCount <= ServerCommands.TimeOutApiRequest)
            //{
            //    cmd = _waitCommandsList.First(c => c.Id == id);
            //    await Task.Delay(100);
            //    timeCount++;
            //}
            do
            {
                var data        = _sr?.ReadLine();
                var dataDescomp = await StringCompressionAsync.DecompressString(data);

                cmd = await SerializerAsync.DeserializeJson <Command>(dataDescomp);

                if (cmd != null && !cmd.IsWait)
                {
                    DelegateAction del;
                    if (!string.IsNullOrEmpty(cmd.Id) &&
                        _srvsCmdsBuffer.TryGetValue(cmd.Id, out del))
                    {
                        del.server[0] = dataDescomp;
                        del.Exec();
                    }
                    else if (_srvsCmdsBuffer.TryGetValue(ServerCommands.BroadCastId, out del))
                    {
                        del.server[0] = dataDescomp;
                        del.Exec();
                    }
                }
            } while (cmd != null && !cmd.IsWait);
            _isWait = false;

            return(cmd);
        }
예제 #21
0
        public static async Task <Command> EditAddress(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                using (var context = new ZZContext())
                {
                    var dto = await SerializerAsync.DeserializeJson <EnderecoDto>(cmd.Json);

                    var repAddress = new Repository <Endereco>(context);
                    var repCity    = new Repository <Cidade>(context);
                    var cidades    = await repCity.Get(c => c.Descricao.Contains(dto.Cidade) && c.Estado.Sigla.Equals(dto.Estado), null, "Estado");

                    var cidade = cidades.FirstOrDefault();
                    if (cidade != null)
                    {
                        var address = await repAddress.GetById(dto.Id);

                        if (address != null)
                        {
                            cmd.Cmd = ServerCommands.LogResultOk;
                            address.UpdateEntity(dto);
                            await repAddress.Save();
                        }
                        else
                        {
                            cmd.Cmd = ServerCommands.LogResultDeny;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #22
0
        public static async Task <Command> GetCityByUf(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                using (var context = new ZZContext())
                {
                    var dto = await SerializerAsync.DeserializeJson <TipoSiglaDto>(cmd.Json);

                    var repEstado = new Repository <Estado>(context);
                    var repCity   = new Repository <Cidade>(context);
                    var list      = await repEstado.Get(e => e.Sigla.Equals(dto.Sigla));

                    var entity  = list.FirstOrDefault();
                    var cidades = await repCity.Get(c => c.EstadoId == entity.Id);

                    var cidadeList = cidades.ToList();

                    if (cidadeList.Any())
                    {
                        cmd.Cmd = ServerCommands.LogResultOk;
                        var dtos = cidadeList.Select(t => t.ConvertDto(dto.Sigla)).ToList();

                        cmd.Json = await SerializerAsync.SerializeJsonList(dtos);
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.LogResultDeny;
                    }
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #23
0
        public static void ReturnServer(Object[] server, Object[] local)
        {
            var dataJson = SerializerAsync.DeserializeJson <Command>(server[0].ToString()).Result;

            try
            {
                if (dataJson != null)
                {
                    if (!dataJson.Cmd.Equals(ServerCommands.Exit))
                    {
                        if (dataJson.Cmd.Equals(ServerCommands.LogResultOk))
                        {
                            Id = dataJson.EntityId;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #24
0
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <CompraManualDto>(command.Json);

                var compras = await MyRepository.Get(t => t.Codigo.Equals(dto.Codigo));

                if (compras != null && !compras.Any())
                {
                    var unidadeMedidaRep = new Repository <UnidadeMedida>(Context);
                    var servicoRep       = new Repository <Servico>(Context);

                    var tipoEntradaRep = new Repository <TipoEntrada>(Context);
                    var tipoEntrada    = await tipoEntradaRep.GetById(dto.TipoEntradaId);

                    var fornecedorRep = new Repository <Fornecedor>(Context);
                    var fornecedor    = await fornecedorRep.GetById(dto.FornecedorId);

                    var entity = new CompraManual();
                    entity.UpdateEntity(dto);

                    if (tipoEntrada != null)
                    {
                        entity.TipoEntrada = tipoEntrada;
                    }

                    if (fornecedor != null)
                    {
                        entity.Fornecedor = fornecedor;
                    }

                    if (entity.DataEmissao == DateTime.MinValue)
                    {
                        entity.DataEmissao = DateTime.Now;
                    }

                    if (String.IsNullOrEmpty(entity.Codigo))
                    {
                        if (!string.IsNullOrEmpty(entity.Fornecedor.NomeFantasia))
                        {
                            entity.Codigo = entity.Fornecedor.NomeFantasia + DateTime.Now.ToString();
                        }
                    }

                    if (entity.Itens != null && entity.Itens.Count > 0)
                    {
                        entity.ValorTotal = 0;
                        foreach (var item in entity.Itens)
                        {
                            var unidadeMedida = await unidadeMedidaRep.GetById(item.UnidadeId);

                            var servico = await servicoRep.GetById(item.ServicoId);

                            if (unidadeMedida != null)
                            {
                                item.Unidade = unidadeMedida;
                            }

                            if (servico != null)
                            {
                                item.Servico = servico;
                                item.Codigo  = servico.Codigo + DateTime.Now.ToString();
                            }

                            entity.ValorTotal += item.ValorTotal;
                        }
                    }

                    var insertEntity = await MyRepository.Insert(entity);

                    if (insertEntity != null)
                    {
                        cmd.Cmd  = ServerCommands.LogResultOk;
                        cmd.Json = await SerializerAsync.SerializeJson(true);

                        await MyRepository.Save();

                        cmd.EntityId = entity.Id;
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.RepeatedHumanCode;
                        ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #25
0
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <UserDadosDto>(command.Json);

                var clientes = await MyRepository.Get(t => t.Codigo.Equals(dto.Codigo));

                if (clientes != null && !clientes.Any())
                {
                    var entity = new Cliente();
                    entity.UpdateEntity(dto);

                    var myCmd = new Command();
                    if (entity.EnderecoId <= 0)
                    {
                        var enderecoCmd = new Command {
                            Json = await SerializerAsync.SerializeJson(dto.Endereco)
                        };

                        if (string.IsNullOrEmpty(entity.Endereco.Cep))
                        {
                            myCmd = await LocalizationManager.GetAddress(enderecoCmd);
                        }
                        else
                        {
                            myCmd = await LocalizationManager.GetAddressByZipCode(enderecoCmd);
                        }
                        entity.Endereco.UpdateEntity(await SerializerAsync.DeserializeJson <EnderecoDto>(myCmd.Json));
                        entity.EnderecoId = entity.Endereco.Id;
                    }

                    if (entity.EnderecoId > 0)
                    {
                        entity.Endereco = null;
                    }

                    var insertEntity = await MyRepository.Insert(entity);

                    if (insertEntity != null)
                    {
                        cmd.Cmd  = ServerCommands.LogResultOk;
                        cmd.Json = await SerializerAsync.SerializeJson(true);

                        await MyRepository.Save();

                        cmd.EntityId = entity.Id;
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.RepeatedHumanCode;
                        ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #26
0
        public static async Task <Command> SaveAddress(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                using (var context = new ZZContext())
                {
                    var dto = await SerializerAsync.DeserializeJson <EnderecoDto>(cmd.Json);

                    var repAddress = new Repository <Endereco>(context);

                    if (dto.Ibge <= 0)
                    {
                        var viacep = await SearchAddressAsync(dto.Cep, CancellationToken.None);

                        dto.Ibge    = viacep.IBGECode;
                        dto.GIACode = viacep.GIACode;
                    }

                    if (String.IsNullOrWhiteSpace(dto.Codigo))
                    {
                        dto.Codigo = dto.Ibge.ToString() + dto.GIACode.ToString() + dto.Cep + dto.Numero.ToString();
                    }
                    var address = await repAddress.Get(e =>
                                                       e.Logradouro.Equals(dto.Logradouro) && e.Numero == dto.Numero);

                    if (!address.Any())
                    {
                        var entity = new Endereco();
                        entity.UpdateEntity(dto);

                        var insertEntity = await repAddress.Insert(entity);

                        if (insertEntity != null)
                        {
                            cmd.Cmd = ServerCommands.LogResultOk;

                            await repAddress.Save();

                            cmd.Json = await SerializerAsync.SerializeJson(entity);

                            cmd.EntityId = entity.Id;
                        }
                        else
                        {
                            cmd.Cmd = ServerCommands.RepeatedHumanCode;
                            ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                        }
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.LogResultDeny;
                    }
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <DtoLigacao>(command.Json);

                if (dto.FirstDtoId > 0 && dto.SecondDtoId > 0)
                {
                    var funcionarioEstoques = await MyRepository.Get(t => t.FuncionarioId == dto.FirstDtoId &&
                                                                     t.EstoqueId == dto.SecondDtoId);

                    var funcRep     = new Repository <Funcionario>(Context);
                    var funcionario = await funcRep.GetById(dto.FirstDtoId);

                    var estoqueRep = new Repository <Estoque>(Context);
                    var estoque    = await estoqueRep.GetById(dto.SecondDtoId);

                    if (funcionarioEstoques != null && !funcionarioEstoques.Any() && funcionario != null && estoque != null)
                    {
                        var entity = new FuncionarioEstoque();
                        entity.UpdateEntity(dto);
                        entity.Codigo      = DateTime.Now.ToString();
                        entity.Funcionario = funcionario;
                        entity.Estoque     = estoque;

                        var insertEntity = await MyRepository.Insert(entity);

                        if (insertEntity != null)
                        {
                            cmd.Cmd  = ServerCommands.LogResultOk;
                            cmd.Json = await SerializerAsync.SerializeJson(true);

                            await MyRepository.Save();

                            cmd.EntityId = entity.Id;
                        }
                        else
                        {
                            cmd.Cmd = ServerCommands.RepeatedHumanCode;
                            ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                        }
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.LogResultDeny;
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
        public override async Task <Command> Add(Command command)
        {
            Command cmd = new Command(command);

            try
            {
                var dto = await SerializerAsync.DeserializeJson <MovimentoEstoqueDto>(command.Json);

                if (await VerififyMovimentoEstoque(dto))
                {
                    var servicoRep = new Repository <Servico>(Context);
                    var servico    = await servicoRep.GetById(dto.ServicoId);

                    var tipoEntradaRep = new Repository <TipoEntrada>(Context);
                    var tipoEntrada    = await tipoEntradaRep.GetById(dto.TipoEntradaId);

                    var estoqueRep = new Repository <Estoque>(Context);
                    var estoque    = await estoqueRep.GetById(dto.EstoqueId);

                    var entity = new MovimentoEstoque();
                    entity.UpdateEntity(dto);

                    entity.Estoque     = estoque;
                    entity.Servico     = servico;
                    entity.TipoEntrada = tipoEntrada;

                    if (String.IsNullOrEmpty(entity.Codigo))
                    {
                        if (!string.IsNullOrEmpty(entity.Servico.Codigo))
                        {
                            entity.Codigo = entity.Servico.Codigo + DateTime.Now.ToString();
                        }
                    }

                    if (entity.DataMovimento == DateTime.MinValue)
                    {
                        entity.DataMovimento = entity.Data;
                    }

                    var insertEntity = await MyRepository.Insert(entity);

                    if (insertEntity != null)
                    {
                        cmd.Cmd  = ServerCommands.LogResultOk;
                        cmd.Json = await SerializerAsync.SerializeJson(true);

                        await MyRepository.Save();

                        cmd.EntityId = entity.Id;
                    }
                    else
                    {
                        cmd.Cmd = ServerCommands.RepeatedHumanCode;
                        ConsoleEx.WriteLine(ServerCommands.RepeatedHumanCode);
                    }
                }
                else
                {
                    cmd.Cmd = ServerCommands.LogResultDeny;
                }
            }
            catch (Exception e)
            {
                ConsoleEx.WriteError(e);
            }

            return(cmd);
        }
예제 #29
0
        private async void Read(Object source, ElapsedEventArgs e)
        {
            try
            {
//                _timerRead.Enabled = false;

                if (!_isWait && _clientSocket?.Client?.Available > 0)
                {
                    _hearthBitCount = 0;

                    var data = _sr?.ReadLine();

                    string dataDescomp = await StringCompressionAsync.DecompressString(data);

                    try
                    {
                        var bt = await SerializerAsync.DeserializeJson <Command>(dataDescomp);

                        if (bt != null)
                        {
                            if (!bt.Cmd.Equals(ServerCommands.HearthBit))
                            {
                                //_bufferRead.Enqueue(dataDescomp);
//                            ConsoleEx.WriteLine("Olha o que eu coloquei aqui: " +dataDescomp);


                                DelegateAction del;
                                if (!string.IsNullOrEmpty(bt.Id) &&
                                    _srvsCmdsBuffer.TryGetValue(bt.Id, out del))
                                {
                                    del.server[0] = dataDescomp;
                                    del.Exec();
                                }
                                else if (_srvsCmdsBuffer.TryGetValue(ServerCommands.BroadCastId, out del))
                                {
                                    del.server[0] = dataDescomp;
                                    del.Exec();
                                }
                            }
                        }
                        else
                        {
                            _bufferRead.Enqueue(dataDescomp);
                        }
                    }
                    catch (Exception ex)
                    {
                        ConsoleEx.WriteError("Read", ex);
                        //Dispose();
                    }
                }
            }
            catch (Exception exception)
            {
                ConsoleEx.WriteError("erro do Read:", exception);
                //Dispose();
            }
            finally
            {
//                _timerRead.Enabled = true;
            }
        }