namespace Admin.NET.Plugin.ApprovalFlow.Service; /// /// 审批代理/委托服务 /// [ApiDescriptionSettings(ApprovalFlowConst.GroupName, Order = 55)] public class FlowDelegateService : IDynamicApiController, ITransient { private readonly SqlSugarRepository _rep; private readonly SqlSugarRepository _userRep; private readonly UserManager _userManager; public FlowDelegateService( SqlSugarRepository rep, SqlSugarRepository userRep, UserManager userManager) { _rep = rep; _userRep = userRep; _userManager = userManager; } /// /// 获取当前用户已设置的代理列表 /// [HttpGet] [ApiDescriptionSettings(Name = "MyList")] [DisplayName("获取我的代理列表")] public async Task> MyList() { var uid = _userManager.UserId; var list = await _rep.AsQueryable() .Where(d => d.UserId == uid) .OrderByDescending(d => d.CreateTime) .ToListAsync(); var now = DateTime.Now; return list.Select(d => new FlowDelegateOutput { Id = d.Id, DelegateUserId = d.DelegateUserId, DelegateUserName = d.DelegateUserName, StartTime = d.StartTime, EndTime = d.EndTime, BizType = d.BizType, Remark = d.Remark, IsEnabled = d.IsEnabled, IsEffective = d.IsEnabled && now >= d.StartTime && now <= d.EndTime, }).ToList(); } /// /// 新增代理 /// [HttpPost] [ApiDescriptionSettings(Name = "Add")] [DisplayName("新增审批代理")] public async Task Add(FlowDelegateAddInput input) { if (input.DelegateUserId <= 0) throw Oops.Oh("代理人不能为空"); if (input.DelegateUserId == _userManager.UserId) throw Oops.Oh("代理人不能是自己"); if (input.EndTime <= input.StartTime) throw Oops.Oh("结束时间必须晚于开始时间"); var delegateUser = await _userRep.GetByIdAsync(input.DelegateUserId) ?? throw Oops.Oh("代理人不存在"); var entity = new ApprovalFlowDelegate { UserId = _userManager.UserId, UserName = _userManager.RealName, DelegateUserId = input.DelegateUserId, DelegateUserName = delegateUser.RealName, StartTime = input.StartTime, EndTime = input.EndTime, BizType = input.BizType, Remark = input.Remark, IsEnabled = true, }; await _rep.InsertAsync(entity); return entity.Id; } /// /// 更新代理 /// [HttpPost] [ApiDescriptionSettings(Name = "Update")] [DisplayName("更新审批代理")] public async Task Update(FlowDelegateUpdateInput input) { var entity = await _rep.GetByIdAsync(input.Id) ?? throw Oops.Oh("代理不存在"); if (entity.UserId != _userManager.UserId) throw Oops.Oh("只能修改自己的代理"); if (input.EndTime <= input.StartTime) throw Oops.Oh("结束时间必须晚于开始时间"); entity.StartTime = input.StartTime; entity.EndTime = input.EndTime; entity.BizType = input.BizType; entity.Remark = input.Remark; entity.IsEnabled = input.IsEnabled; if (input.DelegateUserId > 0 && input.DelegateUserId != entity.DelegateUserId) { if (input.DelegateUserId == _userManager.UserId) throw Oops.Oh("代理人不能是自己"); var delegateUser = await _userRep.GetByIdAsync(input.DelegateUserId) ?? throw Oops.Oh("代理人不存在"); entity.DelegateUserId = input.DelegateUserId; entity.DelegateUserName = delegateUser.RealName; } await _rep.AsUpdateable(entity).ExecuteCommandAsync(); } /// /// 删除代理 /// [HttpPost] [ApiDescriptionSettings(Name = "Delete")] [DisplayName("删除审批代理")] public async Task Delete(BaseIdInput input) { var entity = await _rep.GetByIdAsync(input.Id) ?? throw Oops.Oh("代理不存在"); if (entity.UserId != _userManager.UserId) throw Oops.Oh("只能删除自己的代理"); await _rep.DeleteByIdAsync(input.Id); } } public class FlowDelegateOutput { public long Id { get; set; } public long DelegateUserId { get; set; } public string? DelegateUserName { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public string? BizType { get; set; } public string? Remark { get; set; } public bool IsEnabled { get; set; } /// 当前是否处于生效状态(启用 + 在时间窗口内) public bool IsEffective { get; set; } } public class FlowDelegateAddInput { [Required(ErrorMessage = "代理人不能为空")] public long DelegateUserId { get; set; } [Required(ErrorMessage = "开始时间不能为空")] public DateTime StartTime { get; set; } [Required(ErrorMessage = "结束时间不能为空")] public DateTime EndTime { get; set; } [MaxLength(64)] public string? BizType { get; set; } [MaxLength(256)] public string? Remark { get; set; } } public class FlowDelegateUpdateInput : FlowDelegateAddInput { [Required] public long Id { get; set; } public bool IsEnabled { get; set; } = true; }