OrderAllocationService.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. try
  36. {
  37. var orderType = await _unitOfWork.OrderType.GetByIdAsync(input.OrderTypeId);
  38. var leaveType = input.LeaveTypeId.HasValue && input.LeaveTypeId > 0 ? await _unitOfWork.LeaveType.GetByIdAsync(input.LeaveTypeId.Value) : null;
  39. var employees = await _user.GetAllEmployees();
  40. var period = DateTime.Now.Year;
  41. var allocations = new List<OrderAllocation>();
  42. foreach (var emp in employees)
  43. {
  44. if (await _unitOfWork.OrderAllocation.AllocationExists(emp.Id, orderType.Id, input.LeaveTypeId, period))
  45. continue;
  46. allocations.Add(new OrderAllocation
  47. {
  48. EmployeeId = emp.Id,
  49. OrderTypeId = orderType.Id,
  50. LeaveTypeId = leaveType != null ? leaveType.Id : null,
  51. NumberOfDays = leaveType != null ? leaveType.DefaultDays : orderType.DefaultDays,
  52. Period = period
  53. });
  54. }
  55. await _unitOfWork.OrderAllocation.AddRangeAsync(allocations);
  56. await _unitOfWork.CompleteAsync();
  57. }catch(Exception ex)
  58. {
  59. var msg = ex.Message;
  60. }
  61. return input;
  62. }
  63. }
  64. }