using Microsoft.Extensions.Options;
using YD_Event.Application.Unit.Dtos;
using YD_Event.Core.Options;
namespace YD_Event.Application.Unit;
///
/// 单位管理(报名平台 / 打分系统账号)
///
[ApiDescriptionSettings("打分系统")]
public class UnitAccountService : BaseService, ITransient
{
private readonly ISqlSugarRepository _repository;
private readonly ISqlSugarRepository _userRoleRepository;
private readonly IIdGenerator _idGenerator;
private readonly IEasyCachingProvider _easyCachingProvider;
private readonly IOptionsMonitor _roleOptionsMonitor;
public UnitAccountService(ISqlSugarRepository repository,
ISqlSugarRepository userRoleRepository,
IIdGenerator idGenerator,
IEasyCachingProvider easyCachingProvider,
IOptionsMonitor roleOptionsMonitor) : base(repository)
{
_repository = repository;
_userRoleRepository = userRoleRepository;
_idGenerator = idGenerator;
_easyCachingProvider = easyCachingProvider;
_roleOptionsMonitor = roleOptionsMonitor;
}
///
/// 单位账号分页查询
///
[DisplayName("单位账号分页查询")]
[HttpGet("page")]
public async Task> Page([FromQuery] UnitAccountQueryInput input)
{
EnsureUnitPlatform(input.Platform);
return await _repository.AsQueryable()
.Where(x => x.UnitPlatform == input.Platform)
.WhereIF(!string.IsNullOrWhiteSpace(input.UnitName), x => x.UnitName.Contains(input.UnitName))
.WhereIF(!string.IsNullOrWhiteSpace(input.Account), x => x.Account.Contains(input.Account))
.WhereIF(input.StartTime.HasValue, x => x.CreatedTime >= input.StartTime.Value.Date)
// 结束日期按当天 23:59:59 处理,避免只传日期时漏掉当天的数据
.WhereIF(input.EndTime.HasValue, x => x.CreatedTime < input.EndTime.Value.Date.AddDays(1))
.OrderByDescending(x => x.CreatedTime)
.Select(x => new UnitAccountOutput
{
Id = x.Id,
UnitName = x.UnitName,
ContactName = x.Name,
Account = x.Account,
Platform = x.UnitPlatform,
Status = x.Status,
CreatedTime = x.CreatedTime
})
.ToPagedListAsync(input);
}
///
/// 单位账号详情
///
[DisplayName("单位账号详情")]
[HttpGet("detail")]
public async Task Detail([FromQuery] long id)
{
var user = await GetUnitAccountAsync(id);
return new UnitAccountOutput
{
Id = user.Id,
UnitName = user.UnitName,
ContactName = user.Name,
Account = user.Account,
Platform = user.UnitPlatform,
Status = user.Status,
CreatedTime = user.CreatedTime
};
}
///
/// 添加单位账号
///
[DisplayName("添加单位账号")]
[HttpPost("add")]
[UnitOfWork]
public async Task Add(AddUnitAccountInput input)
{
EnsureUnitPlatform(input.Platform);
// 账号是全局登录标识,需在整张 SysUser 表内查重
if (await _repository.IsAnyAsync(x => x.Account == input.Account))
{
throw Oops.Bah($"账号 {input.Account} 已存在");
}
var user = new SysUser
{
Id = _idGenerator.NextId(),
Account = input.Account,
Name = input.ContactName,
UnitName = input.UnitName,
UnitPlatform = input.Platform,
Gender = Gender.Unknown,
OrgId = 0,
Status = AvailabilityStatus.Enable,
DeleteMark = false,
CreatedTime = DateTime.Now
};
// 加密盐依赖主键,必须在 Id 生成之后计算
user.Password = MD5Encryption.Encrypt($"{_idGenerator.Encode(user.Id)}{input.Password}");
await _repository.InsertAsync(user);
await _userRoleRepository.InsertAsync(new SysUserRole
{
Id = _idGenerator.NextId(),
UserId = user.Id,
RoleId = GetRoleId(input.Platform),
CreatedTime = DateTime.Now,
DeleteMark = false
});
await ClearCache();
}
///
/// 更新单位账号
///
[DisplayName("更新单位账号")]
[HttpPut("edit")]
[UnitOfWork]
public async Task Update(UpdateUnitAccountInput input)
{
var user = await GetUnitAccountAsync(input.Id);
if (await _repository.IsAnyAsync(x => x.Account == input.Account && x.Id != input.Id))
{
throw Oops.Bah($"账号 {input.Account} 已被使用");
}
user.Account = input.Account;
user.Name = input.ContactName;
user.UnitName = input.UnitName;
await _repository.UpdateAsync(user);
await ClearCache();
}
///
/// 重置单位账号密码
///
[DisplayName("重置单位账号密码")]
[HttpPost("resetPassword")]
public async Task ResetPassword(ResetUnitAccountPasswordInput input)
{
await GetUnitAccountAsync(input.Id);
string password = MD5Encryption.Encrypt($"{_idGenerator.Encode(input.Id)}{input.Password}");
await _repository.UpdateAsync(x => new SysUser
{
Password = password
}, x => x.Id == input.Id);
}
///
/// 启用/禁用单位账号(禁用后禁止登录)
///
[DisplayName("启用禁用单位账号")]
[HttpPost("setStatus")]
public override async Task SetStatus(AvailabilityDto dto)
{
await GetUnitAccountAsync(dto.Id);
await base.SetStatus(dto);
}
///
/// 删除单位账号
///
[DisplayName("删除单位账号")]
[HttpDelete("delete")]
[UnitOfWork]
public override async Task Delete(KeyDto dto)
{
await GetUnitAccountAsync(dto.Id);
await base.Delete(dto);
await _userRoleRepository.SoftDeleteAsync(x => x.UserId == dto.Id);
}
///
/// 取单位账号,拦截对普通系统用户的越权操作
///
private async Task GetUnitAccountAsync(long id)
{
var user = await _repository.GetByIdAsync(id);
if (user == null) throw Oops.Bah("单位账号不存在");
if (user.UnitPlatform == UnitAccountPlatform.EventSystem) throw Oops.Bah("该账号不是单位账号,请在系统用户中管理");
return user;
}
private static void EnsureUnitPlatform(UnitAccountPlatform platform)
{
if (platform is not UnitAccountPlatform.Registration and not UnitAccountPlatform.Scoring)
throw Oops.Bah("所属平台只能选择报名平台或打分系统");
}
///
/// 平台对应的角色
///
private long GetRoleId(UnitAccountPlatform platform)
{
var options = _roleOptionsMonitor.CurrentValue;
return platform == UnitAccountPlatform.Scoring
? options.ScoringUnitRoleId
: options.UnitUserRoleId;
}
internal override Task ClearCache()
{
return _easyCachingProvider.RemoveByPrefixAsync(CacheConst.PermissionKey);
}
}