199 lines
8.3 KiB
C#

using Volo.Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Users;
using HospitalManagementSystem.Documents;
using HospitalManagementSystem.Patients.Dto;
using HospitalManagementSystem.Patients;
using HospitalManagementSystem.Permissions;
using Microsoft.AspNetCore.Authorization;
using HospitalManagementSystem.Appoinments.Dto;
using Volo.Abp.ObjectMapping;
using HospitalManagementSystem.Dto;
using Abp.Application.Services.Dto;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;
using Abp.UI;
using HospitalManagementSystem.Doctors;
using ClosedXML.Excel;
using System.IO;
using Microsoft.AspNetCore.Hosting;
namespace HospitalManagementSystem.Appointments
{
public class AppointmentAppService : ApplicationService, IAppointmentAppService, ITransientDependency
{
private readonly ICurrentUser _currentUser;
private readonly ICurrentTenant _currentTenant;
private IRepository<Appointment, Guid> _appointmentsRepository;
private readonly IRepository<Doctor, Guid> _doctorRepository;
private readonly IWebHostEnvironment _env;
public AppointmentAppService(ICurrentUser currentUser, ICurrentTenant currentTenant, IRepository<Appointment, Guid> appointmentsRepository, IRepository<Doctor, Guid> doctorRepository, IWebHostEnvironment env)
{
_currentUser = currentUser;
_currentTenant = currentTenant;
_appointmentsRepository = appointmentsRepository;
_doctorRepository = doctorRepository;
_env = env;
}
public async Task<string> GetAsync()
{
var x = _currentUser.Id;
var y = _currentTenant.Id;
return _currentUser.Name + " checking done.";
}
#region Get Patient List with Paging and Searching
public async Task<PagedResultDto<AppointmentDto>> GetAppointmentListAsync(PagingSortResultDto input)
{
var queryable = await _appointmentsRepository.GetQueryableAsync();
var filteredQuery = queryable
.WhereIf(!string.IsNullOrEmpty(input.Search), x => x.FirstName.ToLower().Contains(input.Search.ToLower()) || x.LastName.ToLower().Contains(input.Search.ToLower()));
var totalCount = await filteredQuery.CountAsync();
filteredQuery = !string.IsNullOrEmpty(input.Sorting)
? filteredQuery.OrderBy(input.Sorting)
: filteredQuery.OrderBy(x => x.Id);
var pagedQuery = await filteredQuery
.Skip(input.SkipCount)
.Take(input.MaxResultCount)
.ToListAsync();
var appoinmentdto = ObjectMapper.Map<List<Appointment>, List<AppointmentDto>>(pagedQuery);
return new PagedResultDto<AppointmentDto>(
totalCount,
appoinmentdto
);
}
#endregion
#region Get Appointment by ID
public async Task<AppointmentDto> GetAppointmentByIdAsync(Guid id)
{
var appointment = await _appointmentsRepository.FirstOrDefaultAsync(x => x.Id == id);
if (appointment == null)
{
throw new UserFriendlyException("Appointment Details not found");
}
return ObjectMapper.Map<Appointment, AppointmentDto>(appointment);
}
#endregion
#region Create Appointment
public async Task CreateAppointmentAsync(CreateOrUpdateAppointmentDto input)
{
Appointment appointment = new Appointment();
var newdata = ObjectMapper.Map<CreateOrUpdateAppointmentDto, Appointment>(input);
newdata.Doctor = await _doctorRepository.GetAsync(input.DoctorId.Value);
appointment = newdata;
appointment = await _appointmentsRepository.InsertAsync(appointment);
}
#endregion
#region Update Appointment
public async Task UpdateAppointmentAsync(CreateOrUpdateAppointmentDto input)
{
try
{
//var appointment = await _appointmentsRepository.GetAsync(input.Id);
Appointment appointment = new Appointment();
var newdata = ObjectMapper.Map<CreateOrUpdateAppointmentDto, Appointment>(input);
newdata.Doctor = await _doctorRepository.GetAsync(input.DoctorId.Value);
appointment = newdata;
appointment = await _appointmentsRepository.UpdateAsync(appointment);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region Delete Appointment
[Authorize(HospitalManagementSystemPermissions.Patient.Delete)]
public async Task DeleteAppointmentRecordAsync(Guid id)
{
await _appointmentsRepository.DeleteAsync(id);
}
#endregion
#region Export Appointment Data to Excel
public async Task<FileDownloadDto> GetExportAppointmentRecordAsync()
{
var Appointmentrecord = await _appointmentsRepository.GetQueryableAsync().Result.ToListAsync();
var folderPath = Path.Combine(_env.WebRootPath, "temp");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath); // Ensure the folder exists
}
var filename = "Appointments_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".xlsx";
var filePath = Path.Combine(folderPath, filename);
// Create a workbook and worksheet
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("Appointment");
// Add headers
worksheet.Cell(1, 1).Value = "Name";
worksheet.Cell(1, 2).Value = "Email";
worksheet.Cell(1, 3).Value = "Gender";
worksheet.Cell(1, 4).Value = "Date";
worksheet.Cell(1, 5).Value = "Time";
worksheet.Cell(1, 6).Value = "Mobile";
worksheet.Cell(1, 7).Value = "Doctor";
worksheet.Cell(1, 8).Value = "Injury";
worksheet.Cell(1, 9).Value = "Appointment Status";
worksheet.Cell(1, 10).Value = "Visit Type";
worksheet.Cell(1, 11).Value = "Payment Status";
worksheet.Cell(1, 12).Value = "Insurance Provider";
worksheet.Cell(1, 13).Value = "Notes";
for (int i = 0; i < Appointmentrecord.Count; i++)
{
worksheet.Cell(i + 2, 1).Value = Appointmentrecord[i].FirstName + "" + Appointmentrecord[i].LastName;
worksheet.Cell(i + 2, 2).Value = Appointmentrecord[i].Email;
worksheet.Cell(i + 2, 3).Value = Appointmentrecord[i].Gender.ToString();
worksheet.Cell(i + 2, 4).Value = Appointmentrecord[i].DateOfAppointment?.ToShortDateString();
worksheet.Cell(i + 2, 5).Value = Appointmentrecord[i].TimeOfAppointment;
worksheet.Cell(i + 2, 6).Value = Appointmentrecord[i].Mobile;
worksheet.Cell(i + 2, 7).Value = "";
worksheet.Cell(i + 2, 8).Value = Appointmentrecord[i].InjuryORContion;
worksheet.Cell(i + 2, 9).Value = Appointmentrecord[i].AppointmentStatus.ToString();
worksheet.Cell(i + 2, 10).Value = Appointmentrecord[i].VisitType.ToString();
worksheet.Cell(i + 2, 11).Value = Appointmentrecord[i].PaymentStatus.ToString();
worksheet.Cell(i + 2, 12).Value = Appointmentrecord[i].InsuranceProvider;
worksheet.Cell(i + 2, 13).Value = Appointmentrecord[i].Note;
}
worksheet.Columns().AdjustToContents();
workbook.SaveAs(filePath);
}
byte[] fileBytes = await File.ReadAllBytesAsync(filePath);
File.Delete(filePath);
return new FileDownloadDto
{
FileName = filename,
FileContent = Convert.ToBase64String(fileBytes) // Use Base64 encoding for file content
};
}
#endregion
}
}