This commit is contained in:
linfeng 2025-03-19 22:04:01 +08:00
parent e186c77f61
commit e290a9d51c
1 changed files with 66 additions and 14 deletions

View File

@ -10,6 +10,8 @@ import type { CascaderProps } from 'antd/es/cascader';
import { utils, writeFile } from 'xlsx';
import { Modal, Input, Button } from 'antd';
import { api } from '@nice/client';
import { StaffDto } from 'packages/common/dist';
import DepartmentSelect from '@web/src/components/models/department/department-select';
// 修改函数类型定义
@ -42,31 +44,60 @@ export default function StaffTable() {
const [fileNameVisible, setFileNameVisible] = useState(false);
const [fileName, setFileName] = useState('');
const [defaultFileName] = useState(`员工数据_${new Date().toISOString().slice(0, 10)}`);
const [exporting, setExporting] = useState(false);
const [selectedDepartment, setSelectedDepartment] = useState('');
const [tempExportData, setTempExportData] = useState<any | null>(null);
const [exportScope, setExportScope] = useState<'current' | 'department'>('current');
const handleExport = async () => {
setConfirmVisible(true);
};
const handleConfirm = async () => {
setConfirmVisible(false);
setExporting(true);
if (exportScope === 'current') {
setExporting(false);
try {
const allStaffs = await api.staff.findMany.useQuery({
where: {
deletedAt: null,
department: { name: selectedDepartment } // 添加部门过滤条件
},
include: { department: true }
});
setTempExportData(allStaffs);
} finally {
setExporting(false);
}
}
setFileNameVisible(true);
};
// 添加导出处理函数
const handleFileNameConfirm = () => {
setFileNameVisible(false)
if (!tempExportData) return;
if (!gridApi) return;
const finalFileName = fileName || defaultFileName;
const flattenColumns = (cols: any[]): any[] => {
return cols.flatMap(col =>
return cols.flatMap(col =>
col.children ? flattenColumns(col.children) : col
);
};
const allColDefs = flattenColumns(gridApi.getColumnDefs());
const rowData = gridApi.getRenderedNodes().map((node: any) => {
let rowData;
if (exportScope === 'current') {
rowData = gridApi.getDisplayedRowNodes().map((node: any) => node.data) || [];
} else {
rowData = tempExportData || [];
}
rowData.map((node: any) => {
const row: Record<string, any> = {};
allColDefs.forEach((colDef: any) => {
const field = colDef.field;
@ -74,7 +105,7 @@ export default function StaffTable() {
const value = node.data[field];
const formatter = colDef.valueFormatter;
const renderer = colDef.cellRenderer;
let renderedValue = value;
if (formatter) {
renderedValue = formatter({ value });
@ -90,14 +121,14 @@ export default function StaffTable() {
renderedValue = html.replace(/<br\s*\/?>/gi, '\n');
}
}
// 增强布尔值处理逻辑
if (typeof renderedValue === 'boolean' ||
(typeof renderedValue === 'string' && ['true','false'].includes(renderedValue.toLowerCase()))) {
if (typeof renderedValue === 'boolean' ||
(typeof renderedValue === 'string' && ['true', 'false'].includes(renderedValue.toLowerCase()))) {
const boolValue = typeof renderedValue === 'boolean' ? renderedValue : renderedValue.toLowerCase() === 'true';
renderedValue = boolValue ? '是' : '否';
}
row[colDef.headerName] = renderedValue;
}
});
@ -313,7 +344,7 @@ export default function StaffTable() {
}}
>
{!isLoading && (
<Button
<Button
onClick={handleExport}
className="mb-2 bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"
>
@ -329,7 +360,28 @@ export default function StaffTable() {
okText="确认"
cancelText="取消"
>
<p></p>
<div className="mb-4">
<span></span>
<select
value={exportScope}
onChange={(e) => setExportScope(e.target.value as any)}
className="ml-2 p-1 border rounded"
>
<option value="current"></option>
<option value="department"></option>
</select>
</div>
{exportScope === 'department' && (
<div>
<p></p>
<DepartmentSelect
value={selectedDepartment}
// onChange={(value) => setSelectedDepartment(value)}
placeholder="请选择部门"
/>
</div>
)}
</Modal>
<Modal
@ -340,7 +392,7 @@ export default function StaffTable() {
okText="导出"
cancelText="取消"
>
<Input
<Input
placeholder={`默认名称: ${defaultFileName}`}
value={fileName}
onChange={(e) => setFileName(e.target.value)}