MeetingService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. namespace MTWorkHR.Application.Services
  11. {
  12. public class MeetingService : BaseService<Meeting, MeetingDto, MeetingDto>, IMeetingService
  13. {
  14. private readonly IUnitOfWork _unitOfWork;
  15. private readonly IUserService _userService;
  16. public MeetingService(IUnitOfWork unitOfWork, IUserService userService) : base(unitOfWork)
  17. {
  18. _unitOfWork = unitOfWork;
  19. _userService = userService;
  20. }
  21. public override async Task<MeetingDto> GetById(long id)
  22. {
  23. var entity = await _unitOfWork.Meeting.GetByIdWithAllChildren(id);
  24. var response = MapperObject.Mapper.Map<MeetingDto>(entity);
  25. if (response != null)
  26. response.MeetingUserIds = response.MeetingUsers != null ? response.MeetingUsers?.Select(u => u.AssignedUserId).ToList() : new List<string>();
  27. return response;
  28. }
  29. public override async Task<MeetingDto> Create(MeetingDto input)
  30. {
  31. var entity = MapperObject.Mapper.Map<Meeting>(input);
  32. if (entity is null)
  33. {
  34. throw new AppException(ExceptionEnum.MapperIssue);
  35. }
  36. entity.MeetingUsers = input.MeetingUserIds?.Select(s => new MeetingUser { AssignedUserId = s, AssignedUserName = _userService.GetUserFullName(s).Result }).ToList();
  37. var team = await _unitOfWork.Meeting.AddAsync(entity);
  38. await _unitOfWork.CompleteAsync();
  39. var response = MapperObject.Mapper.Map<MeetingDto>(team);
  40. response.MeetingUserIds = input.MeetingUserIds;
  41. return response;
  42. }
  43. public override async Task<MeetingDto> Update(MeetingDto input)
  44. {
  45. var entity = await _unitOfWork.Meeting.GetByIdWithAllChildren(input.Id);
  46. if (entity is null)
  47. throw new AppException(ExceptionEnum.RecordNotExist);
  48. entity.Description = input.Description;
  49. entity.Location = input.Location;
  50. entity.Title = input.Title;
  51. entity.MeetingLink = input.MeetingLink;
  52. entity.MeetingDate = input.MeetingDate;
  53. entity.StartTime = input.StartTime;
  54. entity.EndTime = input.EndTime;
  55. entity.RepeatId = input.RepeatId;
  56. var oldUsers = entity.MeetingUsers?.Select(s => s.AssignedUserId);
  57. var NewItems = input.MeetingUserIds?.Where(u => !oldUsers.Contains(u));
  58. var newMeetingUsers = NewItems?.Select(s => new MeetingUser { MeetingId = input.Id, AssignedUserId = s, AssignedUserName = _userService.GetUserFullName(s).Result }).ToList();
  59. var DeletedItems = oldUsers?.Where(u => !input.MeetingUserIds.Contains(u));
  60. _unitOfWork.BeginTran();
  61. foreach (var delUser in DeletedItems)
  62. {
  63. var removeItem = entity.MeetingUsers?.FirstOrDefault(u => u.AssignedUserId == delUser);
  64. if (removeItem != null) await _unitOfWork.MeetingUser.DeleteAsync(removeItem);
  65. }
  66. foreach (var newUsr in newMeetingUsers)
  67. {
  68. await _unitOfWork.MeetingUser.AddAsync(newUsr);
  69. }
  70. await _unitOfWork.CompleteAsync();
  71. _unitOfWork.CommitTran();
  72. var response = Mapper.MapperObject.Mapper.Map<MeetingDto>(entity);
  73. return response;
  74. }
  75. public override async Task<PagingResultDto<MeetingDto>> GetAll(PagingInputDto PagingInputDto)
  76. {
  77. var res = await _unitOfWork.Meeting.GetAllWithChildrenAsync();
  78. var query = res.Item1;
  79. if (PagingInputDto.Filter != null)
  80. {
  81. var filter = PagingInputDto.Filter;
  82. query = query.Where(u => u.Title.Contains(filter) || u.Description.Contains(filter));
  83. }
  84. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  85. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  86. var total = await query.CountAsync();
  87. var list = MapperObject.Mapper
  88. .Map<IList<MeetingDto>>(await page.ToListAsync());
  89. var response = new PagingResultDto<MeetingDto>
  90. {
  91. Result = list,
  92. Total = total
  93. };
  94. return response;
  95. }
  96. //public override async Task Delete(long id)
  97. //{
  98. // var entity = await _unitOfWork.Meeting.GetByIdAsync(id);
  99. // await _unitOfWork.Meeting.DeleteAsync(entity);
  100. //}
  101. }
  102. }