lrj
2 天以前 93eb6b470773bc49ea6e1a9d4cbd914eb95d525b
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
// 员工管理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 data = await graphqlRequest(EMPLOYEE_QUERIES.GET_EMPLOYEES)
      return data?.employees || []
    } catch (error: any) {
      throw new Error(error.message || '获取员工列表失败')
    }
  },
 
  // 搜索员工
  async searchEmployees(keyword: string): Promise<Employee[]> {
    try {
      const data = await graphqlRequest(EMPLOYEE_QUERIES.SEARCH_EMPLOYEES, { keyword })
      return data?.searchEmployees || []
    } catch (error: any) {
      throw new Error(error.message || '搜索员工失败')
    }
  },
 
  // 根据ID获取员工详情
  async getEmployee(id: string): Promise<Employee | null> {
    try {
      const data = await graphqlRequest(EMPLOYEE_QUERIES.GET_EMPLOYEE, { id })
      return data?.employee || null
    } catch (error: any) {
      throw new Error(error.message || '获取员工详情失败')
    }
  },
 
  // 保存员工(新增或更新)
  async saveEmployee(employee: EmployeeInput): Promise<Employee> {
    try {
      const data = await graphqlRequest(EMPLOYEE_MUTATIONS.SAVE_EMPLOYEE, { input: employee })
      return data?.saveEmployee
    } catch (error: any) {
      throw new Error(error.message || '保存员工失败')
    }
  },
 
  // 删除员工
  async deleteEmployee(id: string): Promise<boolean> {
    try {
      const data = await graphqlRequest(EMPLOYEE_MUTATIONS.DELETE_EMPLOYEE, { id })
      return data?.deleteEmployee || false
    } catch (error: any) {
      throw new Error(error.message || '删除员工失败')
    }
  }
}