yearning
18 小时以前 e3e6db2d9974588f91ac1567a58fa4846655ce1b
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
211
212
213
214
215
216
217
218
219
220
221
222
223
import { useEffect, useRef, useState } from "react";
import { Button, Card, Form, Input, message, Select, Table } from "antd";
import { useNavigate, useSearchParams } from "react-router-dom";
import { api } from "../api";
import {
  attemptDetailPath,
  readScoreListQuery,
  writeScoreListQuery,
  type ScoreListQuery,
} from "../scoreQueryParams";
import { useSystemSettings } from "../systemSettings";
import type { UserMe } from "../App";
import "./ScoreQueryPage.css";
 
type ScoreRow = {
  examinee_id: string;
  student_no: string;
  name: string | null;
  id_card: string | null;
  subject_name: string;
  score: number;
  attempt_id: string;
  start_time?: string | null;
  end_time?: string | null;
};
 
export default function ScoreQueryPage() {
  const nav = useNavigate();
  const [searchParams, setSearchParams] = useSearchParams();
  const query = readScoreListQuery(searchParams);
  const [form] = Form.useForm<ScoreListQuery>();
  const tableWrapRef = useRef<HTMLDivElement>(null);
  const [subjects, setSubjects] = useState<string[]>([]);
  const [data, setData] = useState<ScoreRow[]>([]);
  const [total, setTotal] = useState(0);
  const [page, setPage] = useState(query.page ?? 1);
  const [pageSize, setPageSize] = useState(query.page_size ?? 20);
  const [filters, setFilters] = useState<Pick<ScoreListQuery, "name" | "id_card" | "subject">>({
    name: query.name,
    id_card: query.id_card,
    subject: query.subject,
  });
  const [tableScrollY, setTableScrollY] = useState(320);
  const [user, setUser] = useState<UserMe | null>(null);
  const { settings } = useSystemSettings();
 
  const hidePrivacy =
    user?.role === "grader" && !settings.grader_can_see_privacy;
 
  useEffect(() => {
    api
      .get<UserMe>("/api/auth/me")
      .then((r) => setUser(r.data))
      .catch(() => setUser(null));
  }, []);
 
  useEffect(() => {
    api.get<{ items: string[] }>("/api/subjects").then((r) => setSubjects(r.data.items));
  }, []);
 
  useEffect(() => {
    const q = readScoreListQuery(searchParams);
    setPage(q.page ?? 1);
    setPageSize(q.page_size ?? 20);
    setFilters({ name: q.name, id_card: q.id_card, subject: q.subject });
    form.setFieldsValue({
      name: q.name,
      id_card: q.id_card,
      subject: q.subject,
    });
  }, [searchParams, form]);
 
  const load = async () => {
    const params: Record<string, string | number> = { page, page_size: pageSize };
    if (!hidePrivacy) {
      if (filters.name) params.name = filters.name;
      if (filters.id_card) params.id_card = filters.id_card;
    }
    if (filters.subject) params.subject = filters.subject;
    const { data: res } = await api.get("/api/scores", { params });
    setData(res.items);
    setTotal(res.total);
  };
 
  useEffect(() => {
    load();
  }, [page, pageSize, filters, hidePrivacy]);
 
  useEffect(() => {
    const el = tableWrapRef.current;
    if (!el) return;
    const update = () => {
      const h = el.clientHeight;
      setTableScrollY(Math.max(120, h - 56 - 52));
    };
    update();
    const ro = new ResizeObserver(update);
    ro.observe(el);
    return () => ro.disconnect();
  }, []);
 
  const listQuery = (): ScoreListQuery => ({
    ...filters,
    page,
    page_size: pageSize,
  });
 
  const pushQuery = (q: ScoreListQuery) => {
    setSearchParams(writeScoreListQuery(q), { replace: true });
  };
 
  const [studentNo, setStudentNo] = useState("");
  const handleLookup = async () => {
    if (!studentNo.trim()) return;
    try {
      const { data } = await api.get<{ attempt_id: string }>(
        `/api/attempts/lookup/${encodeURIComponent(studentNo.trim().toUpperCase())}`
      );
      nav(attemptDetailPath(data.attempt_id, listQuery()));
    } catch {
      message.error("考生编号不存在");
    }
  };
 
  const columns = [
    {
      title: "序号",
      width: 64,
      fixed: "left" as const,
      render: (_: unknown, __: unknown, index: number) => (page - 1) * pageSize + index + 1,
    },
    { title: "考生编号", dataIndex: "student_no", width: 180, fixed: "left" as const },
    ...(hidePrivacy
      ? []
      : [
          { title: "姓名", dataIndex: "name", width: 100 },
          { title: "证件号", dataIndex: "id_card", width: 180 },
        ]),
    { title: "科目", dataIndex: "subject_name", width: 200, ellipsis: true },
    { title: "开始考试时间", dataIndex: "start_time", width: 170, render: (v: string | null) => v || "—" },
    { title: "结束考试时间", dataIndex: "end_time", width: 170, render: (v: string | null) => v || "—" },
    { title: "成绩", dataIndex: "score", width: 80 },
    {
      title: "操作",
      width: 80,
      fixed: "right" as const,
      render: (_: unknown, row: ScoreRow) => (
        <Button
          type="link"
          onClick={() => nav(attemptDetailPath(row.attempt_id, listQuery()))}
        >
          查看
        </Button>
      ),
    },
  ];
 
  return (
    <div className="score-query-page">
      <Card bordered className="score-query-page__card">
        <Form
          form={form}
          layout="inline"
          className="score-query-page__form"
          onFinish={(v) => {
            pushQuery({ ...v, page: 1, page_size: pageSize });
          }}
        >
          {!hidePrivacy && (
            <>
              <Form.Item name="name" label="姓名">
                <Input allowClear style={{ width: 120 }} />
              </Form.Item>
              <Form.Item name="id_card" label="证件号">
                <Input allowClear style={{ width: 180 }} />
              </Form.Item>
            </>
          )}
          <Form.Item name="subject" label="科目">
            <Select
              allowClear
              style={{ width: 220 }}
              options={subjects.map((s) => ({ value: s, label: s }))}
            />
          </Form.Item>
          <Form.Item>
            <Button type="primary" htmlType="submit">查询</Button>
          </Form.Item>
        </Form>
        <div style={{ marginBottom: 12, display: "flex", gap: 8, alignItems: "center" }}>
          <Input
            placeholder="输入考生编号直接查看答卷"
            value={studentNo}
            onChange={(e) => setStudentNo(e.target.value.toUpperCase())}
            onPressEnter={handleLookup}
            style={{ width: 200, fontFamily: "monospace" }}
            maxLength={10}
          />
          <Button onClick={handleLookup} disabled={!studentNo.trim()}>查看答卷</Button>
        </div>
        <div ref={tableWrapRef} className="score-query-page__table-wrap">
          <Table
            rowKey="attempt_id"
            scroll={{ x: hidePrivacy ? 800 : 1100, y: tableScrollY }}
            dataSource={data}
            pagination={{
              current: page,
              pageSize,
              total,
              showSizeChanger: true,
              showTotal: (t) => `共 ${t} 条`,
              position: ["bottomRight"],
              onChange: (p, ps) => {
                pushQuery({ ...filters, page: p, page_size: ps });
              },
            }}
            columns={columns}
          />
        </div>
      </Card>
    </div>
  );
}