WorkDayStatisticsService.ts 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  1. import { AppDataSource, WorkDayStatistics, Work, PlatformAccount, UserDayStatistics } from '../models/index.js';
  2. import { In } from 'typeorm';
  3. import { logger } from '../utils/logger.js';
  4. interface StatisticsItem {
  5. workId: number;
  6. playCount?: number;
  7. exposureCount?: number;
  8. likeCount?: number;
  9. recommendCount?: number;
  10. commentCount?: number;
  11. shareCount?: number;
  12. collectCount?: number;
  13. fansIncrease?: number;
  14. followCount?: number;
  15. coverClickRate?: string;
  16. avgWatchDuration?: string;
  17. totalWatchDuration?: string;
  18. completionRate?: string;
  19. twoSecondExitRate?: string;
  20. }
  21. interface SaveResult {
  22. inserted: number;
  23. updated: number;
  24. }
  25. // 单个平台的趋势数据
  26. interface PlatformTrendItem {
  27. platform: string; // 平台标识
  28. platformName: string; // 平台中文名
  29. fansIncrease: number[]; // 涨粉数
  30. views: number[]; // 播放数
  31. likes: number[]; // 点赞数
  32. comments: number[]; // 评论数
  33. }
  34. // 趋势数据(按平台分组)
  35. interface TrendData {
  36. dates: string[]; // 日期数组
  37. platforms: PlatformTrendItem[]; // 各平台数据
  38. }
  39. interface PlatformStatItem {
  40. platform: string;
  41. fansCount: number;
  42. fansIncrease: number;
  43. viewsCount: number;
  44. likesCount: number;
  45. commentsCount: number;
  46. collectsCount: number;
  47. updateTime?: string;
  48. }
  49. interface WorkStatisticsItem {
  50. recordDate: string;
  51. playCount: number;
  52. exposureCount?: number;
  53. likeCount: number;
  54. commentCount: number;
  55. shareCount: number;
  56. collectCount: number;
  57. fansIncrease?: number;
  58. followCount?: number; // 视频号:关注数
  59. totalWatchDuration?: string;
  60. avgWatchDuration?: string;
  61. coverClickRate?: string;
  62. completionRate?: string;
  63. twoSecondExitRate?: string;
  64. }
  65. export class WorkDayStatisticsService {
  66. private statisticsRepository = AppDataSource.getRepository(WorkDayStatistics);
  67. private workRepository = AppDataSource.getRepository(Work);
  68. private accountRepository = AppDataSource.getRepository(PlatformAccount);
  69. private userDayStatisticsRepository = AppDataSource.getRepository(UserDayStatistics);
  70. private formatDate(d: Date) {
  71. const yyyy = d.getFullYear();
  72. const mm = String(d.getMonth() + 1).padStart(2, '0');
  73. const dd = String(d.getDate()).padStart(2, '0');
  74. return `${yyyy}-${mm}-${dd}`;
  75. }
  76. /**
  77. * 按作品 ID 删除该作品的所有每日统计(work 被删除时调用,或用于清理孤儿数据)
  78. * @returns 被删除的行数
  79. */
  80. async deleteByWorkId(workId: number): Promise<number> {
  81. const result = await this.statisticsRepository.delete({ workId });
  82. return result.affected ?? 0;
  83. }
  84. /**
  85. * 获取某个账号在指定日期(<= targetDate)时各作品的“最新一条”累计数据总和
  86. * 口径:对该账号所有作品,每个作品取 record_date <= targetDate 的最大日期那条记录,然后把 play/like/comment/collect 求和
  87. */
  88. private async getWorkSumsAtDate(
  89. workIds: number[],
  90. targetDate: string
  91. ): Promise<{ views: number; likes: number; comments: number; collects: number }> {
  92. if (!workIds.length) {
  93. return { views: 0, likes: 0, comments: 0, collects: 0 };
  94. }
  95. // MySQL: 派生表先取每个作品 <= targetDate 的最新日期,再回连取该日数据求和
  96. // 注意:workIds 使用 IN (...),由 TypeORM 负责参数化,避免注入
  97. const placeholders = workIds.map(() => '?').join(',');
  98. const sql = `
  99. SELECT
  100. COALESCE(SUM(wds.play_count), 0) AS views,
  101. COALESCE(SUM(wds.like_count), 0) AS likes,
  102. COALESCE(SUM(wds.comment_count), 0) AS comments,
  103. COALESCE(SUM(wds.collect_count), 0) AS collects
  104. FROM work_day_statistics wds
  105. INNER JOIN (
  106. SELECT wds2.work_id, MAX(wds2.record_date) AS record_date
  107. FROM work_day_statistics wds2
  108. WHERE wds2.work_id IN (${placeholders})
  109. AND wds2.record_date <= ?
  110. GROUP BY wds2.work_id
  111. ) latest
  112. ON latest.work_id = wds.work_id AND latest.record_date = wds.record_date
  113. `;
  114. const rows = await AppDataSource.query(sql, [...workIds, targetDate]);
  115. const row = rows?.[0] || {};
  116. return {
  117. views: Number(row.views) || 0,
  118. likes: Number(row.likes) || 0,
  119. comments: Number(row.comments) || 0,
  120. collects: Number(row.collects) || 0,
  121. };
  122. }
  123. /**
  124. * 保存作品日统计数据(按「今天」的中国时间日历日)
  125. * 当天的数据走更新流,日期变化走新增流
  126. */
  127. async saveStatistics(statistics: StatisticsItem[]): Promise<SaveResult> {
  128. const today = this.getTodayInChina();
  129. let insertedCount = 0;
  130. let updatedCount = 0;
  131. for (const stat of statistics) {
  132. if (!stat.workId) continue;
  133. // 检查当天是否已有记录
  134. const existing = await this.statisticsRepository.findOne({
  135. where: {
  136. workId: stat.workId,
  137. recordDate: today,
  138. },
  139. });
  140. if (existing) {
  141. // 更新已有记录
  142. await this.statisticsRepository.update(existing.id, {
  143. playCount: stat.playCount ?? existing.playCount,
  144. exposureCount: stat.exposureCount ?? existing.exposureCount,
  145. likeCount: stat.likeCount ?? existing.likeCount,
  146. recommendCount: stat.recommendCount ?? existing.recommendCount,
  147. commentCount: stat.commentCount ?? existing.commentCount,
  148. shareCount: stat.shareCount ?? existing.shareCount,
  149. collectCount: stat.collectCount ?? existing.collectCount,
  150. fansIncrease: stat.fansIncrease ?? existing.fansIncrease,
  151. followCount: stat.followCount ?? existing.followCount,
  152. coverClickRate: stat.coverClickRate ?? existing.coverClickRate ?? '0',
  153. avgWatchDuration: stat.avgWatchDuration ?? existing.avgWatchDuration ?? '0',
  154. totalWatchDuration: stat.totalWatchDuration ?? existing.totalWatchDuration ?? '0',
  155. completionRate: stat.completionRate ?? existing.completionRate ?? '0',
  156. twoSecondExitRate: stat.twoSecondExitRate ?? existing.twoSecondExitRate ?? '0',
  157. });
  158. updatedCount++;
  159. } else {
  160. // 插入新记录
  161. const newStat = this.statisticsRepository.create({
  162. workId: stat.workId,
  163. recordDate: today,
  164. playCount: stat.playCount ?? 0,
  165. exposureCount: stat.exposureCount ?? 0,
  166. likeCount: stat.likeCount ?? 0,
  167. recommendCount: stat.recommendCount ?? 0,
  168. commentCount: stat.commentCount ?? 0,
  169. shareCount: stat.shareCount ?? 0,
  170. collectCount: stat.collectCount ?? 0,
  171. fansIncrease: stat.fansIncrease ?? 0,
  172. followCount: stat.followCount ?? 0,
  173. coverClickRate: stat.coverClickRate ?? '0',
  174. avgWatchDuration: stat.avgWatchDuration ?? '0',
  175. totalWatchDuration: stat.totalWatchDuration ?? '0',
  176. completionRate: stat.completionRate ?? '0',
  177. twoSecondExitRate: stat.twoSecondExitRate ?? '0',
  178. });
  179. await this.statisticsRepository.save(newStat);
  180. insertedCount++;
  181. }
  182. }
  183. return { inserted: insertedCount, updated: updatedCount };
  184. }
  185. /**
  186. * 获取「中国时区(Asia/Shanghai)当前日历日」的 Date
  187. * 用于存储 record_date,避免服务器时区与业务日期不一致
  188. */
  189. private getTodayInChina(): Date {
  190. const formatter = new Intl.DateTimeFormat('en-CA', {
  191. timeZone: 'Asia/Shanghai',
  192. year: 'numeric',
  193. month: '2-digit',
  194. day: '2-digit',
  195. });
  196. const parts = formatter.formatToParts(new Date());
  197. const get = (type: string) => parts.find((p) => p.type === type)?.value ?? '0';
  198. const y = parseInt(get('year'), 10);
  199. const m = parseInt(get('month'), 10) - 1;
  200. const d = parseInt(get('day'), 10);
  201. return new Date(Date.UTC(y, m, d, 0, 0, 0, 0));
  202. }
  203. /**
  204. * 保存指定日期的作品日统计数据(按 workId + recordDate 维度 upsert)
  205. * 说明:recordDate 会被归零到当天 00:00:00(本地时间),避免主键冲突
  206. */
  207. async saveStatisticsForDate(
  208. workId: number,
  209. recordDate: Date,
  210. patch: Omit<StatisticsItem, 'workId'>
  211. ): Promise<SaveResult> {
  212. const d = new Date(recordDate);
  213. d.setHours(0, 0, 0, 0);
  214. const existing = await this.statisticsRepository.findOne({
  215. where: { workId, recordDate: d },
  216. });
  217. if (existing) {
  218. await this.statisticsRepository.update(existing.id, {
  219. playCount: patch.playCount ?? existing.playCount,
  220. exposureCount: patch.exposureCount ?? existing.exposureCount,
  221. likeCount: patch.likeCount ?? existing.likeCount,
  222. recommendCount: patch.recommendCount ?? existing.recommendCount,
  223. commentCount: patch.commentCount ?? existing.commentCount,
  224. shareCount: patch.shareCount ?? existing.shareCount,
  225. collectCount: patch.collectCount ?? existing.collectCount,
  226. fansIncrease: patch.fansIncrease ?? existing.fansIncrease,
  227. followCount: patch.followCount ?? existing.followCount,
  228. coverClickRate: patch.coverClickRate ?? existing.coverClickRate ?? '0',
  229. avgWatchDuration: patch.avgWatchDuration ?? existing.avgWatchDuration ?? '0',
  230. totalWatchDuration: patch.totalWatchDuration ?? existing.totalWatchDuration ?? '0',
  231. completionRate: patch.completionRate ?? existing.completionRate ?? '0',
  232. twoSecondExitRate: patch.twoSecondExitRate ?? existing.twoSecondExitRate ?? '0',
  233. });
  234. return { inserted: 0, updated: 1 };
  235. }
  236. const newStat = this.statisticsRepository.create({
  237. workId,
  238. recordDate: d,
  239. playCount: patch.playCount ?? 0,
  240. exposureCount: patch.exposureCount ?? 0,
  241. likeCount: patch.likeCount ?? 0,
  242. recommendCount: patch.recommendCount ?? 0,
  243. commentCount: patch.commentCount ?? 0,
  244. shareCount: patch.shareCount ?? 0,
  245. collectCount: patch.collectCount ?? 0,
  246. fansIncrease: patch.fansIncrease ?? 0,
  247. followCount: patch.followCount ?? 0,
  248. coverClickRate: patch.coverClickRate ?? '0',
  249. avgWatchDuration: patch.avgWatchDuration ?? '0',
  250. totalWatchDuration: patch.totalWatchDuration ?? '0',
  251. completionRate: patch.completionRate ?? '0',
  252. twoSecondExitRate: patch.twoSecondExitRate ?? '0',
  253. });
  254. await this.statisticsRepository.save(newStat);
  255. return { inserted: 1, updated: 0 };
  256. }
  257. /**
  258. * 批量保存指定日期范围的作品日统计数据(每条记录自带日期)
  259. */
  260. async saveStatisticsForDateBatch(
  261. items: Array<{ workId: number; recordDate: Date } & Omit<StatisticsItem, 'workId'>>
  262. ): Promise<SaveResult> {
  263. let insertedCount = 0;
  264. let updatedCount = 0;
  265. for (const it of items) {
  266. const { workId, recordDate, ...patch } = it;
  267. const result = await this.saveStatisticsForDate(workId, recordDate, patch);
  268. insertedCount += result.inserted;
  269. updatedCount += result.updated;
  270. }
  271. logger.info(
  272. `[WorkDayStatistics] Date-batch save completed: inserted=${insertedCount}, updated=${updatedCount}`
  273. );
  274. return { inserted: insertedCount, updated: updatedCount };
  275. }
  276. // 平台名称映射
  277. private platformNameMap: Record<string, string> = {
  278. xiaohongshu: '小红书',
  279. douyin: '抖音',
  280. kuaishou: '快手',
  281. weixin: '视频号',
  282. weixin_video: '视频号',
  283. shipinhao: '视频号',
  284. baijiahao: '百家号',
  285. };
  286. /**
  287. * 获取数据趋势
  288. * 从 user_day_statistics 表获取近30天的数据
  289. * 按平台分组返回,每个平台下所有账号的总和为一条曲线
  290. */
  291. async getTrend(
  292. userId: number,
  293. options: {
  294. days?: number;
  295. startDate?: string;
  296. endDate?: string;
  297. accountId?: number;
  298. }
  299. ): Promise<TrendData> {
  300. const { days = 30, startDate, endDate, accountId } = options;
  301. // 计算日期范围
  302. let dateStart: Date;
  303. let dateEnd: Date;
  304. if (startDate && endDate) {
  305. dateStart = new Date(startDate);
  306. dateEnd = new Date(endDate);
  307. } else {
  308. dateEnd = new Date();
  309. dateStart = new Date();
  310. dateStart.setDate(dateStart.getDate() - Math.min(days, 30) + 1);
  311. }
  312. // 获取该用户所有的账号(包含平台信息)
  313. const userAccounts = await this.accountRepository.find({
  314. where: { userId },
  315. select: ['id', 'platform'],
  316. });
  317. if (userAccounts.length === 0) {
  318. // 用户没有账号,返回空数据
  319. return this.generateEmptyTrendData(dateStart, dateEnd);
  320. }
  321. // 按平台分组账号
  322. const platformAccountsMap = new Map<string, number[]>();
  323. for (const account of userAccounts) {
  324. const platform = account.platform;
  325. if (!platformAccountsMap.has(platform)) {
  326. platformAccountsMap.set(platform, []);
  327. }
  328. platformAccountsMap.get(platform)!.push(account.id);
  329. }
  330. // 如果指定了特定账号,只查询该账号所属平台
  331. if (accountId) {
  332. const targetAccount = userAccounts.find(a => a.id === accountId);
  333. if (targetAccount) {
  334. platformAccountsMap.clear();
  335. platformAccountsMap.set(targetAccount.platform, [accountId]);
  336. }
  337. }
  338. // 生成完整的日期数组
  339. const dates: string[] = [];
  340. const dateKeys: string[] = [];
  341. const d = new Date(dateStart);
  342. while (d <= dateEnd) {
  343. const dateKey = this.formatDate(d);
  344. const displayDate = `${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
  345. dates.push(displayDate);
  346. dateKeys.push(dateKey);
  347. d.setDate(d.getDate() + 1);
  348. }
  349. // 获取用户拥有的所有平台
  350. const userPlatforms = Array.from(platformAccountsMap.keys());
  351. // 查询数据:按平台和日期分组
  352. const allAccountIds = userAccounts.map(a => a.id);
  353. const results = await this.userDayStatisticsRepository
  354. .createQueryBuilder('uds')
  355. .innerJoin(PlatformAccount, 'pa', 'uds.account_id = pa.id')
  356. .select('pa.platform', 'platform')
  357. .addSelect('uds.record_date', 'recordDate')
  358. .addSelect('SUM(uds.fans_increase)', 'totalFansIncrease')
  359. .addSelect('SUM(uds.play_count)', 'totalViews')
  360. .addSelect('SUM(uds.like_count)', 'totalLikes')
  361. .addSelect('SUM(uds.comment_count)', 'totalComments')
  362. .where('uds.account_id IN (:...accountIds)', { accountIds: allAccountIds })
  363. .andWhere('uds.record_date >= :dateStart', { dateStart })
  364. .andWhere('uds.record_date <= :dateEnd', { dateEnd })
  365. .groupBy('pa.platform')
  366. .addGroupBy('uds.record_date')
  367. .orderBy('uds.record_date', 'ASC')
  368. .getRawMany();
  369. // 构建 平台 -> 日期 -> 数据 的映射
  370. const platformDateMap = new Map<string, Map<string, {
  371. fansIncrease: number;
  372. views: number;
  373. likes: number;
  374. comments: number;
  375. }>>();
  376. for (const row of results) {
  377. const platform = row.platform;
  378. const dateKey = row.recordDate instanceof Date
  379. ? row.recordDate.toISOString().split('T')[0]
  380. : String(row.recordDate).split('T')[0];
  381. if (!platformDateMap.has(platform)) {
  382. platformDateMap.set(platform, new Map());
  383. }
  384. platformDateMap.get(platform)!.set(dateKey, {
  385. fansIncrease: parseInt(row.totalFansIncrease) || 0,
  386. views: parseInt(row.totalViews) || 0,
  387. likes: parseInt(row.totalLikes) || 0,
  388. comments: parseInt(row.totalComments) || 0,
  389. });
  390. }
  391. // 构建各平台的数据数组
  392. const platforms: PlatformTrendItem[] = [];
  393. for (const platform of userPlatforms) {
  394. const dateDataMap = platformDateMap.get(platform) || new Map();
  395. const fansIncrease: number[] = [];
  396. const views: number[] = [];
  397. const likes: number[] = [];
  398. const comments: number[] = [];
  399. for (const dateKey of dateKeys) {
  400. const data = dateDataMap.get(dateKey);
  401. if (data) {
  402. fansIncrease.push(data.fansIncrease);
  403. views.push(data.views);
  404. likes.push(data.likes);
  405. comments.push(data.comments);
  406. } else {
  407. fansIncrease.push(0);
  408. views.push(0);
  409. likes.push(0);
  410. comments.push(0);
  411. }
  412. }
  413. platforms.push({
  414. platform,
  415. platformName: this.platformNameMap[platform] || platform,
  416. fansIncrease,
  417. views,
  418. likes,
  419. comments,
  420. });
  421. }
  422. return { dates, platforms };
  423. }
  424. /**
  425. * 生成空的趋势数据
  426. */
  427. private generateEmptyTrendData(dateStart: Date, dateEnd: Date): TrendData {
  428. const dates: string[] = [];
  429. const d = new Date(dateStart);
  430. while (d <= dateEnd) {
  431. dates.push(`${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`);
  432. d.setDate(d.getDate() + 1);
  433. }
  434. return { dates, platforms: [] };
  435. }
  436. /**
  437. * 按平台分组获取统计数据
  438. */
  439. async getStatisticsByPlatform(
  440. userId: number,
  441. options: {
  442. days?: number;
  443. startDate?: string;
  444. endDate?: string;
  445. }
  446. ): Promise<PlatformStatItem[]> {
  447. const { days = 30, startDate, endDate } = options;
  448. // 计算日期范围
  449. let dateStart: Date;
  450. let dateEnd: Date;
  451. if (startDate && endDate) {
  452. dateStart = new Date(startDate);
  453. dateEnd = new Date(endDate);
  454. } else {
  455. dateEnd = new Date();
  456. dateStart = new Date();
  457. dateStart.setDate(dateStart.getDate() - Math.min(days, 30) + 1);
  458. }
  459. const endDateStr = endDate ? endDate : this.formatDate(dateEnd);
  460. const startDateStr = startDate ? startDate : this.formatDate(dateStart);
  461. /**
  462. * 口径变更:user_day_statistics 的 play/comment/like/collect/fans_increase 等字段为“每日单独值”
  463. * 因此:
  464. * - 区间统计:直接按日期范围 SUM
  465. * - 单日统计:startDate=endDate 时,也同样按该日 SUM(无需再做“累计差”)
  466. * 粉丝数:使用 platform_accounts.fans_count(当前值)
  467. */
  468. const [fansRows, udsRows] = await Promise.all([
  469. this.accountRepository
  470. .createQueryBuilder('pa')
  471. .select('pa.platform', 'platform')
  472. .addSelect('COALESCE(SUM(pa.fansCount), 0)', 'fansCount')
  473. .where('pa.userId = :userId', { userId })
  474. .groupBy('pa.platform')
  475. .getRawMany(),
  476. this.userDayStatisticsRepository
  477. .createQueryBuilder('uds')
  478. .innerJoin(PlatformAccount, 'pa', 'pa.id = uds.account_id')
  479. .select('pa.platform', 'platform')
  480. .addSelect('COALESCE(SUM(uds.play_count), 0)', 'viewsCount')
  481. .addSelect('COALESCE(SUM(uds.comment_count), 0)', 'commentsCount')
  482. .addSelect('COALESCE(SUM(uds.like_count), 0)', 'likesCount')
  483. .addSelect('COALESCE(SUM(uds.collect_count), 0)', 'collectsCount')
  484. .addSelect('COALESCE(SUM(uds.fans_increase), 0)', 'fansIncrease')
  485. .addSelect('MAX(uds.updated_at)', 'latestUpdateTime')
  486. .where('pa.user_id = :userId', { userId })
  487. .andWhere('DATE(uds.record_date) >= :startDate', { startDate: startDateStr })
  488. .andWhere('DATE(uds.record_date) <= :endDate', { endDate: endDateStr })
  489. .groupBy('pa.platform')
  490. .getRawMany(),
  491. ]);
  492. const fansMap = new Map<string, number>();
  493. for (const row of fansRows || []) {
  494. const platform = String(row.platform || '');
  495. if (!platform) continue;
  496. fansMap.set(platform, Number(row.fansCount) || 0);
  497. }
  498. const statMap = new Map<string, {
  499. viewsCount: number;
  500. commentsCount: number;
  501. likesCount: number;
  502. collectsCount: number;
  503. fansIncrease: number;
  504. latestUpdateTime?: string | Date | null;
  505. }>();
  506. for (const row of udsRows || []) {
  507. const platform = String(row.platform || '');
  508. if (!platform) continue;
  509. statMap.set(platform, {
  510. viewsCount: Number(row.viewsCount) || 0,
  511. commentsCount: Number(row.commentsCount) || 0,
  512. likesCount: Number(row.likesCount) || 0,
  513. collectsCount: Number(row.collectsCount) || 0,
  514. fansIncrease: Number(row.fansIncrease) || 0,
  515. latestUpdateTime: row.latestUpdateTime ?? null,
  516. });
  517. }
  518. const platforms = new Set<string>([...fansMap.keys(), ...statMap.keys()]);
  519. const platformData: PlatformStatItem[] = Array.from(platforms).map((platform) => {
  520. const stat = statMap.get(platform);
  521. const fansCount = fansMap.get(platform) ?? 0;
  522. const latestUpdate = stat?.latestUpdateTime ? new Date(stat.latestUpdateTime as any) : null;
  523. return {
  524. platform,
  525. fansCount,
  526. fansIncrease: stat?.fansIncrease ?? 0,
  527. viewsCount: stat?.viewsCount ?? 0,
  528. likesCount: stat?.likesCount ?? 0,
  529. commentsCount: stat?.commentsCount ?? 0,
  530. collectsCount: stat?.collectsCount ?? 0,
  531. updateTime: latestUpdate ? latestUpdate.toISOString() : undefined,
  532. };
  533. });
  534. platformData.sort((a, b) => (b.fansCount || 0) - (a.fansCount || 0));
  535. return platformData;
  536. }
  537. /**
  538. * 批量获取作品的历史统计数据
  539. */
  540. async getWorkStatisticsHistory(
  541. workIds: number[],
  542. options: {
  543. startDate?: string;
  544. endDate?: string;
  545. }
  546. ): Promise<Record<string, WorkStatisticsItem[]>> {
  547. const { startDate, endDate } = options;
  548. const queryBuilder = this.statisticsRepository
  549. .createQueryBuilder('wds')
  550. .select('wds.work_id', 'workId')
  551. .addSelect('wds.record_date', 'recordDate')
  552. .addSelect('wds.play_count', 'playCount')
  553. .addSelect('wds.exposure_count', 'exposureCount')
  554. .addSelect('wds.like_count', 'likeCount')
  555. .addSelect('wds.comment_count', 'commentCount')
  556. .addSelect('wds.share_count', 'shareCount')
  557. .addSelect('wds.collect_count', 'collectCount')
  558. .addSelect('wds.fans_increase', 'fansIncrease')
  559. .addSelect('wds.follow_count', 'followCount')
  560. .addSelect('wds.total_watch_duration', 'totalWatchDuration')
  561. .addSelect('wds.avg_watch_duration', 'avgWatchDuration')
  562. .addSelect('wds.cover_click_rate', 'coverClickRate')
  563. .addSelect('wds.completion_rate', 'completionRate')
  564. .addSelect('wds.two_second_exit_rate', 'twoSecondExitRate')
  565. .where('wds.work_id IN (:...workIds)', { workIds })
  566. .orderBy('wds.work_id', 'ASC')
  567. .addOrderBy('wds.record_date', 'ASC');
  568. if (startDate) {
  569. queryBuilder.andWhere('wds.record_date >= :startDate', { startDate });
  570. }
  571. if (endDate) {
  572. queryBuilder.andWhere('wds.record_date <= :endDate', { endDate });
  573. }
  574. const results = await queryBuilder.getRawMany();
  575. // 按 workId 分组
  576. const groupedData: Record<string, WorkStatisticsItem[]> = {};
  577. for (const row of results) {
  578. const workId = String(row.workId);
  579. if (!groupedData[workId]) {
  580. groupedData[workId] = [];
  581. }
  582. const recordDate = row.recordDate instanceof Date
  583. ? row.recordDate.toISOString().split('T')[0]
  584. : String(row.recordDate).split('T')[0];
  585. groupedData[workId].push({
  586. recordDate,
  587. playCount: parseInt(row.playCount) || 0,
  588. exposureCount: parseInt(row.exposureCount) || 0,
  589. likeCount: parseInt(row.likeCount) || 0,
  590. commentCount: parseInt(row.commentCount) || 0,
  591. shareCount: parseInt(row.shareCount) || 0,
  592. collectCount: parseInt(row.collectCount) || 0,
  593. fansIncrease: parseInt(row.fansIncrease) || 0,
  594. followCount: parseInt(row.followCount) || 0,
  595. totalWatchDuration: row.totalWatchDuration || '0',
  596. avgWatchDuration: row.avgWatchDuration || '0',
  597. coverClickRate: row.coverClickRate || '0',
  598. completionRate: row.completionRate || '0',
  599. twoSecondExitRate: row.twoSecondExitRate || '0',
  600. });
  601. }
  602. return groupedData;
  603. }
  604. /**
  605. * 获取每个账号在指定日期(<= targetDate)时,user_day_statistics 的“最新一条”
  606. * 主要用于:总粉丝、更新时间等需要“最新状态”的字段。
  607. */
  608. private async getLatestUserDayStatsAtDate(
  609. accountIds: number[],
  610. targetDate: string
  611. ): Promise<Map<number, { fansCount: number; updatedAt: Date | null; recordDate: Date | null }>> {
  612. const map = new Map<number, { fansCount: number; updatedAt: Date | null; recordDate: Date | null }>();
  613. if (!accountIds.length) return map;
  614. // MySQL: 派生表先取每个账号 <= targetDate 的最新日期,再回连取该日数据
  615. const placeholders = accountIds.map(() => '?').join(',');
  616. const sql = `
  617. SELECT
  618. uds.account_id AS accountId,
  619. uds.fans_count AS fansCount,
  620. uds.record_date AS recordDate,
  621. uds.updated_at AS updatedAt
  622. FROM user_day_statistics uds
  623. INNER JOIN (
  624. SELECT account_id, MAX(record_date) AS record_date
  625. FROM user_day_statistics
  626. WHERE account_id IN (${placeholders})
  627. AND record_date <= ?
  628. GROUP BY account_id
  629. ) latest
  630. ON latest.account_id = uds.account_id AND latest.record_date = uds.record_date
  631. `;
  632. const rows: any[] = await AppDataSource.query(sql, [...accountIds, targetDate]);
  633. for (const row of rows || []) {
  634. const accountId = Number(row.accountId) || 0;
  635. if (!accountId) continue;
  636. const fansCount = Number(row.fansCount) || 0;
  637. const recordDate = row.recordDate ? new Date(row.recordDate) : null;
  638. const updatedAt = row.updatedAt ? new Date(row.updatedAt) : null;
  639. map.set(accountId, { fansCount, updatedAt, recordDate });
  640. }
  641. return map;
  642. }
  643. /**
  644. * 获取指定日期(= dateStr)每个账号在 user_day_statistics 的数据(用于“昨日”口径的一一对应)。
  645. */
  646. private async getUserDayStatsByExactDate(
  647. accountIds: number[],
  648. dateStr: string
  649. ): Promise<Map<number, { playCount: number; commentCount: number; likeCount: number; fansIncrease: number; updatedAt: Date | null }>> {
  650. const map = new Map<number, { playCount: number; commentCount: number; likeCount: number; fansIncrease: number; updatedAt: Date | null }>();
  651. if (!accountIds.length) return map;
  652. const rows = await this.userDayStatisticsRepository
  653. .createQueryBuilder('uds')
  654. .select('uds.account_id', 'accountId')
  655. .addSelect('uds.play_count', 'playCount')
  656. .addSelect('uds.comment_count', 'commentCount')
  657. .addSelect('uds.like_count', 'likeCount')
  658. .addSelect('uds.fans_increase', 'fansIncrease')
  659. .addSelect('uds.updated_at', 'updatedAt')
  660. .where('uds.account_id IN (:...accountIds)', { accountIds })
  661. .andWhere('DATE(uds.record_date) = :d', { d: dateStr })
  662. .getRawMany();
  663. for (const row of rows || []) {
  664. const accountId = Number(row.accountId) || 0;
  665. if (!accountId) continue;
  666. map.set(accountId, {
  667. playCount: Number(row.playCount) || 0,
  668. commentCount: Number(row.commentCount) || 0,
  669. likeCount: Number(row.likeCount) || 0,
  670. fansIncrease: Number(row.fansIncrease) || 0,
  671. updatedAt: row.updatedAt ? new Date(row.updatedAt) : null,
  672. });
  673. }
  674. return map;
  675. }
  676. /**
  677. * 获取数据总览
  678. * 返回账号列表和汇总统计数据
  679. */
  680. async getOverview(userId: number): Promise<{
  681. accounts: Array<{
  682. id: number;
  683. nickname: string;
  684. username: string;
  685. avatarUrl: string | null;
  686. platform: string;
  687. groupId: number | null;
  688. groupName?: string | null;
  689. fansCount: number;
  690. worksCount: number | null;
  691. totalIncome: number | null;
  692. yesterdayIncome: number | null;
  693. totalViews: number | null;
  694. yesterdayViews: number | null;
  695. yesterdayComments: number;
  696. yesterdayLikes: number;
  697. yesterdayFansIncrease: number;
  698. updateTime: string;
  699. status: string;
  700. }>;
  701. summary: {
  702. totalAccounts: number;
  703. totalWorks: number;
  704. totalIncome: number;
  705. yesterdayIncome: number;
  706. totalViews: number;
  707. yesterdayViews: number;
  708. totalFans: number;
  709. yesterdayComments: number;
  710. yesterdayLikes: number;
  711. yesterdayFansIncrease: number;
  712. };
  713. }> {
  714. // 只查询支持的平台:抖音、百家号、视频号、小红书
  715. const allowedPlatforms = ['douyin', 'baijiahao', 'weixin_video', 'xiaohongshu'];
  716. // 获取用户的所有账号(只包含支持的平台)
  717. const accounts = await this.accountRepository.find({
  718. where: {
  719. userId,
  720. platform: In(allowedPlatforms),
  721. },
  722. relations: ['group'],
  723. });
  724. // 使用中国时区(UTC+8)计算“今天/昨天”的业务日期
  725. // 思路:在当前 UTC 时间基础上 +8 小时,再取 ISO 日期部分,即为中国日历日期
  726. const now = new Date();
  727. const chinaNow = new Date(now.getTime() + 8 * 60 * 60 * 1000);
  728. const chinaYesterday = new Date(chinaNow.getTime() - 24 * 60 * 60 * 1000);
  729. // 格式化为 YYYY-MM-DD,与 MySQL DATE 字段匹配
  730. const todayStr = chinaNow.toISOString().split('T')[0];
  731. const yesterdayStr = chinaYesterday.toISOString().split('T')[0];
  732. logger.info(`[WorkDayStatistics] getOverview - userId: ${userId}, today: ${todayStr}, yesterday: ${yesterdayStr}`);
  733. const accountIds = accounts.map(a => a.id);
  734. // 账号总数:platform_accounts 中 user_id 对应数量(等价于 accounts.length)
  735. const totalAccounts = accounts.length;
  736. // 列表“总播放/汇总总播放”:统一从 works.play_count 聚合(累计)
  737. const worksPlayRows = accountIds.length
  738. ? await this.workRepository
  739. .createQueryBuilder('w')
  740. .select('w.accountId', 'accountId')
  741. .addSelect('COALESCE(SUM(w.playCount), 0)', 'playCount')
  742. .where('w.userId = :userId', { userId })
  743. .andWhere('w.accountId IN (:...accountIds)', { accountIds })
  744. .groupBy('w.accountId')
  745. .getRawMany()
  746. : [];
  747. const totalPlayMap = new Map<number, number>();
  748. for (const row of worksPlayRows || []) {
  749. totalPlayMap.set(Number(row.accountId) || 0, Number(row.playCount) || 0);
  750. }
  751. // “昨日”口径:只取 user_day_statistics 指定日期那一行,一一对应
  752. const yesterdayUdsMap = await this.getUserDayStatsByExactDate(accountIds, yesterdayStr);
  753. // 粉丝数口径:直接取 platform_accounts.fans_count(不跟随 user_day_statistics)
  754. const accountList: Array<{
  755. id: number;
  756. nickname: string;
  757. username: string;
  758. avatarUrl: string | null;
  759. platform: string;
  760. groupId: number | null;
  761. groupName?: string | null;
  762. fansCount: number;
  763. totalIncome: number | null;
  764. yesterdayIncome: number | null;
  765. totalViews: number | null;
  766. yesterdayViews: number | null;
  767. yesterdayComments: number;
  768. yesterdayLikes: number;
  769. yesterdayFansIncrease: number;
  770. updateTime: string;
  771. status: string;
  772. }> = [];
  773. // 汇总统计数据
  774. let totalIncome = 0;
  775. let yesterdayIncome = 0;
  776. let totalWorks = 0;
  777. let totalViews = 0;
  778. let yesterdayViews = 0;
  779. let totalFans = 0;
  780. let yesterdayComments = 0;
  781. let yesterdayLikes = 0;
  782. let yesterdayFansIncrease = 0;
  783. for (const account of accounts) {
  784. const accountTotalViews = totalPlayMap.get(account.id) ?? 0;
  785. const yesterdayUds = yesterdayUdsMap.get(account.id);
  786. const accountFansCount = account.fansCount || 0;
  787. const accountWorksCount = account.worksCount || 0;
  788. const accountYesterdayViews = yesterdayUds?.playCount ?? 0;
  789. const accountYesterdayComments = yesterdayUds?.commentCount ?? 0;
  790. const accountYesterdayLikes = yesterdayUds?.likeCount ?? 0;
  791. const accountYesterdayFansIncrease = yesterdayUds?.fansIncrease ?? 0;
  792. const updateTime = (yesterdayUds?.updatedAt ?? account.updatedAt).toISOString();
  793. accountList.push({
  794. id: account.id,
  795. nickname: account.accountName || '',
  796. username: account.accountId || '',
  797. avatarUrl: account.avatarUrl,
  798. platform: account.platform,
  799. groupId: account.groupId,
  800. groupName: account.group?.name ?? null,
  801. fansCount: accountFansCount,
  802. worksCount: accountWorksCount,
  803. totalIncome: null,
  804. yesterdayIncome: null,
  805. totalViews: accountTotalViews,
  806. yesterdayViews: accountYesterdayViews,
  807. yesterdayComments: accountYesterdayComments,
  808. yesterdayLikes: accountYesterdayLikes,
  809. yesterdayFansIncrease: accountYesterdayFansIncrease,
  810. updateTime,
  811. status: account.status,
  812. });
  813. totalWorks += accountWorksCount;
  814. totalViews += accountTotalViews;
  815. totalFans += accountFansCount;
  816. yesterdayViews += accountYesterdayViews;
  817. yesterdayComments += accountYesterdayComments;
  818. yesterdayLikes += accountYesterdayLikes;
  819. yesterdayFansIncrease += accountYesterdayFansIncrease;
  820. }
  821. return {
  822. accounts: accountList,
  823. summary: {
  824. totalAccounts,
  825. totalWorks,
  826. totalIncome,
  827. yesterdayIncome,
  828. totalViews,
  829. yesterdayViews,
  830. totalFans,
  831. yesterdayComments,
  832. yesterdayLikes,
  833. yesterdayFansIncrease,
  834. },
  835. };
  836. }
  837. /**
  838. * 获取账号维度的区间统计数据
  839. * 用于「账号数据」页,支持按日期区间、平台、分组筛选
  840. */
  841. async getAccountsAnalytics(
  842. userId: number,
  843. options: {
  844. startDate: string;
  845. endDate: string;
  846. platform?: string;
  847. groupId?: number;
  848. }
  849. ): Promise<{
  850. accounts: Array<{
  851. id: number;
  852. nickname: string;
  853. username: string;
  854. avatarUrl: string | null;
  855. platform: string;
  856. groupId: number | null;
  857. income: number | null;
  858. recommendationCount: number | null;
  859. viewsCount: number;
  860. commentsCount: number;
  861. likesCount: number;
  862. fansIncrease: number;
  863. fansCount: number;
  864. updateTime: string;
  865. status: string;
  866. }>;
  867. summary: {
  868. totalAccounts: number;
  869. totalIncome: number;
  870. recommendationCount: number | null;
  871. viewsCount: number;
  872. commentsCount: number;
  873. likesCount: number;
  874. fansIncrease: number;
  875. totalFans: number;
  876. };
  877. }> {
  878. const { startDate, endDate, platform, groupId } = options;
  879. // 只查询支持的平台:抖音、百家号、视频号、小红书
  880. const allowedPlatforms = ['douyin', 'baijiahao', 'weixin_video', 'xiaohongshu'];
  881. const accountQuery = this.accountRepository
  882. .createQueryBuilder('pa')
  883. .where('pa.userId = :userId', { userId })
  884. .andWhere('pa.platform IN (:...allowedPlatforms)', { allowedPlatforms });
  885. if (platform) {
  886. accountQuery.andWhere('pa.platform = :platform', { platform });
  887. }
  888. if (groupId) {
  889. accountQuery.andWhere('pa.groupId = :groupId', { groupId });
  890. }
  891. const accounts = await accountQuery.getMany();
  892. if (accounts.length === 0) {
  893. return {
  894. accounts: [],
  895. summary: {
  896. totalAccounts: 0,
  897. totalIncome: 0,
  898. recommendationCount: null,
  899. viewsCount: 0,
  900. commentsCount: 0,
  901. likesCount: 0,
  902. fansIncrease: 0,
  903. totalFans: 0,
  904. },
  905. };
  906. }
  907. const accountIds = accounts.map(a => a.id);
  908. // 使用 user_day_statistics 统计区间内的播放/评论/点赞/涨粉等
  909. const statsRows = await this.userDayStatisticsRepository
  910. .createQueryBuilder('uds')
  911. .select('uds.account_id', 'accountId')
  912. .addSelect('COALESCE(SUM(uds.play_count), 0)', 'viewsCount')
  913. .addSelect('COALESCE(SUM(uds.comment_count), 0)', 'commentsCount')
  914. .addSelect('COALESCE(SUM(uds.like_count), 0)', 'likesCount')
  915. .addSelect('COALESCE(SUM(uds.fans_increase), 0)', 'fansIncrease')
  916. .addSelect('MAX(uds.updated_at)', 'latestUpdateTime')
  917. .where('uds.account_id IN (:...accountIds)', { accountIds })
  918. .andWhere('DATE(uds.record_date) >= :startDate', { startDate })
  919. .andWhere('DATE(uds.record_date) <= :endDate', { endDate })
  920. .groupBy('uds.account_id')
  921. .getRawMany();
  922. const statMap = new Map<
  923. number,
  924. {
  925. viewsCount: number;
  926. commentsCount: number;
  927. likesCount: number;
  928. fansIncrease: number;
  929. latestUpdateTime: Date | null;
  930. }
  931. >();
  932. for (const row of statsRows || []) {
  933. const accountId = Number(row.accountId) || 0;
  934. if (!accountId) continue;
  935. statMap.set(accountId, {
  936. viewsCount: Number(row.viewsCount) || 0,
  937. commentsCount: Number(row.commentsCount) || 0,
  938. likesCount: Number(row.likesCount) || 0,
  939. fansIncrease: Number(row.fansIncrease) || 0,
  940. latestUpdateTime: row.latestUpdateTime ? new Date(row.latestUpdateTime) : null,
  941. });
  942. }
  943. const resultAccounts: Array<{
  944. id: number;
  945. nickname: string;
  946. username: string;
  947. avatarUrl: string | null;
  948. platform: string;
  949. groupId: number | null;
  950. income: number | null;
  951. recommendationCount: number | null;
  952. viewsCount: number;
  953. commentsCount: number;
  954. likesCount: number;
  955. fansIncrease: number;
  956. fansCount: number;
  957. updateTime: string;
  958. status: string;
  959. }> = [];
  960. let totalViews = 0;
  961. let totalComments = 0;
  962. let totalLikes = 0;
  963. let totalFansIncrease = 0;
  964. let totalFans = 0;
  965. for (const account of accounts) {
  966. const stat =
  967. statMap.get(account.id) ?? {
  968. viewsCount: 0,
  969. commentsCount: 0,
  970. likesCount: 0,
  971. fansIncrease: 0,
  972. latestUpdateTime: account.updatedAt ?? null,
  973. };
  974. const fansCount = account.fansCount || 0;
  975. const updateTime = stat.latestUpdateTime
  976. ? this.formatUpdateTime(stat.latestUpdateTime)
  977. : this.formatUpdateTime(account.updatedAt ?? new Date());
  978. resultAccounts.push({
  979. id: account.id,
  980. nickname: account.accountName || '',
  981. username: account.accountId || '',
  982. avatarUrl: account.avatarUrl,
  983. platform: account.platform,
  984. groupId: account.groupId ?? null,
  985. income: null,
  986. recommendationCount: null,
  987. viewsCount: stat.viewsCount,
  988. commentsCount: stat.commentsCount,
  989. likesCount: stat.likesCount,
  990. fansIncrease: stat.fansIncrease,
  991. fansCount,
  992. updateTime,
  993. status: account.status,
  994. });
  995. totalViews += stat.viewsCount;
  996. totalComments += stat.commentsCount;
  997. totalLikes += stat.likesCount;
  998. totalFansIncrease += stat.fansIncrease;
  999. totalFans += fansCount;
  1000. }
  1001. return {
  1002. accounts: resultAccounts,
  1003. summary: {
  1004. totalAccounts: accounts.length,
  1005. totalIncome: 0,
  1006. recommendationCount: null,
  1007. viewsCount: totalViews,
  1008. commentsCount: totalComments,
  1009. likesCount: totalLikes,
  1010. fansIncrease: totalFansIncrease,
  1011. totalFans,
  1012. },
  1013. };
  1014. }
  1015. /**
  1016. * 获取单个账号的详情数据
  1017. * 包括:汇总统计、每日数据、作品列表
  1018. * 说明:
  1019. * - 收益、推荐量目前数据库尚未接入,统一返回 0
  1020. */
  1021. async getAccountDetail(
  1022. userId: number,
  1023. accountId: number,
  1024. options: {
  1025. startDate: string;
  1026. endDate: string;
  1027. }
  1028. ): Promise<{
  1029. summary: {
  1030. income: number;
  1031. recommendationCount: number;
  1032. viewsCount: number;
  1033. commentsCount: number;
  1034. likesCount: number;
  1035. fansIncrease: number;
  1036. };
  1037. dailyData: Array<{
  1038. date: string;
  1039. income: number;
  1040. recommendationCount: number;
  1041. viewsCount: number;
  1042. commentsCount: number;
  1043. likesCount: number;
  1044. fansIncrease: number;
  1045. }>;
  1046. works: Array<{
  1047. id: number;
  1048. title: string;
  1049. coverUrl: string;
  1050. platform: string;
  1051. publishTime: string | null;
  1052. recommendCount: number;
  1053. viewsCount: number;
  1054. commentsCount: number;
  1055. sharesCount: number;
  1056. collectsCount: number;
  1057. likesCount: number;
  1058. }>;
  1059. }> {
  1060. const { startDate, endDate } = options;
  1061. const account = await this.accountRepository.findOne({
  1062. where: { id: accountId, userId },
  1063. });
  1064. if (!account) {
  1065. // 账号不存在或不属于该用户,返回空数据
  1066. return {
  1067. summary: {
  1068. income: 0,
  1069. recommendationCount: 0,
  1070. viewsCount: 0,
  1071. commentsCount: 0,
  1072. likesCount: 0,
  1073. fansIncrease: 0,
  1074. },
  1075. dailyData: [],
  1076. works: [],
  1077. };
  1078. }
  1079. const startDateStr = startDate;
  1080. const endDateStr = endDate;
  1081. // 1. 每日数据:直接从 user_day_statistics 获取指定账号的每日记录
  1082. const udsRows = await this.userDayStatisticsRepository
  1083. .createQueryBuilder('uds')
  1084. .select('uds.record_date', 'recordDate')
  1085. .addSelect('uds.play_count', 'viewsCount')
  1086. .addSelect('uds.comment_count', 'commentsCount')
  1087. .addSelect('uds.like_count', 'likesCount')
  1088. .addSelect('uds.fans_increase', 'fansIncrease')
  1089. .where('uds.account_id = :accountId', { accountId })
  1090. .andWhere('DATE(uds.record_date) >= :startDate', { startDate: startDateStr })
  1091. .andWhere('DATE(uds.record_date) <= :endDate', { endDate: endDateStr })
  1092. .orderBy('uds.record_date', 'ASC')
  1093. .getRawMany();
  1094. const dailyMap = new Map<string, { views: number; comments: number; likes: number; fansIncrease: number }>();
  1095. for (const row of udsRows || []) {
  1096. if (!row.recordDate) continue;
  1097. let dateKey: string;
  1098. if (row.recordDate instanceof Date) {
  1099. const y = row.recordDate.getFullYear();
  1100. const m = String(row.recordDate.getMonth() + 1).padStart(2, '0');
  1101. const d = String(row.recordDate.getDate()).padStart(2, '0');
  1102. dateKey = `${y}-${m}-${d}`;
  1103. } else {
  1104. dateKey = String(row.recordDate).slice(0, 10);
  1105. }
  1106. dailyMap.set(dateKey, {
  1107. views: Number(row.viewsCount) || 0,
  1108. comments: Number(row.commentsCount) || 0,
  1109. likes: Number(row.likesCount) || 0,
  1110. fansIncrease: Number(row.fansIncrease) || 0,
  1111. });
  1112. }
  1113. const dStart = new Date(startDateStr);
  1114. const dEnd = new Date(endDateStr);
  1115. const cursor = new Date(dStart);
  1116. const dailyData: Array<{
  1117. date: string;
  1118. income: number;
  1119. recommendationCount: number;
  1120. viewsCount: number;
  1121. commentsCount: number;
  1122. likesCount: number;
  1123. fansIncrease: number;
  1124. }> = [];
  1125. let totalViews = 0;
  1126. let totalComments = 0;
  1127. let totalLikes = 0;
  1128. let totalFansIncrease = 0;
  1129. while (cursor <= dEnd) {
  1130. const dateKey = this.formatDate(cursor);
  1131. const value = dailyMap.get(dateKey) ?? {
  1132. views: 0,
  1133. comments: 0,
  1134. likes: 0,
  1135. fansIncrease: 0,
  1136. };
  1137. dailyData.push({
  1138. date: dateKey,
  1139. income: 0,
  1140. recommendationCount: 0,
  1141. viewsCount: value.views,
  1142. commentsCount: value.comments,
  1143. likesCount: value.likes,
  1144. fansIncrease: value.fansIncrease,
  1145. });
  1146. totalViews += value.views;
  1147. totalComments += value.comments;
  1148. totalLikes += value.likes;
  1149. totalFansIncrease += value.fansIncrease;
  1150. cursor.setDate(cursor.getDate() + 1);
  1151. }
  1152. // 2. 作品列表:按作品聚合 work_day_statistics 区间内的数据
  1153. const worksRows = await this.workRepository
  1154. .createQueryBuilder('w')
  1155. .leftJoin(WorkDayStatistics, 'wds', 'wds.work_id = w.id AND wds.record_date >= :wStart AND wds.record_date <= :wEnd', {
  1156. wStart: startDateStr,
  1157. wEnd: endDateStr,
  1158. })
  1159. .select('w.id', 'id')
  1160. .addSelect('w.title', 'title')
  1161. .addSelect('w.cover_url', 'coverUrl')
  1162. .addSelect('w.platform', 'platform')
  1163. .addSelect('w.publish_time', 'publishTime')
  1164. .addSelect('COALESCE(SUM(wds.play_count), 0)', 'viewsCount')
  1165. .addSelect('COALESCE(SUM(wds.comment_count), 0)', 'commentsCount')
  1166. .addSelect('COALESCE(SUM(wds.share_count), 0)', 'sharesCount')
  1167. .addSelect('COALESCE(SUM(wds.collect_count), 0)', 'collectsCount')
  1168. .addSelect('COALESCE(SUM(wds.like_count), 0)', 'likesCount')
  1169. .where('w.userId = :userId', { userId })
  1170. .andWhere('w.accountId = :accountId', { accountId })
  1171. .groupBy('w.id')
  1172. .orderBy('w.publish_time', 'DESC')
  1173. .getRawMany();
  1174. const works = (worksRows || []).map((row) => {
  1175. const publishTime =
  1176. row.publishTime instanceof Date
  1177. ? row.publishTime.toISOString()
  1178. : row.publishTime
  1179. ? String(row.publishTime)
  1180. : null;
  1181. return {
  1182. id: Number(row.id),
  1183. title: row.title || '',
  1184. coverUrl: row.coverUrl || '',
  1185. platform: row.platform || '',
  1186. publishTime,
  1187. // 推荐量目前没有独立字段,统一返回 0
  1188. recommendCount: 0,
  1189. viewsCount: Number(row.viewsCount) || 0,
  1190. commentsCount: Number(row.commentsCount) || 0,
  1191. sharesCount: Number(row.sharesCount) || 0,
  1192. collectsCount: Number(row.collectsCount) || 0,
  1193. likesCount: Number(row.likesCount) || 0,
  1194. };
  1195. });
  1196. return {
  1197. summary: {
  1198. income: 0,
  1199. recommendationCount: 0,
  1200. viewsCount: totalViews,
  1201. commentsCount: totalComments,
  1202. likesCount: totalLikes,
  1203. fansIncrease: totalFansIncrease,
  1204. },
  1205. dailyData,
  1206. works,
  1207. };
  1208. }
  1209. /**
  1210. * 获取作品数据列表(用于「作品数据」页)
  1211. * 依据 work_day_statistics 进行区间汇总统计
  1212. */
  1213. async getWorksAnalytics(
  1214. userId: number,
  1215. options: {
  1216. startDate: string;
  1217. endDate: string;
  1218. platform?: string;
  1219. accountIds?: number[];
  1220. groupId?: number;
  1221. keyword?: string;
  1222. sortBy?: 'publish_desc' | 'publish_asc' | 'views_desc' | 'likes_desc' | 'comments_desc';
  1223. page?: number;
  1224. pageSize?: number;
  1225. }
  1226. ): Promise<{
  1227. summary: {
  1228. totalWorks: number;
  1229. recommendCount: number;
  1230. viewsCount: number;
  1231. commentsCount: number;
  1232. sharesCount: number;
  1233. collectsCount: number;
  1234. likesCount: number;
  1235. };
  1236. total: number;
  1237. works: Array<{
  1238. id: number;
  1239. title: string;
  1240. coverUrl: string;
  1241. platform: string;
  1242. accountId: number;
  1243. accountName: string;
  1244. accountAvatar: string | null;
  1245. workType: string;
  1246. publishTime: string | null;
  1247. recommendCount: number;
  1248. viewsCount: number;
  1249. commentsCount: number;
  1250. sharesCount: number;
  1251. collectsCount: number;
  1252. likesCount: number;
  1253. }>;
  1254. }> {
  1255. const {
  1256. startDate,
  1257. endDate,
  1258. platform,
  1259. accountIds,
  1260. groupId,
  1261. keyword,
  1262. sortBy = 'publish_desc',
  1263. page = 1,
  1264. pageSize = 20,
  1265. } = options;
  1266. const startDateStr = startDate;
  1267. const endDateStr = endDate;
  1268. // 作品列表:指标直接取 works 表 yesterday_* 快照;时间范围仅用于筛选「发布时间」落在范围内的作品
  1269. // 说明:不再按日期范围聚合 work_day_statistics(避免口径随筛选范围变化)
  1270. const qb = this.workRepository
  1271. .createQueryBuilder('w')
  1272. .innerJoin(PlatformAccount, 'pa', 'pa.id = w.accountId')
  1273. .select('w.id', 'id')
  1274. .addSelect('w.title', 'title')
  1275. .addSelect('w.cover_url', 'coverUrl')
  1276. .addSelect('w.platform', 'platform')
  1277. .addSelect('w.accountId', 'accountId')
  1278. .addSelect('pa.accountName', 'accountName')
  1279. .addSelect('pa.avatarUrl', 'accountAvatar')
  1280. .addSelect('w.status', 'workType')
  1281. .addSelect('w.publish_time', 'publishTime')
  1282. .addSelect('COALESCE(w.yesterday_play_count, 0)', 'viewsCount')
  1283. .addSelect('COALESCE(w.yesterday_comment_count, 0)', 'commentsCount')
  1284. .addSelect('COALESCE(w.yesterday_share_count, 0)', 'sharesCount')
  1285. .addSelect('COALESCE(w.yesterday_collect_count, 0)', 'collectsCount')
  1286. .addSelect('COALESCE(w.yesterday_like_count, 0)', 'likesCount')
  1287. .where('w.userId = :userId', { userId })
  1288. .andWhere('w.publish_time IS NOT NULL')
  1289. .andWhere('DATE(w.publish_time) >= :startDate AND DATE(w.publish_time) <= :endDate', {
  1290. startDate: startDateStr,
  1291. endDate: endDateStr,
  1292. });
  1293. if (platform) {
  1294. qb.andWhere('w.platform = :platform', { platform });
  1295. }
  1296. if (accountIds && accountIds.length > 0) {
  1297. qb.andWhere('w.accountId IN (:...accountIds)', { accountIds });
  1298. }
  1299. if (groupId) {
  1300. qb.andWhere('pa.groupId = :groupId', { groupId });
  1301. }
  1302. if (keyword && keyword.trim()) {
  1303. const kw = `%${keyword.trim()}%`;
  1304. qb.andWhere('w.title LIKE :kw', { kw });
  1305. }
  1306. // SQL 里只按发布时间倒序,具体排序口径在内存中根据 sortBy 再处理
  1307. qb.orderBy('w.publish_time', 'DESC');
  1308. // 先获取全部满足条件的作品聚合行,再在内存中做分页和汇总
  1309. const allRows = await qb.getRawMany();
  1310. // 根据 sortBy 对结果进行排序
  1311. const sortedRows = [...allRows];
  1312. sortedRows.sort((a, b) => {
  1313. const getNum = (v: unknown) => Number(v) || 0;
  1314. if (sortBy === 'views_desc') {
  1315. return getNum(b.viewsCount) - getNum(a.viewsCount);
  1316. }
  1317. if (sortBy === 'likes_desc') {
  1318. return getNum(b.likesCount) - getNum(a.likesCount);
  1319. }
  1320. if (sortBy === 'comments_desc') {
  1321. return getNum(b.commentsCount) - getNum(a.commentsCount);
  1322. }
  1323. // 发布时间排序(默认 publish_desc)
  1324. const toTime = (v: unknown) => {
  1325. if (!v) return 0;
  1326. if (v instanceof Date) return v.getTime();
  1327. const t = new Date(String(v)).getTime();
  1328. return Number.isNaN(t) ? 0 : t;
  1329. };
  1330. const ta = toTime(a.publishTime);
  1331. const tb = toTime(b.publishTime);
  1332. if (sortBy === 'publish_asc') {
  1333. return ta - tb;
  1334. }
  1335. // publish_desc
  1336. return tb - ta;
  1337. });
  1338. const total = sortedRows.length;
  1339. const offset = (page - 1) * pageSize;
  1340. const pagedRows = sortedRows.slice(offset, offset + pageSize);
  1341. let totalViews = 0;
  1342. let totalComments = 0;
  1343. let totalShares = 0;
  1344. let totalCollects = 0;
  1345. let totalLikes = 0;
  1346. // 汇总统计使用所有作品(而不是当前页),确保顶部统计口径统一
  1347. for (const row of sortedRows) {
  1348. const views = Number(row.viewsCount) || 0;
  1349. const comments = Number(row.commentsCount) || 0;
  1350. const shares = Number(row.sharesCount) || 0;
  1351. const collects = Number(row.collectsCount) || 0;
  1352. const likes = Number(row.likesCount) || 0;
  1353. totalViews += views;
  1354. totalComments += comments;
  1355. totalShares += shares;
  1356. totalCollects += collects;
  1357. totalLikes += likes;
  1358. }
  1359. // 当前页作品列表只返回分页后的数据
  1360. const works = pagedRows.map((row) => {
  1361. const views = Number(row.viewsCount) || 0;
  1362. const comments = Number(row.commentsCount) || 0;
  1363. const shares = Number(row.sharesCount) || 0;
  1364. const collects = Number(row.collectsCount) || 0;
  1365. const likes = Number(row.likesCount) || 0;
  1366. const publishTime =
  1367. row.publishTime instanceof Date
  1368. ? row.publishTime.toISOString()
  1369. : row.publishTime
  1370. ? String(row.publishTime)
  1371. : null;
  1372. return {
  1373. id: Number(row.id),
  1374. title: row.title || '',
  1375. coverUrl: row.coverUrl || '',
  1376. platform: row.platform || '',
  1377. accountId: Number(row.accountId) || 0,
  1378. accountName: row.accountName || '',
  1379. accountAvatar: row.accountAvatar || null,
  1380. workType: row.workType || '动态',
  1381. publishTime,
  1382. recommendCount: 0,
  1383. viewsCount: views,
  1384. commentsCount: comments,
  1385. sharesCount: shares,
  1386. collectsCount: collects,
  1387. likesCount: likes,
  1388. };
  1389. });
  1390. return {
  1391. summary: {
  1392. totalWorks: total,
  1393. recommendCount: 0,
  1394. viewsCount: totalViews,
  1395. commentsCount: totalComments,
  1396. sharesCount: totalShares,
  1397. collectsCount: totalCollects,
  1398. likesCount: totalLikes,
  1399. },
  1400. total,
  1401. works,
  1402. };
  1403. }
  1404. /**
  1405. * 获取平台详情数据
  1406. * 包括汇总统计、每日汇总数据和账号列表
  1407. */
  1408. async getPlatformDetail(
  1409. userId: number,
  1410. platform: string,
  1411. options: {
  1412. startDate: string;
  1413. endDate: string;
  1414. }
  1415. ): Promise<{
  1416. summary: {
  1417. totalAccounts: number;
  1418. totalIncome: number;
  1419. viewsCount: number;
  1420. commentsCount: number;
  1421. likesCount: number;
  1422. fansIncrease: number;
  1423. recommendationCount: number | null; // 推荐量(部分平台支持)
  1424. };
  1425. dailyData: Array<{
  1426. date: string;
  1427. income: number;
  1428. recommendationCount: number | null;
  1429. viewsCount: number;
  1430. commentsCount: number;
  1431. likesCount: number;
  1432. fansIncrease: number;
  1433. }>;
  1434. accounts: Array<{
  1435. id: number;
  1436. nickname: string;
  1437. username: string;
  1438. avatarUrl: string | null;
  1439. platform: string;
  1440. income: number | null;
  1441. recommendationCount: number | null;
  1442. viewsCount: number | null;
  1443. commentsCount: number;
  1444. likesCount: number;
  1445. fansIncrease: number;
  1446. updateTime: string;
  1447. }>;
  1448. }> {
  1449. const { startDate, endDate } = options;
  1450. const startDateStr = startDate;
  1451. const endDateStr = endDate;
  1452. // 获取该平台的所有账号
  1453. const accounts = await this.accountRepository.find({
  1454. where: {
  1455. userId,
  1456. platform: platform as any,
  1457. },
  1458. relations: ['group'],
  1459. });
  1460. if (accounts.length === 0) {
  1461. return {
  1462. summary: {
  1463. totalAccounts: 0,
  1464. totalIncome: 0,
  1465. viewsCount: 0,
  1466. commentsCount: 0,
  1467. likesCount: 0,
  1468. fansIncrease: 0,
  1469. recommendationCount: null,
  1470. },
  1471. dailyData: [],
  1472. accounts: [],
  1473. };
  1474. }
  1475. /**
  1476. * 口径变更:user_day_statistics 的各项数据为“每日单独值”,不再是累计值
  1477. * 因此平台详情:
  1478. * - 区间汇总:直接 SUM(user_day_statistics.*)(按账号、按天)
  1479. * - 每日汇总:按 record_date 分组 SUM
  1480. */
  1481. const accountIds = accounts.map(a => a.id);
  1482. const totalAccounts = accounts.length;
  1483. const [dailyRows, perAccountRows] = await Promise.all([
  1484. // 按日期维度汇总(每天所有账号数据之和)
  1485. // 这里直接使用原生 SQL,以保证和文档/手动验证时看到的 SQL 完全一致:
  1486. //
  1487. // SELECT
  1488. // uds.record_date AS recordDate,
  1489. // COALESCE(SUM(uds.play_count), 0) AS viewsCount,
  1490. // COALESCE(SUM(uds.comment_count), 0) AS commentsCount,
  1491. // COALESCE(SUM(uds.like_count), 0) AS likesCount,
  1492. // COALESCE(SUM(uds.fans_increase), 0) AS fansIncrease
  1493. // FROM user_day_statistics uds
  1494. // WHERE uds.account_id IN (...)
  1495. // AND uds.record_date >= ?
  1496. // AND uds.record_date <= ?
  1497. // GROUP BY uds.record_date
  1498. // ORDER BY uds.record_date ASC;
  1499. (async () => {
  1500. if (!accountIds.length) return [];
  1501. const inPlaceholders = accountIds.map(() => '?').join(',');
  1502. const sql = `
  1503. SELECT
  1504. uds.record_date AS recordDate,
  1505. COALESCE(SUM(uds.play_count), 0) AS viewsCount,
  1506. COALESCE(SUM(uds.comment_count), 0) AS commentsCount,
  1507. COALESCE(SUM(uds.like_count), 0) AS likesCount,
  1508. COALESCE(SUM(uds.fans_increase), 0) AS fansIncrease
  1509. FROM user_day_statistics uds
  1510. WHERE uds.account_id IN (${inPlaceholders})
  1511. AND uds.record_date >= ?
  1512. AND uds.record_date <= ?
  1513. GROUP BY uds.record_date
  1514. ORDER BY uds.record_date ASC
  1515. `;
  1516. const params = [...accountIds, startDateStr, endDateStr];
  1517. return await AppDataSource.query(sql, params);
  1518. })(),
  1519. // 按账号维度汇总(区间内所有天的和)
  1520. this.userDayStatisticsRepository
  1521. .createQueryBuilder('uds')
  1522. .select('uds.account_id', 'accountId')
  1523. .addSelect('COUNT(1)', 'rowCount')
  1524. .addSelect('COALESCE(SUM(uds.play_count), 0)', 'viewsCount')
  1525. .addSelect('COALESCE(SUM(uds.comment_count), 0)', 'commentsCount')
  1526. .addSelect('COALESCE(SUM(uds.like_count), 0)', 'likesCount')
  1527. .addSelect('COALESCE(SUM(uds.fans_increase), 0)', 'fansIncrease')
  1528. .addSelect('MAX(uds.updated_at)', 'latestUpdateTime')
  1529. .where('uds.account_id IN (:...accountIds)', { accountIds })
  1530. .andWhere('DATE(uds.record_date) >= :startDate', { startDate: startDateStr })
  1531. .andWhere('DATE(uds.record_date) <= :endDate', { endDate: endDateStr })
  1532. .groupBy('uds.account_id')
  1533. .getRawMany(),
  1534. ]);
  1535. // ===== 按日期汇总:每日汇总数据 =====
  1536. const dailyMap = new Map<string, { views: number; comments: number; likes: number; fansIncrease: number }>();
  1537. for (const row of dailyRows || []) {
  1538. if (!row.recordDate) continue;
  1539. /**
  1540. * 注意:record_date 在实体里是 DATE 类型,TypeORM 读出来通常是 Date 对象。
  1541. * 之前用 String(row.recordDate).slice(0, 10) 会得到类似 "Wed Jan 28" 这样的字符串前 10 位,
  1542. * 导致 key 和下面 this.formatDate(cursor) 生成的 "YYYY-MM-DD" 不一致,从而 dailyMap 命中失败,全部变成 0。
  1543. *
  1544. * 这里改成显式按本地时间拼出 "YYYY-MM-DD",确保与 startDate/endDate 的格式一致。
  1545. */
  1546. let dateKey: string;
  1547. if (row.recordDate instanceof Date) {
  1548. const y = row.recordDate.getFullYear();
  1549. const m = String(row.recordDate.getMonth() + 1).padStart(2, '0');
  1550. const d = String(row.recordDate.getDate()).padStart(2, '0');
  1551. dateKey = `${y}-${m}-${d}`;
  1552. } else {
  1553. // 数据库如果已经返回字符串,例如 "2026-01-28",直接截前 10 位即可
  1554. dateKey = String(row.recordDate).slice(0, 10);
  1555. }
  1556. const prev = dailyMap.get(dateKey) ?? { views: 0, comments: 0, likes: 0, fansIncrease: 0 };
  1557. dailyMap.set(dateKey, {
  1558. views: prev.views + (Number(row.viewsCount) || 0),
  1559. comments: prev.comments + (Number(row.commentsCount) || 0),
  1560. likes: prev.likes + (Number(row.likesCount) || 0),
  1561. fansIncrease: prev.fansIncrease + (Number(row.fansIncrease) || 0),
  1562. });
  1563. }
  1564. // 补齐日期区间(没有数据也返回 0)
  1565. const dailyData: Array<{
  1566. date: string;
  1567. income: number;
  1568. recommendationCount: number | null;
  1569. viewsCount: number;
  1570. commentsCount: number;
  1571. likesCount: number;
  1572. fansIncrease: number;
  1573. }> = [];
  1574. const dStart = new Date(startDateStr);
  1575. const dEnd = new Date(endDateStr);
  1576. const cursor = new Date(dStart);
  1577. while (cursor <= dEnd) {
  1578. const dateKey = this.formatDate(cursor);
  1579. const v = dailyMap.get(dateKey) ?? { views: 0, comments: 0, likes: 0, fansIncrease: 0 };
  1580. dailyData.push({
  1581. date: dateKey,
  1582. income: 0,
  1583. recommendationCount: null,
  1584. viewsCount: v.views,
  1585. commentsCount: v.comments,
  1586. likesCount: v.likes,
  1587. fansIncrease: v.fansIncrease,
  1588. });
  1589. cursor.setDate(cursor.getDate() + 1);
  1590. }
  1591. // ===== 按账号汇总:账号列表 & 顶部汇总 =====
  1592. const perAccountMap = new Map<
  1593. number,
  1594. { rowCount: number; views: number; comments: number; likes: number; fansIncrease: number; latestUpdateTime: Date | null }
  1595. >();
  1596. for (const row of perAccountRows || []) {
  1597. const accountId = Number(row.accountId) || 0;
  1598. if (!accountId) continue;
  1599. perAccountMap.set(accountId, {
  1600. rowCount: Number(row.rowCount) || 0,
  1601. views: Number(row.viewsCount) || 0,
  1602. comments: Number(row.commentsCount) || 0,
  1603. likes: Number(row.likesCount) || 0,
  1604. fansIncrease: Number(row.fansIncrease) || 0,
  1605. latestUpdateTime: row.latestUpdateTime ? new Date(row.latestUpdateTime) : null,
  1606. });
  1607. }
  1608. // 顶部汇总:直接用账号维度汇总,确保和“账号详细数据”一致
  1609. let totalViews = 0;
  1610. let totalComments = 0;
  1611. let totalLikes = 0;
  1612. let totalFansIncrease = 0;
  1613. for (const agg of perAccountMap.values()) {
  1614. totalViews += agg.views;
  1615. totalComments += agg.comments;
  1616. totalLikes += agg.likes;
  1617. totalFansIncrease += agg.fansIncrease;
  1618. }
  1619. const accountList: Array<{
  1620. id: number;
  1621. nickname: string;
  1622. username: string;
  1623. avatarUrl: string | null;
  1624. platform: string;
  1625. income: number | null;
  1626. recommendationCount: number | null;
  1627. viewsCount: number | null;
  1628. commentsCount: number;
  1629. likesCount: number;
  1630. fansIncrease: number;
  1631. updateTime: string;
  1632. }> = accounts.map((account) => {
  1633. const agg =
  1634. perAccountMap.get(account.id) ?? { rowCount: 0, views: 0, comments: 0, likes: 0, fansIncrease: 0, latestUpdateTime: null };
  1635. const updateTime = agg.latestUpdateTime ? this.formatUpdateTime(agg.latestUpdateTime) : '';
  1636. return {
  1637. id: account.id,
  1638. nickname: account.accountName || '',
  1639. username: account.accountId || '',
  1640. avatarUrl: account.avatarUrl,
  1641. platform: account.platform,
  1642. income: null,
  1643. recommendationCount: null,
  1644. // 没有任何记录时,前端展示“获取失败”,避免把“无数据”误显示成 0
  1645. viewsCount: agg.rowCount > 0 ? agg.views : null,
  1646. commentsCount: agg.comments,
  1647. likesCount: agg.likes,
  1648. fansIncrease: agg.fansIncrease,
  1649. updateTime,
  1650. };
  1651. });
  1652. return {
  1653. summary: {
  1654. totalAccounts,
  1655. totalIncome: 0, // 收益数据需要从其他表获取
  1656. viewsCount: totalViews,
  1657. commentsCount: totalComments,
  1658. likesCount: totalLikes,
  1659. fansIncrease: totalFansIncrease,
  1660. recommendationCount: null, // 推荐量(部分平台支持)
  1661. },
  1662. dailyData,
  1663. accounts: accountList,
  1664. };
  1665. }
  1666. /**
  1667. * 格式化更新时间为统一的人类可读格式:
  1668. * - 如果是今年:MM-DD HH:mm(例如:01-22 10:22)
  1669. * - 如果是往年:YYYY-MM-DD HH:mm(例如:2025-12-22 10:22)
  1670. */
  1671. private formatUpdateTime(date: Date): string {
  1672. const y = date.getFullYear();
  1673. const month = String(date.getMonth() + 1).padStart(2, '0');
  1674. const day = String(date.getDate()).padStart(2, '0');
  1675. const hours = String(date.getHours()).padStart(2, '0');
  1676. const minutes = String(date.getMinutes()).padStart(2, '0');
  1677. // 始终返回完整的 YYYY-MM-DD HH:mm 格式,避免前端 dayjs 解析省略年份时被误解析为 2001 年
  1678. return `${y}-${month}-${day} ${hours}:${minutes}`;
  1679. }
  1680. }