| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- namespace Admin.NET.Plugin.ApprovalFlow.Service;
- /// <summary>
- /// 审批代理/委托服务
- /// </summary>
- [ApiDescriptionSettings(ApprovalFlowConst.GroupName, Order = 55)]
- public class FlowDelegateService : IDynamicApiController, ITransient
- {
- private readonly SqlSugarRepository<ApprovalFlowDelegate> _rep;
- private readonly SqlSugarRepository<SysUser> _userRep;
- private readonly UserManager _userManager;
- public FlowDelegateService(
- SqlSugarRepository<ApprovalFlowDelegate> rep,
- SqlSugarRepository<SysUser> userRep,
- UserManager userManager)
- {
- _rep = rep;
- _userRep = userRep;
- _userManager = userManager;
- }
- /// <summary>
- /// 获取当前用户已设置的代理列表
- /// </summary>
- [HttpGet]
- [ApiDescriptionSettings(Name = "MyList")]
- [DisplayName("获取我的代理列表")]
- public async Task<List<FlowDelegateOutput>> 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();
- }
- /// <summary>
- /// 新增代理
- /// </summary>
- [HttpPost]
- [ApiDescriptionSettings(Name = "Add")]
- [DisplayName("新增审批代理")]
- public async Task<long> 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;
- }
- /// <summary>
- /// 更新代理
- /// </summary>
- [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();
- }
- /// <summary>
- /// 删除代理
- /// </summary>
- [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; }
- /// <summary>当前是否处于生效状态(启用 + 在时间窗口内)</summary>
- 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;
- }
|