SysDepartmentRepository.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using QM.AuthServer.Models.Roles;
  2. using QM.AuthServer.IRepository;
  3. using QM.OrmSqlSugar;
  4. using System;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using WS;
  8. using WS.AutoMapper;
  9. namespace QM.AuthServer.Repository
  10. {
  11. public class SysDepartmentRepository : SqlSugarRepository<SysDepartment>, ISysDepartmentRepository
  12. {
  13. /// <summary>
  14. /// 检查
  15. /// </summary>
  16. /// <param name="bid"></param>
  17. /// <returns></returns>
  18. public SysDepartment CheckId(string bid)
  19. {
  20. var model = Get(t => t.bid == bid);
  21. if (model == null)
  22. throw new UseArgumentException("部门不存在,请检查数据是否正确");
  23. return model;
  24. }
  25. /// <summary>
  26. /// 创建
  27. /// </summary>
  28. /// <param name="input"></param>
  29. /// <returns></returns>
  30. public async Task<bool> Create(DepartmentInputDto input, string user)
  31. {
  32. var model = Get(t => t.name == input.name);
  33. if (model != null)
  34. throw new UseArgumentException("部门已存在,请重新输入");
  35. SysDepartment role = input.MapTo<SysDepartment>();
  36. role.bid = Guid.NewGuid().ToStringN();
  37. role.cuser = user;
  38. await InsertAsync(role);
  39. return true;
  40. }
  41. /// <summary>
  42. /// 修改
  43. /// </summary>
  44. /// <param name="input"></param>
  45. /// <returns></returns>
  46. public async Task<bool> Update(DepartmentUpdateDto input, string user)
  47. {
  48. var person = CheckId(input.bid);
  49. var model = input.Map(person);
  50. await UpdateAsync(model);
  51. return true;
  52. }
  53. /// <summary>
  54. /// 删除
  55. /// </summary>
  56. /// <param name="bid"></param>
  57. /// <returns></returns>
  58. public async Task<bool> Delete(string bid)
  59. {
  60. var person = CheckId(bid);
  61. try
  62. {
  63. BeginTran();
  64. var gids = Db.Queryable<SysPost>().Where(t => t.bid == bid).Select(t => t.gid).ToList();
  65. await Db.Deleteable<SysPostMenu>(t => gids.Contains(t.gid)).ExecuteCommandAsync();
  66. await Db.Deleteable<SysPost>(t => t.bid == bid).ExecuteCommandAsync();
  67. await Db.Deleteable<SysDepartment>(t => t.bid == bid).ExecuteCommandAsync();
  68. CommitTran();
  69. return true;
  70. }
  71. catch (Exception ex)
  72. {
  73. RollbackTran();
  74. }
  75. return false;
  76. }
  77. }
  78. }