生产管理系统 部门全页面开发完毕
This commit is contained in:
@@ -22,6 +22,7 @@ import {
|
||||
flattenDepartments,
|
||||
type DepartmentTreeState
|
||||
} from './components/departmentApi';
|
||||
import { deleteDepartment } from './components/departmentCreateApi';
|
||||
|
||||
// 部门管理状态管理
|
||||
interface DepartmentManagementState {
|
||||
@@ -135,8 +136,8 @@ export default function DepartmentManagementPage() {
|
||||
// 转换API数据为页面所需的格式
|
||||
const departments = transformDepartmentList(response.data);
|
||||
|
||||
// 转换为与现有页面兼容的数据格式
|
||||
const compatibleDepartments: Department[] = departments.map(dept => ({
|
||||
// 递归转换部门数据为与现有页面兼容的数据格式
|
||||
const transformDepartmentRecursive = (dept: any): Department => ({
|
||||
id: dept.id,
|
||||
name: dept.name,
|
||||
code: dept.code,
|
||||
@@ -150,22 +151,10 @@ export default function DepartmentManagementPage() {
|
||||
parentId: dept.parentId || undefined,
|
||||
createdAt: dept.createdAt,
|
||||
updatedAt: dept.updatedAt,
|
||||
children: dept.children.map(child => ({
|
||||
id: child.id,
|
||||
name: child.name,
|
||||
code: child.code,
|
||||
level: child.level + 1,
|
||||
manager: child.manager, // 从API的manager_name字段获取
|
||||
phone: child.phone, // 从API的manager_phone字段获取
|
||||
email: child.email, // 从API的manager_email字段获取
|
||||
description: child.description,
|
||||
sort: child.sortOrder,
|
||||
status: child.status as 'active' | 'inactive',
|
||||
parentId: child.parentId || undefined,
|
||||
createdAt: child.createdAt,
|
||||
updatedAt: child.updatedAt,
|
||||
})),
|
||||
}));
|
||||
children: dept.children ? dept.children.map(transformDepartmentRecursive) : [],
|
||||
});
|
||||
|
||||
const compatibleDepartments: Department[] = departments.map(transformDepartmentRecursive);
|
||||
|
||||
dispatch({ type: 'SET_DEPARTMENTS', payload: compatibleDepartments });
|
||||
// 默认展开所有一级部门
|
||||
@@ -334,29 +323,26 @@ export default function DepartmentManagementPage() {
|
||||
};
|
||||
|
||||
// 确认删除
|
||||
const confirmDelete = () => {
|
||||
const confirmDelete = async () => {
|
||||
if (!state.deletingDepartment) return;
|
||||
|
||||
const deleteFromTree = (items: Department[]): Department[] => {
|
||||
return items
|
||||
.filter(item => item.id !== state.deletingDepartment!.id)
|
||||
.map(item => {
|
||||
if (item.children) {
|
||||
return {
|
||||
...item,
|
||||
children: deleteFromTree(item.children),
|
||||
};
|
||||
}
|
||||
return item;
|
||||
});
|
||||
};
|
||||
try {
|
||||
// 调用删除API
|
||||
await deleteDepartment(state.deletingDepartment.id);
|
||||
|
||||
const updated = deleteFromTree(state.departments);
|
||||
dispatch({ type: 'SET_DEPARTMENTS', payload: updated });
|
||||
toast.success('部门删除成功');
|
||||
// 删除成功后,刷新部门列表
|
||||
await loadDepartments();
|
||||
|
||||
dispatch({ type: 'TOGGLE_DELETE_DIALOG', payload: false });
|
||||
dispatch({ type: 'SET_DELETING_DEPARTMENT', payload: null });
|
||||
toast.success('部门删除成功');
|
||||
|
||||
// 关闭删除对话框
|
||||
dispatch({ type: 'TOGGLE_DELETE_DIALOG', payload: false });
|
||||
dispatch({ type: 'SET_DELETING_DEPARTMENT', payload: null });
|
||||
} catch (error) {
|
||||
console.error('Failed to delete department:', error);
|
||||
const errorMessage = error instanceof Error ? error.message : '删除部门失败';
|
||||
toast.error(errorMessage);
|
||||
}
|
||||
};
|
||||
|
||||
// 拖拽功能
|
||||
|
||||
Reference in New Issue
Block a user