using System;
using System.Data.Common;
using System.Threading.Tasks;
using GB5Shared.DTO.Framework.Login;
using GB5Shared.QueryExecutor;
using GoodBooks.PAY.PAYDAL.DTO.Loyalty;
using GoodBooks.PAY.PAYDAL.Query.Loyalty;
using Newtonsoft.Json;
namespace GoodBooks.PAY.PAYDAL.CustomCode.Loyalty
{
///
/// DAL implementation for MCUSTLOYALTYENROLLMENT.
///
public class CustomerLoyaltyEnrollmentDAL : ICustomerLoyaltyEnrollmentDAL
{
private readonly IQueryExecutor _queryExecutor;
public CustomerLoyaltyEnrollmentDAL(IQueryExecutor queryExecutor)
{
_queryExecutor = queryExecutor;
}
///
public async Task GetEnrollment(int enrollmentId, LoginDTO login)
{
try
{
CustomerLoyaltyEnrollmentDTO dto = await _queryExecutor.QuerySingleAsync(
login,
CustomerLoyaltyEnrollmentQB.GET_ENROLLMENT,
new
{
EnrollmentId = enrollmentId,
TenantId = login.ClientId
}).ConfigureAwait(false);
return JsonConvert.SerializeObject(dto);
}
catch (Exception)
{
throw;
}
}
///
public async Task GetEnrollmentByCustomerProgram(
int customerId, int programId, LoginDTO login)
{
try
{
CustomerLoyaltyEnrollmentDTO? dto = await _queryExecutor.QuerySingleAsync(
login,
CustomerLoyaltyEnrollmentQB.GET_ENROLLMENT_BY_CUSTOMER_PROGRAM,
new
{
CustomerId = customerId,
LoyaltyProgramId = programId,
TenantId = login.ClientId
}).ConfigureAwait(false);
return dto;
}
catch (Exception)
{
throw;
}
}
///
public async Task CountEnrollment(int customerId, int programId, LoginDTO login)
{
try
{
int count = await _queryExecutor.ExecuteScalarAsync(
login,
CustomerLoyaltyEnrollmentQB.COUNT_ENROLLMENT,
new
{
CustomerId = customerId,
LoyaltyProgramId = programId,
TenantId = login.ClientId
}).ConfigureAwait(false);
return count;
}
catch (Exception)
{
throw;
}
}
///
public async Task InsertEnrollment(
CustomerLoyaltyEnrollmentDTO dto, LoginDTO login, DbTransaction? tx)
{
try
{
int rows = await _queryExecutor.ExecuteAsync(
login,
CustomerLoyaltyEnrollmentQB.INSERT_ENROLLMENT,
new
{
dto.EnrollmentId,
dto.CustomerId,
dto.LoyaltyProgramId,
dto.LoyaltyTierId,
dto.EnrolledOn,
dto.MembershipNo,
dto.CurrentPoints,
dto.LifetimePoints,
dto.LastActivityDate,
dto.Remarks,
dto.Version,
dto.Status,
dto.SortOrder,
CreatedById = login.UserId,
ModifiedById = login.UserId,
dto.SourceType,
TenantId = login.ClientId
},
tx).ConfigureAwait(false);
return rows;
}
catch (Exception)
{
throw;
}
}
///
public async Task UpdateEnrollmentPoints(
int enrollmentId, decimal currentPoints, decimal lifetimePoints,
LoginDTO login, DbTransaction? tx)
{
try
{
int rows = await _queryExecutor.ExecuteAsync(
login,
CustomerLoyaltyEnrollmentQB.UPDATE_ENROLLMENT_POINTS,
new
{
EnrollmentId = enrollmentId,
CurrentPoints = currentPoints,
LifetimePoints = lifetimePoints
},
tx).ConfigureAwait(false);
return rows;
}
catch (Exception)
{
throw;
}
}
///
public async Task UpdateEnrollmentTier(
int enrollmentId, int loyaltyTierId, LoginDTO login, DbTransaction? tx)
{
try
{
int rows = await _queryExecutor.ExecuteAsync(
login,
CustomerLoyaltyEnrollmentQB.UPDATE_ENROLLMENT_TIER,
new
{
EnrollmentId = enrollmentId,
LoyaltyTierId = loyaltyTierId
},
tx).ConfigureAwait(false);
return rows;
}
catch (Exception)
{
throw;
}
}
}
}