MeetingService.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Microsoft.EntityFrameworkCore;
  2. using MTWorkHR.Application.Identity;
  3. using MTWorkHR.Application.Mapper;
  4. using MTWorkHR.Application.Models;
  5. using MTWorkHR.Core.Global;
  6. using MTWorkHR.Core.UnitOfWork;
  7. using MTWorkHR.Application.Services.Interfaces;
  8. using MTWorkHR.Core.Entities;
  9. using System.Linq.Dynamic.Core;
  10. using System.Globalization;
  11. namespace MTWorkHR.Application.Services
  12. {
  13. public class MeetingService : BaseService<Meeting, MeetingDto, MeetingDto>, IMeetingService
  14. {
  15. private readonly IUnitOfWork _unitOfWork;
  16. private readonly IUserService _userService;
  17. private readonly GlobalInfo _globalInfo;
  18. public MeetingService(IUnitOfWork unitOfWork, IUserService userService, GlobalInfo globalInfo) : base(unitOfWork)
  19. {
  20. _unitOfWork = unitOfWork;
  21. _userService = userService;
  22. _globalInfo = globalInfo;
  23. }
  24. public override async Task<MeetingDto> GetById(long id)
  25. {
  26. var entity = await _unitOfWork.Meeting.GetByIdWithAllChildren(id);
  27. var response = MapperObject.Mapper.Map<MeetingDto>(entity);
  28. if (response != null)
  29. response.MeetingUserIds = response.MeetingUsers != null ? response.MeetingUsers?.Select(u => u.AssignedUserId).ToList() : new List<string>();
  30. return response;
  31. }
  32. public override async Task<MeetingDto> Create(MeetingDto input)
  33. {
  34. var entity = MapperObject.Mapper.Map<Meeting>(input);
  35. if (entity is null)
  36. {
  37. throw new AppException(ExceptionEnum.MapperIssue);
  38. }
  39. entity.MeetingUsers = input.MeetingUserIds?.Select(s => new MeetingUser { AssignedUserId = s, AssignedUserName = _userService.GetUserFullName(s).Result }).ToList();
  40. //-----------Time conversion to utc
  41. var startTime = DateTime.Parse( entity.StartTime);
  42. entity.StartTime = ConvertToUtc(entity.StartTime, entity.MeetingDate);
  43. entity.EndTime = ConvertToUtc(entity.EndTime, entity.MeetingDate);
  44. var team = await _unitOfWork.Meeting.AddAsync(entity);
  45. await _unitOfWork.CompleteAsync();
  46. var response = MapperObject.Mapper.Map<MeetingDto>(team);
  47. response.MeetingUserIds = input.MeetingUserIds;
  48. return response;
  49. }
  50. public override async Task<MeetingDto> Update(MeetingDto input)
  51. {
  52. var entity = await _unitOfWork.Meeting.GetByIdWithAllChildren(input.Id);
  53. if (entity is null)
  54. throw new AppException(ExceptionEnum.RecordNotExist);
  55. entity.Description = input.Description;
  56. entity.Location = input.Location;
  57. entity.Title = input.Title;
  58. entity.MeetingLink = input.MeetingLink;
  59. entity.MeetingDate = input.MeetingDate;
  60. entity.StartTime = ConvertToUtc(input.StartTime, input.MeetingDate);
  61. entity.EndTime = ConvertToUtc(input.EndTime, input.MeetingDate);
  62. entity.RepeatId = input.RepeatId;
  63. var oldUsers = entity.MeetingUsers?.Select(s => s.AssignedUserId);
  64. var NewItems = input.MeetingUserIds?.Where(u => !oldUsers.Contains(u));
  65. var newMeetingUsers = NewItems?.Select(s => new MeetingUser { MeetingId = input.Id, AssignedUserId = s, AssignedUserName = _userService.GetUserFullName(s).Result }).ToList();
  66. var DeletedItems = oldUsers?.Where(u => !input.MeetingUserIds.Contains(u));
  67. _unitOfWork.BeginTran();
  68. foreach (var delUser in DeletedItems)
  69. {
  70. var removeItem = entity.MeetingUsers?.FirstOrDefault(u => u.AssignedUserId == delUser);
  71. if (removeItem != null) await _unitOfWork.MeetingUser.DeleteAsync(removeItem);
  72. }
  73. foreach (var newUsr in newMeetingUsers)
  74. {
  75. await _unitOfWork.MeetingUser.AddAsync(newUsr);
  76. }
  77. await _unitOfWork.CompleteAsync();
  78. _unitOfWork.CommitTran();
  79. var response = Mapper.MapperObject.Mapper.Map<MeetingDto>(entity);
  80. return response;
  81. }
  82. public override async Task<PagingResultDto<MeetingDto>> GetAll(PagingInputDto PagingInputDto)
  83. {
  84. var res = await _unitOfWork.Meeting.GetAllWithChildrenAsync();
  85. var query = res.Item1;
  86. if (_globalInfo.UserType != UserTypeEnum.Business)
  87. {
  88. query = query.Where(m => m.MeetingUsers != null && m.MeetingUsers.Count > 0 && m.MeetingUsers.Count(u=> u.AssignedUserId == _globalInfo.UserId) > 0);
  89. }
  90. if (PagingInputDto.Filter != null)
  91. {
  92. var filter = PagingInputDto.Filter;
  93. query = query.Where(u => u.Title.Contains(filter) || u.Description!.Contains(filter));
  94. }
  95. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  96. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  97. var total = await query.CountAsync();
  98. var list = MapperObject.Mapper
  99. .Map<IList<MeetingDto>>(await page.ToListAsync());
  100. var response = new PagingResultDto<MeetingDto>
  101. {
  102. Result = list,
  103. Total = total
  104. };
  105. return response;
  106. }
  107. /// <summary>
  108. /// Converts a local time and date to UTC based on the specified time zone.
  109. /// </summary>
  110. /// <param name="startTime">Time string (e.g., "7:00 PM").</param>
  111. /// <param name="date">Date as DateTime (e.g., 2025-04-22).</param>
  112. /// <param name="timeZoneId">Time zone ID (e.g., "Eastern Standard Time").</param>
  113. /// <returns>UTC DateTime with Kind = DateTimeKind.Utc.</returns>
  114. /// <exception cref="ArgumentException">Thrown if timeZoneId or input formats are invalid.</exception>
  115. public static string ConvertToUtc(string startTime, DateTime date)
  116. {
  117. string timeZoneId = GlobalInfo.timeZone;
  118. if (string.IsNullOrWhiteSpace(startTime))
  119. {
  120. throw new ArgumentException("Start time cannot be null or empty.");
  121. }
  122. if (string.IsNullOrWhiteSpace(timeZoneId))
  123. {
  124. throw new ArgumentException("Time zone ID cannot be null or empty.");
  125. }
  126. try
  127. {
  128. // Parse the time string
  129. DateTime parsedTime = DateTime.Parse(startTime, new CultureInfo("en-US"), DateTimeStyles.NoCurrentDateDefault);
  130. // Combine date from DateTime and time from parsedTime
  131. DateTime localDateTime = new DateTime(
  132. date.Year, date.Month, date.Day,
  133. parsedTime.Hour, parsedTime.Minute, parsedTime.Second, parsedTime.Millisecond,
  134. DateTimeKind.Unspecified);
  135. // Get source time zone
  136. TimeZoneInfo sourceTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
  137. // Convert to UTC
  138. DateTime utcDateTime = TimeZoneInfo.ConvertTimeToUtc(localDateTime, sourceTimeZone);
  139. return utcDateTime.ToShortTimeString();
  140. }
  141. catch (TimeZoneNotFoundException)
  142. {
  143. throw new ArgumentException($"Invalid time zone ID: {timeZoneId}");
  144. }
  145. catch (FormatException)
  146. {
  147. throw new ArgumentException("Invalid time format. Use 'h:mm tt' (e.g., '7:00 PM').");
  148. }
  149. }
  150. //public override async Task Delete(long id)
  151. //{
  152. // var entity = await _unitOfWork.Meeting.GetByIdAsync(id);
  153. // await _unitOfWork.Meeting.DeleteAsync(entity);
  154. //}
  155. }
  156. }