New Layout
This commit is contained in:
@@ -1,46 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GeradoresService
|
||||
{
|
||||
internal class CC
|
||||
public static class CartaoCidadao
|
||||
{
|
||||
|
||||
/*static bool Validate(string CardNumber)
|
||||
// Função estática para gerar o número de documento
|
||||
public static string Generate()
|
||||
{
|
||||
int sum = 0;
|
||||
bool alternate = false;
|
||||
Random rand = new Random();
|
||||
|
||||
// Percorre os dígitos do número de trás para frente
|
||||
for (int i = creditCardNumber.Length - 1; i >= 0; i--)
|
||||
// Gerar o Número de Identificação Civil (8 dígitos)
|
||||
string numeroIdentificacaoCivil = rand.Next(0, 100000000).ToString("D8");
|
||||
|
||||
// Gerar o Check Digit C
|
||||
int checkDigitC = CalcularCheckDigit(numeroIdentificacaoCivil);
|
||||
|
||||
// Gerar a Versão (duas letras ou números aleatórios entre A-Z ou 0-9)
|
||||
string versao = GerarVersao();
|
||||
|
||||
// Gerar o Check Digit T (Número aleatório entre 0-9)
|
||||
int checkDigitT = rand.Next(0, 10); // T: Número aleatório por enquanto, conforme exemplo.
|
||||
|
||||
// Montar o número completo do documento com o formato desejado
|
||||
string numeroDocumento = $"{numeroIdentificacaoCivil} {checkDigitC} {versao}{checkDigitT}";
|
||||
|
||||
return numeroDocumento;
|
||||
}
|
||||
|
||||
// Função para calcular o Check Digit C com base no Número de Identificação Civil
|
||||
private static int CalcularCheckDigit(string numeroIdentificacaoCivil)
|
||||
{
|
||||
int soma = 0;
|
||||
bool segundoElemento = false;
|
||||
|
||||
// Percorrer o número da direita para a esquerda
|
||||
for (int i = numeroIdentificacaoCivil.Length - 1; i >= 0; i--)
|
||||
{
|
||||
int digit = creditCardNumber[i] - '0';
|
||||
int valor = numeroIdentificacaoCivil[i] - '0'; // Converte o char para inteiro
|
||||
|
||||
// Se é um dígito
|
||||
if (digit >= 0 && digit <= 9)
|
||||
// Multiplicar cada segundo número por 2
|
||||
if (segundoElemento)
|
||||
{
|
||||
if (alternate)
|
||||
{
|
||||
digit *= 2;
|
||||
if (digit > 9)
|
||||
digit -= 9;
|
||||
}
|
||||
valor *= 2;
|
||||
if (valor >= 10) valor -= 9; // Subtrair 9 se o valor for maior ou igual a 10
|
||||
}
|
||||
|
||||
sum += digit;
|
||||
alternate = !alternate;
|
||||
soma += valor;
|
||||
segundoElemento = !segundoElemento;
|
||||
}
|
||||
|
||||
// Retornar o check digit (resto da soma por 10)
|
||||
return soma % 10;
|
||||
}
|
||||
|
||||
// Função para gerar a versão (letras ou números)
|
||||
private static string GerarVersao()
|
||||
{
|
||||
Random rand = new Random();
|
||||
string versao = "";
|
||||
|
||||
for (int i = 0; i < 2; i++) // Gerar duas letras/números
|
||||
{
|
||||
int numeroAleatorio = rand.Next(0, 36); // 26 letras + 10 números
|
||||
|
||||
if (numeroAleatorio < 26)
|
||||
{
|
||||
// Gerar uma letra de A a Z
|
||||
versao += (char)('A' + numeroAleatorio);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Se não é um dígito, o número é inválido
|
||||
return false;
|
||||
// Gerar um número de 0 a 9
|
||||
versao += (char)('0' + (numeroAleatorio - 26));
|
||||
}
|
||||
}
|
||||
|
||||
// O número de cartão de crédito é válido se a soma for divisível por 10
|
||||
return sum % 10 == 0;
|
||||
}*/
|
||||
return versao;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using GeradoresService.DAL;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace GeradoresService
|
||||
{
|
||||
@@ -13,6 +15,7 @@ namespace GeradoresService
|
||||
_geradoresContext = geradoresContext;
|
||||
}
|
||||
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public enum NIFType
|
||||
{
|
||||
[Description("Pessoa singular (1)")]
|
||||
@@ -37,102 +40,58 @@ namespace GeradoresService
|
||||
PessoaColectivaIrregular = 9
|
||||
}
|
||||
|
||||
public string Generate(string? type)
|
||||
public string Generate(NIFType? type)
|
||||
{
|
||||
var nif = GenerateRandomNIF(type);
|
||||
//SaveNIF(nif);
|
||||
return nif;
|
||||
}
|
||||
|
||||
public string GenerateRandomNIF(string? nifType)
|
||||
public string GenerateRandomNIF(NIFType? nifType)
|
||||
{
|
||||
var firstDigitValidate = new char[] { '1', '2', '3', '5', '6', '8', '9' };
|
||||
Random rnd = new Random();
|
||||
char firstDigit;
|
||||
Random rnd = new Random();
|
||||
|
||||
if (string.IsNullOrEmpty(nifType))
|
||||
if (nifType.HasValue)
|
||||
{
|
||||
// Gera o primeiro dígito aleatório dentro dos válidos
|
||||
int firstDigitIndex = rnd.Next(0, 6); // Escolhe um índice de 0 a 5
|
||||
firstDigit = firstDigitValidate[firstDigitIndex];
|
||||
firstDigit = ((int)nifType.Value).ToString()[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (firstDigitValidate.Contains(nifType[0]))
|
||||
{
|
||||
firstDigit = nifType[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
int firstDigitIndex = rnd.Next(0, 6); // Escolhe um índice de 0 a 5
|
||||
firstDigit = firstDigitValidate[firstDigitIndex];
|
||||
}
|
||||
var validFirstDigits = Enum.GetValues(typeof(NIFType)).Cast<int>().Select(v => v.ToString()[0]).ToArray();
|
||||
firstDigit = validFirstDigits[rnd.Next(validFirstDigits.Length)];
|
||||
}
|
||||
|
||||
// Gera os próximos 7 dígitos aleatórios
|
||||
string nextDigits = "";
|
||||
string nextDigits = string.Concat(Enumerable.Range(0, 7).Select(_ => rnd.Next(0, 10).ToString()));
|
||||
|
||||
int checkDigit = (firstDigit - '0') * 9;
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
nextDigits += rnd.Next(0, 10); // Gera um dígito aleatório de 0 a 9
|
||||
}
|
||||
|
||||
// Calcula o dígito de controlo
|
||||
int checkDigit = (firstDigit - '0') * 9;
|
||||
for (int i = 2; i <= 8; i++)
|
||||
{
|
||||
checkDigit += (nextDigits[i - 2] - '0') * (10 - i);
|
||||
checkDigit += (nextDigits[i] - '0') * (8 - i);
|
||||
}
|
||||
checkDigit = 11 - (checkDigit % 11);
|
||||
if (checkDigit >= 10)
|
||||
checkDigit = 0;
|
||||
|
||||
// Concatena os dígitos gerados e o dígito de controlo
|
||||
string randomNIF = firstDigit + nextDigits + checkDigit;
|
||||
|
||||
return randomNIF;
|
||||
return firstDigit + nextDigits + checkDigit;
|
||||
}
|
||||
|
||||
public bool Validate(string nif)
|
||||
{
|
||||
// Verificar se o NIF tem 9 dígitos
|
||||
if (nif.Length != 9)
|
||||
{
|
||||
if (nif.Length != 9 || !nif.All(char.IsDigit))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Converter o NIF para um array de dígitos
|
||||
int[] digitos = new int[9];
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
if (!int.TryParse(nif[i].ToString(), out digitos[i]))
|
||||
{
|
||||
return false; // Se algum caractere não for um dígito, o NIF é inválido
|
||||
}
|
||||
}
|
||||
int[] digitos = nif.Select(c => c - '0').ToArray();
|
||||
int soma = digitos.Take(8).Select((num, i) => num * (9 - i)).Sum();
|
||||
int digitoControlo = (11 - (soma % 11)) % 10;
|
||||
|
||||
// Calcular o dígito de controlo
|
||||
int soma = 0;
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
soma += digitos[i] * (9 - i);
|
||||
}
|
||||
int resto = soma % 11;
|
||||
int digitoControlo = resto <= 1 ? 0 : 11 - resto;
|
||||
|
||||
// Verificar se o dígito de controlo coincide com o último dígito do NIF
|
||||
return digitoControlo == digitos[8];
|
||||
}
|
||||
|
||||
public void SaveNIF(string NIF)
|
||||
{
|
||||
var ger = new Geradore()
|
||||
{
|
||||
Valor = NIF
|
||||
};
|
||||
|
||||
var ger = new Geradore() { Valor = NIF };
|
||||
_geradoresContext.Geradores.Add(ger);
|
||||
_geradoresContext.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user