ContractService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. namespace MTWorkHR.Application.Services
  13. {
  14. public class ContractService : BaseService<Contract, ContractDto, ContractDto>, IContractService
  15. {
  16. private readonly IUnitOfWork _unitOfWork;
  17. private readonly IUserService _userService;
  18. private readonly GlobalInfo _globalInfo;
  19. public ContractService(IUnitOfWork unitOfWork, IUserService userService, GlobalInfo globalInfo) : base(unitOfWork)
  20. {
  21. _unitOfWork = unitOfWork;
  22. _userService = userService;
  23. _globalInfo = globalInfo;
  24. }
  25. public override async Task<ContractDto> GetById(long id)
  26. {
  27. var entity = await _unitOfWork.Contract.GetByIdWithAllChildren(id);
  28. var response = MapperObject.Mapper.Map<ContractDto>(entity);
  29. return response;
  30. }
  31. public override async Task<ContractDto> Create(ContractDto input)
  32. {
  33. var entity = MapperObject.Mapper.Map<Contract>(input);
  34. if (entity is null)
  35. {
  36. throw new AppException(ExceptionEnum.MapperIssue);
  37. }
  38. var team = await _unitOfWork.Contract.AddAsync(entity);
  39. await _unitOfWork.CompleteAsync();
  40. var response = MapperObject.Mapper.Map<ContractDto>(team);
  41. return response;
  42. }
  43. //public override async Task<ContractDto> Update(ContractDto input)
  44. //{
  45. // var entity = MapperObject.Mapper.Map<Contract>(input);
  46. // if (entity is null)
  47. // {
  48. // throw new AppException(ExceptionEnum.MapperIssue);
  49. // }
  50. // var contract = await _unitOfWork.Contract.upd(entity);
  51. // await _unitOfWork.CompleteAsync();
  52. // var response = Mapper.MapperObject.Mapper.Map<ContractDto>(entity);
  53. // return response;
  54. //}
  55. public async Task<PagingResultDto<ContractDto>> GetAll(ContractPagingInputDto PagingInputDto)
  56. {
  57. var res = await _unitOfWork.Contract.GetAllWithChildrenAsync();
  58. var query = res.Item1;
  59. if (_globalInfo.UserType != UserTypeEnum.Business)
  60. {
  61. // query = query.Where(m => m.ContractUsers != null && m.ContractUsers.Count > 0 && m.ContractUsers.Count(u=> u.AssignedUserId == _globalInfo.UserId) > 0);
  62. }
  63. if (PagingInputDto.Filter != null)
  64. {
  65. var filter = PagingInputDto.Filter;
  66. query = query.Where(u => u.JobDescription.Contains(filter));
  67. }
  68. if (PagingInputDto.ContractStatusId != null)
  69. {
  70. query = query.Where(u => (int)u.ContractStatusId == PagingInputDto.ContractStatusId);
  71. }
  72. if (PagingInputDto.ContractTypeId != null)
  73. {
  74. query = query.Where(u => (int)u.ContractTypeId == PagingInputDto.ContractTypeId);
  75. }
  76. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  77. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  78. var total = await query.CountAsync();
  79. var list = MapperObject.Mapper
  80. .Map<IList<ContractDto>>(await page.ToListAsync());
  81. var response = new PagingResultDto<ContractDto>
  82. {
  83. Result = list,
  84. Total = total
  85. };
  86. return response;
  87. }
  88. public async Task<bool> ChangeStatus(long contractId, int statusId)
  89. {
  90. var entity = await _unitOfWork.Contract.GetByIdAsync(contractId);
  91. if (entity == null)
  92. throw new AppException(ExceptionEnum.RecordNotExist);
  93. entity.ContractStatusId = statusId;
  94. bool result = true;
  95. if (statusId == (int)ContractStatusEnum.Approved)
  96. {
  97. var updatedSuccess = await _userService.Update(entity.UserId, entity.CompanyId);
  98. if (!updatedSuccess) { result = false; }
  99. }
  100. await _unitOfWork.CompleteAsync();
  101. return result;
  102. }
  103. }
  104. }