feat(grades): gpa formula system - linear per-band gp (start~top), course category weights (editable), formula display, seed aligned to school standard
This commit is contained in:
+5
-1
@@ -123,7 +123,7 @@ export const gradesOverview = () => call<GradesOverview>('grades_overview');
|
||||
export const semesterUpsert = (p: { id?: number; term: string; gpa?: number | null; rank?: number | null; total_students?: number | null }) =>
|
||||
call<{ item: { id: number } }>('semester_upsert', p);
|
||||
export const semesterDelete = (id: number) => call<{ ok: true }>('semester_delete', { id });
|
||||
export const courseUpsert = (p: { id?: number; semester_id: number; course_name: string; credit: number; score?: number | null; grade_point?: number | null }) =>
|
||||
export const courseUpsert = (p: { id?: number; semester_id: number; course_name: string; credit: number; score?: number | null; grade_point?: number | null; category?: string | null }) =>
|
||||
call<{ item: { id: number } }>('course_upsert', p);
|
||||
export const courseDelete = (id: number) => call<{ ok: true }>('course_delete', { id });
|
||||
export const bandUpsert = (p: GradeBand) => call<{ item: { id: number } }>('band_upsert', p);
|
||||
@@ -218,6 +218,10 @@ export const adminDeleteUser = (userId: number | string, baseUrl?: string) =>
|
||||
export const adminDeleteInvite = (id: number, baseUrl?: string) =>
|
||||
adminCall<{ deleted: number }>('/admin/invites/delete', { id }, baseUrl);
|
||||
|
||||
// ---- 绩点公式设置 ----
|
||||
export const gpaSettingsGet = () => call<{ weights: Record<string, number> }>('gpa_settings_get', {});
|
||||
export const gpaSettingsUpsert = (weights: Record<string, number>) => call<{ ok: true; weights: Record<string, number> }>('gpa_settings_upsert', { weights });
|
||||
|
||||
// ---- 个人信息 / 简历 ----
|
||||
export const profileGet = () => call<{ content: { info?: Partial<ProfileInfo>; resume?: ResumeConfig } }>('profile_get', {});
|
||||
export const profileUpsert = (content: { info?: Partial<ProfileInfo>; resume?: ResumeConfig }) =>
|
||||
|
||||
@@ -196,6 +196,7 @@ export interface GradeBand {
|
||||
max: number;
|
||||
sort: number;
|
||||
gp?: number | null;
|
||||
gpTop?: number | null;
|
||||
}
|
||||
|
||||
export interface BandInfo { id: number; label: string; color: string; }
|
||||
@@ -207,6 +208,7 @@ export interface CourseGrade {
|
||||
credit: number;
|
||||
score?: number | null;
|
||||
grade_point?: number | null;
|
||||
category?: string | null;
|
||||
band?: BandInfo | null;
|
||||
}
|
||||
|
||||
@@ -230,6 +232,7 @@ export interface GradesOverview {
|
||||
gpa_trend: { term: string; gpa: number; avg: number | null }[];
|
||||
tier_dist: { label: string; color: string; count: number }[];
|
||||
};
|
||||
weights?: Record<string, number>;
|
||||
}
|
||||
|
||||
// ---------- 个人信息 ----------
|
||||
|
||||
@@ -5,9 +5,9 @@ import { BarChart, LineChart, PieChart } from 'echarts/charts';
|
||||
import { GridComponent, TooltipComponent, LegendComponent } from 'echarts/components';
|
||||
import { CanvasRenderer } from 'echarts/renderers';
|
||||
import { Plus, Trash2, Pencil, Calculator, GraduationCap } from 'lucide-react';
|
||||
import { bandDelete, bandUpsert, courseDelete, courseUpsert, gradesOverview, semesterDelete, semesterUpsert } from '@/lib/api';
|
||||
import { bandDelete, bandUpsert, courseDelete, courseUpsert, gpaSettingsUpsert, gradesOverview, semesterDelete, semesterUpsert } from '@/lib/api';
|
||||
import type { CourseGrade, GradeBand, SemesterGrades } from '@/lib/types';
|
||||
import { Badge, Button, Card, CardBody, CardHeader, Confirm, Dialog, Empty, Field, Input, useToast } from '@/components/ui';
|
||||
import { Badge, Button, Card, CardBody, CardHeader, Confirm, Dialog, Empty, Field, Input, Select, useToast } from '@/components/ui';
|
||||
import { useChart, inkColor, useThemeTick } from '@/hooks/useChart';
|
||||
|
||||
echarts.use([BarChart, LineChart, PieChart, GridComponent, TooltipComponent, LegendComponent, CanvasRenderer]);
|
||||
@@ -17,20 +17,31 @@ function termYear(term: string): string {
|
||||
return m ? m[1] + '~' + m[2] + ' 学年' : '其他学期';
|
||||
}
|
||||
|
||||
function autoGpa(courses: CourseGrade[], bands?: { min: number; max: number; gp?: number | null }[]): number | null {
|
||||
// 按梯度公式补算缺失绩点:分数落在哪一档 → 该档绩点(各校规则可在梯度设置中修改)
|
||||
function autoGpa(courses: CourseGrade[], bands?: { min: number; max: number; gp?: number | null; gpTop?: number | null }[], weights?: Record<string, number>): number | null {
|
||||
// 绩点公式(各校规则不同,梯度设置中可改):
|
||||
// 课程绩点 = 档内线性折算(分数落在哪档 → gp(档首)~gpTop(档顶) 插值;未填 gpTop 则取固定档绩点)
|
||||
// 课程学分绩点 = 课程绩点 × 课程权重系数 × 课程学分
|
||||
// 平均学分绩点 GPA = Σ(课程学分绩点) ÷ Σ(课程学分)
|
||||
const gpOf = (c: CourseGrade): number | null => {
|
||||
if (c.grade_point != null) return c.grade_point;
|
||||
if (bands && c.score != null) {
|
||||
const hit = bands.find(b => c.score! >= b.min && (c.score! < b.max || (b.max >= 100 && c.score! <= b.max)));
|
||||
if (hit && hit.gp != null) return hit.gp;
|
||||
const b = bands.find(x => c.score! >= x.min && (c.score! < x.max || (x.max >= 100 && c.score! <= x.max)));
|
||||
if (b && b.gp != null) {
|
||||
if (b.gpTop != null) {
|
||||
const topScore = b.max >= 100 ? b.max : b.max - 1;
|
||||
const slope = (b.gpTop - b.gp) / Math.max(1, topScore - b.min);
|
||||
return Math.round((b.gp + (c.score! - b.min) * slope) * 100) / 100;
|
||||
}
|
||||
return b.gp;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
const wOf = (c: CourseGrade): number => (weights ? (weights[c.category ?? '其他'] ?? weights['其他'] ?? 1) : 1);
|
||||
const withGp = courses.map(c => ({ ...c, grade_point: gpOf(c) })).filter(c => c.grade_point != null && c.credit > 0);
|
||||
if (!withGp.length) return null;
|
||||
const credits = withGp.reduce((a, c) => a + c.credit, 0);
|
||||
const weighted = withGp.reduce((a, c) => a + (c.grade_point as number) * c.credit, 0);
|
||||
const weighted = withGp.reduce((a, c) => a + (c.grade_point as number) * wOf(c) * c.credit, 0);
|
||||
return Math.round((weighted / credits) * 100) / 100;
|
||||
}
|
||||
|
||||
@@ -105,7 +116,7 @@ export function GradesPage() {
|
||||
</div>
|
||||
|
||||
{/* 成绩梯度设置 */}
|
||||
<BandEditor bands={s?.bands ?? []} />
|
||||
<BandEditor bands={s?.bands ?? []} weights={s?.weights} />
|
||||
|
||||
{/* 学年学科成绩 */}
|
||||
<div className="mb-3 mt-5 flex items-center justify-between">
|
||||
@@ -155,7 +166,7 @@ function MetricCard({ label, value }: { label: string; value: number | string })
|
||||
}
|
||||
|
||||
// ---------- 梯度设置 ----------
|
||||
function BandEditor({ bands }: { bands: GradeBand[] }) {
|
||||
function BandEditor({ bands, weights }: { bands: GradeBand[]; weights?: Record<string, number> }) {
|
||||
const qc = useQueryClient();
|
||||
const toast = useToast();
|
||||
const [adding, setAdding] = useState<GradeBand | null>(null);
|
||||
@@ -178,7 +189,7 @@ function BandEditor({ bands }: { bands: GradeBand[] }) {
|
||||
<Card>
|
||||
<CardHeader
|
||||
title="成绩梯度"
|
||||
extra={<span className="text-[11px] text-ink-3">各校规则不同:每档可设定级与绩点,「自动绩点」按此计算</span>}
|
||||
extra={<span className="text-[11px] text-ink-3">绩点按档内线性折算(起~顶),各校规则不同可修改</span>}
|
||||
/>
|
||||
<CardBody className="p-0">
|
||||
<div className="divide-y divide-line">
|
||||
@@ -196,12 +207,35 @@ function BandEditor({ bands }: { bands: GradeBand[] }) {
|
||||
onCancel={() => setAdding(null)}
|
||||
/>
|
||||
) : (
|
||||
<Button size="sm" variant="secondary" onClick={() => setAdding({ id: 0, label: '', color: '#3056d3', min: 0, max: 60, gp: null, sort: (bands.at(-1)?.sort ?? 0) + 10 })}>
|
||||
<Button size="sm" variant="secondary" onClick={() => setAdding({ id: 0, label: '', color: '#3056d3', min: 0, max: 60, gp: null, gpTop: null, sort: (bands.at(-1)?.sort ?? 0) + 10 })}>
|
||||
<Plus size={13} /> 添加等级
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</CardBody>
|
||||
<div className="border-t border-line px-4 py-3">
|
||||
<div className="mb-2 flex items-center justify-between">
|
||||
<span className="text-[12px] font-semibold text-ink-2">课程权重系数</span>
|
||||
<span className="text-[11px] text-ink-3">课程学分绩点 = 绩点 × 权重 × 学分</span>
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{Object.entries(weights ?? {}).map(([cat, w]) => (
|
||||
<div key={cat} className="flex items-center gap-1.5 rounded-md border border-line bg-surface-2 px-2.5 py-1">
|
||||
<span className="text-[11.5px] text-ink-2">{cat}</span>
|
||||
<Input type="number" step="0.1" className="num h-6 w-[56px] border-0 bg-transparent p-0"
|
||||
value={String(w)} onChange={e => {
|
||||
const next = { ...(weights ?? {}), [cat]: Number(e.target.value) || 1 };
|
||||
void gpaSettingsUpsert(next)
|
||||
.then(() => qc.invalidateQueries({ queryKey: ['grades'] }))
|
||||
.catch((err: Error) => toast('error', err.message));
|
||||
}} />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className="mt-2 text-[11px] text-ink-3">
|
||||
公式:平均学分绩点 = Σ(课程绩点 × 权重系数 × 学分) ÷ Σ(学分) · 各校规则不同,可直接修改
|
||||
</div>
|
||||
</div>
|
||||
<Confirm
|
||||
open={!!deleting}
|
||||
title="删除等级"
|
||||
@@ -243,7 +277,9 @@ function BandRow({ band, onSave, onDelete, onChange, onCancel, isNew }: {
|
||||
<span className="text-[11.5px] text-ink-3">—</span>
|
||||
<Input type="number" className="num h-6 w-[64px] border-0 bg-transparent p-0" value={String(value.max)} onChange={e => set({ max: Number(e.target.value) })} />
|
||||
<span className="ml-1 text-[11px] text-ink-3">绩点</span>
|
||||
<Input type="number" step="0.1" className="num h-6 w-[56px] border-0 bg-transparent p-0" value={value.gp != null ? String(value.gp) : ''} placeholder="—" onChange={e => set({ gp: e.target.value === '' ? null : Number(e.target.value) })} />
|
||||
<Input type="number" step="0.1" className="num h-6 w-[52px] border-0 bg-transparent p-0 text-right" value={value.gp != null ? String(value.gp) : ''} placeholder="起" onChange={e => set({ gp: e.target.value === '' ? null : Number(e.target.value) })} />
|
||||
<span className="text-[11.5px] text-ink-3">~</span>
|
||||
<Input type="number" step="0.1" className="num h-6 w-[52px] border-0 bg-transparent p-0" value={value.gpTop != null ? String(value.gpTop) : ''} placeholder="顶" onChange={e => set({ gpTop: e.target.value === '' ? null : Number(e.target.value) })} />
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5 sm:ml-auto">
|
||||
{isNew ? (
|
||||
@@ -283,7 +319,7 @@ function SemesterCard({ sem }: { sem: SemesterGrades }) {
|
||||
|
||||
const autoGpaMut = useMutation({
|
||||
mutationFn: () => {
|
||||
const gpa = autoGpa(sem.courses, overviewQ.data?.bands);
|
||||
const gpa = autoGpa(sem.courses, overviewQ.data?.bands, overviewQ.data?.weights);
|
||||
if (gpa == null) throw new Error('请先为课程填写绩点');
|
||||
return semesterUpsert({ id: sem.id, term: sem.term, gpa, rank: sem.rank ?? null, total_students: sem.total_students ?? null });
|
||||
},
|
||||
@@ -318,7 +354,7 @@ function SemesterCard({ sem }: { sem: SemesterGrades }) {
|
||||
<div className="-mx-1 overflow-x-auto px-1"><table className="w-full border-collapse text-[13px]">
|
||||
<thead>
|
||||
<tr>
|
||||
{['课程', '学分', '分数', '等级', '绩点', ''].map(h => (
|
||||
{['课程', '学分', '分数', '类别', '等级', '绩点', ''].map(h => (
|
||||
<th key={h} className="border-b border-line bg-surface-2 px-3 py-2 text-left text-[12px] font-semibold text-ink-3">{h}</th>
|
||||
))}
|
||||
</tr>
|
||||
@@ -334,6 +370,7 @@ function SemesterCard({ sem }: { sem: SemesterGrades }) {
|
||||
<td className="num border-b border-line px-3 py-2">
|
||||
{c.score != null ? <span className="font-medium">{c.score}</span> : <span className="text-ink-3">未录入</span>}
|
||||
</td>
|
||||
<td className="border-b border-line px-3 py-2 text-[12px] text-ink-3">{c.category ?? '—'}</td>
|
||||
<td className="border-b border-line px-3 py-2">
|
||||
{c.band ? <Badge color={c.band.color}>{c.band.label}</Badge> : <span className="text-ink-3">—</span>}
|
||||
</td>
|
||||
@@ -410,6 +447,7 @@ function CourseAddRow({ sem }: { sem: SemesterGrades }) {
|
||||
const [credit, setCredit] = useState('');
|
||||
const [score, setScore] = useState('');
|
||||
const [gp, setGp] = useState('');
|
||||
const [category, setCategory] = useState('必修课');
|
||||
|
||||
const addMut = useMutation({
|
||||
mutationFn: () => courseUpsert({
|
||||
@@ -428,6 +466,7 @@ function CourseAddRow({ sem }: { sem: SemesterGrades }) {
|
||||
<td className="px-3 py-2"><Input className="h-7 w-[160px]" placeholder="课程名称" value={name} onChange={e => setName(e.target.value)} /></td>
|
||||
<td className="px-3 py-2"><Input type="number" step="0.5" className="num h-7 w-[64px]" placeholder="0" value={credit} onChange={e => setCredit(e.target.value)} /></td>
|
||||
<td className="px-3 py-2"><Input type="number" className="num h-7 w-[72px]" placeholder="0-100" value={score} onChange={e => setScore(e.target.value)} /></td>
|
||||
<td className="px-3 py-2"><Select className="h-7 w-[110px]" value={category} onChange={e => setCategory(e.target.value)} options={['必修课', '专业选修课', '人文素质选修课', '其他'].map(o => ({ value: o, label: o }))} /></td>
|
||||
<td className="px-3 py-2 text-ink-3">—</td>
|
||||
<td className="px-3 py-2"><Input type="number" step="0.1" className="num h-7 w-[64px]" placeholder="选填" value={gp} onChange={e => setGp(e.target.value)} /></td>
|
||||
<td className="px-3 py-2">
|
||||
@@ -446,11 +485,12 @@ function CourseEditRow({ sem, course, onDone }: { sem: SemesterGrades; course: C
|
||||
const [credit, setCredit] = useState(String(course.credit));
|
||||
const [score, setScore] = useState(course.score != null ? String(course.score) : '');
|
||||
const [gp, setGp] = useState(course.grade_point != null ? String(course.grade_point) : '');
|
||||
const [category, setCategory] = useState(course.category ?? '必修课');
|
||||
|
||||
const saveMut = useMutation({
|
||||
mutationFn: () => courseUpsert({
|
||||
id: course.id, semester_id: sem.id, course_name: name, credit: Number(credit) || 0,
|
||||
score: score === '' ? null : Number(score), grade_point: gp === '' ? null : Number(gp)
|
||||
score: score === '' ? null : Number(score), grade_point: gp === '' ? null : Number(gp), category
|
||||
}),
|
||||
onSuccess: () => { void qc.invalidateQueries({ queryKey: ['grades'] }); onDone(); },
|
||||
onError: (e: Error) => toast('error', e.message)
|
||||
@@ -461,6 +501,7 @@ function CourseEditRow({ sem, course, onDone }: { sem: SemesterGrades; course: C
|
||||
<td className="border-b border-line px-3 py-2"><Input className="h-7 w-[160px]" value={name} onChange={e => setName(e.target.value)} /></td>
|
||||
<td className="border-b border-line px-3 py-2"><Input type="number" step="0.5" className="num h-7 w-[64px]" value={credit} onChange={e => setCredit(e.target.value)} /></td>
|
||||
<td className="border-b border-line px-3 py-2"><Input type="number" className="num h-7 w-[72px]" value={score} onChange={e => setScore(e.target.value)} /></td>
|
||||
<td className="border-b border-line px-3 py-2"><Select className="h-7 w-[110px]" value={category} onChange={e => setCategory(e.target.value)} options={['必修课', '专业选修课', '人文素质选修课', '其他'].map(o => ({ value: o, label: o }))} /></td>
|
||||
<td className="border-b border-line px-3 py-2 text-[12px] text-ink-3">保存后按梯度定级</td>
|
||||
<td className="border-b border-line px-3 py-2"><Input type="number" step="0.1" className="num h-7 w-[64px]" value={gp} onChange={e => setGp(e.target.value)} /></td>
|
||||
<td className="border-b border-line px-3 py-2">
|
||||
@@ -482,10 +523,11 @@ function MobileCourseAdd({ sem }: { sem: SemesterGrades }) {
|
||||
const [credit, setCredit] = useState('');
|
||||
const [score, setScore] = useState('');
|
||||
const [gp, setGp] = useState('');
|
||||
const [category, setCategory] = useState('必修课');
|
||||
const saveMut = useMutation({
|
||||
mutationFn: () => courseUpsert({
|
||||
semester_id: sem.id, course_name: name, credit: Number(credit) || 0,
|
||||
score: score === '' ? null : Number(score), grade_point: gp === '' ? null : Number(gp)
|
||||
score: score === '' ? null : Number(score), grade_point: gp === '' ? null : Number(gp), category
|
||||
}),
|
||||
onSuccess: () => {
|
||||
setName(''); setCredit(''); setScore(''); setGp('');
|
||||
@@ -502,6 +544,7 @@ function MobileCourseAdd({ sem }: { sem: SemesterGrades }) {
|
||||
<Input type="number" placeholder="分数" value={score} onChange={e => setScore(e.target.value)} />
|
||||
<Input type="number" step="0.1" placeholder="绩点" value={gp} onChange={e => setGp(e.target.value)} />
|
||||
</div>
|
||||
<Select className="mb-2" value={category} onChange={e => setCategory(e.target.value)} options={['必修课', '专业选修课', '人文素质选修课', '其他'].map(o => ({ value: o, label: o }))} />
|
||||
<Button size="sm" variant="primary" disabled={!name.trim() || saveMut.isPending} onClick={() => saveMut.mutate()}>
|
||||
<Plus size={13} /> 添加课程
|
||||
</Button>
|
||||
@@ -516,10 +559,11 @@ function MobileCourseEdit({ sem, course, onDone }: { sem: SemesterGrades; course
|
||||
const [credit, setCredit] = useState(String(course.credit));
|
||||
const [score, setScore] = useState(course.score != null ? String(course.score) : '');
|
||||
const [gp, setGp] = useState(course.grade_point != null ? String(course.grade_point) : '');
|
||||
const [category, setCategory] = useState(course.category ?? '必修课');
|
||||
const saveMut = useMutation({
|
||||
mutationFn: () => courseUpsert({
|
||||
id: course.id, semester_id: sem.id, course_name: name, credit: Number(credit) || 0,
|
||||
score: score === '' ? null : Number(score), grade_point: gp === '' ? null : Number(gp)
|
||||
score: score === '' ? null : Number(score), grade_point: gp === '' ? null : Number(gp), category
|
||||
}),
|
||||
onSuccess: () => { void qc.invalidateQueries({ queryKey: ['grades'] }); onDone(); },
|
||||
onError: (e: Error) => toast('error', e.message)
|
||||
@@ -532,6 +576,7 @@ function MobileCourseEdit({ sem, course, onDone }: { sem: SemesterGrades; course
|
||||
<Input type="number" placeholder="分数" value={score} onChange={e => setScore(e.target.value)} />
|
||||
<Input type="number" step="0.1" placeholder="绩点" value={gp} onChange={e => setGp(e.target.value)} />
|
||||
</div>
|
||||
<Select className="mb-2" value={category} onChange={e => setCategory(e.target.value)} options={['必修课', '专业选修课', '人文素质选修课', '其他'].map(o => ({ value: o, label: o }))} />
|
||||
<div className="flex gap-1.5">
|
||||
<Button size="sm" variant="primary" onClick={() => saveMut.mutate()}>保存</Button>
|
||||
<Button size="sm" variant="ghost" onClick={onDone}>取消</Button>
|
||||
|
||||
Reference in New Issue
Block a user