平安校园
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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