WXL (wul)
4 天以前 fb34ba28cb908bd328cf6690ab0b19a56428f4e2
src/views/Satisfaction/sfstatistics/components/ChartsPanel.vue
@@ -21,6 +21,18 @@
          <el-radio-button label="month">按月</el-radio-button>
          <el-radio-button label="year">按年</el-radio-button>
        </el-radio-group>
        <span class="filter-label">满意度类型:</span>
        <el-select
          v-model="satisfactionScope"
          size="small"
          clearable
          placeholder="默认离院"
          class="filter-scope"
          @change="refreshAll"
        >
          <el-option label="离院满意度" value="leave" />
          <el-option label="在院满意度" value="hospital" />
        </el-select>
        <span class="filter-label">统计维度:</span>
        <el-radio-group v-model="queryParams.dimension" size="small" @change="onDimensionChange">
          <el-radio-button label="ward">按病区统计</el-radio-button>
@@ -29,7 +41,7 @@
        <el-button type="primary" size="small" icon="el-icon-search" @click="refreshAll">查询</el-button>
        <el-button size="small" icon="el-icon-refresh" @click="handleReset">重置</el-button>
      </div>
      <el-button type="danger" size="small" icon="el-icon-download" @click="exportPDF">导出PDF</el-button>
      <el-button type="danger" size="small" icon="el-icon-download" @click="exportExcel">导出Excel</el-button>
    </div>
    <el-row :gutter="20">
