Skip to content

Instantly share code, notes, and snippets.

@YurePereira
Last active April 10, 2024 04:27
Show Gist options
  • Save YurePereira/e84e48dfdfe0adf5f301f2d5e09eb588 to your computer and use it in GitHub Desktop.
Save YurePereira/e84e48dfdfe0adf5f301f2d5e09eb588 to your computer and use it in GitHub Desktop.
Método para gerar protocolo de atendimento baseado no ano e mês atual.
using System;
namespace Projeto.Application.Utilities
{
public static class ProtocolGenerator
{
private static readonly Random RandomGenerator = new Random();
public static string GenerateRandomProtocol(int length = 10)
{
const string allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//Remoção de 6 caracteres, 4 do ano e mais 2 do mês.
length = length - 6;
DateTime today = DateTime.Now;
string year = today.Year.ToString();
string month = today.Month.ToString("00");
char[] result = new char[length];
for (int i = 0; i < length; i++)
{
result[i] = allowedChars[RandomGenerator.Next(allowedChars.Length)];
}
return year + month + new string(result);
}
}
}
@YurePereira
Copy link
Author

YurePereira commented Apr 7, 2024

Atualizado para remover os 6 caracteres a mais, 4 dígitos do ano e outros 2 do mês. Pois estava somando esses 6 caracteres no string final, sendo que a soma final da quantidade de caracteres deve bater com a passada via parâmetro da variável length.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment