123
This commit is contained in:
parent
3af6216828
commit
d13e197c82
|
@ -30,6 +30,7 @@ export class BaseService<
|
|||
A extends DelegateArgs<D> = DelegateArgs<D>,
|
||||
R extends DelegateReturnTypes<D> = DelegateReturnTypes<D>,
|
||||
> {
|
||||
[x: string]: any;
|
||||
protected ORDER_INTERVAL = 100;
|
||||
/**
|
||||
* Initializes the BaseService with the specified model.
|
||||
|
@ -152,27 +153,27 @@ export class BaseService<
|
|||
* @example
|
||||
* const newUser = await service.create({ data: { name: 'John Doe' } });
|
||||
*/
|
||||
async create(args: A['create'], params?: any): Promise<R['create']> {
|
||||
try {
|
||||
if (this.enableOrder && !(args as any).data.order) {
|
||||
// 查找当前最大的 order 值
|
||||
const maxOrderItem = (await this.getModel(params?.tx).findFirst({
|
||||
orderBy: { order: 'desc' },
|
||||
})) as any;
|
||||
// 设置新记录的 order 值
|
||||
const newOrder = maxOrderItem
|
||||
? maxOrderItem.order + this.ORDER_INTERVAL
|
||||
: 1;
|
||||
// 将 order 添加到创建参数中
|
||||
(args as any).data.order = newOrder;
|
||||
}
|
||||
return this.getModel(params?.tx).create(args as any) as Promise<
|
||||
R['create']
|
||||
>;
|
||||
} catch (error) {
|
||||
this.handleError(error, 'create');
|
||||
}
|
||||
}
|
||||
// async create(args: A['create'], params?: any): Promise<R['create']> {
|
||||
// try {
|
||||
// if (this.enableOrder && !(args as any).data.order) {
|
||||
// // 查找当前最大的 order 值
|
||||
// const maxOrderItem = (await this.getModel(params?.tx).findFirst({
|
||||
// orderBy: { order: 'desc' },
|
||||
// })) as any;
|
||||
// // 设置新记录的 order 值
|
||||
// const newOrder = maxOrderItem
|
||||
// ? maxOrderItem.order + this.ORDER_INTERVAL
|
||||
// : 1;
|
||||
// // 将 order 添加到创建参数中
|
||||
// (args as any).data.order = newOrder;
|
||||
// }
|
||||
// return this.getModel(params?.tx).create(args as any) as Promise<
|
||||
// R['create']
|
||||
// >;
|
||||
// } catch (error) {
|
||||
// this.handleError(error, 'create');
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* Creates multiple new records with the given data.
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { Table, Select, Form, Button, Space, InputNumber, Modal } from 'antd';
|
||||
import { useState } from 'react';
|
||||
import { Table, Select, Form, Button, Space, InputNumber, Modal, message } from 'antd';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { api } from '@nice/client';
|
||||
|
||||
// 模拟接口调用函数
|
||||
const addAgeRangeApi = async (start: number, end: number | null) => {
|
||||
|
@ -14,14 +15,25 @@ const addScoreStandardApi = async (score: number, standards: (number | null)[])
|
|||
|
||||
export default function AssessmentStandardPage() {
|
||||
const [form] = Form.useForm();
|
||||
const [ageRanges, setAgeRanges] = useState<{ start: number; end: number; label: string; }[]>([
|
||||
{ start: 18, end: 24, label: '18-24岁' },
|
||||
{ start: 25, end: 34, label: '25-34岁' },
|
||||
{ start: 35, end: 44, label: '35-44岁' },
|
||||
{ start: 45, end: null, label: '45岁以上' },
|
||||
]);
|
||||
const [isAgeModalVisible, setIsAgeModalVisible] = useState(false);
|
||||
const [isScoreModalVisible, setIsScoreModalVisible] = useState(false);
|
||||
const [standardForm] = Form.useForm();
|
||||
const [ageRanges, setAgeRanges] = useState<{ start: number; end: number | null; label: string; }[]>([]);
|
||||
const [dataSource, setDataSource] = useState<any[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
// 新增状态控制模态框显示隐藏
|
||||
const [isModalVisible, setIsModalVisible] = useState(false);
|
||||
// 新增状态存储年龄范围起始值
|
||||
const [startAge, setStartAge] = useState<number | null>(null);
|
||||
// 新增状态存储年龄范围结束值
|
||||
const [endAge, setEndAge] = useState<number | null>(null);
|
||||
|
||||
// TRPC mutations
|
||||
// const createStandardMutation = api.sportStandard.createStandard.useMutation();
|
||||
// const updateStandardMutation = api.sportStandard.update.useMutation();
|
||||
// const standardsQuery = api.sportStandard.findMany.useQuery({
|
||||
// include: {
|
||||
// project: true
|
||||
// }
|
||||
// });
|
||||
|
||||
const columns = [
|
||||
{
|
||||
|
@ -45,64 +57,121 @@ export default function AssessmentStandardPage() {
|
|||
];
|
||||
|
||||
const handleStandardChange = (score: number, ageIndex: number, value: number | null) => {
|
||||
// 处理标准值变化
|
||||
const newDataSource = dataSource.map(item => {
|
||||
if (item.score === score) {
|
||||
return {
|
||||
...item,
|
||||
[`standard_${ageIndex}`]: value
|
||||
};
|
||||
}
|
||||
return item;
|
||||
});
|
||||
setDataSource(newDataSource);
|
||||
};
|
||||
|
||||
const showAgeModal = () => {
|
||||
setIsAgeModalVisible(true);
|
||||
const handleAddAgeRange = () => {
|
||||
// 点击按钮显示模态框
|
||||
setIsModalVisible(true);
|
||||
};
|
||||
|
||||
const handleAgeOk = async (values: any) => {
|
||||
const { start, end } = values;
|
||||
await addAgeRangeApi(start, end);
|
||||
const handleOk = async () => {
|
||||
if (startAge === null) {
|
||||
message.error('请输入起始年龄');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// 调用模拟接口
|
||||
await addAgeRangeApi(startAge, endAge);
|
||||
const newRange = {
|
||||
start,
|
||||
end,
|
||||
label: end ? `${start}-${end}岁` : `${start}岁以上`,
|
||||
start: startAge,
|
||||
end: endAge,
|
||||
label: endAge ? `${startAge}-${endAge}岁` : `${startAge}岁以上`
|
||||
};
|
||||
setAgeRanges([...ageRanges, newRange]);
|
||||
setIsAgeModalVisible(false);
|
||||
setIsModalVisible(false);
|
||||
message.success('年龄范围添加成功');
|
||||
} catch (error) {
|
||||
console.error('添加年龄范围失败:', error);
|
||||
message.error('添加年龄范围失败');
|
||||
}
|
||||
};
|
||||
|
||||
const handleAgeCancel = () => {
|
||||
setIsAgeModalVisible(false);
|
||||
const handleCancel = () => {
|
||||
// 关闭模态框
|
||||
setIsModalVisible(false);
|
||||
};
|
||||
|
||||
const showScoreModal = () => {
|
||||
setIsScoreModalVisible(true);
|
||||
const handleSave = async () => {
|
||||
try {
|
||||
const values = await form.validateFields();
|
||||
const { projectId, gender, personType, ageRanges} = values;
|
||||
|
||||
// 将表格数据转换为后端需要的格式
|
||||
const scoreTable: Record<string, number[]> = {};
|
||||
dataSource.forEach(row => {
|
||||
const standards = ageRanges?.map((_, index) => row[`standard_${index}`]);
|
||||
scoreTable[row.score] = standards;
|
||||
});
|
||||
|
||||
|
||||
setLoading(true);
|
||||
await api.sportStandard.createStandard.useMutation().mutateAsync({
|
||||
data: {
|
||||
projectId,
|
||||
gender,
|
||||
personType,
|
||||
ageRanges: ageRanges.map(range => ({
|
||||
start: range.start,
|
||||
end: range.end,
|
||||
label: range.label
|
||||
})),
|
||||
scoreTable
|
||||
}
|
||||
});
|
||||
message.success('保存成功');
|
||||
} catch (error) {
|
||||
console.error('保存失败:', error);
|
||||
message.error('保存失败');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleScoreOk = async (values: any) => {
|
||||
const { score, standards } = values;
|
||||
await addScoreStandardApi(score, standards);
|
||||
// 这里可以更新表格的数据源
|
||||
setIsScoreModalVisible(false);
|
||||
};
|
||||
|
||||
const handleScoreCancel = () => {
|
||||
setIsScoreModalVisible(false);
|
||||
};
|
||||
useEffect(() => {
|
||||
// 初始化表格数据
|
||||
const initialData = [
|
||||
{ score: 100, standard_0: null, standard_1: null, standard_2: null, standard_3: null },
|
||||
{ score: 90, standard_0: null, standard_1: null, standard_2: null, standard_3: null },
|
||||
];
|
||||
setDataSource(initialData);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<h1 className="text-2xl font-bold mb-6">考核标准管理</h1>
|
||||
<Button onClick={showAgeModal} className="mb-2">添加年龄范围</Button>
|
||||
<Button onClick={showScoreModal} className="mb-2 ml-2">添加分数标准</Button>
|
||||
|
||||
<Form form={form} layout="vertical">
|
||||
<Space size="large" className="mb-6">
|
||||
<Form.Item label="项目" name="projectId">
|
||||
<Form.Item
|
||||
label="项目"
|
||||
name="projectId"
|
||||
rules={[{ required: true, message: '请选择考核项目' }]}
|
||||
>
|
||||
<Select
|
||||
style={{ width: 200 }}
|
||||
placeholder="选择考核项目"
|
||||
options={[
|
||||
{ value: '1', label: '引体向上' },
|
||||
{ value: '2', label: '3000米跑' },
|
||||
// 更多项目...
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="性别" name="gender">
|
||||
<Form.Item
|
||||
label="性别"
|
||||
name="gender"
|
||||
rules={[{ required: true, message: '请选择性别' }]}
|
||||
>
|
||||
<Select
|
||||
style={{ width: 120 }}
|
||||
placeholder="选择性别"
|
||||
|
@ -113,68 +182,64 @@ export default function AssessmentStandardPage() {
|
|||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="人员类型" name="personType">
|
||||
<Form.Item
|
||||
label="人员类型"
|
||||
name="personType"
|
||||
rules={[{ required: true, message: '请选择人员类型' }]}
|
||||
>
|
||||
<Select
|
||||
style={{ width: 160 }}
|
||||
placeholder="选择人员类型"
|
||||
options={[
|
||||
{ value: 'OFFICER', label: '警员' },
|
||||
{ value: 'STAFF', label: '职工' },
|
||||
{ value: 'officer', label: '警员' },
|
||||
{ value: 'civilian', label: '职工' },
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="添加年龄范围" name="ageRanges">
|
||||
<Button type="primary" onClick={handleAddAgeRange}>
|
||||
添加年龄范围
|
||||
</Button>
|
||||
</Form.Item>
|
||||
</Space>
|
||||
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={[
|
||||
{ score: 100, standard_0: 20, standard_1: 18, standard_2: 15, standard_3: 12 },
|
||||
{ score: 90, standard_0: 18, standard_1: 16, standard_2: 13, standard_3: 10 },
|
||||
{ score: 80, standard_0: 16, standard_1: 14, standard_2: 11, standard_3: 8 },
|
||||
// 更多分数标准...
|
||||
]}
|
||||
dataSource={dataSource}
|
||||
bordered
|
||||
pagination={false}
|
||||
scroll={{ x: 'max-content' }}
|
||||
loading={loading}
|
||||
rowKey="score"
|
||||
/>
|
||||
|
||||
<div className="mt-6">
|
||||
<Button type="primary">保存标准</Button>
|
||||
<Button type="primary" onClick={handleSave} loading={loading}>
|
||||
保存标准
|
||||
</Button>
|
||||
</div>
|
||||
</Form>
|
||||
|
||||
{/* 模态框组件 */}
|
||||
<Modal
|
||||
title="添加年龄范围"
|
||||
visible={isAgeModalVisible}
|
||||
onOk={form.submit}
|
||||
onCancel={handleAgeCancel}
|
||||
visible={isModalVisible}
|
||||
onOk={handleOk}
|
||||
onCancel={handleCancel}
|
||||
>
|
||||
<Form form={form} onFinish={handleAgeOk}>
|
||||
<Form.Item name="start" label="起始年龄" rules={[{ required: true }]}>
|
||||
<InputNumber min={1} />
|
||||
<Form.Item label="起始年龄" required>
|
||||
<InputNumber
|
||||
value={startAge}
|
||||
onChange={(value) => setStartAge(value)}
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item name="end" label="结束年龄">
|
||||
<InputNumber min={1} />
|
||||
<Form.Item label="结束年龄">
|
||||
<InputNumber
|
||||
value={endAge}
|
||||
onChange={(value) => setEndAge(value)}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
|
||||
<Modal
|
||||
title="添加分数标准"
|
||||
visible={isScoreModalVisible}
|
||||
onOk={form.submit}
|
||||
onCancel={handleScoreCancel}
|
||||
>
|
||||
<Form form={form} onFinish={handleScoreOk}>
|
||||
<Form.Item name="score" label="分数" rules={[{ required: true }]}>
|
||||
<InputNumber min={0} />
|
||||
</Form.Item>
|
||||
{ageRanges.map((range, index) => (
|
||||
<Form.Item key={index} name={`standards[${index}]`} label={range.label}>
|
||||
<InputNumber min={0} />
|
||||
</Form.Item>
|
||||
))}
|
||||
</Form>
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue