1
0

EmployeeService.cs 5.1 KB

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