OrderAllocationService.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.AspNetCore.WebUtilities;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.Configuration;
  5. using MTWorkHR.Application.Identity;
  6. using MTWorkHR.Application.Mapper;
  7. using MTWorkHR.Application.Models;
  8. using MTWorkHR.Core.Global;
  9. using MTWorkHR.Core.IRepositories;
  10. using MTWorkHR.Core.UnitOfWork;
  11. using MTWorkHR.Infrastructure.Entities;
  12. using MTWorkHR.Application.Services.Interfaces;
  13. using MTWorkHR.Core.Email;
  14. using MTWorkHR.Core.Entities;
  15. using MTWorkHR.Infrastructure.UnitOfWorks;
  16. namespace MTWorkHR.Application.Services
  17. {
  18. public class OrderAllocationService : BaseService<OrderAllocation, OrderAllocationDto, OrderAllocationDto>, IOrderAllocationService
  19. {
  20. private readonly IUnitOfWork _unitOfWork;
  21. private readonly IUserService _user;
  22. public OrderAllocationService(IUnitOfWork unitOfWork, IUserService user) :base(unitOfWork)
  23. {
  24. _unitOfWork = unitOfWork;
  25. _user = user;
  26. }
  27. public override async Task<OrderAllocationDto> GetById(long id)
  28. {
  29. var entity = await _unitOfWork.OrderAllocation.GetByIdWithAllChildren(id);
  30. var response = MapperObject.Mapper.Map<OrderAllocationDto>(entity);
  31. return response;
  32. }
  33. public override async Task<OrderAllocationDto> Create(OrderAllocationDto input)
  34. {
  35. var orderType = await _unitOfWork.OrderType.GetByIdAsync(input.OrderTypeId);
  36. var leaveType = input.LeaveTypeId.HasValue && input.LeaveTypeId > 0 ? await _unitOfWork.LeaveType.GetByIdAsync(input.LeaveTypeId.Value): null;
  37. var employees = await _user.GetAllEmployees();
  38. var period = DateTime.Now.Year;
  39. var allocations = new List<OrderAllocation>();
  40. foreach (var emp in employees)
  41. {
  42. if (await _unitOfWork.OrderAllocation.AllocationExists(emp.Id, orderType.Id, input.LeaveTypeId, period))
  43. continue;
  44. allocations.Add(new OrderAllocation
  45. {
  46. EmployeeId = emp.Id,
  47. OrderTypeId = orderType.Id,
  48. LeaveTypeId = leaveType != null ? leaveType.Id : null,
  49. NumberOfDays = leaveType != null ? leaveType.DefaultDays : orderType.DefaultDays,
  50. Period = period
  51. });
  52. }
  53. await _unitOfWork.OrderAllocation.AddRangeAsync(allocations);
  54. await _unitOfWork.CompleteAsync();
  55. return input;
  56. }
  57. }
  58. }