一、需求分析
随机抽检管理是小象买菜系统中重要的质量控制环节,主要功能包括:
1. 自动从订单或商品中随机抽取样本进行质量检查
2. 记录抽检结果并生成报告
3. 触发预警机制(当抽检不合格时)
4. 提供抽检数据统计分析
二、系统设计
1. 数据库设计
```sql
-- 抽检记录表
CREATE TABLE inspection_records (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
order_id VARCHAR(32) COMMENT 关联订单ID,
product_id VARCHAR(32) COMMENT 商品ID,
inspection_type TINYINT COMMENT 抽检类型(1:订单抽检,2:商品抽检),
inspector_id VARCHAR(32) COMMENT 抽检人员ID,
inspection_time DATETIME COMMENT 抽检时间,
result TINYINT COMMENT 结果(1:合格,2:不合格),
description VARCHAR(500) COMMENT 抽检描述,
images TEXT COMMENT 抽检图片JSON数组,
status TINYINT DEFAULT 1 COMMENT 状态(1:有效,0:无效)
);
-- 抽检规则表
CREATE TABLE inspection_rules (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
rule_name VARCHAR(100) COMMENT 规则名称,
inspection_type TINYINT COMMENT 抽检类型,
sample_rate DECIMAL(5,2) COMMENT 抽检比例(0-100),
frequency INT COMMENT 抽检频率(次/天),
enable_flag TINYINT DEFAULT 1 COMMENT 是否启用,
create_time DATETIME COMMENT 创建时间,
update_time DATETIME COMMENT 更新时间
);
```
2. 核心功能模块
2.1 随机抽检算法
```java
public class RandomInspectionService {
@Autowired
private OrderRepository orderRepository;
@Autowired
private ProductRepository productRepository;
@Autowired
private InspectionRuleRepository ruleRepository;
/
* 执行随机抽检
*/
public void executeRandomInspection() {
// 获取所有启用的抽检规则
List
rules = ruleRepository.findByEnableFlag(1);
for (InspectionRule rule : rules) {
switch (rule.getInspectionType()) {
case 1: // 订单抽检
performOrderInspection(rule);
break;
case 2: // 商品抽检
performProductInspection(rule);
break;
}
}
}
private void performOrderInspection(InspectionRule rule) {
// 获取今日订单总数
long totalOrders = orderRepository.countTodayOrders();
// 计算需要抽检的订单数
int sampleSize = (int) Math.ceil(totalOrders * rule.getSampleRate() / 100);
if (sampleSize > 0) {
// 随机获取订单
List samples = orderRepository.findRandomOrders(sampleSize);
// 创建抽检记录
for (Order order : samples) {
InspectionRecord record = new InspectionRecord();
record.setOrderId(order.getId());
record.setInspectionType(1);
// 设置其他字段...
saveInspectionRecord(record);
}
}
}
// 商品抽检逻辑类似...
}
```
2.2 抽检结果处理
```java
public class InspectionResultService {
@Autowired
private InspectionRecordRepository recordRepository;
@Autowired
private AlertService alertService;
/
* 提交抽检结果
*/
public void submitInspectionResult(Long recordId, Integer result, String description) {
InspectionRecord record = recordRepository.findById(recordId)
.orElseThrow(() -> new RuntimeException("抽检记录不存在"));
record.setResult(result);
record.setDescription(description);
record.setInspectionTime(new Date());
recordRepository.save(record);
// 如果不合格,触发预警
if (result == 2) { // 不合格
alertService.triggerAlert(record);
}
}
}
```
3. 预警机制实现
```java
public class AlertService {
@Autowired
private AlertRepository alertRepository;
@Autowired
private NotificationService notificationService;
public void triggerAlert(InspectionRecord record) {
Alert alert = new Alert();
alert.setInspectionRecordId(record.getId());
alert.setAlertType(1); // 1表示质量不合格
alert.setCreateTime(new Date());
alertRepository.save(alert);
// 发送通知
notificationService.sendAlertNotification(record);
}
}
```
三、前端实现
1. 抽检任务列表页面
```javascript
// Vue组件示例
export default {
data() {
return {
inspectionTasks: []
}
},
created() {
this.fetchInspectionTasks();
},
methods: {
fetchInspectionTasks() {
axios.get(/api/inspection/tasks)
.then(response => {
this.inspectionTasks = response.data;
});
},
submitResult(taskId, result) {
axios.post(/api/inspection/submit, {
taskId: taskId,
result: result,
description: this.getDescription(taskId)
}).then(() => {
this.$message.success(抽检结果提交成功);
this.fetchInspectionTasks();
});
}
}
}
```
2. 抽检结果展示页面
```html
抽检结果详情
{{ record.orderId }} |
{{ record.result === 1 ? 合格 : 不合格 }} |
{{ record.inspectionTime }} |
```
四、定时任务实现
使用Spring的@Scheduled注解实现每日定时抽检:
```java
@Component
public class InspectionScheduler {
@Autowired
private RandomInspectionService inspectionService;
// 每天凌晨1点执行抽检
@Scheduled(cron = "0 0 1 * * ?")
public void executeDailyInspection() {
inspectionService.executeRandomInspection();
}
}
```
五、扩展功能
1. 抽检规则配置:允许管理员配置抽检比例、频率等参数
2. 多维度抽检:按商品类别、供应商、配送区域等维度抽检
3. 移动端支持:开发抽检人员使用的移动端应用
4. 大数据分析:分析抽检数据,找出质量问题高发环节
六、安全与权限
1. 抽检记录只能由有权限的人员查看和修改
2. 抽检过程需要留痕,记录操作日志
3. 敏感数据加密存储
七、部署与监控
1. 抽检服务应独立部署,避免影响主业务
2. 设置监控告警,当抽检服务异常时及时通知
3. 定期生成抽检报告,供管理层查看
八、测试要点
1. 随机算法的正确性测试
2. 高并发下的性能测试
3. 异常情况处理测试(如抽检时订单已取消)
4. 权限控制测试
此随机抽检管理模块可有效提升小象买菜系统的商品质量控制能力,通过科学合理的抽检机制保障食品安全。