ContractService.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 Microsoft.AspNetCore.Identity;
  11. using MTWorkHR.Infrastructure.Entities;
  12. using System.Linq;
  13. namespace MTWorkHR.Application.Services
  14. {
  15. public class ContractService : BaseService<Contract, ContractDto, ContractDto>, IContractService
  16. {
  17. private readonly IUnitOfWork _unitOfWork;
  18. private readonly IUserService _userService;
  19. private readonly GlobalInfo _globalInfo;
  20. private readonly OrderAllocationService _orderAllocationService;
  21. public ContractService(IUnitOfWork unitOfWork, IUserService userService, GlobalInfo globalInfo) : base(unitOfWork)
  22. {
  23. _unitOfWork = unitOfWork;
  24. _userService = userService;
  25. _globalInfo = globalInfo;
  26. }
  27. public override async Task<ContractDto> GetById(long id)
  28. {
  29. var entity = await _unitOfWork.Contract.GetByIdWithAllChildren(id);
  30. var response = MapperObject.Mapper.Map<ContractDto>(entity);
  31. return response;
  32. }
  33. public override async Task<ContractDto> Create(ContractDto input)
  34. {
  35. var entity = MapperObject.Mapper.Map<Contract>(input);
  36. if (entity is null)
  37. {
  38. throw new AppException(ExceptionEnum.MapperIssue);
  39. }
  40. var team = await _unitOfWork.Contract.AddAsync(entity);
  41. await _unitOfWork.CompleteAsync();
  42. var response = MapperObject.Mapper.Map<ContractDto>(team);
  43. return response;
  44. }
  45. //public override async Task<ContractDto> Update(ContractDto input)
  46. //{
  47. // var entity = MapperObject.Mapper.Map<Contract>(input);
  48. // if (entity is null)
  49. // {
  50. // throw new AppException(ExceptionEnum.MapperIssue);
  51. // }
  52. // var contract = await _unitOfWork.Contract.upd(entity);
  53. // await _unitOfWork.CompleteAsync();
  54. // var response = Mapper.MapperObject.Mapper.Map<ContractDto>(entity);
  55. // return response;
  56. //}
  57. public async Task<PagingResultDto<ContractDto>> GetAll(ContractPagingInputDto PagingInputDto)
  58. {
  59. var res = await _unitOfWork.Contract.GetAllWithChildrenAsync();
  60. var query = res.Item1;
  61. //var query = queryx
  62. // .Join(
  63. // teamUsersList.Item1,
  64. // c => c.UserId,
  65. // teamUser => teamUser.AssignedUserId,
  66. // (contract, teamUser) => new
  67. // {
  68. // Contract = contract,
  69. // Teams = teamUser.Team.NameEn
  70. // }
  71. // )
  72. // .AsQueryable();
  73. if (_globalInfo.UserType != UserTypeEnum.Business)
  74. {
  75. // query = query.Where(m => m.ContractUsers != null && m.ContractUsers.Count > 0 && m.ContractUsers.Count(u=> u.AssignedUserId == _globalInfo.UserId) > 0);
  76. }
  77. if (PagingInputDto.Filter != null)
  78. {
  79. var filter = PagingInputDto.Filter;
  80. query = query.Where(u => u.JobDescription.Contains(filter));
  81. }
  82. if (PagingInputDto.ContractStatusId != null)
  83. {
  84. query = query.Where(u => (int)u.ContractStatusId == PagingInputDto.ContractStatusId);
  85. }
  86. if (PagingInputDto.ContractTypeId != null)
  87. {
  88. query = query.Where(u => (int)u.ContractTypeId == PagingInputDto.ContractTypeId);
  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<ContractDto>>(await page.ToListAsync());
  95. foreach (var item in list)
  96. {
  97. if (item.UserId != null)
  98. {
  99. var user = await _userService.GetUserWithAttachmentById(item.UserId);
  100. if (user != null)
  101. {
  102. item.EmployeeName = user.FirstName + " " + user.LastName;
  103. item.EmployeeEmail = user.Email;
  104. }
  105. }
  106. }
  107. var teamUsersList = await _unitOfWork.TeamUser.GetAllWithChildrenAsync();
  108. foreach (var item in list)
  109. {
  110. if (item.UserId != null)
  111. {
  112. item.Teams = teamUsersList.Item1.Where(t => t.AssignedUserId == item.UserId).Select(t=> t.Team.NameEn).Aggregate((a,b)=> a+","+b);
  113. }
  114. }
  115. var response = new PagingResultDto<ContractDto>
  116. {
  117. Result = list,
  118. Total = total
  119. };
  120. return response;
  121. }
  122. public async Task<bool> ChangeStatus(long contractId, int statusId)
  123. {
  124. var entity = await _unitOfWork.Contract.GetByIdAsync(contractId);
  125. if (entity == null)
  126. throw new AppException(ExceptionEnum.RecordNotExist);
  127. entity.ContractStatusId = statusId;
  128. bool result = true;
  129. if (statusId == (int)ContractStatusEnum.Approved)
  130. {
  131. var updatedSuccess = await _userService.Update(entity.UserId, entity.CompanyId);
  132. addVacationAllocation(entity.VacationDays.HasValue ? entity.VacationDays.Value : 0, entity.UserId);
  133. if (!updatedSuccess) { result = false; }
  134. }
  135. await _unitOfWork.CompleteAsync();
  136. return result;
  137. }
  138. private void addVacationAllocation(int noVacationDays, string employeeId)
  139. {
  140. _orderAllocationService.Create(new OrderAllocationDto { EmployeeId = employeeId,OrderTypeId = 1, LeaveTypeId = 1, NumberOfDays = noVacationDays, Period = DateTime.Now.Year});
  141. }
  142. }
  143. }