155 lines
4.6 KiB
JavaScript
155 lines
4.6 KiB
JavaScript
import { hiprint } from "vue-plugin-hiprint";
|
|
|
|
const templateMap = {};
|
|
|
|
/**
|
|
* 创建打印模板
|
|
* @param {string} key - 模板唯一标识
|
|
* @param {object} options - 模板配置
|
|
* @returns {object} 打印模板对象
|
|
*/
|
|
export function newHiprintPrintTemplate(key, options) {
|
|
let template = new hiprint.PrintTemplate(options);
|
|
// 添加表头样式
|
|
template.on('beforePrint', (data) => {
|
|
const table = document.querySelector('.hiprint-printPanel table');
|
|
if (table) {
|
|
const ths = table.querySelectorAll('th');
|
|
ths.forEach(th => {
|
|
th.style.fontWeight = 'bold';
|
|
th.style.fontSize = '12px';
|
|
});
|
|
}
|
|
});
|
|
templateMap[key] = template;
|
|
return template;
|
|
}
|
|
|
|
/**
|
|
* 获取打印模板
|
|
* @param {string} key - 模板唯一标识
|
|
* @returns {object} 打印模板对象
|
|
*/
|
|
export function getHiprintPrintTemplate(key) {
|
|
return templateMap[key];
|
|
}
|
|
|
|
/**
|
|
* 准备打印数据
|
|
* @param {Array} data - 原始数据
|
|
* @param {Array} timeRange - 时间范围
|
|
* @returns {object} 格式化后的打印数据
|
|
*/
|
|
export function preparePrintData(data, timeRange) {
|
|
// 防错处理
|
|
if (!data || !Array.isArray(data)) {
|
|
console.error('打印数据格式错误:', data);
|
|
return {
|
|
table: [],
|
|
dateRange: timeRange ? `${timeRange[0]} 至 ${timeRange[1]}` : '',
|
|
printDate: formatDate(new Date(), 'YYYY-MM-DD'),
|
|
printUser: '管理员',
|
|
title: '体检工作量统计表'
|
|
};
|
|
}
|
|
|
|
try {
|
|
// 将树形结构展平为表格结构
|
|
const flattenedData = [];
|
|
|
|
// 用于计算合计的变量
|
|
let totalOldmanflag = 0;
|
|
let totalHtnflag = 0;
|
|
let totalDiaflag = 0;
|
|
let totalSmiflag = 0;
|
|
let totalPulflag = 0;
|
|
let totalSum = 0;
|
|
|
|
// 处理每个卫生院及其下属的行政村
|
|
data.forEach(org => {
|
|
if (!org || typeof org !== 'object') return; // 跳过无效数据
|
|
|
|
// 确保orgname存在
|
|
const orgname = org.orgname || '未知卫生院';
|
|
|
|
// 添加该卫生院下的所有行政村
|
|
if (org.children && Array.isArray(org.children) && org.children.length > 0) {
|
|
// 先对子项进行排序,保持数据一致性
|
|
const sortedChildren = [...org.children]
|
|
.filter(village => village && typeof village === 'object') // 过滤无效数据
|
|
.sort((a, b) => (b.sum || 0) - (a.sum || 0));
|
|
|
|
sortedChildren.forEach((village, index) => {
|
|
// 累加各项数据
|
|
totalOldmanflag += village.oldmanflag || 0;
|
|
totalHtnflag += village.htnflag || 0;
|
|
totalDiaflag += village.diaflag || 0;
|
|
totalSmiflag += village.smiflag || 0;
|
|
totalPulflag += village.pulflag || 0;
|
|
totalSum += village.sum || 0;
|
|
|
|
flattenedData.push({
|
|
orgname: index === 0 ? orgname : '', // 只在第一行显示卫生院名称
|
|
districtname: village.districtname || '未知行政村',
|
|
oldmanflag: village.oldmanflag || 0,
|
|
htnflag: village.htnflag || 0,
|
|
diaflag: village.diaflag || 0,
|
|
smiflag: village.smiflag || 0,
|
|
pulflag: village.pulflag || 0,
|
|
sum: village.sum || 0,
|
|
isChild: true
|
|
});
|
|
});
|
|
}
|
|
});
|
|
|
|
// 添加合计行
|
|
flattenedData.push({
|
|
orgname: '合计',
|
|
districtname: '',
|
|
oldmanflag: totalOldmanflag,
|
|
htnflag: totalHtnflag,
|
|
diaflag: totalDiaflag,
|
|
smiflag: totalSmiflag,
|
|
pulflag: totalPulflag,
|
|
sum: totalSum,
|
|
isTotal: true
|
|
});
|
|
|
|
console.log('处理后的打印数据:', flattenedData);
|
|
|
|
return {
|
|
table: flattenedData,
|
|
dateRange: timeRange ? `${timeRange[0]} 至 ${timeRange[1]}` : '',
|
|
printDate: formatDate(new Date(), 'YYYY-MM-DD'),
|
|
printUser: '管理员',
|
|
title: '体检工作量统计表'
|
|
};
|
|
} catch (error) {
|
|
console.error('准备打印数据出错:', error);
|
|
return {
|
|
table: [],
|
|
dateRange: timeRange ? `${timeRange[0]} 至 ${timeRange[1]}` : '',
|
|
printDate: formatDate(new Date(), 'YYYY-MM-DD'),
|
|
printUser: '管理员',
|
|
title: '体检工作量统计表'
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 格式化日期
|
|
* @param {Date} date - 日期对象
|
|
* @param {string} format - 格式
|
|
* @returns {string} 格式化后的日期字符串
|
|
*/
|
|
function formatDate(date, format) {
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
|
|
return format
|
|
.replace('YYYY', year)
|
|
.replace('MM', month)
|
|
.replace('DD', day);
|
|
}
|