FlowDelegateService.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. namespace Admin.NET.Plugin.ApprovalFlow.Service;
  2. /// <summary>
  3. /// 审批代理/委托服务
  4. /// </summary>
  5. [ApiDescriptionSettings(ApprovalFlowConst.GroupName, Order = 55)]
  6. public class FlowDelegateService : IDynamicApiController, ITransient
  7. {
  8. private readonly SqlSugarRepository<ApprovalFlowDelegate> _rep;
  9. private readonly SqlSugarRepository<SysUser> _userRep;
  10. private readonly UserManager _userManager;
  11. public FlowDelegateService(
  12. SqlSugarRepository<ApprovalFlowDelegate> rep,
  13. SqlSugarRepository<SysUser> userRep,
  14. UserManager userManager)
  15. {
  16. _rep = rep;
  17. _userRep = userRep;
  18. _userManager = userManager;
  19. }
  20. /// <summary>
  21. /// 获取当前用户已设置的代理列表
  22. /// </summary>
  23. [HttpGet]
  24. [ApiDescriptionSettings(Name = "MyList")]
  25. [DisplayName("获取我的代理列表")]
  26. public async Task<List<FlowDelegateOutput>> MyList()
  27. {
  28. var uid = _userManager.UserId;
  29. var list = await _rep.AsQueryable()
  30. .Where(d => d.UserId == uid)
  31. .OrderByDescending(d => d.CreateTime)
  32. .ToListAsync();
  33. var now = DateTime.Now;
  34. return list.Select(d => new FlowDelegateOutput
  35. {
  36. Id = d.Id,
  37. DelegateUserId = d.DelegateUserId,
  38. DelegateUserName = d.DelegateUserName,
  39. StartTime = d.StartTime,
  40. EndTime = d.EndTime,
  41. BizType = d.BizType,
  42. Remark = d.Remark,
  43. IsEnabled = d.IsEnabled,
  44. IsEffective = d.IsEnabled && now >= d.StartTime && now <= d.EndTime,
  45. }).ToList();
  46. }
  47. /// <summary>
  48. /// 新增代理
  49. /// </summary>
  50. [HttpPost]
  51. [ApiDescriptionSettings(Name = "Add")]
  52. [DisplayName("新增审批代理")]
  53. public async Task<long> Add(FlowDelegateAddInput input)
  54. {
  55. if (input.DelegateUserId <= 0)
  56. throw Oops.Oh("代理人不能为空");
  57. if (input.DelegateUserId == _userManager.UserId)
  58. throw Oops.Oh("代理人不能是自己");
  59. if (input.EndTime <= input.StartTime)
  60. throw Oops.Oh("结束时间必须晚于开始时间");
  61. var delegateUser = await _userRep.GetByIdAsync(input.DelegateUserId)
  62. ?? throw Oops.Oh("代理人不存在");
  63. var entity = new ApprovalFlowDelegate
  64. {
  65. UserId = _userManager.UserId,
  66. UserName = _userManager.RealName,
  67. DelegateUserId = input.DelegateUserId,
  68. DelegateUserName = delegateUser.RealName,
  69. StartTime = input.StartTime,
  70. EndTime = input.EndTime,
  71. BizType = input.BizType,
  72. Remark = input.Remark,
  73. IsEnabled = true,
  74. };
  75. await _rep.InsertAsync(entity);
  76. return entity.Id;
  77. }
  78. /// <summary>
  79. /// 更新代理
  80. /// </summary>
  81. [HttpPost]
  82. [ApiDescriptionSettings(Name = "Update")]
  83. [DisplayName("更新审批代理")]
  84. public async Task Update(FlowDelegateUpdateInput input)
  85. {
  86. var entity = await _rep.GetByIdAsync(input.Id)
  87. ?? throw Oops.Oh("代理不存在");
  88. if (entity.UserId != _userManager.UserId)
  89. throw Oops.Oh("只能修改自己的代理");
  90. if (input.EndTime <= input.StartTime)
  91. throw Oops.Oh("结束时间必须晚于开始时间");
  92. entity.StartTime = input.StartTime;
  93. entity.EndTime = input.EndTime;
  94. entity.BizType = input.BizType;
  95. entity.Remark = input.Remark;
  96. entity.IsEnabled = input.IsEnabled;
  97. if (input.DelegateUserId > 0 && input.DelegateUserId != entity.DelegateUserId)
  98. {
  99. if (input.DelegateUserId == _userManager.UserId)
  100. throw Oops.Oh("代理人不能是自己");
  101. var delegateUser = await _userRep.GetByIdAsync(input.DelegateUserId)
  102. ?? throw Oops.Oh("代理人不存在");
  103. entity.DelegateUserId = input.DelegateUserId;
  104. entity.DelegateUserName = delegateUser.RealName;
  105. }
  106. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  107. }
  108. /// <summary>
  109. /// 删除代理
  110. /// </summary>
  111. [HttpPost]
  112. [ApiDescriptionSettings(Name = "Delete")]
  113. [DisplayName("删除审批代理")]
  114. public async Task Delete(BaseIdInput input)
  115. {
  116. var entity = await _rep.GetByIdAsync(input.Id)
  117. ?? throw Oops.Oh("代理不存在");
  118. if (entity.UserId != _userManager.UserId)
  119. throw Oops.Oh("只能删除自己的代理");
  120. await _rep.DeleteByIdAsync(input.Id);
  121. }
  122. }
  123. public class FlowDelegateOutput
  124. {
  125. public long Id { get; set; }
  126. public long DelegateUserId { get; set; }
  127. public string? DelegateUserName { get; set; }
  128. public DateTime StartTime { get; set; }
  129. public DateTime EndTime { get; set; }
  130. public string? BizType { get; set; }
  131. public string? Remark { get; set; }
  132. public bool IsEnabled { get; set; }
  133. /// <summary>当前是否处于生效状态(启用 + 在时间窗口内)</summary>
  134. public bool IsEffective { get; set; }
  135. }
  136. public class FlowDelegateAddInput
  137. {
  138. [Required(ErrorMessage = "代理人不能为空")]
  139. public long DelegateUserId { get; set; }
  140. [Required(ErrorMessage = "开始时间不能为空")]
  141. public DateTime StartTime { get; set; }
  142. [Required(ErrorMessage = "结束时间不能为空")]
  143. public DateTime EndTime { get; set; }
  144. [MaxLength(64)]
  145. public string? BizType { get; set; }
  146. [MaxLength(256)]
  147. public string? Remark { get; set; }
  148. }
  149. public class FlowDelegateUpdateInput : FlowDelegateAddInput
  150. {
  151. [Required]
  152. public long Id { get; set; }
  153. public bool IsEnabled { get; set; } = true;
  154. }