刘光辉
2 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<script lang="ts" setup>
import type { EChartsOption } from 'echarts';
 
import type { TrainStatsDetailRow, TrainStatsQuery } from '#/api/x/tms/trainStats';
 
import { computed, ref } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
import {
  Descriptions as ADescriptions,
  DescriptionsItem as ADescriptionsItem,
} from 'ant-design-vue';
import dayjs from 'dayjs';
 
import { getTrainStatsDetail } from '#/api/x/tms/trainStats';
import { TMS_BTN } from '#/views/x/tms/shared/ui';
import {
  labelOfCategory,
  labelOfEvalMode,
  loadRecordDics,
} from '#/views/x/tms/record/constants';
import HomeChart from '#/views/x/tms/home/HomeChart.vue';
 
const DT_FMT = 'YYYY-MM-DD HH:mm:ss';
 
defineOptions({ name: 'TmsTrainStatsDetailModal' });
 
defineEmits(['register']);
 
const { createMessage } = useMessage();
const loading = ref(false);
const rows = ref<TrainStatsDetailRow[]>([]);
const modalTitle = ref('统计明细');
/** 字典加载完成后再渲染表格标签,避免偶发回显编码 */
const dicReady = ref(0);
 
const columns = [
  { title: '编号', dataIndex: 'recordNo', key: 'recordNo', width: 140 },
  { title: '培训主题', dataIndex: 'subject', key: 'subject', ellipsis: true },
  { title: '培训分类', dataIndex: 'category', key: 'category', width: 120 },
  { title: '考核方式', dataIndex: 'evalMode', key: 'evalMode', width: 110 },
  { title: '开始时间', dataIndex: 'startTime', key: 'startTime', width: 170 },
  { title: '应到', dataIndex: 'personCount', key: 'personCount', width: 70, align: 'center' as const },
  { title: '签到', dataIndex: 'signedCount', key: 'signedCount', width: 70, align: 'center' as const },
  { title: '合格', dataIndex: 'passedCount', key: 'passedCount', width: 70, align: 'center' as const },
  { title: '签到率', dataIndex: 'signRate', key: 'signRate', width: 80, align: 'center' as const },
  { title: '合格率', dataIndex: 'passRate', key: 'passRate', width: 80, align: 'center' as const },
];
 
const summary = computed(() => {
  const list = rows.value;
  let person = 0;
  let signed = 0;
  let passed = 0;
  for (const r of list) {
    person += Number(r.personCount) || 0;
    signed += Number(r.signedCount) || 0;
    passed += Number(r.passedCount) || 0;
  }
  return {
    sessionCount: list.length,
    personCount: person,
    signedCount: signed,
    passedCount: passed,
    unsignedCount: Math.max(person - signed, 0),
    signRate: rateFloor(signed, person),
    passRate: rateFloor(passed, person),
  };
});
 
const showCharts = computed(() => !loading.value && rows.value.length > 0);
 
const axisLabelStyle = {
  interval: 0,
  fontSize: 11,
  lineHeight: 14,
  width: 72,
  overflow: 'break' as const,
};
 
/** 各场次人次柱状图 */
const barOption = computed<EChartsOption>(() => {
  const list = rows.value;
  const labels = list.map((r, i) => nameLabel(r, i));
  return {
    color: ['#1677ff', '#52c41a', '#faad14'],
    tooltip: {
      trigger: 'axis',
      axisPointer: { type: 'shadow' },
      formatter: (params: any) => formatAxisTooltip(params, list, false),
    },
    legend: { data: ['应到', '签到', '合格'], top: 0 },
    grid: { left: 24, right: 36, top: 40, bottom: 16, containLabel: true },
    xAxis: {
      type: 'category',
      data: labels,
      axisLabel: axisLabelStyle,
    },
    yAxis: {
      type: 'value',
      name: '人次',
      minInterval: 1,
      splitLine: { lineStyle: { type: 'dashed' } },
    },
    series: [
      { name: '应到', type: 'bar', barMaxWidth: 22, data: list.map((r) => Number(r.personCount) || 0) },
      { name: '签到', type: 'bar', barMaxWidth: 22, data: list.map((r) => Number(r.signedCount) || 0) },
      { name: '合格', type: 'bar', barMaxWidth: 22, data: list.map((r) => Number(r.passedCount) || 0) },
    ],
  };
});
 
