平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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