ContractService.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 ContractService : BaseService<Contract, ContractDto, ContractDto>, IContractService
  13. {
  14. private readonly IUnitOfWork _unitOfWork;
  15. private readonly IUserService _userService;
  16. private readonly GlobalInfo _globalInfo;
  17. public ContractService(IUnitOfWork unitOfWork, IUserService userService, GlobalInfo globalInfo) : base(unitOfWork)
  18. {
  19. _unitOfWork = unitOfWork;
  20. _userService = userService;
  21. _globalInfo = globalInfo;
  22. }
  23. public override async Task<ContractDto> GetById(long id)
  24. {
  25. var entity = await _unitOfWork.Contract.GetByIdWithAllChildren(id);
  26. var response = MapperObject.Mapper.Map<ContractDto>(entity);
  27. return response;
  28. }
  29. public override async Task<ContractDto> Create(ContractDto input)
  30. {
  31. var entity = MapperObject.Mapper.Map<Contract>(input);
  32. if (entity is null)
  33. {
  34. throw new AppException(ExceptionEnum.MapperIssue);
  35. }
  36. var team = await _unitOfWork.Contract.AddAsync(entity);
  37. await _unitOfWork.CompleteAsync();
  38. var response = MapperObject.Mapper.Map<ContractDto>(team);
  39. return response;
  40. }
  41. //public override async Task<ContractDto> Update(ContractDto input)
  42. //{
  43. // var entity = MapperObject.Mapper.Map<Contract>(input);
  44. // if (entity is null)
  45. // {
  46. // throw new AppException(ExceptionEnum.MapperIssue);
  47. // }
  48. // var contract = await _unitOfWork.Contract.upd(entity);
  49. // await _unitOfWork.CompleteAsync();
  50. // var response = Mapper.MapperObject.Mapper.Map<ContractDto>(entity);
  51. // return response;
  52. //}
  53. public override async Task<PagingResultDto<ContractDto>> GetAll(PagingInputDto PagingInputDto)
  54. {
  55. var res = await _unitOfWork.Contract.GetAllWithChildrenAsync();
  56. var query = res.Item1;
  57. if (_globalInfo.UserType != UserTypeEnum.Business)
  58. {
  59. // query = query.Where(m => m.ContractUsers != null && m.ContractUsers.Count > 0 && m.ContractUsers.Count(u=> u.AssignedUserId == _globalInfo.UserId) > 0);
  60. }
  61. if (PagingInputDto.Filter != null)
  62. {
  63. var filter = PagingInputDto.Filter;
  64. query = query.Where(u => u.JobDescription.Contains(filter));
  65. }
  66. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  67. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  68. var total = await query.CountAsync();
  69. var list = MapperObject.Mapper
  70. .Map<IList<ContractDto>>(await page.ToListAsync());
  71. var response = new PagingResultDto<ContractDto>
  72. {
  73. Result = list,
  74. Total = total
  75. };
  76. return response;
  77. }
  78. }
  79. }