using System;
using Erp.SqlQueryManager.Domain.Shared;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;
namespace Erp.SqlQueryManager.Domain.Entities;
///
/// SQL Function entity for creating and managing database functions
///
public class SqlFunction : FullAuditedEntity, IMultiTenant
{
public Guid? TenantId { get; set; }
///
/// Function name in database
///
public string FunctionName { get; set; }
///
/// Schema name (default: dbo)
///
public string SchemaName { get; set; }
///
/// Display name
///
public string DisplayName { get; set; }
///
/// Description
///
public string Description { get; set; }
///
/// Function type
///
public SqlFunctionType FunctionType { get; set; }
///
/// Full function definition (CREATE/ALTER)
///
public string FunctionBody { get; set; }
///
/// Return type definition
///
public string ReturnType { get; set; }
///
/// DataSource code
///
public string DataSourceCode { get; set; }
///
/// Status
///
public SqlQueryStatus Status { get; set; }
///
/// Category
///
public string Category { get; set; }
///
/// Whether function exists in database
///
public bool IsDeployed { get; set; }
///
/// Last deployment time
///
public DateTime? LastDeployedAt { get; set; }
///
/// Parameter definitions (JSON)
///
public string Parameters { get; set; }
protected SqlFunction()
{
}
public SqlFunction(
Guid id,
string functionName,
string schemaName,
string displayName,
SqlFunctionType functionType,
string functionBody,
string returnType,
string dataSourceCode,
Guid? tenantId = null) : base(id)
{
FunctionName = functionName;
SchemaName = schemaName ?? "dbo";
DisplayName = displayName;
FunctionType = functionType;
FunctionBody = functionBody;
ReturnType = returnType;
DataSourceCode = dataSourceCode;
TenantId = tenantId;
Status = SqlQueryStatus.Draft;
IsDeployed = false;
}
public void UpdateBody(string body)
{
FunctionBody = body;
}
public void MarkAsDeployed()
{
IsDeployed = true;
LastDeployedAt = DateTime.UtcNow;
Status = SqlQueryStatus.Active;
}
public string GetFullName() => $"{SchemaName}.{FunctionName}";
}