|
|
@@ -440,12 +440,12 @@ export class WorkService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 保存每日统计数据
|
|
|
- try {
|
|
|
- await this.saveWorkDayStatistics(account);
|
|
|
- } catch (error) {
|
|
|
- logger.error(`[SyncAccountWorks] Failed to save day statistics for account ${account.id}:`, error);
|
|
|
- }
|
|
|
+ // 同步作品后:不再基于 works 累计值写 work_day_statistics,当天快照全部交给各平台专门的“每日数据”任务
|
|
|
+ // try {
|
|
|
+ // await this.saveWorkDayStatistics(account);
|
|
|
+ // } catch (error) {
|
|
|
+ // logger.error(`[SyncAccountWorks] Failed to save day statistics for account ${account.id}:`, error);
|
|
|
+ // }
|
|
|
|
|
|
// 小红书:如果是新作品且 work_day_statistics 中尚无任何记录,则补首批日统计 & works.yesterday_*(不受14天限制)
|
|
|
if (platform === 'xiaohongshu') {
|
|
|
@@ -488,6 +488,46 @@ export class WorkService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 抖音:如果是新作品且 work_day_statistics 中尚无任何记录,则异步补齐历史日统计 & works.yesterday_*(使用 DouyinWorkStatisticsImportService)
|
|
|
+ if (platform === 'douyin') {
|
|
|
+ try {
|
|
|
+ const works = await this.workRepository.find({
|
|
|
+ where: { accountId: account.id, platform },
|
|
|
+ select: ['id'],
|
|
|
+ });
|
|
|
+ const workIds = works.map((w) => w.id);
|
|
|
+ if (workIds.length > 0) {
|
|
|
+ const rows = await AppDataSource.getRepository(WorkDayStatistics)
|
|
|
+ .createQueryBuilder('wds')
|
|
|
+ .select('DISTINCT wds.work_id', 'workId')
|
|
|
+ .where('wds.work_id IN (:...ids)', { ids: workIds })
|
|
|
+ .getRawMany();
|
|
|
+ const hasStats = new Set<number>(rows.map((r: any) => Number(r.workId)));
|
|
|
+ const needInitIds = workIds.filter((id) => !hasStats.has(id));
|
|
|
+
|
|
|
+ if (needInitIds.length > 0) {
|
|
|
+ logger.info(
|
|
|
+ `[SyncAccountWorks] DY account ${account.id} has ${needInitIds.length} works without statistics, enqueue dy_work_stats_backfill task.`
|
|
|
+ );
|
|
|
+ taskQueueService.createTask(account.userId, {
|
|
|
+ type: 'dy_work_stats_backfill',
|
|
|
+ title: `抖音作品补数(${needInitIds.length})`,
|
|
|
+ accountId: account.id,
|
|
|
+ platform: 'douyin',
|
|
|
+ data: {
|
|
|
+ workIds: needInitIds,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ logger.error(
|
|
|
+ `[SyncAccountWorks] Failed to enqueue DY work_day_statistics backfill for account ${account.id}:`,
|
|
|
+ err
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return {
|
|
|
syncedCount,
|
|
|
worksListLength: accountInfo.worksList?.length || 0,
|
|
|
@@ -501,47 +541,11 @@ export class WorkService {
|
|
|
* 保存作品每日统计数据
|
|
|
*/
|
|
|
private async saveWorkDayStatistics(account: PlatformAccount): Promise<void> {
|
|
|
- // 小红书作品的细分日统计通过 XiaohongshuWorkNoteStatisticsImportService 定时任务单独采集,
|
|
|
- // 这里的基于「作品当前总量」的快照统计对小红书意义不大,避免口径混乱,直接跳过。
|
|
|
- if (account.platform === 'xiaohongshu') {
|
|
|
- logger.info(
|
|
|
- `[SaveWorkDayStatistics] Skip snapshot-based work_day_statistics for xiaohongshu account ${account.id}, will be filled by dedicated XHS note statistics importer.`
|
|
|
- );
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取该账号下所有作品
|
|
|
- const works = await this.workRepository.find({
|
|
|
- where: { accountId: account.id },
|
|
|
- });
|
|
|
-
|
|
|
- if (works.length === 0) {
|
|
|
- logger.info(`[SaveWorkDayStatistics] No works found for account ${account.id}`);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 构建统计数据列表(不再包含粉丝数,粉丝数从 user_day_statistics 表获取)
|
|
|
- const statisticsList = works.map(work => ({
|
|
|
- workId: work.id,
|
|
|
- playCount: work.playCount || 0,
|
|
|
- likeCount: work.likeCount || 0,
|
|
|
- commentCount: work.commentCount || 0,
|
|
|
- shareCount: work.shareCount || 0,
|
|
|
- collectCount: work.collectCount || 0,
|
|
|
- }));
|
|
|
-
|
|
|
- logger.info(`[SaveWorkDayStatistics] Saving ${statisticsList.length} work statistics for account ${account.id}`);
|
|
|
-
|
|
|
- // 直接使用 WorkDayStatisticsService 保存统计数据
|
|
|
- try {
|
|
|
- const workDayStatisticsService = new WorkDayStatisticsService();
|
|
|
- const result = await workDayStatisticsService.saveStatistics(statisticsList);
|
|
|
-
|
|
|
- logger.info(`[SaveWorkDayStatistics] Success: inserted=${result.inserted}, updated=${result.updated}`);
|
|
|
- } catch (error) {
|
|
|
- logger.error(`[SaveWorkDayStatistics] Failed to save statistics:`, error);
|
|
|
- throw error;
|
|
|
- }
|
|
|
+ // 已废弃:同步作品时不再基于 works 表当前累计值,往 work_day_statistics 写“今天快照”。
|
|
|
+ // 保留空实现仅为兼容旧调用点,避免影响同步主流程。
|
|
|
+ logger.info(
|
|
|
+ `[SaveWorkDayStatistics] Skip snapshot-based work_day_statistics when syncing works for account ${account.id} (disabled by design).`
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
/**
|