MeetingService.cs 5.2 KB

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