平安校园
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DepartmentService.cs 2.7 KiB

1 månad sedan
1 månad sedan
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. }