Last active
April 10, 2024 04:27
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.