using CRMDAL.CustomCode.Contact; using CRMDAL.DTO.Contact; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using System; using System.Data.Common; using System.Threading.Tasks; using static GB5Shared.GB5Constant.Constant; namespace CRMBLL.Contact { public class ContactBLL : IContactBLL { private readonly IContactDAL _contactDAL; private readonly AutoNumber _autoNumber; private readonly IQueryExecutor _queryExecutor; public ContactBLL( IContactDAL contactDAL, AutoNumber autoNumber, IQueryExecutor queryExecutor) { _contactDAL = contactDAL; _autoNumber = autoNumber; _queryExecutor = queryExecutor; } public async Task SaveContact(ContactDTO dto, LoginDTO login) => await SaveContact(dto, login, null); public async Task SaveContact(ContactDTO dto, LoginDTO login, DbTransaction? tx = null) { bool ownTransaction = tx == null; var trans = ownTransaction ? await _queryExecutor.BeginTransactionAsync(login) : tx!; try { var autoDto = await _autoNumber.GetNumberAsync(1, AUTONUMBERCONSTANT.CONTACT, login); dto.ContactId = autoDto.StartNumber; // IDs from login dto.ContactCreatedById = login.UserId; dto.ContactModifiedById = login.UserId; dto.ContactUserLinkId = login.UserId; // FK → MUSER.USERID // FK → MENTITY.ENTITYID — use correct value from MENTITY table dto.ObjectTypeId = dto.ObjectTypeId == 0 ? -2147481759 : dto.ObjectTypeId; dto.ObjectId = dto.ObjectId == 0 ? -1 : dto.ObjectId; // Self-ref FK → MCONTACT.CONTACTID (null = no reporting contact) dto.ReportingToId = dto.ReportingToId == 0 ? null : dto.ReportingToId; // String defaults dto.ContactSalutation = string.IsNullOrEmpty(dto.ContactSalutation) ? "NONE" : dto.ContactSalutation; dto.ContactMiddleName = string.IsNullOrEmpty(dto.ContactMiddleName) ? "NONE" : dto.ContactMiddleName; dto.ContactLastName = string.IsNullOrEmpty(dto.ContactLastName) ? "NONE" : dto.ContactLastName; dto.ContactRelationShip = string.IsNullOrEmpty(dto.ContactRelationShip) ? "NONE" : dto.ContactRelationShip; dto.ContactDesignation = string.IsNullOrEmpty(dto.ContactDesignation) ? "NONE" : dto.ContactDesignation; dto.ContactCompany = string.IsNullOrEmpty(dto.ContactCompany) ? "NONE" : dto.ContactCompany; dto.ContactDefaultImageId = string.IsNullOrEmpty(dto.ContactDefaultImageId) ? "NONE" : dto.ContactDefaultImageId; dto.ContactPhoneNo = string.IsNullOrEmpty(dto.ContactPhoneNo) ? "NONE" : dto.ContactPhoneNo; dto.ContactMailId = string.IsNullOrEmpty(dto.ContactMailId) ? "NONE" : dto.ContactMailId; // Numeric defaults dto.ContactSex = dto.ContactSex == 0 ? (byte)1 : dto.ContactSex; dto.ContactSortOrder = dto.ContactSortOrder == 0 ? (short)1 : dto.ContactSortOrder; dto.ContactLinkType = dto.ContactLinkType == 0 ? (byte)1 : dto.ContactLinkType; dto.ContactContactNature = dto.ContactContactNature == 0 ? (byte)1 : dto.ContactContactNature; // System fields dto.ContactCreatedOn = DateTime.UtcNow; dto.ContactModifiedOn = DateTime.UtcNow; dto.ContactVersion = 1; dto.ContactStatus = 1; dto.ContactSourceType = 5; await _contactDAL.SaveContact(dto, login, trans); if (ownTransaction) await _queryExecutor.CommitAsync(trans); return dto.ContactId; } catch (Exception) { if (ownTransaction) await _queryExecutor.RollbackAsync(trans); throw; } } public async Task UpdateContact(ContactDTO dto, LoginDTO login) { var trans = await _queryExecutor.BeginTransactionAsync(login); try { dto.ContactModifiedById = login.UserId; dto.ContactModifiedOn = DateTime.UtcNow; dto.ContactVersion = (short)(dto.ContactVersion + 1); await _contactDAL.UpdateContact(dto, login, trans); await _queryExecutor.CommitAsync(trans); return dto.ContactId; } catch (Exception) { await _queryExecutor.RollbackAsync(trans); throw; } } public async Task GetContact(int contactId, LoginDTO login) { try { return await _contactDAL.GetContact(contactId, login); } catch (Exception) { throw; } } } }