/** 各场次签到率 / 合格率折线图 */
const lineOption = computed<EChartsOption>(() => {
  const list = rows.value;
  const labels = list.map((r, i) => nameLabel(r, i));
  return {
    color: ['#1677ff', '#eb2f96'],
    tooltip: {
      trigger: 'axis',
      formatter: (params: any) => formatAxisTooltip(params, list, true),
    },
    legend: { data: ['签到率', '合格率'], top: 0 },
    // 右侧加宽,避免末点与换行类目贴边被裁切
    grid: { left: 24, right: 48, top: 40, bottom: 16, containLabel: true },
    xAxis: {
      type: 'category',
      data: labels,
      // true:首尾类目留白,最右标签/折点不被盖住
      boundaryGap: true,
      axisLabel: {
        ...axisLabelStyle,
        align: 'center',
        margin: 10,
      },
    },
    yAxis: {
      type: 'value',
      name: '比率',
      min: 0,
      max: 100,
      axisLabel: { formatter: '{value}%' },
      splitLine: { lineStyle: { type: 'dashed' } },
    },
    series: [
      {
        name: '签到率',
        type: 'line',
        smooth: true,
        symbolSize: 7,
        data: list.map((r) => Number(r.signRate) || 0),
      },
      {
        name: '合格率',
        type: 'line',
        smooth: true,
        symbolSize: 7,
        data: list.map((r) => Number(r.passRate) || 0),
      },
    ],
  };
});
 
const [registerModal, { closeModal, changeLoading, setModalProps }] = useModalInner(
  async (data?: { title?: string; query?: TrainStatsQuery | null }) => {
    modalTitle.value = data?.title || '统计明细';
    setModalProps({ title: modalTitle.value });
    await loadDetail(data?.query);
  },
);
 
async function loadDetail(query?: TrainStatsQuery | null) {
  rows.value = [];
  if (!query?.groupBy || query.dimCode == null || query.dimCode === '') {
    createMessage.warning('缺少明细查询条件');
    return;
  }
  loading.value = true;
  changeLoading(true);
  try {
    await loadRecordDics();
    dicReady.value += 1;
    rows.value = (await getTrainStatsDetail(query)) || [];
  } catch (e: any) {
    rows.value = [];
    createMessage.error(e?.message || '加载明细失败');
  } finally {
    loading.value = false;
    changeLoading(false);
  }
}
 
/** 横轴培训主题:自动换行,完整名称在 tooltip */
function nameLabel(row: TrainStatsDetailRow, index: number) {
  const name = String(row.subject || row.recordNo || `场次${index + 1}`).trim();
  return wrapAxisName(name, 5, 3);
}
 
function wrapAxisName(text: string, width: number, maxLines: number) {
  const s = text.trim();
  if (s.length <= width) return s;
  const lines: string[] = [];
  for (let i = 0; i < s.length && lines.length < maxLines; i += width) {
    lines.push(s.slice(i, i + width));
  }
  if (s.length > width * maxLines) {
    const last = lines[lines.length - 1] || '';
    lines[lines.length - 1] = `${last.slice(0, Math.max(width - 1, 1))}…`;
  }
  return lines.join('\n');
}
 
function formatAxisTooltip(params: any, list: TrainStatsDetailRow[], isRate: boolean) {
  const arr = Array.isArray(params) ? params : [params];
  const idx = Number(arr[0]?.dataIndex ?? 0);
  const row = list[idx];
  const title = row?.subject || row?.recordNo || '-';
  const no = row?.recordNo ? `(${row.recordNo})` : '';
  const lines = arr.map((p: any) => {
    const unit = isRate ? '%' : '';
    return `${p?.marker || ''}${p?.seriesName}: ${p?.value ?? '-'}${unit}`;
  });
  return [`${title}${no}`, ...lines].join('<br/>');
}
 
