receptionDetail.qualityTransform.uts 3.03 KB
/**
 * 将网关 findCheckResultListByReceptionId / findCheckResultByBatchId 返回的质检行
 * 加工为 ReceptionQualityAnalysis / DetailData 所需字段(与功能清单第六节对齐)。
 */
import { formatMmSs, getNum, parseStringArray } from "./lists/reception-detail-helpers.uts";

function formatPlainText(s: string): string {
  return s.replace(/\r\n/g, "\n").replace(/\n/g, " ").trim();
}

function firstScriptRecLine(raw: any | null): string {
  const lines = parseStringArray(raw);
  if (lines.length > 0) {
    return formatPlainText(lines[0]);
  }
  return "";
}

/**
 * 单条质检维度加工:formatScore、命中意图、话术首行、对话行 time/text_id、话术挂载
 */
export function transformSingleQualityRow(row: UTSJSONObject): UTSJSONObject {
  const out = {} as UTSJSONObject;
  for (const k in row) {
    out[k] = row[k];
  }

  const scoreRaw = out["score"];
  const sumRaw = out["sumScore"];
  if (scoreRaw != null && sumRaw != null) {
    out["formatScore"] = `${scoreRaw}/${sumRaw}`;
  } else {
    const fs = out["formatScore"] as string | null;
    if (fs == null || fs.trim() == "") {
      out["formatScore"] = "-";
    }
  }

  const hitTextList = parseStringArray(out["hitText"]);
  if (hitTextList.length > 0) {
    out["matchedClass"] = "hit";
    out["matchedText"] = "意图匹配";
  } else {
    out["matchedClass"] = "";
    out["matchedText"] = "意图不匹配";
  }

  const scriptLine = firstScriptRecLine(out["scriptRec"]);

  let list: UTSJSONObject[] = [];
  const listRaw = out["conversationDetailDTOList"];
  if (listRaw != null && listRaw instanceof Array) {
    list = listRaw as UTSJSONObject[];
  }

  const itemOffset = getNum(out, "offset");
  let i = 0;
  while (i < list.length) {
    const line = list[i];
    const mo = getNum(line, "mergedOffset");
    if (mo != null && mo >= 0) {
      const sec = Math.floor(mo);
      line["time"] = formatMmSs(sec);
      line["text_id"] = `text_${Math.ceil(mo)}`;
    }
    i++;
  }

  if (scriptLine != "") {
    let attached = false;
    let j = 0;
    while (j < list.length) {
      const line = list[j];
      const mo = getNum(line, "mergedOffset");
      if (itemOffset != null && mo != null) {
        const offSec = itemOffset > 1000 ? itemOffset / 1000.0 : itemOffset;
        if (Math.abs(mo - offSec) < 0.001) {
          line["scriptRec"] = scriptLine;
          attached = true;
          break;
        }
      }
      j++;
    }
    if (!attached && list.length > 0) {
      const last = list[list.length - 1];
      last["scriptRec"] = scriptLine;
    }
    out["scriptRec"] = scriptLine;
  }

  if (list.length == 0) {
    const stub = {} as UTSJSONObject;
    stub["scriptRec"] = scriptLine != "" ? scriptLine : "暂无话术推荐";
    list = [stub];
  }

  out["conversationDetailDTOList"] = list;
  return out;
}

export function transformQualityCheckRows(rawRows: UTSJSONObject[]): UTSJSONObject[] {
  const out: UTSJSONObject[] = [];
  let i = 0;
  while (i < rawRows.length) {
    out.push(transformSingleQualityRow(rawRows[i]));
    i++;
  }
  return out;
}