From d13e197c82c66f35a63f0a57ec56cdca58c5e64c Mon Sep 17 00:00:00 2001 From: linfeng <2819853134@qq.com> Date: Sun, 13 Apr 2025 19:00:53 +0800 Subject: [PATCH] 123 --- apps/server/src/models/base/base.service.ts | 43 ++-- .../assessment-standardpage.tsx | 227 +++++++++++------- 2 files changed, 168 insertions(+), 102 deletions(-) diff --git a/apps/server/src/models/base/base.service.ts b/apps/server/src/models/base/base.service.ts index ae45e06..2b10978 100755 --- a/apps/server/src/models/base/base.service.ts +++ b/apps/server/src/models/base/base.service.ts @@ -30,6 +30,7 @@ export class BaseService< A extends DelegateArgs = DelegateArgs, R extends DelegateReturnTypes = DelegateReturnTypes, > { + [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 { - 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 { + // 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. diff --git a/apps/web/src/app/admin/assessmentstandard/assessment-standardpage.tsx b/apps/web/src/app/admin/assessmentstandard/assessment-standardpage.tsx index 42af287..ab4f2ca 100644 --- a/apps/web/src/app/admin/assessmentstandard/assessment-standardpage.tsx +++ b/apps/web/src/app/admin/assessmentstandard/assessment-standardpage.tsx @@ -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([]); + const [loading, setLoading] = useState(false); + // 新增状态控制模态框显示隐藏 + const [isModalVisible, setIsModalVisible] = useState(false); + // 新增状态存储年龄范围起始值 + const [startAge, setStartAge] = useState(null); + // 新增状态存储年龄范围结束值 + const [endAge, setEndAge] = useState(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 newRange = { - start, - end, - label: end ? `${start}-${end}岁` : `${start}岁以上`, - }; - setAgeRanges([...ageRanges, newRange]); - setIsAgeModalVisible(false); + const handleOk = async () => { + if (startAge === null) { + message.error('请输入起始年龄'); + return; + } + try { + // 调用模拟接口 + await addAgeRangeApi(startAge, endAge); + const newRange = { + start: startAge, + end: endAge, + label: endAge ? `${startAge}-${endAge}岁` : `${startAge}岁以上` + }; + setAgeRanges([...ageRanges, newRange]); + 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 = {}; + 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 (

考核标准管理

- - +
- + - +