// 员工管理API
|
|
import { API_CONFIG, graphqlRequest } from '@/config/api'
|
|
// 员工相关的 GraphQL 查询和变更
|
export const EMPLOYEE_QUERIES = {
|
// 获取所有员工
|
GET_EMPLOYEES: `
|
query GetEmployees {
|
employees {
|
id
|
name
|
phone
|
roleId
|
description
|
createTime
|
updateTime
|
}
|
}
|
`,
|
|
// 搜索员工
|
SEARCH_EMPLOYEES: `
|
query SearchEmployees($name: String) {
|
employeesByName(name: $name) {
|
id
|
name
|
phone
|
roleId
|
description
|
createTime
|
updateTime
|
}
|
}
|
`,
|
|
// 获取员工详情
|
GET_EMPLOYEE: `
|
query GetEmployee($id: Long!) {
|
employee(id: $id) {
|
id
|
name
|
phone
|
roleId
|
description
|
createTime
|
updateTime
|
}
|
}
|
`
|
}
|
|
export const EMPLOYEE_MUTATIONS = {
|
// 保存员工(新增或更新)
|
SAVE_EMPLOYEE: `
|
mutation SaveEmployee($input: EmployeeInput!) {
|
saveEmployee(input: $input) {
|
id
|
name
|
phone
|
roleId
|
description
|
createTime
|
updateTime
|
}
|
}
|
`,
|
|
// 删除员工
|
DELETE_EMPLOYEE: `
|
mutation DeleteEmployee($id: Long!) {
|
deleteEmployee(id: $id)
|
}
|
`
|
}
|
|
// 类型定义
|
export interface Employee {
|
id: string
|
name: string
|
phone: string
|
roleId: string
|
description?: string
|
createTime?: string
|
updateTime?: string
|
}
|
|
export interface EmployeeInput {
|
id?: string
|
name: string
|
phone: string
|
password?: string
|
roleId: string
|
description?: string
|
}
|
|
// API 函数
|
export const employeeApi = {
|
// 获取员工列表
|
async getEmployees(): Promise<Employee[]> {
|
try {
|
const result = await graphqlRequest(EMPLOYEE_QUERIES.GET_EMPLOYEES)
|
return result?.data?.employees || []
|
} catch (error: any) {
|
throw new Error(error.message || '获取员工列表失败')
|
}
|
},
|
|
// 搜索员工
|
async searchEmployees(name: string): Promise<Employee[]> {
|
try {
|
const result = await graphqlRequest(EMPLOYEE_QUERIES.SEARCH_EMPLOYEES, { name })
|
return result?.data?.employeesByName || []
|
} catch (error: any) {
|
throw new Error(error.message || '搜索员工失败')
|
}
|
},
|
|
// 根据ID获取员工详情
|
async getEmployee(id: string): Promise<Employee | null> {
|
try {
|
const result = await graphqlRequest(EMPLOYEE_QUERIES.GET_EMPLOYEE, { id })
|
return result?.data?.employee || null
|
} catch (error: any) {
|
throw new Error(error.message || '获取员工详情失败')
|
}
|
},
|
|
// 保存员工(新增或更新)
|
async saveEmployee(employee: EmployeeInput): Promise<Employee> {
|
try {
|
const result = await graphqlRequest(EMPLOYEE_MUTATIONS.SAVE_EMPLOYEE, { input: employee })
|
return result?.data?.saveEmployee
|
} catch (error: any) {
|
throw new Error(error.message || '保存员工失败')
|
}
|
},
|
|
// 删除员工
|
async deleteEmployee(id: string): Promise<boolean> {
|
try {
|
const result = await graphqlRequest(EMPLOYEE_MUTATIONS.DELETE_EMPLOYEE, { id })
|
return result?.data?.deleteEmployee || false
|
} catch (error: any) {
|
throw new Error(error.message || '删除员工失败')
|
}
|
}
|
}
|