101 lines
2.8 KiB
C#
101 lines
2.8 KiB
C#
using LevelCode.License.DataAccess;
|
|
using LevelCode.License.Models;
|
|
using LevelCode.License.Models.Enums;
|
|
using LevelCode.License.Security;
|
|
using System;
|
|
using System.Text.Json;
|
|
|
|
namespace LevelCode.License.Business
|
|
{
|
|
public class LicencaService
|
|
{
|
|
private readonly LicencaDAL _licencaDal;
|
|
|
|
public LicencaService(LicencaDAL licencaDal)
|
|
{
|
|
_licencaDal = licencaDal;
|
|
}
|
|
|
|
// =========================
|
|
// BUSCAR LICENÇA POR KEY
|
|
// =========================
|
|
public ModeloLicenca ObterPorLicenseKey(string licenseKey)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(licenseKey))
|
|
return null;
|
|
|
|
return _licencaDal.BuscarPorLicenseKey(licenseKey);
|
|
}
|
|
|
|
// =========================
|
|
// VALIDAR LICENÇA
|
|
// =========================
|
|
public bool LicencaValida(ModeloLicenca licenca, out string mensagem)
|
|
{
|
|
mensagem = "";
|
|
|
|
if (licenca == null)
|
|
{
|
|
mensagem = "Licença não encontrada.";
|
|
return false;
|
|
}
|
|
|
|
if (licenca.Status != (int)StatusLicencaEnum.Ativa)
|
|
{
|
|
mensagem = "Licença inativa ou bloqueada.";
|
|
return false;
|
|
}
|
|
|
|
if (licenca.ExpiraEm.HasValue && licenca.ExpiraEm.Value.Date < DateTime.Now.Date)
|
|
{
|
|
mensagem = "Licença expirada.";
|
|
return false;
|
|
}
|
|
|
|
mensagem = "Licença válida.";
|
|
return true;
|
|
}
|
|
|
|
// =========================
|
|
// CRIAR LICENÇA (básico)
|
|
// =========================
|
|
|
|
public int CriarLicenca(ModeloLicenca modelo)
|
|
{
|
|
if (modelo == null)
|
|
throw new ArgumentNullException(nameof(modelo));
|
|
|
|
// 🔑 GERAR LICENSE KEY AUTOMATICAMENTE
|
|
modelo.LicenseKey = LicenseKeyGenerator.Gerar();
|
|
|
|
// 📦 SERIALIZAR MÓDULOS
|
|
if (modelo.ModulosLista != null && modelo.ModulosLista.Count > 0)
|
|
modelo.Modulos = JsonSerializer.Serialize(modelo.ModulosLista);
|
|
else
|
|
modelo.Modulos = "[]";
|
|
|
|
modelo.Status = 1;
|
|
modelo.DataCriacao = DateTime.Now;
|
|
|
|
return _licencaDal.Inserir(modelo);
|
|
}
|
|
|
|
|
|
// =========================
|
|
// BLOQUEAR LICENÇA
|
|
// =========================
|
|
public void BloquearLicenca(int idLicenca)
|
|
{
|
|
_licencaDal.AtualizarStatus(idLicenca, (int)StatusLicencaEnum.Bloqueada);
|
|
}
|
|
|
|
// =========================
|
|
// ATIVAR LICENÇA
|
|
// =========================
|
|
public void AtivarLicenca(int idLicenca)
|
|
{
|
|
_licencaDal.AtualizarStatus(idLicenca, (int)StatusLicencaEnum.Ativa);
|
|
}
|
|
}
|
|
}
|