EmployeeService.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Linq;
  2. using System.Threading.Tasks;
  3. using ZhonTai.Common.Attributes;
  4. using ZhonTai.Common.Domain.Dto;
  5. using ZhonTai.Plate.Personnel.Domain;
  6. using ZhonTai.Plate.Personnel.Service.Employee.Input;
  7. using ZhonTai.Plate.Personnel.Service.Employee.Output;
  8. using ZhonTai.Plate.Admin.Service;
  9. using ZhonTai.Common.Domain.Repositories;
  10. using ZhonTai.Plate.Personnel.Domain.Employee;
  11. using ZhonTai.Plate.Personnel.Domain.Organization;
  12. namespace ZhonTai.Plate.Personnel.Service.Employee
  13. {
  14. /// <summary>
  15. /// 员工服务
  16. /// </summary>
  17. public class EmployeeService : BaseService, IEmployeeService
  18. {
  19. private readonly IEmployeeRepository _employeeRepository;
  20. private readonly IRepositoryBase<EmployeeOrganizationEntity> _employeeOrganizationRepository;
  21. public EmployeeService(
  22. IEmployeeRepository userRepository,
  23. IRepositoryBase<EmployeeOrganizationEntity> employeeOrganizationRepository
  24. )
  25. {
  26. _employeeRepository = userRepository;
  27. _employeeOrganizationRepository = employeeOrganizationRepository;
  28. }
  29. public async Task<ResultOutput<EmployeeGetOutput>> GetAsync(long id)
  30. {
  31. var res = new ResultOutput<EmployeeGetOutput>();
  32. var dto = await _employeeRepository.Select
  33. .WhereDynamic(id)
  34. .IncludeMany(a => a.Organizations.Select(b => new OrganizationEntity { Id = b.Id }))
  35. .ToOneAsync(a => new EmployeeGetOutput
  36. {
  37. OrganizationName = a.Organization.Name,
  38. PositionName = a.Position.Name
  39. });
  40. return res.Ok(dto);
  41. }
  42. public async Task<IResultOutput> GetPageAsync(PageInput<EmployeeEntity> input)
  43. {
  44. var list = await _employeeRepository.Select
  45. .WhereDynamicFilter(input.DynamicFilter)
  46. .Count(out var total)
  47. .OrderByDescending(true, a => a.Id)
  48. .IncludeMany(a => a.Organizations.Select(b => new OrganizationEntity { Name = b.Name }))
  49. .Page(input.CurrentPage, input.PageSize)
  50. .ToListAsync(a => new EmployeeListOutput
  51. {
  52. OrganizationName = a.Organization.Name,
  53. PositionName = a.Position.Name
  54. });
  55. var data = new PageOutput<EmployeeListOutput>()
  56. {
  57. List = list,
  58. Total = total
  59. };
  60. return ResultOutput.Ok(data);
  61. }
  62. [Transaction]
  63. public async Task<IResultOutput> AddAsync(EmployeeAddInput input)
  64. {
  65. var entity = Mapper.Map<EmployeeEntity>(input);
  66. var employeeId = (await _employeeRepository.InsertAsync(entity))?.Id;
  67. if (!(employeeId > 0))
  68. {
  69. return ResultOutput.NotOk();
  70. }
  71. //附属部门
  72. if (input.OrganizationIds != null && input.OrganizationIds.Any())
  73. {
  74. var organizations = input.OrganizationIds.Select(organizationId => new EmployeeOrganizationEntity
  75. {
  76. EmployeeId = employeeId.Value,
  77. OrganizationId = organizationId
  78. });
  79. await _employeeOrganizationRepository.InsertAsync(organizations);
  80. }
  81. return ResultOutput.Ok();
  82. }
  83. [Transaction]
  84. public async Task<IResultOutput> UpdateAsync(EmployeeUpdateInput input)
  85. {
  86. if (!(input?.Id > 0))
  87. {
  88. return ResultOutput.NotOk();
  89. }
  90. var employee = await _employeeRepository.GetAsync(input.Id);
  91. if (!(employee?.Id > 0))
  92. {
  93. return ResultOutput.NotOk("用户不存在!");
  94. }
  95. Mapper.Map(input, employee);
  96. await _employeeRepository.UpdateAsync(employee);
  97. await _employeeOrganizationRepository.DeleteAsync(a => a.EmployeeId == employee.Id);
  98. //附属部门
  99. if (input.OrganizationIds != null && input.OrganizationIds.Any())
  100. {
  101. var organizations = input.OrganizationIds.Select(organizationId => new EmployeeOrganizationEntity
  102. {
  103. EmployeeId = employee.Id,
  104. OrganizationId = organizationId
  105. });
  106. await _employeeOrganizationRepository.InsertAsync(organizations);
  107. }
  108. return ResultOutput.Ok();
  109. }
  110. [Transaction]
  111. public async Task<IResultOutput> DeleteAsync(long id)
  112. {
  113. //删除员工附属部门
  114. await _employeeOrganizationRepository.DeleteAsync(a => a.EmployeeId == id);
  115. //删除员工
  116. await _employeeRepository.DeleteAsync(m => m.Id == id);
  117. return ResultOutput.Ok();
  118. }
  119. public async Task<IResultOutput> SoftDeleteAsync(long id)
  120. {
  121. await _employeeRepository.SoftDeleteAsync(id);
  122. return ResultOutput.Ok();
  123. }
  124. public async Task<IResultOutput> BatchSoftDeleteAsync(long[] ids)
  125. {
  126. await _employeeRepository.SoftDeleteAsync(ids);
  127. return ResultOutput.Ok();
  128. }
  129. }
  130. }