|
@@ -405,25 +405,29 @@ export class XiaohongshuAccountOverviewImportService {
|
|
|
const filePath = path.join(this.downloadDir, filename);
|
|
const filePath = path.join(this.downloadDir, filename);
|
|
|
await download.saveAs(filePath);
|
|
await download.saveAs(filePath);
|
|
|
|
|
|
|
|
- const perDay = parseXhsExcel(filePath, mode);
|
|
|
|
|
|
|
+ let perDay = new Map<string, { recordDate: Date } & Record<string, any>>();
|
|
|
let inserted = 0;
|
|
let inserted = 0;
|
|
|
let updated = 0;
|
|
let updated = 0;
|
|
|
- for (const v of perDay.values()) {
|
|
|
|
|
- const { recordDate, ...patch } = v;
|
|
|
|
|
- const r = await this.userDayStatisticsService.saveStatisticsForDate(account.id, recordDate, patch);
|
|
|
|
|
- inserted += r.inserted;
|
|
|
|
|
- updated += r.updated;
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ perDay = parseXhsExcel(filePath, mode);
|
|
|
|
|
+ for (const v of perDay.values()) {
|
|
|
|
|
+ const { recordDate, ...patch } = v;
|
|
|
|
|
+ const r = await this.userDayStatisticsService.saveStatisticsForDate(account.id, recordDate, patch);
|
|
|
|
|
+ inserted += r.inserted;
|
|
|
|
|
+ updated += r.updated;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ `[XHS Import] ${tabText} imported. accountId=${account.id} days=${perDay.size} inserted=${inserted} updated=${updated}`
|
|
|
|
|
+ );
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ // 默认导入后删除 Excel,避免磁盘堆积;仅在显式 KEEP_XHS_XLSX=true 时保留(用于调试)
|
|
|
|
|
+ if (process.env.KEEP_XHS_XLSX === 'true') {
|
|
|
|
|
+ logger.warn(`[XHS Import] KEEP_XHS_XLSX=true, keep file: ${filePath}`);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ await fs.unlink(filePath).catch(() => undefined);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- if (process.env.KEEP_XHS_XLSX === '1') {
|
|
|
|
|
- logger.warn(`[XHS Import] KEEP_XHS_XLSX=1, keep file: ${filePath}`);
|
|
|
|
|
- } else {
|
|
|
|
|
- await fs.unlink(filePath).catch(() => undefined);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- logger.info(
|
|
|
|
|
- `[XHS Import] ${tabText} imported. accountId=${account.id} days=${perDay.size} inserted=${inserted} updated=${updated}`
|
|
|
|
|
- );
|
|
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// 1) 观看数据:播放数 + 点击率/时长/完播率
|
|
// 1) 观看数据:播放数 + 点击率/时长/完播率
|