收藏夹 服务

This commit is contained in:
jianzhichu
2026-01-19 14:11:23 +08:00
parent f5449f3ace
commit 65e6b48d8e
13 changed files with 401 additions and 15 deletions
+157
View File
@@ -0,0 +1,157 @@
using ClockSnowFlake;
using dy.net.model.dto;
using dy.net.model.entity;
using SqlSugar;
using System.Linq.Expressions;
namespace dy.net.repository
{
public class DouyinCollectCateRepository : BaseRepository<DouyinCollectCate>
{
// 注入SQLSugar客户端
public DouyinCollectCateRepository(ISqlSugarClient db) : base(db)
{
}
/// <summary>
/// 分页查询
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<(List<DouyinCollectCate> list, int totalCount)> GetPagedAsync(DouyinCollectCateRequestDto dto)
{
var where = this.Db.Queryable<DouyinCollectCate>()
.WhereIF(!string.IsNullOrWhiteSpace(dto.cookieId), x => x.CookieId == dto.cookieId);
var totalCount = await where.CountAsync();
var list = await where.Skip((dto.PageIndex - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
return (list, totalCount);
}
/// <summary>
/// 批量修改状态
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<bool> SwitchBatchAsync(List<DouyinCollectCateSwitchDto> dto)
{
// 空值/空列表防护 + 提取ID列表
var ids = dto?.Select(x => x.Id).ToList() ?? new List<string>();
if (!ids.Any()) return false;
// 1. 查询需要更新的分类数据
var cates = await GetListAsync(x => ids.Contains(x.Id));
// 2. 用字典优化查找(避免循环内FirstOrDefault,提升性能)
var dtoDict = dto.ToDictionary(x => x.Id);
// 3. 批量更新状态(简化循环写法)
cates.ForEach(item =>
{
if (dtoDict.TryGetValue(item.Id, out var dtocate))
{
item.Sync = dtocate.Sync;
}
});
// 4. 执行更新并返回结果
return await Db.Updateable(cates).ExecuteCommandAsync() > 0;
}
internal async Task<(int add, int update, int delete, bool succ)> Sync(List<DouyinCollectCate> cates, string ckId,int cateType)
{
// 初始化返回结果
int addCount = 0;
int updateCount = 0;
int deleteCount = 0;
bool isSuccess = false;
try
{
// 1. 参数校验
if (string.IsNullOrWhiteSpace(ckId))
{
throw new ArgumentNullException(nameof(ckId), "CookieId信息不能为空");
}
if (cates == null)
{
cates = new List<DouyinCollectCate>(); // 空列表则执行删除所有操作
}
// 2. 开启SqlSugar事务
await Db.Ado.BeginTranAsync();
// 3. 查询数据库中当前用户的所有分类
var dbCates = await Db.Queryable<DouyinCollectCate>()
.Where(c => c.CookieId == ckId && c.CateType == cateType)
.ToListAsync();
// 4. 处理新增和更新逻辑
var cateIds = cates.Select(c => c.Id).ToList();
foreach (var cate in cates)
{
cate.UpdateTime = DateTime.Now; // 更新时间戳
// 检查是否已存在
if (string.IsNullOrWhiteSpace(cate.Id))
{
cate.CreateTime = DateTime.Now;
cate.Id = IdGener.GetLong().ToString();
// 新增
await Db.Insertable(cate).ExecuteCommandAsync();
addCount++;
}
else
{
var existCate = dbCates.FirstOrDefault(x => x.Id == cate.Id);
if (existCate == null)
{
Serilog.Log.Error($" cate {cate.Id} id 不存在");
continue;
}
// 对比字段是否有变化(根据实际业务字段调整)
bool isChanged = !string.Equals(existCate.Name, cate.Name, StringComparison.Ordinal);
if (isChanged)
{
// 更新(只更新有变化的字段,或全量更新)
await Db.Updateable(cate)
.IgnoreColumns(ignoreAllNullColumns: true) // 忽略空值字段
.Where(c => c.Id == cate.Id)
.ExecuteCommandAsync();
updateCount++;
}
}
}
// 5. 处理删除逻辑:数据库中有但传入列表中没有的分类
var deleteCates = dbCates.Where(c => !cateIds.Contains(c.Id)).ToList();
if (deleteCates.Any())
{
var deleteIds = deleteCates.Select(c => c.Id).ToList();
deleteCount = await Db.Deleteable<DouyinCollectCate>()
.Where(c => deleteIds.Contains(c.Id) && c.CookieId == ckId)
.ExecuteCommandAsync();
}
// 6. 提交事务
await Db.Ado.CommitTranAsync();
isSuccess = true;
}
catch (Exception ex)
{
// 异常时回滚事务
await Db.Ado.RollbackTranAsync();
isSuccess = false;
// throw;
Serilog.Log.Error($"同步收藏夹:类型-{cateType}异常,{ex.StackTrace}" );
}
// 7. 返回最终结果
return (addCount, updateCount, deleteCount, isSuccess);
}
}
}
+1 -1
View File
@@ -44,7 +44,7 @@ namespace dy.net.repository
}
public async Task<bool> SwitchAsync(DouyinCookieStopDto dto)
public async Task<bool> SwitchAsync(DouyinCookieSwitchDto dto)
{
var res =await Db.Updateable<DouyinCookie>().SetColumns(x => new DouyinCookie { Status = dto.Status }).Where(x => x.Id == dto.Id).ExecuteCommandAsync();
+5
View File
@@ -107,6 +107,8 @@ namespace dy.net.repository
try
{
// 2. 开启SqlSugar事务
await Db.Ado.BeginTranAsync();
// 1. 提取当前批次的SecUid集合(去重)
HashSet<string> currentSecUids = followInfos.Select(x => x.SecUid).ToHashSet();
if (!currentSecUids.Any())
@@ -203,12 +205,15 @@ namespace dy.net.repository
}
}
// 6. 提交事务
await Db.Ado.CommitTranAsync();
// 【重要】删除逻辑已移除:增量场景下不能通过批次对比删除,需单独设计取消关注逻辑
Serilog.Log.Debug($"dy_followed_users{ck.UserName})关注列表同步完成:新增{toAddFollows.Count}条,更新{toUpdateFollows.Count}条");
return (toAddFollows.Count, toUpdateFollows.Count, true);
}
catch (Exception ex)
{
await Db.Ado.RollbackTranAsync();
Serilog.Log.Error(ex, $"同步关注列表失败({ck.UserName}):{ex.Message}");
return (0, 0, false);