lrj
4 天以前 4fa9591629721797386fc11836e3a9deb69cd58c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// 员工管理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 response = await fetch('http://localhost:8080/api/graphql', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          query: EMPLOYEE_QUERIES.GET_EMPLOYEES
        })
      })
      const result = await response.json()
      if (result.errors) {
        throw new Error(result.errors[0].message)
      }
      return result.data?.employees || []
    } catch (error: any) {
      throw new Error(error.message || '获取员工列表失败')
    }
  },
 
  // 根据名称搜索员工
  async searchEmployees(name?: string): Promise<Employee[]> {
    try {
      const response = await fetch('http://localhost:8080/api/graphql', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          query: EMPLOYEE_QUERIES.SEARCH_EMPLOYEES,
          variables: { name }
        })
      })
      const result = await response.json()
      if (result.errors) {
        throw new Error(result.errors[0].message)
      }
      return result.data?.employeesByName || []
    } catch (error: any) {
      throw new Error(error.message || '搜索员工失败')
    }
  },
 
  // 获取员工详情
  async getEmployee(id: string): Promise<Employee | null> {
    try {
      const response = await fetch('http://localhost:8080/api/graphql', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          query: EMPLOYEE_QUERIES.GET_EMPLOYEE,
          variables: { id }
        })
      })
      const result = await response.json()
      if (result.errors) {
        throw new Error(result.errors[0].message)
      }
      return result.data?.employee || null
    } catch (error: any) {
      throw new Error(error.message || '获取员工详情失败')
    }
  },
 
  // 保存员工
  async saveEmployee(input: EmployeeInput): Promise<Employee> {
    try {
      const response = await fetch('http://localhost:8080/api/graphql', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          query: EMPLOYEE_MUTATIONS.SAVE_EMPLOYEE,
          variables: { input }
        })
      })
      const result = await response.json()
      if (result.errors) {
        throw new Error(result.errors[0].message)
      }
      return result.data?.saveEmployee
    } catch (error: any) {
      throw new Error(error.message || '保存员工失败')
    }
  },
 
  // 删除员工
  async deleteEmployee(id: string): Promise<boolean> {
    try {
      const response = await fetch('http://localhost:8080/api/graphql', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          query: EMPLOYEE_MUTATIONS.DELETE_EMPLOYEE,
          variables: { id }
        })
      })
      const result = await response.json()
      if (result.errors) {
        throw new Error(result.errors[0].message)
      }
      return result.data?.deleteEmployee || false
    } catch (error: any) {
      throw new Error(error.message || '删除员工失败')
    }
  }
}