平安校园
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

75 lines
2.4 KiB

  1. namespace SafeCampus.Application.Services.Business.DepartmentService;
  2. public class DepartmentService: DbRepository<DepartmentInfo>, IDepartmentService
  3. {
  4. public async Task<bool> Add(DepartmentDto input)
  5. {
  6. var model = input.Adapt<DepartmentInfo>();
  7. await InsertAsync(model);
  8. return true;
  9. }
  10. public async Task<bool> Update(DepartmentDto input)
  11. {
  12. if (!input.Id.HasValue)
  13. {
  14. throw Oops.Oh("请填写ID");
  15. }
  16. var model = await GetFirstAsync(p => p.Id == input.Id);
  17. if (model == null)
  18. {
  19. throw Oops.Oh("信息不存在");
  20. }
  21. var res = input.Adapt(model);
  22. await UpdateAsync(res);
  23. return true;
  24. }
  25. public async Task<DepartmentDto> GetInfo(long id)
  26. {
  27. var query = await Context.Queryable<DepartmentInfo>()
  28. .FirstAsync(x => x.Id == id);
  29. return query.Adapt<DepartmentDto>();
  30. }
  31. public async Task<bool> Delete(List<long> id)
  32. {
  33. var majorInfo = ChangeRepository<DbRepository<MajorInfo>>();//切换仓储
  34. var model = await GetListAsync(x => id.Contains(x.Id));
  35. if (!model.Any())
  36. {
  37. throw Oops.Oh("信息不存在");
  38. }
  39. foreach (var departmentInfo in model)
  40. {
  41. var isOK = await majorInfo.IsAnyAsync(x => x.DepId == departmentInfo.Id);
  42. if (isOK)
  43. {
  44. throw Oops.Oh($"{departmentInfo.Name}院系下还有专业禁止删除!");
  45. }
  46. }
  47. await DeleteAsync(model);
  48. return true;
  49. }
  50. public async Task<SqlSugarPagedList<DepartmentDto>> GetPageList(DepartmentSearch search)
  51. {
  52. var query = Context.Queryable<DepartmentInfo>()
  53. .WhereIF(!string.IsNullOrEmpty(search.Name), x => x.Name.Contains(search.Name));
  54. var list = await query.OrderByDescending(x => x.Code)
  55. .ToPagedListAsyncMapster<DepartmentInfo, DepartmentDto>(search.PageNum, search.PageSize);
  56. return list;
  57. }
  58. public async Task<List<DepartmentDto>> GetNoPageList(DepartmentSearch search)
  59. {
  60. var query = Context.Queryable<DepartmentInfo>()
  61. .WhereIF(!string.IsNullOrEmpty(search.Name), x => x.Name.Contains(search.Name));
  62. var list = await query.OrderByDescending(x => x.Code)
  63. .ToListAsync();
  64. return list.Adapt<List<DepartmentDto>>();
  65. }
  66. }