import ExcelJS from "exceljs";
|
import { getSfStatisticsHyperlink } from "@/api/AiCentre/satisfactionse";
|
import { exportPatItem } from "@/api/AiCentre/SingleTask";
|
|
// 出院随访分析:已完成患者明细 + 后端导出 Excel 中的「问题详情占比」题目统计
|
export default {
|
data() {
|
return {
|
analysisVisitCount: 1,
|
analysisLoaded: false,
|
followupRows: [],
|
followupRowsLoading: false,
|
followupRowsPromise: null,
|
followupTopics: [],
|
followupTopicLoading: false,
|
followupQuestionLoaded: false,
|
followupQuestionLoading: false,
|
followupQuestionSeq: 0,
|
followupRowsSeq: 0,
|
followupExportTaskName: "",
|
followupPatientMeta: {
|
visitCount: 1,
|
total: 0,
|
dateRangeText: "",
|
},
|
};
|
},
|
computed: {
|
followupTaskName() {
|
if (this.followupExportTaskName) {
|
return this.followupExportTaskName;
|
}
|
const names = [
|
...new Set(
|
(this.followupRows || [])
|
.map((row) => row.taskName)
|
.filter(Boolean)
|
),
|
];
|
if (!names.length) return "";
|
if (names.length === 1) return names[0];
|
return `${names[0]} 等${names.length}个任务`;
|
},
|
},
|
created() {
|
this.loadFollowupAnalysis();
|
},
|
methods: {
|
setAnalysisVisitCount(count) {
|
if (this.analysisVisitCount === count) return;
|
this.analysisVisitCount = count;
|
this.analysisLoaded = false;
|
this.loadFollowupAnalysis(true);
|
},
|
|
loadFollowupAnalysis(force = false) {
|
if (this.analysisLoaded && !force) return Promise.resolve();
|
this.analysisLoaded = true;
|
this.resetFollowupQuestions();
|
this.followupExportTaskName = "";
|
return this.fetchFollowupPatients();
|
},
|
|
resetFollowupQuestions() {
|
this.followupQuestionSeq += 1;
|
this.followupTopics = [];
|
this.followupQuestionLoaded = false;
|
this.followupQuestionLoading = false;
|
this.followupTopicLoading = false;
|
},
|
|
ensureFollowupQuestionStats() {
|
if (this.followupQuestionLoaded || this.followupQuestionLoading) return;
|
this.loadFollowupQuestionStats();
|
},
|
|
// 直接解析后端导出 Excel 的「问题详情占比」sheet,保证和导出文件完全一致
|
loadFollowupQuestionStats() {
|
if (this.followupQuestionLoading || this.followupQuestionLoaded) return;
|
|
this.followupQuestionLoading = true;
|
this.followupTopicLoading = true;
|
const seq = ++this.followupQuestionSeq;
|
const params = this.buildQuestionExportParams();
|
|
exportPatItem(params)
|
.then(async (data) => {
|
if (seq !== this.followupQuestionSeq) return;
|
|
const buffer =
|
data && typeof data.arrayBuffer === "function"
|
? await data.arrayBuffer()
|
: data;
|
const workbook = new ExcelJS.Workbook();
|
await workbook.xlsx.load(buffer);
|
|
if (seq !== this.followupQuestionSeq) return;
|
|
const result = this.parseQuestionSheet(workbook);
|
this.followupTopics = result.topics;
|
this.followupExportTaskName = result.taskName;
|
this.followupQuestionLoaded = true;
|
})
|
.catch((error) => {
|
if (seq !== this.followupQuestionSeq) return;
|
console.error("解析随访导出 Excel 题目失败:", error);
|
this.followupTopics = [];
|
})
|
.finally(() => {
|
if (seq !== this.followupQuestionSeq) return;
|
this.followupQuestionLoading = false;
|
this.followupTopicLoading = false;
|
});
|
},
|
|
buildQuestionExportParams() {
|
const base = this.analysisBaseParams();
|
const range = this.queryParams.dateRangecy || [];
|
const dateOnly = (value) => {
|
if (!value) return undefined;
|
if (typeof value === "string") return value.slice(0, 10);
|
return this.parseTime(value, "{y}-{m}-{d}");
|
};
|
const serviceType = Array.isArray(this.queryParams.serviceType)
|
? this.queryParams.serviceType[0]
|
: this.queryParams.serviceType;
|
|
return {
|
sendstateView: 2,
|
sort: 1,
|
serviceType: serviceType || 2,
|
searchscope: 3,
|
visitCount: this.analysisVisitCount,
|
leaveldeptcodes: base.depts,
|
leavehospitaldistrictcodes: base.wards,
|
deptOrDistrict: 2,
|
startOutHospTime: dateOnly(range[0]),
|
endOutHospTime: dateOnly(range[1]),
|
};
|
},
|
|
parseQuestionSheet(workbook) {
|
const sheet =
|
workbook.getWorksheet("问题详情占比") ||
|
(workbook.worksheets || []).find(
|
(item) => item && String(item.name).includes("问题详情占比")
|
) ||
|
(workbook.worksheets || [])[1];
|
|
if (!sheet) {
|
return { topics: [], taskName: "" };
|
}
|
|
let headerRowNumber = 1;
|
const scanLimit = Math.min(20, sheet.rowCount || 0);
|
for (let rowNumber = 1; rowNumber <= scanLimit; rowNumber++) {
|
const firstText = this.cellText(
|
sheet.getRow(rowNumber).getCell(1)
|
);
|
if (firstText.includes("问题内容")) {
|
headerRowNumber = rowNumber;
|
break;
|
}
|
}
|
|
const headerRow = sheet.getRow(headerRowNumber);
|
const header = {};
|
for (let column = 1; column <= (headerRow.cellCount || 0); column++) {
|
const name = this.cellText(headerRow.getCell(column));
|
if (name) header[name] = column;
|
}
|
|
const questionColumn = header["问题内容"] || 1;
|
const optionColumn = header["选项结果"] || header["选项"] || 2;
|
const countColumn = header["数量"] || 3;
|
const taskColumn = header["任务名称"] || 7;
|
|
const questionMap = new Map();
|
const taskNames = new Set();
|
|
for (
|
let rowNumber = headerRowNumber + 1;
|
rowNumber <= (sheet.rowCount || 0);
|
rowNumber++
|
) {
|
const row = sheet.getRow(rowNumber);
|
const question = this.cellText(row.getCell(questionColumn));
|
if (!question) continue;
|
if (question.includes("合计") || question.includes("总计")) continue;
|
|
const option = this.cellText(row.getCell(optionColumn));
|
const count = Number(this.cellText(row.getCell(countColumn))) || 0;
|
const taskName = taskColumn
|
? this.cellText(row.getCell(taskColumn))
|
: "";
|
if (taskName) taskNames.add(taskName);
|
|
if (!questionMap.has(question)) {
|
questionMap.set(question, {
|
scriptid: question,
|
scriptContent: question,
|
scriptType: 1,
|
optionOrder: [],
|
optionMap: new Map(),
|
});
|
}
|
const topic = questionMap.get(question);
|
if (option && !topic.optionMap.has(option)) {
|
topic.optionMap.set(option, { optionText: option, count: 0 });
|
topic.optionOrder.push(option);
|
}
|
if (option) {
|
topic.optionMap.get(option).count += count;
|
}
|
}
|
|
const topics = Array.from(questionMap.values())
|
.map((topic) => {
|
const total = topic.optionOrder.reduce(
|
(sum, option) =>
|
sum + (topic.optionMap.get(option).count || 0),
|
0
|
);
|
return {
|
scriptid: topic.scriptid,
|
scriptContent: topic.scriptContent,
|
scriptType: topic.scriptType,
|
details: topic.optionOrder.map((option) => {
|
const item = topic.optionMap.get(option);
|
return {
|
optionText: option,
|
chosenQuantity: item.count,
|
chosenPercentage: total ? item.count / total : 0,
|
};
|
}),
|
};
|
})
|
.filter((topic) => topic.details.length > 0);
|
|
const names = Array.from(taskNames);
|
const taskName = names.length
|
? names.length === 1
|
? names[0]
|
: `${names[0]} 等${names.length}个任务`
|
: "";
|
|
return { topics, taskName };
|
},
|
|
cellText(cell) {
|
if (!cell) return "";
|
const value = cell.value;
|
if (value === null || value === undefined) return "";
|
|
if (typeof value === "object") {
|
if (Array.isArray(value.richText)) {
|
return value.richText
|
.map((item) => item.text || "")
|
.join("")
|
.trim();
|
}
|
if (value.text !== undefined) {
|
return String(value.text).trim();
|
}
|
if (value.result !== undefined) {
|
return String(value.result).trim();
|
}
|
if (value instanceof Date) {
|
return this.parseTime(value);
|
}
|
}
|
|
return String(value).trim();
|
},
|
|
analysisBaseParams() {
|
const query = this.queryParams || {};
|
const stat = query.statisticaltype;
|
const wards = this.pickAnalysisCodes(
|
query.leavehospitaldistrictcodes,
|
this.$store.getters.belongWards,
|
"districtCode"
|
);
|
const depts = this.pickAnalysisCodes(
|
query.deptcodes,
|
this.$store.getters.belongDepts,
|
"deptCode"
|
);
|
const parseDate = (value) => (value ? this.parseTime(value) : undefined);
|
|
return {
|
stat,
|
wards,
|
depts,
|
serviceType: query.serviceType,
|
startTime:
|
query.startTime || parseDate((query.dateRange || [])[0]),
|
endTime: query.endTime || parseDate((query.dateRange || [])[1]),
|
hospitalStartTime:
|
query.hospitalStartTime ||
|
parseDate((query.dateRangecy || [])[0]),
|
hospitalEndTime:
|
query.hospitalEndTime || parseDate((query.dateRangecy || [])[1]),
|
};
|
},
|
|
pickAnalysisCodes(selected, allList, codeKey) {
|
const raw = selected || [];
|
if (!raw.length || raw.includes("all")) {
|
return (allList || []).map((item) => item[codeKey]).filter(Boolean);
|
}
|
return raw.filter((code) => code && code !== "all");
|
},
|
|
fetchFollowupPatients() {
|
const base = this.analysisBaseParams();
|
const infoKey =
|
this.analysisVisitCount === 2
|
? "followUpSuccessAgainInfo"
|
: "followUpSuccessInfo";
|
|
this.followupRowsLoading = true;
|
const seq = ++this.followupRowsSeq;
|
const params = {
|
hyperLinkInfoType: infoKey,
|
serviceType: base.serviceType,
|
hospitalStartTime: base.hospitalStartTime,
|
hospitalEndTime: base.hospitalEndTime,
|
statisticaltype: base.stat,
|
leavehospitaldistrictcodes: base.stat === 1 ? base.wards : [],
|
deptcodes: base.stat === 2 ? base.depts : [],
|
};
|
|
const request = getSfStatisticsHyperlink(params);
|
this.followupRowsPromise = request;
|
|
return request
|
.then((response) => {
|
if (seq !== this.followupRowsSeq) return;
|
const rows = Array.isArray(response.data)
|
? response.data
|
: [];
|
const filtered = this.filterFollowupRows(rows);
|
this.followupRows = filtered;
|
this.followupPatientMeta = {
|
visitCount: this.analysisVisitCount,
|
total: filtered.length,
|
dateRangeText: this.analysisDateRangeText(),
|
};
|
})
|
.catch((error) => {
|
if (seq !== this.followupRowsSeq) return;
|
console.error("获取出院随访患者明细失败:", error);
|
this.followupRows = [];
|
this.followupPatientMeta = {
|
visitCount: this.analysisVisitCount,
|
total: 0,
|
dateRangeText: this.analysisDateRangeText(),
|
};
|
})
|
.finally(() => {
|
if (seq !== this.followupRowsSeq) return;
|
this.followupRowsLoading = false;
|
});
|
},
|
|
filterFollowupRows(rows) {
|
const dischargeRange = this.queryParams.dateRangecy || [];
|
|
return (rows || []).filter((row) => {
|
// 只保留已完成的任务
|
if (row.sendstate != 6) {
|
return false;
|
}
|
if (
|
dischargeRange.length === 2 &&
|
dischargeRange[0] &&
|
dischargeRange[1] &&
|
!this.inDateRange(row.endtime, dischargeRange)
|
) {
|
return false;
|
}
|
return true;
|
});
|
},
|
|
inDateRange(value, range) {
|
if (!value) return false;
|
|
const toTime = (val) => {
|
if (!val) return NaN;
|
const date = new Date(String(val).replace(/-/g, "/"));
|
return date.getTime();
|
};
|
|
const time = toTime(value);
|
if (isNaN(time)) return false;
|
|
const start = toTime(range[0]);
|
let end = toTime(range[1]);
|
if (isNaN(start) || isNaN(end)) return true;
|
|
// 日期范围只到天时,结束日期按当天 23:59:59 处理
|
if (String(range[1]).length <= 10) {
|
end += 24 * 60 * 60 * 1000 - 1;
|
}
|
|
return time >= start && time <= end;
|
},
|
|
analysisDateRangeText() {
|
const discharge = this.formatDateRange(this.queryParams.dateRangecy);
|
return `出院时间:${discharge}`;
|
},
|
|
formatDateRange(range) {
|
const list = range || [];
|
if (list.length === 2 && list[0] && list[1]) {
|
return `${String(list[0]).slice(0, 10)} 至 ${String(list[1]).slice(
|
0,
|
10
|
)}`;
|
}
|
return "全部";
|
},
|
},
|
};
|