lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
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
const GRAPHQL_ENDPOINT = 'http://localhost:8080/api/graphql'
 
// GraphQL请求函数
async function graphqlRequest(query, variables = {}) {
  const response = await fetch(GRAPHQL_ENDPOINT, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      query,
      variables,
    }),
  })
 
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`)
  }
 
  const result = await response.json()
  
  if (result.errors) {
    throw new Error(result.errors[0].message)
  }
 
  return result.data
}
 
// 分页查询评分模板列表
export const getRatingSchemes = async (page = 0, size = 10, name = '') => {
  const query = `
    query GetRatingSchemes($page: Int, $size: Int, $name: String) {
      ratingSchemes(page: $page, size: $size, name: $name) {
        content {
          id
          name
          description
          totalScore
          createTime
          updateTime
        }
        totalElements
        totalPages
        number
        size
        first
        last
      }
    }
  `
  
  const variables = { page, size }
  if (name) {
    variables.name = name
  }
  
  const data = await graphqlRequest(query, variables)
  return data.ratingSchemes
}
 
// 根据ID获取评分模板详情
export const getRatingScheme = async (id) => {
  const query = `
    query GetRatingScheme($id: ID!) {
      ratingScheme(id: $id) {
        id
        name
        description
        totalScore
        items {
          id
          name
          maxScore
          orderNo
        }
        createTime
        updateTime
      }
    }
  `
  
  const data = await graphqlRequest(query, { id })
  return data.ratingScheme
}
 
// 获取所有评分模板(用于下拉选择)
export const getAllRatingSchemes = async () => {
  const query = `
    query GetAllRatingSchemes {
      allRatingSchemes {
        id
        name
        description
        totalScore
      }
    }
  `
  
  const data = await graphqlRequest(query)
  return data.allRatingSchemes
}
 
// 保存评分模板
export const saveRatingScheme = async (input) => {
  const mutation = `
    mutation SaveRatingScheme($input: RatingSchemeInput!) {
      saveRatingScheme(input: $input) {
        id
        name
        description
        totalScore
        items {
          id
          name
          maxScore
          orderNo
        }
        createTime
        updateTime
      }
    }
  `
  
  const data = await graphqlRequest(mutation, { input })
  return data.saveRatingScheme
}
 
// 删除评分模板
export const deleteRatingScheme = async (id) => {
  const mutation = `
    mutation DeleteRatingScheme($id: ID!) {
      deleteRatingScheme(id: $id)
    }
  `
  
  const data = await graphqlRequest(mutation, { id })
  return data.deleteRatingScheme
}