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

MajorService.cs 3.1 KiB

1 month ago
1 month ago
1 month ago
1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 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(MajorDto 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<MajorDto> GetInfo(long id)
  36. {
  37. var query = await Context.Queryable<MajorInfo>()
  38. .Includes(x => x.DepartmentInfoItem)
  39. .FirstAsync(x => x.Id == id);
  40. return query.Adapt<MajorDto>();
  41. }
  42. public async Task<bool> Delete(List<long> id)
  43. {
  44. var personSetInfo = ChangeRepository<DbRepository<PersonSetInfo>>();//切换仓储
  45. var model = await GetListAsync(x => id.Contains(x.Id));
  46. if (!model.Any())
  47. {
  48. throw Oops.Oh("信息不存在");
  49. }
  50. foreach (var majorInfo in model)
  51. {
  52. var isOK = await personSetInfo.IsAnyAsync(x => x.MajorId == majorInfo.Id);
  53. if (isOK)
  54. {
  55. throw Oops.Oh($"{majorInfo.Name}专业下还有班级禁止删除!");
  56. }
  57. }
  58. await DeleteAsync(model);
  59. return true;
  60. }
  61. public async Task<SqlSugarPagedList<MajorDto>> GetPageList(MajorSearch search)
  62. {
  63. var query = Context.Queryable<MajorInfo>()
  64. .Includes(x=>x.DepartmentInfoItem)
  65. .WhereIF(!string.IsNullOrEmpty(search.Name), x => x.Name.Contains(search.Name))
  66. .WhereIF(!string.IsNullOrEmpty(search.Code),x=>x.Code.Contains(search.Code))
  67. .WhereIF(search.DepId.HasValue,x=>x.DepId==search.DepId);
  68. var list = await query.OrderBy(x => x.Sort)
  69. .ToPagedListAsyncMapster<MajorInfo, MajorDto>(search.PageNum, search.PageSize);
  70. return list;
  71. }
  72. public async Task<List<MajorDto>> GetNoPageList(MajorSearch search)
  73. {
  74. var query = Context.Queryable<MajorInfo>()
  75. .Includes(x => x.DepartmentInfoItem)
  76. .WhereIF(!string.IsNullOrEmpty(search.Name), x => x.Name.Contains(search.Name))
  77. .WhereIF(!string.IsNullOrEmpty(search.Code), x => x.Code.Contains(search.Code))
  78. .WhereIF(search.DepId.HasValue, x => x.DepId == search.DepId);
  79. var list = await query.OrderBy(x => x.Sort).ToListAsync();
  80. return list.Adapt<List<MajorDto>>();
  81. }
  82. }