平安校园
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.
 
 
 
 
 
 

80 lines
2.7 KiB

  1. namespace SafeCampus.Application.Services.Business.MajorService;
  2. public class MajorService: DbRepository<MajorInfo>, IMajorService
  3. {
  4. public async Task<bool> Add(MajorDto input)
  5. {
  6. var model = input.Adapt<MajorInfo>();
  7. await InsertAsync(model);
  8. return true;
  9. }
  10. public async Task<bool> Update(MajorDto 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<MajorDto> GetInfo(long id)
  26. {
  27. var query = await Context.Queryable<MajorInfo>()
  28. .Includes(x => x.DepartmentInfoItem)
  29. .FirstAsync(x => x.Id == id);
  30. return query.Adapt<MajorDto>();
  31. }
  32. public async Task<bool> Delete(List<long> id)
  33. {
  34. var personSetInfo = ChangeRepository<DbRepository<PersonSetInfo>>();//切换仓储
  35. var model = await GetListAsync(x => id.Contains(x.Id));
  36. if (!model.Any())
  37. {
  38. throw Oops.Oh("信息不存在");
  39. }
  40. foreach (var majorInfo in model)
  41. {
  42. var isOK = await personSetInfo.IsAnyAsync(x => x.MajorId == majorInfo.Id);
  43. if (isOK)
  44. {
  45. throw Oops.Oh($"{majorInfo.Name}专业下还有班级禁止删除!");
  46. }
  47. }
  48. await DeleteAsync(model);
  49. return true;
  50. }
  51. public async Task<SqlSugarPagedList<MajorDto>> GetPageList(MajorSearch search)
  52. {
  53. var query = Context.Queryable<MajorInfo>()
  54. .Includes(x=>x.DepartmentInfoItem)
  55. .WhereIF(!string.IsNullOrEmpty(search.Name), x => x.Name.Contains(search.Name))
  56. .WhereIF(!string.IsNullOrEmpty(search.Code),x=>x.Code.Contains(search.Code))
  57. .WhereIF(search.DepId.HasValue,x=>x.DepId==search.DepId);
  58. var list = await query.OrderByDescending(x => x.Sort)
  59. .ToPagedListAsyncMapster<MajorInfo, MajorDto>(search.PageNum, search.PageSize);
  60. return list;
  61. }
  62. public async Task<List<MajorDto>> GetNoPageList(MajorSearch search)
  63. {
  64. var query = Context.Queryable<MajorInfo>()
  65. .Includes(x => x.DepartmentInfoItem)
  66. .WhereIF(!string.IsNullOrEmpty(search.Name), x => x.Name.Contains(search.Name))
  67. .WhereIF(!string.IsNullOrEmpty(search.Code), x => x.Code.Contains(search.Code))
  68. .WhereIF(search.DepId.HasValue, x => x.DepId == search.DepId);
  69. var list = await query.OrderByDescending(x => x.Sort).ToListAsync();
  70. return list.Adapt<List<MajorDto>>();
  71. }
  72. }