public IActionResult Atualizar(CadastroPerfilDTO model)
        {
            try
            {
                string tela = this.ControllerContext.RouteData.Values["action"].ToString();
                this._auditoria.cadastrar(tela, this.User.Identity.Name, model);

                var l = this._service.getPerfilById((int)model.Id);
                if (l == null)
                {
                    return(NotFound("Licença não encontrada!"));
                }

                var perfil = new Perfil();
                _mapper.Map(model, perfil);
                var empresaid = _mapper.Map <int>(model.EmpresaId);

                var empPerfil = new EmpresaPerfil();

                empPerfil.EmpresaId = model.EmpresaId;
                empPerfil.PerfilId  = (int)model.Id;

                var empsPerfis = new List <EmpresaPerfil>();
                empsPerfis.Add(empPerfil);

                perfil.EmpresasPerfis = empsPerfis;

                // Funcionalidades

                var perfisFuncionalidades = new List <PerfilFuncionalidade>();

                foreach (var f in model.FuncionalidadesDTO)
                {
                    var perfilFuncionalidade = new PerfilFuncionalidade();

                    perfilFuncionalidade.FuncionalidadeId = f.Id;
                    perfilFuncionalidade.PerfilId         = (int)model.Id;

                    perfisFuncionalidades.Add(perfilFuncionalidade);
                }
                perfil.PerfisFuncionalidades = perfisFuncionalidades;

                this._service.Autalizar(perfil, empresaid);

                return(Ok(perfil));
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }
        /// <summary></summary>
        private bool cadastro()
        {
            if (string.IsNullOrEmpty(this._empresa.CNPJ))
            {
                throw new Exception("CNPJ da empresa é obrigatório!");
            }

            if (string.IsNullOrEmpty(this._empresa.RazaoSocial))
            {
                throw new Exception("Razão Social/Nome da empresa é obrigatório!");
            }

            if (string.IsNullOrEmpty(this._empresa.NomeFantasia))
            {
                throw new Exception("Nome Fantasia/Nomde de Exibição da empresa é obrigatório!");
            }

            if (string.IsNullOrEmpty(this._empresa.Endereco))
            {
                throw new Exception("Endereço da empresa é obrigatório!");
            }

            if (string.IsNullOrEmpty(this._empresa.Bairro))
            {
                throw new Exception("Bairro da empresa é obrigatório!");
            }

            if (string.IsNullOrEmpty(this._empresa.Cidade))
            {
                throw new Exception("Cidade da empresa é obrigatório!");
            }

            if (string.IsNullOrEmpty(this._empresa.Estado))
            {
                throw new Exception("Estado da empresa é obrigatório!");
            }

            this._empresa.CaminhoDiretorio = Path.Combine(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "GED" + Path.DirectorySeparatorChar + this._empresa.CNPJ.ToString() + Path.DirectorySeparatorChar);

            if (!Directory.Exists(this._empresa.CaminhoDiretorio))
            {
                Directory.CreateDirectory(this._empresa.CaminhoDiretorio);
            }

            this._context.Empresa.Add(this._empresa);
            this._context.SaveChanges();

            //CADASTRO MENU PERMISSÃO
            var licencaid          = this._context.Licenca.FirstOrDefault(x => x.NomeLicenca.Equals(this._empresa.NomeLicenca)).Id;
            var licencamenu        = this._context.LicencaMenu.Where(X => X.LicencaId == licencaid).Select(x => x.MenuId).ToList();
            var listamenupermissao = new List <MenuPermissao>();

            foreach (var item in licencamenu)
            {
                var menupermissao = new MenuPermissao()
                {
                    MenuId    = item,
                    EmpresaId = this._empresa.Id,
                    PerfilId  = 1
                };

                this._context.MenuPermissao.Add(menupermissao);
            }
            this._context.SaveChanges();

            //CADASTRO EMPRESA PERFIL ADM AUTOMATICAMENTE
            var empPerfil = new EmpresaPerfil()
            {
                EmpresaId = this._empresa.Id,
                PerfilId  = 1,
            };

            this._context.EmpresaPerfil.Add(empPerfil);
            this._context.SaveChanges();

            return(true);
        }
        /// <summary></summary>
        private bool cadastro()
        {
            if (string.IsNullOrEmpty(this._perfil.Nome))
            {
                throw new Exception("Nome da perfil é obrigatório!");
            }

            _perfil.EmpresasPerfis = new List <EmpresaPerfil>();

            var EmpresasPerfis = new List <EmpresaPerfil>();
            var empPerfil      = new EmpresaPerfil();

            // SALVAR PERFIL
            this._context.Perfil.Add(this._perfil);
            this._context.SaveChanges();

            // SALVAR EMPRESAPERFIL
            empPerfil.PerfilId  = _perfil.Id;
            empPerfil.EmpresaId = _empresaid;

            EmpresasPerfis.Add(empPerfil);
            this._context.SaveChanges();

            // SALVAR FUNCIONALIDADE
            var PerFuncionalidades = new List <PerfilFuncionalidade>();

            foreach (var func in _funcionalidades)
            {
                var perFunc = new PerfilFuncionalidade()
                {
                    FuncionalidadeId = func.Id,
                    PerfilId         = _perfil.Id
                };

                PerFuncionalidades.Add(perFunc);
            }
            ;

            _perfil.EmpresasPerfis        = EmpresasPerfis;
            _perfil.PerfisFuncionalidades = PerFuncionalidades;

            //SALVAR MENU PERMISSÃO
            var menufunc = _funcionalidades.Select(x => x.MenuId).Distinct().ToList();

            foreach (var item in menufunc)
            {
                var m = new MenuPermissao()
                {
                    MenuId    = item,
                    EmpresaId = _empresaid,
                    PerfilId  = this._perfil.Id,
                };

                this._context.MenuPermissao.Add(m);
            }

            var menusPaiId = _context.Menu.Where(x => menufunc.Contains((int)x.Id)).Select(x => (int)x.PaiId).Distinct().ToList();

            foreach (var item in menusPaiId)
            {
                var menuPai = new MenuPermissao()
                {
                    MenuId    = item,
                    EmpresaId = _empresaid,
                    PerfilId  = this._perfil.Id,
                };

                this._context.MenuPermissao.Add(menuPai);
            }

            return(this._context.SaveChanges() > 0);
        }