OrganizationService.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Admin.Core.Common.Input;
  2. using Admin.Core.Common.Output;
  3. using Admin.Core.Model.Personnel;
  4. using Admin.Core.Repository.Personnel;
  5. using Admin.Core.Service.Personnel.Organization.Input;
  6. using Admin.Core.Service.Personnel.Organization.Output;
  7. using System.Threading.Tasks;
  8. namespace Admin.Core.Service.Personnel.Organization
  9. {
  10. public class OrganizationService : BaseService, IOrganizationService
  11. {
  12. private readonly IOrganizationRepository _organizationRepository;
  13. public OrganizationService(IOrganizationRepository organizationRepository)
  14. {
  15. _organizationRepository = organizationRepository;
  16. }
  17. public async Task<IResponseOutput> GetAsync(long id)
  18. {
  19. var result = await _organizationRepository.GetAsync<OrganizationGetOutput>(id);
  20. return ResponseOutput.Ok(result);
  21. }
  22. public async Task<IResponseOutput> GetListAsync(string key)
  23. {
  24. var data = await _organizationRepository
  25. .WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Code.Contains(key))
  26. .OrderBy(a => a.ParentId)
  27. .OrderBy(a => a.Sort)
  28. .ToListAsync<OrganizationListOutput>();
  29. return ResponseOutput.Ok(data);
  30. }
  31. public async Task<IResponseOutput> AddAsync(OrganizationAddInput input)
  32. {
  33. var dictionary = Mapper.Map<OrganizationEntity>(input);
  34. var id = (await _organizationRepository.InsertAsync(dictionary)).Id;
  35. return ResponseOutput.Result(id > 0);
  36. }
  37. public async Task<IResponseOutput> UpdateAsync(OrganizationUpdateInput input)
  38. {
  39. if (!(input?.Id > 0))
  40. {
  41. return ResponseOutput.NotOk();
  42. }
  43. var entity = await _organizationRepository.GetAsync(input.Id);
  44. if (!(entity?.Id > 0))
  45. {
  46. return ResponseOutput.NotOk("数据字典不存在!");
  47. }
  48. Mapper.Map(input, entity);
  49. await _organizationRepository.UpdateAsync(entity);
  50. return ResponseOutput.Ok();
  51. }
  52. public async Task<IResponseOutput> DeleteAsync(long id)
  53. {
  54. var result = await _organizationRepository.DeleteRecursiveAsync(a => a.Id == id);
  55. return ResponseOutput.Result(result);
  56. }
  57. public async Task<IResponseOutput> SoftDeleteAsync(long id)
  58. {
  59. var result = await _organizationRepository.SoftDeleteRecursiveAsync(a => a.Id == id);
  60. return ResponseOutput.Result(result);
  61. }
  62. }
  63. }