lists.ts 5.03 KB
/**
 * 跑完之后那三份「接下来怎么办」的清单。
 *
 * 处理日志四万多列宽行、什么都在里面,但人要的是「这一批我该干什么」:
 * 哪些是被限流挡住的(再跑一次就有)、哪些是怎么跑都补不出的(要人工)、
 * 哪些填满了但备注值得看一眼(区划不一致、需复核)。
 *
 * 三份都带上原表的全部列,所以它可以脱离日志单独看;某一类没有行就不生成文件,
 * 免得桌面上一堆空表。判据只读处理状态、空白格数与备注,不引入新的运行状态。
 */
import path from 'node:path';
import { UserError } from './errors.ts';
import { readTable } from './csv-store.ts';
import { text } from './client.ts';

export type ListKind = 'retry' | 'stuck' | 'review';

export type ListKindInfo = {
  kind: ListKind;
  /** 界面与总结里的名字 */
  title: string;
  /** 文件名后缀,接在输出文件名之后 */
  suffix: string;
  /** 每一行都带上的那句「怎么办」 */
  advice: string;
  /** 总结里那一行的行名与括号里的提示 */
  summaryLabel: string;
  summaryHint: string;
};

export const LIST_KINDS: ListKindInfo[] = [
  {
    kind: 'retry', title: '限流待重试', suffix: '.限流待重试.csv',
    advice: '同一输入文件、同一缓存路径再点一次开始,接口只问这些坐标,别处的值不会重复花钱',
    summaryLabel: '限流没问成', summaryHint: '同一输入、同一缓存再点一次开始就会补上',
  },
  {
    kind: 'stuck', title: '补不出', suffix: '.补不出.csv',
    advice: '再跑也是同样结果:坐标问不了接口或接口没给,需要人工按地址原文与案件资料补',
    summaryLabel: '怎么跑都补不出', summaryHint: '要人工按地址原文与案件资料补',
  },
  {
    kind: 'review', title: '待复核', suffix: '.待复核.csv',
    advice: '三项都填上了,但备注里有值得核对的事实,按备注抽样看即可,不需要重跑',
    summaryLabel: '填上了但值得看一眼', summaryHint: '备注写明了为什么,抽样核对即可',
  },
];

/** 清单的行身份列:有这一列就按它匹配回原表,没有就退回按原表行号 */
export const ID_COLUMN = '案件号';

const EXTRA = ['原表行号', '输出行号', '缺哪些列', '处理状态', '原因', '备注', '怎么办'];

export function listHeader(sourceHeader: readonly string[]): string[] {
  return [EXTRA[0], EXTRA[1], ...sourceHeader, ...EXTRA.slice(2)];
}

/**
 * 清单也进临时目录:它们是「接下来怎么办」的工作文件,不是交付物。
 * 文件名跟着输出走,一次运行的三份清单排在一起,便于整包拷走或清理。
 */
export function listPath(output: string, info: ListKindInfo, tempDir: string): string {
  const stem = path.basename(output, path.extname(output));
  return path.join(tempDir, `${stem}${info.suffix}`);
}

/** 一行属于哪份清单;返回 '' 表示这一行不需要出现在任何清单里。 */
export function classify(status: string, blanks: number, notes: number): ListKind | '' {
  if (status === '请求失败' || status === '未处理') return 'retry';
  if (blanks > 0) return 'stuck';
  if (notes > 0) return 'review';
  return '';
}

export type ListRow = {
  /** 原表全部列的当前值(含工具补出来的三列) */
  values: readonly string[];
  sourceLine: number;
  outputLine: number;
  missing: string;
  status: string;
  reason: string;
  notes: string;
};

export function buildRows(info: ListKindInfo, list: readonly ListRow[]): string[][] {
  return list.map((row) => [String(row.sourceLine), String(row.outputLine), ...row.values,
    row.missing, row.status, row.reason, row.notes, info.advice]);
}

/**
 * 读「限流待重试」这类清单,得到本次允许问接口的行。
 * 有案件号列就按案件号匹配(换过一次排序也对得上),没有就按原表行号。
 */
export function readOnlyList(file: string): { column: string; values: Set<string>; kind: ListKind } {
  // 光有「原表行号」列不够:任何手工 CSV 都能伪造出这一列。清单是三类固定产物,
  // 文件名后缀就是它的身份 —— 拿「补不出」当「限流待重试」续跑会锁错一批行、少补数据。
  const info = LIST_KINDS.find((item) => path.basename(file).endsWith(item.suffix));
  if (info === undefined) {
    throw new UserError(`这份文件不是补齐工具生成的清单(文件名要带 ${LIST_KINDS.map((item) => item.suffix).join(' / ')} 后缀)`);
  }
  const table = readTable(file, null);
  if (!table.header.includes('原表行号')) throw new UserError('这份文件不是补齐工具生成的清单(缺少原表行号列)');
  const column = table.header.includes(ID_COLUMN) ? ID_COLUMN : '原表行号';
  const at = table.header.indexOf(column);
  const values = new Set(table.rows.map((row) => text(row[at] ?? '')).filter((value) => value !== ''));
  if (values.size === 0) throw new UserError('清单里没有可用的行号或案件号');
  return { column, values, kind: info.kind };
}