function categoryText(v?: string) {
  void dicReady.value;
  return labelOfCategory(v);
}
 
function evalModeText(v?: string) {
  void dicReady.value;
  return labelOfEvalMode(v);
}
 
function rateFloor(num: number, den: number) {
  if (den <= 0) return 0;
  return Math.floor((num * 100) / den);
}
 
function rateText(v?: number | null) {
  if (v == null || Number.isNaN(Number(v))) return '-';
  return `${Number(v)}%`;
}
 
function formatDateTime(v?: string | null) {
  if (v == null || v === '') return '-';
  const d = dayjs(v);
  return d.isValid() ? d.format(DT_FMT) : String(v);
}
 
function handleClose() {
  closeModal();
}
</script>
 
<template>
  <BasicModal
    v-bind="$attrs"
    :title="modalTitle"
    width="1100px"
    destroy-on-close
    :show-ok-btn="false"
    :cancel-text="TMS_BTN.close"
    :can-fullscreen="true"
    @register="registerModal"
    @cancel="handleClose"
  >
    <a-divider orientation="left">汇总</a-divider>
    <ADescriptions :column="4" bordered size="small" class="tms-stats-desc">
      <ADescriptionsItem label="培训场次">{{ summary.sessionCount }}</ADescriptionsItem>
      <ADescriptionsItem label="应到人次">{{ summary.personCount }}</ADescriptionsItem>
      <ADescriptionsItem label="签到人次">{{ summary.signedCount }}</ADescriptionsItem>
      <ADescriptionsItem label="合格人次">{{ summary.passedCount }}</ADescriptionsItem>
      <ADescriptionsItem label="签到率">{{ summary.signRate }}%</ADescriptionsItem>
      <ADescriptionsItem label="合格率">{{ summary.passRate }}%</ADescriptionsItem>
      <ADescriptionsItem label="未签到" :span="2">{{ summary.unsignedCount }}</ADescriptionsItem>
    </ADescriptions>
 
    <template v-if="showCharts">
      <a-divider orientation="left">统计图</a-divider>
      <div class="tms-stats-charts">
        <div class="tms-stats-charts__panel">
          <div class="tms-stats-charts__title">各场次人次对比</div>
          <HomeChart :option="barOption" height="300px" />
        </div>
        <div class="tms-stats-charts__panel">
          <div class="tms-stats-charts__title">各场次签到率 / 合格率</div>
          <HomeChart :option="lineOption" height="300px" />
        </div>
      </div>
    </template>
 
    <a-divider orientation="left">场次明细</a-divider>
    <a-table
      size="small"
      bordered
      row-key="recordId"
      :loading="loading"
      :columns="columns"
      :data-source="rows"
      :pagination="rows.length > 10 ? { pageSize: 10, showSizeChanger: true } : false"
      :locale="{ emptyText: '该维度下暂无培训场次' }"
      :scroll="{ x: 960 }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'category'">
          {{ categoryText(record.category) }}
        </template>
        <template v-else-if="column.key === 'evalMode'">
          {{ evalModeText(record.evalMode) }}
        </template>
        <template v-else-if="column.key === 'startTime'">
          {{ formatDateTime(record.startTime) }}
        </template>
        <template v-else-if="column.key === 'signRate'">
          {{ rateText(record.signRate) }}
        </template>
        <template v-else-if="column.key === 'passRate'">
          {{ rateText(record.passRate) }}
        </template>
      </template>
    </a-table>
  </BasicModal>
</template>
 
<style scoped>
.tms-stats-desc {
  margin-bottom: 4px;
}
 
.tms-stats-charts {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}
 
.tms-stats-charts__panel {
  min-width: 0;
}
 
.tms-stats-charts__title {
  margin-bottom: 4px;
  color: rgba(0, 0, 0, 0.45);
  font-size: 13px;
}
</style>