JobAppService.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using BaseService.BaseData.JobManagement.Dto;
  2. using BaseService.Permissions;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.EntityFrameworkCore;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Linq.Dynamic.Core;
  9. using System.Threading.Tasks;
  10. using Volo.Abp;
  11. using Volo.Abp.Application.Dtos;
  12. using Volo.Abp.Application.Services;
  13. using Volo.Abp.Domain.Repositories;
  14. namespace BaseService.BaseData.JobManagement
  15. {
  16. [Authorize(BaseServicePermissions.Job.Default)]
  17. public class JobAppService : ApplicationService, IJobAppService
  18. {
  19. private readonly IRepository<Job, Guid> _repository;
  20. public JobAppService(IRepository<Job, Guid> repository)
  21. {
  22. _repository = repository;
  23. }
  24. [Authorize(BaseServicePermissions.Job.Create)]
  25. public async Task<JobDto> Create(CreateOrUpdateJobDto input)
  26. {
  27. var exist = await _repository.FirstOrDefaultAsync(_ => _.Name == input.Name);
  28. if (exist != null)
  29. {
  30. throw new BusinessException("名称:" + input.Name + "岗位已存在");
  31. }
  32. var result = await _repository.InsertAsync(new Job(GuidGenerator.Create(),CurrentTenant.Id, input.Name, input.Enabled, input.Sort, input.Description));
  33. return ObjectMapper.Map<Job, JobDto>(result);
  34. }
  35. [Authorize(BaseServicePermissions.Job.Delete)]
  36. public async Task Delete(List<Guid> ids)
  37. {
  38. foreach (var id in ids)
  39. {
  40. await _repository.DeleteAsync(id);
  41. }
  42. }
  43. public async Task<JobDto> Get(Guid id)
  44. {
  45. var result = await _repository.GetAsync(id);
  46. return ObjectMapper.Map<Job, JobDto>(result);
  47. }
  48. public async Task<PagedResultDto<JobDto>> GetAll(GetJobInputDto input)
  49. {
  50. var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), _ => _.Name.Contains(input.Filter));
  51. var totalCount = await query.CountAsync();
  52. var items = await query.OrderBy(input.Sorting ?? "Sort")
  53. .Skip(input.SkipCount)
  54. .Take(input.MaxResultCount)
  55. .ToListAsync();
  56. var dots = ObjectMapper.Map<List<Job>, List<JobDto>>(items);
  57. return new PagedResultDto<JobDto>(totalCount, dots);
  58. }
  59. public async Task<ListResultDto<JobDto>> GetAllJobs()
  60. {
  61. var jobs = await _repository.GetListAsync();
  62. return new ListResultDto<JobDto>(ObjectMapper.Map<List<Job>, List<JobDto>>(jobs));
  63. }
  64. [Authorize(BaseServicePermissions.Job.Update)]
  65. public async Task<JobDto> Update(Guid id, CreateOrUpdateJobDto input)
  66. {
  67. var job = await _repository.GetAsync(id);
  68. job.Name = input.Name;
  69. job.Enabled = input.Enabled;
  70. job.Sort = input.Sort;
  71. job.Description = input.Description;
  72. return ObjectMapper.Map<Job, JobDto>(job);
  73. }
  74. }
  75. }