liusheng
2026-11-02 d6aa663043a23bb37f1c712a19424690c5a2062f
src/views/Satisfaction/sfstatistics/components/FollowupStatistics.vue
@@ -389,6 +389,7 @@
} from "@/api/system/user";
import ExcelJS from "exceljs";
import { saveAs } from "file-saver";
import { getTaskservelist } from "@/api/AiCentre/SingleTask";
import SeedetailsDialog from "./components/SeedetailsDialog.vue";
import TopicDialog from "./components/TopicDialog.vue";
@@ -397,6 +398,13 @@
  components: {
    SeedetailsDialog,
    TopicDialog,
  },
  props: {
    // 是否在顶部生成患者表单 / 问题详情分析 tab(仅满意度统计主页开启)
    showAnalysisTabs: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
@@ -458,6 +466,10 @@
      // 加载状态
      loading: false,
      orgname: "",
      // 已完成明细请求序号,避免快速切换筛选时旧请求覆盖新结果
      completedRequestSeq: 0,
      // 问题详情请求序号
      questionRequestSeq: 0,
      // 选中的行
      ids: [],
@@ -522,12 +534,21 @@
    isInHospitalSatisfaction() {
      return this.satisfactionScope !== "leave";
    },
    // patItem 接口的 serviceType 只接收单个值,多选时取第一个
    currentServiceType() {
      const value = this.queryParams.serviceType;
      const first = Array.isArray(value) ? value[0] : value;
      return first || 6;
    },
  },
  created() {
    this.orgname = localStorage.getItem("orgname");
    this.initData();
    // 顶部患者表单 / 问题详情分析 tab:默认按当前统计口径加载一次
    this.maybeRefreshCompletedGroups();
    this.maybeRefreshQuestionDetails();
  },
  methods: {
@@ -595,41 +616,41 @@
          // 病区统计
          let wardCodes = params.leavehospitaldistrictcodes || [];
          if (wardCodes.includes("all")) {
            // 如果选择了"全部",则使用当前用户所属的全部病区
            wardCodes = (this.$store.getters.belongWards || []).map(
              (ward) => ward.districtCode
            );
            // 选择"全部"时不加病区限制,查全部病区
            wardCodes = [];
          }
          delete params.leavehospitaldistrictcodes;
          delete params.hospitaldistrictcodes;
          delete params.deptcodes;
          delete params.leaveldeptcodes;
          delete params.hospitaldeptcodes;
          if (this.isInHospitalSatisfaction) {
            params.hospitaldistrictcodes = wardCodes;
          } else {
            params.leavehospitaldistrictcodes = wardCodes;
          if (wardCodes.length) {
            if (this.isInHospitalSatisfaction) {
              params.hospitaldistrictcodes = wardCodes;
            } else {
              params.leavehospitaldistrictcodes = wardCodes;
            }
          }
        } else if (params.statisticaltype == 2) {
          // 科室统计
          let deptCodes = params.deptcodes || [];
          if (deptCodes.includes("all")) {
            // 如果选择了"全部",则移除"all"值
            // 选择"全部"时不加科室限制,查全部科室
            deptCodes = [];
          } else {
            deptCodes = deptCodes.filter((item) => item !== "all");
            // 如果需要传所有科室代码,可以从store中获取
            deptCodes = (this.$store.getters.belongDepts || []).map(
              (dept) => dept.deptCode
            );
          }
          delete params.deptcodes;
          delete params.leaveldeptcodes;
          delete params.hospitaldeptcodes;
          delete params.leavehospitaldistrictcodes;
          delete params.hospitaldistrictcodes;
          if (this.isInHospitalSatisfaction) {
            params.hospitaldeptcodes = deptCodes;
          } else {
            params.leaveldeptcodes = deptCodes;
          if (deptCodes.length) {
            if (this.isInHospitalSatisfaction) {
              params.hospitaldeptcodes = deptCodes;
            } else {
              params.leaveldeptcodes = deptCodes;
            }
          }
        }
@@ -843,18 +864,371 @@
      }
      this.queryParams.pageNum = 1;
      this.getList();
      this.maybeRefreshCompletedGroups();
      this.maybeRefreshQuestionDetails();
    },
    // 处理查询
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.maybeRefreshCompletedGroups();
      this.maybeRefreshQuestionDetails();
    },
    // 切换满意度类型(离院/在院)后重新查询
    handleSatisfactionScopeChange() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.maybeRefreshCompletedGroups();
      this.maybeRefreshQuestionDetails();
    },
    /**
     * 顶部「已完成患者明细」tab 数据
     * 维度沿用下方「统计类型」:病区统计 -> 按病区分组;科室统计 -> 按科室分组
     * 时间沿用下方「随访时间」,状态固定为已完成
     */
    maybeRefreshCompletedGroups() {
      if (this.showAnalysisTabs) {
        this.refreshCompletedGroups();
      }
    },
    async refreshCompletedGroups() {
      const seq = ++this.completedRequestSeq;
      const payload = this.buildCompletedPayload();
      // 没有科室/病区权限时不发起请求,避免把全量数据查出来
      if (!payload) {
        this.emitCompletedGroups([], seq);
        return;
      }
      this.$emit("completed-rows-change", {
        ...this.buildCompletedMeta(),
        loading: true,
      });
      try {
        const res = await getTaskservelist(payload);
        if (seq !== this.completedRequestSeq) return;
        const rows = this.pickCompletedRows(res);
        this.emitCompletedGroups(rows, seq);
      } catch (error) {
        if (seq !== this.completedRequestSeq) return;
        console.error("获取已完成患者明细失败:", error);
        this.emitCompletedGroups([], seq);
      }
    },
    // 组装第一张「满意度状态查询」页同款查询条件
    buildCompletedPayload() {
      const scope = this.buildCompletedScope();
      if (!scope.all && !scope.depts.length && !scope.wards.length) {
        return null;
      }
      const payload = {
        pageNum: null,
        pageSize: null,
        sendstate: null,
        sendstateView: 2, // 2 = 已完成
        sort: 2,
        serviceType: this.currentServiceType,
        searchscope: scope.searchscope,
        visitCount: 1,
        scopetype: [],
        // 与第一张图一致:选了具体科室/病区时只传对应维度;「全部」时两个维度都传
        deptOrDistrict: scope.deptOrDistrict,
      };
      // 在院满意度用 hospital*,离院满意度用 leave*
      if (this.satisfactionScope === "hospital") {
        if (scope.depts.length) {
          payload.deptcodes = scope.depts;
        }
        if (scope.wards.length) {
          payload.hospitaldistrictcodes = scope.wards;
        }
      } else {
        if (scope.depts.length) {
          payload.leaveldeptcodes = scope.depts;
        }
        if (scope.wards.length) {
          payload.leavehospitaldistrictcodes = scope.wards;
        }
      }
      const range = this.queryParams.dateRange || [];
      if (range.length === 2 && range[0] && range[1]) {
        payload.finishtimeStart = range[0];
        payload.finishtimeEnd = range[1];
      }
      return payload;
    },
    // 组装查询范围:统计类型决定按病区还是科室过滤
    buildCompletedScope() {
      const allDepts = (this.$store.getters.belongDepts || [])
        .map((dept) => dept.deptCode)
        .filter(Boolean);
      const allWards = (this.$store.getters.belongWards || [])
        .map((ward) => ward.districtCode)
        .filter(Boolean);
      // 病区统计(含统计类型为空的情况)
      if (this.queryParams.statisticaltype != 2) {
        const rawSelected = this.queryParams.leavehospitaldistrictcodes || [];
        const selected = rawSelected.filter(
          (code) => code && code !== "all"
        );
        const useAll = !rawSelected.length || rawSelected.includes("all");
        if (!useAll && selected.length) {
          return {
            depts: [],
            wards: selected,
            deptOrDistrict: 1,
            searchscope: 2,
          };
        }
        // 全部:不传病区/科室编码,由后端返回当前机构全部数据
        return {
          depts: [],
          wards: [],
          deptOrDistrict: 2,
          searchscope: 3,
          all: true,
        };
      }
      // 科室统计
      const rawSelected = this.queryParams.deptcodes || [];
      const selected = rawSelected.filter((code) => code && code !== "all");
      const useAll = !rawSelected.length || rawSelected.includes("all");
      if (!useAll && selected.length) {
        return {
          depts: selected,
          wards: [],
          deptOrDistrict: 1,
          searchscope: 1,
        };
      }
      return {
        depts: [],
        wards: [],
        deptOrDistrict: 2,
        searchscope: 3,
        all: true,
      };
    },
    // 问题详情占比 / 图表 / 饼图 共用数据
    maybeRefreshQuestionDetails() {
      if (this.showAnalysisTabs) {
        this.refreshQuestionDetails();
      }
    },
    async refreshQuestionDetails() {
      const seq = ++this.questionRequestSeq;
      const payload = this.buildQuestionPayload();
      if (!payload) {
        this.$emit("question-details-change", { loading: false, list: [] });
        return;
      }
      this.$emit("question-details-change", { loading: true });
      try {
        const res = await getSfStatisticsJoyInfo(payload);
        if (seq !== this.questionRequestSeq) return;
        this.$emit("question-details-change", {
          loading: false,
          list: res.data || [],
        });
      } catch (error) {
        if (seq !== this.questionRequestSeq) return;
        console.error("获取问题详情占比失败:", error);
        this.$emit("question-details-change", { loading: false, list: [] });
      }
    },
    buildQuestionPayload() {
      const scope = this.buildCompletedScope();
      if (!scope.all && !scope.depts.length && !scope.wards.length) {
        return null;
      }
      const params = {
        configKey: "joyCount",
        statisticaltype: this.queryParams.statisticaltype,
        serviceType: this.queryParams.serviceType,
        searchscope: scope.searchscope,
        pageNum: 1,
        pageSize: 999,
      };
      const range = this.queryParams.dateRange || [];
      if (range.length === 2 && range[0] && range[1]) {
        params.startTime = range[0];
        params.endTime = range[1];
      }
      if (this.queryParams.statisticaltype == 2) {
        if (scope.depts.length) {
          if (this.isInHospitalSatisfaction) {
            params.hospitaldeptcodes = scope.depts;
          } else {
            params.leaveldeptcodes = scope.depts;
          }
        }
      } else if (scope.wards.length) {
        if (this.isInHospitalSatisfaction) {
          params.hospitaldistrictcodes = scope.wards;
        } else {
          params.leavehospitaldistrictcodes = scope.wards;
        }
      }
      return params;
    },
    // 兼容接口不同的返回层级
    pickCompletedRows(res) {
      if (!res) return [];
      const rowsWrap = res.rows || (res.data && res.data.rows);
      let list = [];
      if (Array.isArray(rowsWrap) && rowsWrap[0]) {
        list =
          rowsWrap[0].serviceSubtaskList ||
          (rowsWrap[0].sendname ? rowsWrap : []);
      } else if (res.data && Array.isArray(res.data.serviceSubtaskList)) {
        list = res.data.serviceSubtaskList;
      } else if (Array.isArray(res.data)) {
        list = res.data;
      }
      return (list || [])
        .filter(Boolean)
        .map((row) => this.normalizeCompletedRow(row));
    },
    normalizeCompletedRow(row) {
      const item = { ...row };
      if (item.endtime) {
        item.endDay = this.daysBetween(item.endtime);
      }
      return item;
    },
    emitCompletedGroups(rows, seq) {
      if (seq !== this.completedRequestSeq) return;
      this.$emit("completed-rows-change", {
        ...this.buildCompletedMeta(),
        loading: false,
        total: rows.length,
        rows,
      });
    },
    buildCompletedMeta() {
      return {
        dimension:
          this.queryParams.statisticaltype == 2 ? "dept" : "ward",
        scope: this.satisfactionScope,
        dateRange: [...(this.queryParams.dateRange || [])],
      };
    },
    // 按当前统计类型分组:病区统计 -> 病区,科室统计 -> 科室
    groupCompletedRows(rows) {
      const dimension =
        this.queryParams.statisticaltype == 2 ? "dept" : "ward";
      const groupMap = new Map();
      (rows || []).forEach((row) => {
        const name =
          dimension === "ward" ? this.wardNameOf(row) : this.deptNameOf(row);
        const code =
          dimension === "ward"
            ? this.wardCodeOf(row)
            : this.deptCodeOf(row);
        const key = String(code || name || "unknown");
        if (!groupMap.has(key)) {
          groupMap.set(key, {
            key,
            label:
              name || (dimension === "ward" ? "未分病区" : "未分科室"),
            rows: [],
          });
        }
        groupMap.get(key).rows.push(row);
      });
      const orderMap = this.buildGroupOrder(dimension);
      return Array.from(groupMap.values()).sort((a, b) => {
        const aIndex = orderMap.has(a.key) ? orderMap.get(a.key) : 99999;
        const bIndex = orderMap.has(b.key) ? orderMap.get(b.key) : 99999;
        if (aIndex !== bIndex) return aIndex - bIndex;
        return a.label.localeCompare(b.label, "zh-Hans-CN");
      });
    },
    // tab 顺序按页面左侧科室/病区权限列表的顺序,保证「一病区、二病区…」的自然顺序
    buildGroupOrder(dimension) {
      const list =
        dimension === "ward"
          ? this.$store.getters.belongWards
          : this.$store.getters.belongDepts;
      const orderMap = new Map();
      (list || []).forEach((item, index) => {
        const code =
          dimension === "ward" ? item.districtCode : item.deptCode;
        if (code) orderMap.set(String(code), index);
      });
      return orderMap;
    },
    wardNameOf(row) {
      if (!row) return "";
      const name =
        this.satisfactionScope === "hospital"
          ? row.hospitaldistrictname || row.leavehospitaldistrictname
          : row.leavehospitaldistrictname || row.hospitaldistrictname;
      return name === "-" ? "" : name;
    },
    wardCodeOf(row) {
      if (!row) return "";
      return this.satisfactionScope === "hospital"
        ? row.hospitaldistrictcode || row.leavehospitaldistrictcode
        : row.leavehospitaldistrictcode || row.hospitaldistrictcode;
    },
    deptNameOf(row) {
      if (!row) return "";
      const name =
        this.satisfactionScope === "hospital"
          ? row.deptname || row.leaveldeptname
          : row.leaveldeptname || row.deptname;
      return name === "-" ? "" : name;
    },
    deptCodeOf(row) {
      if (!row) return "";
      return this.satisfactionScope === "hospital"
        ? row.hospitaldeptcode || row.deptcode
        : row.deptcode || row.hospitaldeptcode;
    },
    // 重置查询(恢复默认:离院满意度 + 服务类型 6)
@@ -870,6 +1244,8 @@
        pageSize: 20,
      };
      this.getList();
      this.maybeRefreshCompletedGroups();
      this.maybeRefreshQuestionDetails();
    },
    // 处理分页大小变化