using ClockSnowFlake; using dy.net.dto; using dy.net.model; using dy.net.repository; using SqlSugar; using System.Linq.Expressions; namespace dy.net.service { public class DouyinFollowService { private readonly DouyinFollowRepository _followRepository; public DouyinFollowService(DouyinFollowRepository followRepository) { _followRepository = followRepository; } public async Task> GetGroupByCookieAsync() { return await _followRepository.GetDouyinFollowGroup(); } /// /// /// /// /// public async Task AddAsync(DouyinFollowed followed) { var foll= await _followRepository.GetFirstAsync(x=>x.SecUid==followed.SecUid && x.mySelfId== followed.mySelfId); if(foll!=null) { return false; } followed.Id = IdGener.GetLong().ToString(); followed.LastSyncTime = DateTime.UtcNow; followed.IsNoFollowed = true; return await _followRepository.InsertAsync(followed); } /// /// /// /// /// public async Task<(List list, int totalCount)> GetPagedAsync(FollowRequestDto dto) { return await _followRepository.GetPagedAsync(dto); } /// /// 获取所有手动添加的非关注者 /// /// public async Task> GetHandFollows() { return await _followRepository.GetListAsync(x=>x.IsNoFollowed); } /// /// 导入非关注 /// /// /// public async Task AddHandFollows(List douyinFolloweds) { if (douyinFolloweds == null || !douyinFolloweds.Any()) return false; var ids = douyinFolloweds.Select(x => x.Id).Distinct().ToList(); var existingFolloweds = await _followRepository.GetListAsync(x => ids.Contains(x.Id)); var toAddList = douyinFolloweds .Where(x => !existingFolloweds.Any(e => e.Id == x.Id)) .ToList(); // 批量更新:先整理需要更新的实体 var toUpdateEntities = new List(); foreach (var updateItem in douyinFolloweds) { var existingItem = existingFolloweds.FirstOrDefault(e => e.Id == updateItem.Id); if (existingItem != null) { toUpdateEntities.Add(updateItem); } } int operateCount = 0; // 批量新增 if (toAddList.Any()) { operateCount += await _followRepository.InsertRangeAsync(toAddList); } // 批量更新 if (toUpdateEntities.Any()) { await _followRepository.UpdateRangeAsync(toUpdateEntities); operateCount += toUpdateEntities.Count; } return operateCount > 0; } /// /// /// /// /// /// public async Task Sync(List followInfos, DouyinCookie ck) { return await _followRepository.Sync(followInfos, ck); } public async Task GetByUperId(string uperId,string myUid) { return await _followRepository.GetBySecUId(uperId, myUid); } /// /// 打开或关闭同步 /// /// /// public async Task OpenOrCloseSync(FollowUpdateDto dto) { var followed = await _followRepository.GetByIdAsync(dto.Id); if (followed != null) { followed.OpenSync = dto.OpenSync; followed.FullSync = dto.FullSync; followed.SavePath = dto.SavePath; return await _followRepository.Update(followed); } return false; } public async Task DeleteFollow(FollowUpdateDto dto) { return await _followRepository.DeleteByIdAsync(dto.Id); } /// /// 打开或关闭同步 /// /// /// public async Task OpenOrCloseFullSync(FollowUpdateDto dto) { return await OpenOrCloseSync(dto); } /// /// 获取需要同步的关注列表 /// /// public async Task> GetSyncFollows(string userUserId) { return await _followRepository.GetSyncFollows(userUserId); } } }