@@ -159,6 +171,8 @@
<script>
import * as echarts from 'echarts';
import ExcelJS from 'exceljs';
import { saveAs } from 'file-saver';
import { mydTrend, sltdMydTotalByDimension, sltdMydTotalByScore ,mzMydScoreRank} from "@/api/AiCentre/satisfaction";
import { getissuelist } from "@/api/AiCentre/questionnaire";
@@ -170,6 +184,8 @@
  dicts: ['dimensionality_type'],
  data() {
    return {
      // 满意度类型:leave=离院满意度(离院科室/病区),hospital=在院满意度(在院科室/病区)
      satisfactionScope: 'leave',
      queryParams: {
        dateRange: [],
        interval: 'month',
@@ -184,10 +200,10 @@
      singleQuestionContent: '',
      singleDeptWards: [],
      // 排名维度筛选
      rankDimension: 'dept',
      // 排名维度筛选(默认按病区)
      rankDimension: 'ward',
      rankDeptWards: [],
      crossDimension: 'dept',
      crossDimension: 'ward',
      crossDeptWards: [],
      // 图表数据
@@ -226,6 +242,10 @@
    },
    defaultWardCodes() {
      return Array.from(new Set(this.wardCodeList.map((w) => w.districtCode)));
    },
    // 是否按「在院满意度」口径查询(清空下拉时按默认的离院口径)
    isInHospitalSatisfaction() {
      return this.satisfactionScope === 'hospital';
    },
    intervalLabel() {
      return { day: '按天', week: '按周', month: '按月', year: '按年' }[this.queryParams.interval] || '按月';
@@ -371,6 +391,11 @@
      if (!den) return '0.00';
      return (num / den).toFixed(2);
    },
    // 综合得分展示:直接使用后端 avgScore,格式化为两位小数
    formatScore(val) {
      const n = Number(val);
      return isNaN(n) ? '0.00' : n.toFixed(2);
    },
    // 解析比例字符串:"14.29%"、"0"、"0.5" → 数值(百分比);无效返回 null
    parsePercent(value) {
      if (value == null || value === '') return null;
@@ -382,7 +407,13 @@
      const arr = Array.isArray(list) ? list : (list && (list.list || list.rows)) || [];
      const options = [];
      let total = 0;
      let avgScore = null;
      arr.forEach((item) => {
        // 综合得分:后端返回的题目级 avgScore(与题目同级)
        if (avgScore == null) {
          const rawAvg = Number(item && item.avgScore);
          if (!isNaN(rawAvg)) avgScore = rawAvg;
        }
        const details = Array.isArray(item && item.subtaskDetailRatioExportList)
          ? item.subtaskDetailRatioExportList
          : [];
@@ -395,7 +426,7 @@
          total += count;
        });
      });
      return { options, total };
      return { options, total, avgScore };
    },
    // 单题选项统计(真实接口 /smartor/serviceSubtaskDetail/sltdMydTotalByScore)
    async fetchSingleQuestion() {
@@ -415,17 +446,22 @@
          questionContent: this.singleQuestionContent,
        };
        const sel = this.singleDeptWards || [];
        // 科室/病区条件随「满意度类型」切换:在院 => deptcodes/hospitaldistrictcodes,离院 => leaveldeptcodes/leavehospitaldistrictcodes
        if (this.queryParams.dimension === 'dept') {
          const names = sel.map((s) => String(s).replace(/^dept:/, ''));
          payload.leaveldeptcodes = names.length ? this.mapDeptNames(names) : this.defaultDeptCodes;
          const codes = names.length ? this.mapDeptNames(names) : this.defaultDeptCodes;
          if (this.isInHospitalSatisfaction) payload.deptcodes = codes;
          else payload.leaveldeptcodes = codes;
        } else {
          const names = sel.map((s) => String(s).replace(/^ward:/, ''));
          payload.leavehospitaldistrictcodes = names.length ? this.mapWardNames(names) : this.defaultWardCodes;
          const codes = names.length ? this.mapWardNames(names) : this.defaultWardCodes;
          if (this.isInHospitalSatisfaction) payload.hospitaldistrictcodes = codes;
          else payload.leavehospitaldistrictcodes = codes;
        }
        const res = await sltdMydTotalByScore(payload);
        console.log('[sltdMydTotalByScore 单题] code=', res && res.code, 'data=', JSON.stringify(res && res.data).slice(0, 1500));
        const list = res && res.code === 200 ? res.data : [];
        const { options, total } = this.normalizeSingleQuestion(list);
        const { options, total, avgScore } = this.normalizeSingleQuestion(list);
        this.singleQuestionData = options.map((o) => ({
          option: o.option,
          count: o.count,
@@ -435,7 +471,7 @@
        }));
        this.singleQuestionMeta = {
          total: total.toLocaleString(),
          score: this.scoreOfOptions(options),
          score: this.formatScore(avgScore),
        };
        if (!options.length) this.notify('当前条件下暂无该题的选项统计数据', 'warning');
      } catch (e) {
@@ -811,52 +847,129 @@
      });
    },
    async exportPDF() {
      const chartRefs = [
        { ref: this.$refs.chart1, id: 'chart1' },
        { ref: this.$refs.chart2, id: 'chart2' },
        { ref: this.$refs.chart4, id: 'chart4' },
      ];
      const chartImages = {};
      for (const { ref, id } of chartRefs) {
        if (ref) {
          const instance = echarts.getInstanceByDom(ref);
          if (instance) {
            chartImages[id] = instance.getDataURL({ type: 'png', pixelRatio: 2, backgroundColor: '#fff' });
    // 导出:图表转图片 + 表格,内容与页面一致(直接下载 xlsx,不依赖打印)
    async exportExcel() {
      try {
        const wb = new ExcelJS.Workbook();
        // 图表 sheet
        const addImageSheet = (sheetName, title, refEl, tip) => {
          const ws = wb.addWorksheet(sheetName, {
            views: [{ showGridLines: false }],
          });
          ws.getCell('A1').value = title;
          ws.getCell('A1').font = { size: 14, bold: true };
          ws.mergeCells('A1:F1');
          let anchorRow = 2.2;
          if (tip) {
            ws.getCell('A2').value = tip;
            ws.mergeCells('A2:F2');
            anchorRow = 2.8;
          }
        }
          for (let c = 1; c <= 6; c++) {
            ws.getColumn(c).width = 18;
          }
          const inst = refEl ? echarts.getInstanceByDom(refEl) : null;
          if (inst) {
            const url = inst.getDataURL({
              type: 'png',
              pixelRatio: 2,
              backgroundColor: '#fff',
            });
            const imgId = wb.addImage({
              base64: url.split(',')[1],
              extension: 'png',
            });
            ws.addImage(imgId, {
              tl: { col: 0.1, row: anchorRow },
              ext: { width: 900, height: 400 },
            });
          } else {
            ws.getCell(`A${Math.ceil(anchorRow)}`).value = '暂无数据';
          }
          return ws;
        };
        addImageSheet(
          '综合得分趋势',
          '患者满意度综合得分趋势',
          this.$refs.chart1,
          `环比 ${this.trendMoM}`
        );
        addImageSheet(
          '分类维度对比',
          `分类维度 · 各指标${this.intervalLabel}对比`,
          this.$refs.chart2,
          ''
        );
        addImageSheet(
          '院区维度对比',
          `院区维度 · 各院区${this.intervalLabel}对比`,
          this.$refs.chart4,
          ''
        );
        // 单题 · 选项统计
        const wsQ = wb.addWorksheet('单题选项统计');
        wsQ.addRow(['单题 · 选项统计']).font = { size: 14, bold: true };
        wsQ.mergeCells(1, 1, 1, 3);
        const qHead = wsQ.addRow(['选项', '合计数量', '占比比例']);
        qHead.font = { bold: true };
        (this.singleQuestionData || []).forEach((r) => {
          wsQ.addRow([r.option, r.count, r.rate]);
        });
        wsQ.addRow([]);
        wsQ.addRow([
          `本题有效填写人次:${this.singleQuestionMeta.total} 综合得分:${this.singleQuestionMeta.score}`,
          '',
          '',
        ]);
        wsQ.mergeCells(wsQ.rowCount, 1, wsQ.rowCount, 3);
        wsQ.columns.forEach((c, i) => {
          c.width = i === 0 ? 36 : 16;
        });
        // 排名表格
        const addRankSheet = (sheetName, title, rows, dimLabel) => {
          const ws = wb.addWorksheet(sheetName);
          ws.addRow([title]).font = { size: 14, bold: true };
          ws.mergeCells(1, 1, 1, 4);
          const head = ws.addRow(['排名', dimLabel, '综合得分', '项数']);
          head.font = { bold: true };
          (rows || []).forEach((r) => {
            ws.addRow([r.rank, r.name, r.score, r.itemCount]);
          });
          if (!rows || !rows.length) {
            ws.addRow(['暂无数据', '', '', '']);
          }
          ws.columns.forEach((c, i) => {
            c.width = i === 1 ? 30 : 12;
          });
        };
        addRankSheet(
          `${this.rankDimLabel}排名`,
          `${this.rankDimLabel}维度 · 门诊患者满意度得分排名`,
          this.rankRows,
          this.rankDimLabel
        );
        addRankSheet(
          `单题×${this.crossDimLabel}排名`,
          `单题×${this.crossDimLabel} · 得分排名`,
          this.crossRows,
          this.crossDimLabel
        );
        const buffer = await wb.xlsx.writeBuffer();
        const blob = new Blob([buffer], {
          type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        });
        const range = (this.queryParams.dateRange || []).join('_');
        saveAs(blob, `各项统计表${range ? '_' + range : ''}.xlsx`);
        this.$message.success('导出成功');
      } catch (e) {
        console.error('导出失败:', e);
        this.$message.error('导出失败:' + (e.message || e));
      }
      const printContent = this.$refs.pdfTarget.cloneNode(true);
      const chartBoxes = printContent.querySelectorAll('.chart-box');
      const imgKeys = Object.keys(chartImages);
      chartBoxes.forEach((box, i) => {
        if (imgKeys[i] && chartImages[imgKeys[i]]) {
          box.innerHTML = `<img src="${chartImages[imgKeys[i]]}" style="width:100%;height:auto;" />`;
        }
      });
      const win = window.open('', '_blank', 'width=900,height=800');
      if (!win) { this.$message.warning('请允许弹出窗口以导出PDF'); return; }
      win.document.write(`
        <html><head><meta charset="utf-8"><title>各项统计表</title>
        <style>
          body { font-family: 'Microsoft YaHei',sans-serif; padding: 20px; }
          .chart-card { break-inside: avoid; margin-bottom: 20px; border: 1px solid #ebeef5; border-radius: 6px; }
          .card-header { padding: 12px 16px; border-bottom: 1px solid #ebeef5; font-size: 15px; font-weight: 600; color: #303133; background: #f5f7fa; }
          .chart-box img { max-width: 100%; display: block; }
          table { width: 100%; border-collapse: collapse; margin: 12px 0; }
          th, td { border: 1px solid #d9d9d9; padding: 8px 12px; text-align: center; font-size: 12px; }
          th { background: #f0f2f5; font-weight: 600; }
          .table-footer { text-align: right; padding: 8px 0; font-size: 13px; color: #606266; }
          .export-bar, .filter-bar, .el-tag, .el-select, .el-radio-group { display: none; }
          @media print { @page { size: A4; margin: 10mm; } }
        </style></head><body>
        ${printContent.innerHTML}
        <script>window.onload=function(){window.print();}<\/script>
        </body></html>
      `);
      win.document.close();
    },
  },
};
@@ -864,7 +977,7 @@
<style lang="scss" scoped>
.export-bar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.filter-bar { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; .filter-label { font-size: 13px; color: #606266; } .filter-date { width: 260px; } }
.filter-bar { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; .filter-label { font-size: 13px; color: #606266; } .filter-date { width: 260px; } .filter-scope { width: 140px; } }
.chart-card { margin-bottom: 0; }
.chart-box { width: 100%; height: 360px; }
.chart-wrap { position: relative; width: 100%; }