综合实践与项目拓展01
- 需求分析
功能概述:允许系统管理员添加新的老年人评估指标,每个评估指标应包含指标名称、指标描述、评分规则、权重等信息。添加成功后,新指标可用于后续的老年人评估。
用户角色:系统管理员。
操作流程:管理员登录系统后,进入评估指标管理页面,点击 “添加新指标” 按钮,填写指标相关信息,点击 “提交”,系统验证信息有效性,若有效则保存到数据库,同时提示添加成功;若无效则提示错误信息。 - 数据库设计
假设已有 elderly_assessment 数据库,现新增 assessment_metrics 表用于存储评估指标信息。
-- 创建评估指标表 CREATE TABLE assessment_metrics ( metric_id INT AUTO_INCREMENT PRIMARY KEY, metric_name VARCHAR(255) NOT NULL, metric_description TEXT, scoring_rules TEXT, weight DECIMAL(5, 2) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
metric_id:评估指标的唯一标识,自增主键。
metric_name:指标名称,不可为空。
metric_description:指标描述。
scoring_rules:评分规则。
weight:指标权重,精确到小数点后两位。
created_at:指标创建时间,默认值为当前时间。 - 后端代码实现(以 Java 和 Spring Boot 为例)
3.1 创建实体类
`import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;
@Entity
public class AssessmentMetric {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer metricId;
private String metricName;
private String metricDescription;
private String scoringRules;
private Double weight;
private Date createdAt;
// 省略 getter 和 setter 方法
}3.2 创建 Repository 接口import org.springframework.data.jpa.repository.JpaRepository;
public interface AssessmentMetricRepository extends JpaRepository<AssessmentMetric, Integer> {
}3.3 创建 Service 类import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class AssessmentMetricService {
@Autowired
private AssessmentMetricRepository repository;
public AssessmentMetric addMetric(AssessmentMetric metric) {
metric.setCreatedAt(new Date());
return repository.save(metric);
}
}3.4 创建 Controller 类import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AssessmentMetricController {
@Autowired
private AssessmentMetricService service;
@PostMapping("/assessment-metrics")
public AssessmentMetric addMetric(@RequestBody AssessmentMetric metric) {
return service.addMetric(metric);
}
}4. 前端页面设计
label {
display: block;
margin-bottom: 5px;
}
input,
textarea {
width: 100%;
padding: 8px;
margin-bottom: 15px;
}
button {
padding: 10px 20px;
background-color: #007BFF;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>添加评估指标</h1>
<form id="addMetricForm">
<label for="metricName">指标名称:</label>
<input type="text" id="metricName" required>
<label for="metricDescription">指标描述:</label>
<textarea id="metricDescription"></textarea>
<label for="scoringRules">评分规则:</label>
<textarea id="scoringRules"></textarea>
<label for="weight">权重:</label>
<input type="number" id="weight" step="0.01" required>
<button type="submit">提交</button>
</form>
<script>
document.getElementById('addMetricForm').addEventListener('submit', function (e) {
e.preventDefault();
const metricName = document.getElementById('metricName').value;
const metricDescription = document.getElementById('metricDescription').value;
const scoringRules = document.getElementById('scoringRules').value;
const weight = parseFloat(document.getElementById('weight').value);
const metric = {
metricName: metricName,
metricDescription: metricDescription,
scoringRules: scoringRules,
weight: weight
};
fetch('/assessment-metrics', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(metric)
})
.then(response => response.json())
.then(data => {
alert('指标添加成功!');
// 可添加清空表单等操作
})
.catch(error => {
alert('添加指标时出错,请稍后重试。');
});
});
</script>
</body>
`

浙公网安备 33010602011771号