Ethanfly 1 päivä sitten
vanhempi
commit
590ebdeecb

+ 20 - 16
server/src/services/XiaohongshuAccountOverviewImportService.ts

@@ -405,25 +405,29 @@ export class XiaohongshuAccountOverviewImportService {
         const filePath = path.join(this.downloadDir, filename);
         await download.saveAs(filePath);
 
-        const perDay = parseXhsExcel(filePath, mode);
+        let perDay = new Map<string, { recordDate: Date } & Record<string, any>>();
         let inserted = 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) 观看数据:播放数 + 点击率/时长/完播率

BIN
server/tmp/xhs-account-overview/35_1769581578665_近30日观看数据.xlsx


BIN
server/tmp/xhs-account-overview/35_1769582575166_近30日观看数据.xlsx


BIN
server/tmp/xhs-account-overview/35_1769582795460_近30日观看数据.xlsx


BIN
server/tmp/xhs-account-overview/35_1769582877553_近30日观看数据.xlsx


BIN
server/tmp/xhs-account-overview/8_1769581512725_近30日观看数据.xlsx


BIN
server/tmp/xhs-account-overview/8_1769582508941_近30日观看数据.xlsx


BIN
server/tmp/xhs-account-overview/8_1769582729004_近30日观看数据.xlsx


BIN
server/tmp/xhs-account-overview/8_1769582810145_近30日观看数据.xlsx