EmployeeService.cs 5.2 KB

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