ZhiYi/ZhiYi.Core.Application/Services/Implements/SharedAppService.cs

153 lines
6.1 KiB
C#
Raw Normal View History

2025-02-21 01:14:39 +00:00

namespace ZhiYi.Core.Application.Services.Implements
{
public class SharedAppService : ISharedAppService
{
private readonly IMapper _mapper;
private readonly SqlSugarClient _client;
private readonly IConfiguration _configuration;
private readonly ILogger<SharedAppService> _logger;
private readonly IDatabase _redis;
private readonly IGroupAppService _groupAppService;
2025-02-24 09:50:17 +00:00
public SharedAppService(
IMapper mapper,
SqlSugarClient client,
IConfiguration configuration,
ILogger<SharedAppService> logger,
IConnectionMultiplexer redis,
IGroupAppService groupAppService
)
2025-02-21 01:14:39 +00:00
{
_mapper = mapper;
_configuration = configuration;
string connStr = _configuration.GetSection("ConnectionStrings:SqlConnection").Value;
if (_client == null)
{
_client = SugarClientInit.Instance.GetDdClient(connStr);
}
_logger = logger;
_redis = redis.GetDatabase();
_groupAppService = groupAppService;
}
public async Task<AppResponse<string>> CreateAsync(SharedCreationDto input)
{
input.TrimStringFields();
var shared = _mapper.Map<ZhiYi_Shared>(input);
switch (shared.Type)
{
case 1:
//次数
if (int.TryParse(input.Value, out int time))
{
if (time <= 0)
throw new Exception("创建分享失败次数至少为1且为整数");
shared.Time = time;
}
else
throw new Exception("创建分享失败,次数输入格式不正确");
break;
case 2:
//过期时间
if (DateTime.TryParse(input.Value, out DateTime exp))
shared.Exp = exp;
else
throw new Exception("创建分享失败,过期时间格式不正确");
break;
case 3:
//指定人员
if (long.TryParse(input.Value, out long opeartor))
shared.Operator = opeartor;
else
throw new Exception("创建分享失败指定人员账号ID不正确");
break;
default:
throw new Exception("创建分享失败类型为1-3的整数");
}
try
{
shared.Code = Guid.NewGuid().ToString();
await _client.Insertable(shared).ExecuteCommandAsync();
return new AppResponse<string> { Data = shared.Code };
}
catch(Exception ex)
{
_logger.LogError("创建分享失败:{message}", ex.Message);
throw new Exception("创建分享失败");
}
}
public async Task DeleteBatchAsync(SharedDeleteDto input)
{
input.TrimStringFields();
if (!input.Codes.Any())
throw new Exception("删除失败,未指定任何分享码");
var codes = input.Codes.ToArray();
await _client.Deleteable<ZhiYi_Shared>()
.Where(x => codes.Contains(x.Code))
.ExecuteCommandAsync();
}
public async Task<AppResponse<List<SharedDto>>> GetListAsync(long id)
{
var sharedList = await _client.Queryable<ZhiYi_Shared>()
.LeftJoin<ZhiYi_User>((a, b) => a.Operator == b.Id)
.LeftJoin<ZhiYi_User>((a, b, c) => a.Shared_User == c.Id)
.Where(a => a.Shared_User == id)
.Select((a, b, c) => new SharedDto
{
Code = a.Code,
Type = a.Type,
Exp = a.Exp,
Time = a.Time,
Operator = a.Operator,
OperatorName = b.NickName,
GroupIds = a.GroupIds,
Shared_User = a.Shared_User,
Shared_UserName = c.NickName
})
.ToListAsync();
return new AppResponse<List<SharedDto>> { Data = sharedList };
}
public async Task UseShared(UseSharedCodeDto input)
{
if (input.Code.IsNullOrEmpty())
throw new Exception("导入失败,分享码不正确");
var shared = await _client.Queryable<ZhiYi_Shared>()
.Where(x => x.Code == input.Code)
.FirstAsync();
if (shared == null)
throw new Exception("导入失败,分享码不存在或已删除");
switch (shared.Type)
{
case 1:
//验证次数
if (shared.Time <= 0)
throw new Exception("分享码超出使用次数限制");
//剩余次数-1
shared.Time -= 1;
await _client.Updateable(shared).ExecuteCommandAsync();
break;
case 2:
//验证期限
if (DateTime.Now >= shared.Exp)
throw new Exception("分享码已过期");
break;
case 3:
//验证人员
if (input.UserId != shared.Operator)
throw new Exception("非当前分享码可使用人员");
break;
default:
throw new Exception("导入失败,分享码类型不正确");
}
var groupArr = shared.GroupIds.Split(",");
var groupIds = groupArr.Select(x => long.Parse(x)).ToArray();
//批量导入
await _groupAppService.CreateBatchAsync(new GroupCreationBatchDto { Ids = groupIds , UserId = input.UserId});
}
}
}