功能概述 临期商品提示功能旨在帮助小象买菜系统自动识别即将过期的商品,及时提醒商家和顾客,减少商品损耗并保障食品安全。 功能实现方案 1.数据库设计 ```sql --商品表扩展 ALTERTABLEproductsADDCOLUMN production_dateDA
功能概述
临期商品提示功能旨在帮助小象买菜系统自动识别即将过期的商品,及时提醒商家和顾客,减少商品损耗并保障食品安全。
功能实现方案
1. 数据库设计
```sql
-- 商品表扩展
ALTER TABLE products ADD COLUMN
production_date DATE COMMENT 生产日期,
expiry_date DATE COMMENT 保质期截止日期,
shelf_life_days INT COMMENT 保质期天数(天);
-- 临期商品提醒表
CREATE TABLE expiry_alerts (
id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT NOT NULL,
alert_type ENUM(near_expiry, expired) COMMENT 提醒类型:临近过期/已过期,
alert_level INT COMMENT 提醒级别(1-3天,4-7天等),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
handled BOOLEAN DEFAULT FALSE COMMENT 是否已处理,
FOREIGN KEY (product_id) REFERENCES products(id)
);
```
2. 核心功能实现
商品保质期计算服务
```java
public class ExpiryCalculator {
// 计算商品剩余保质期(天)
public static int calculateRemainingDays(Date expiryDate) {
long diffInMillis = expiryDate.getTime() - new Date().getTime();
return (int) TimeUnit.DAYS.convert(diffInMillis, TimeUnit.MILLISECONDS);
}
// 检查是否临期
public static boolean isNearExpiry(Date expiryDate, int thresholdDays) {
int remainingDays = calculateRemainingDays(expiryDate);
return remainingDays <= thresholdDays && remainingDays >= 0;
}
}
```
定时任务扫描临期商品
```java
@Scheduled(cron = "0 0 8 * * ?") // 每天早上8点执行
public void scanNearExpiryProducts() {
// 获取所有在售商品
List
products = productRepository.findAllActiveProducts();
// 设置不同提醒级别的阈值(天)
Map alertThresholds = Map.of(
1, 3, // 3天内过期为一级提醒
2, 7, // 7天内过期为二级提醒
3, 15 // 15天内过期为三级提醒
);
for (Product product : products) {
if (product.getExpiryDate() == null) continue;
int remainingDays = ExpiryCalculator.calculateRemainingDays(product.getExpiryDate());
// 检查各提醒级别
alertThresholds.forEach((level, threshold) -> {
if (remainingDays <= threshold && remainingDays > 0) {
createAlert(product.getId(), "near_expiry", level);
}
});
// 已过期商品
if (remainingDays < 0) {
createAlert(product.getId(), "expired", 0);
}
}
}
private void createAlert(int productId, String alertType, int alertLevel) {
ExpiryAlert alert = new ExpiryAlert();
alert.setProductId(productId);
alert.setAlertType(alertType);
alert.setAlertLevel(alertLevel);
expiryAlertRepository.save(alert);
}
```
3. 提醒方式实现
商家端提醒
1. 管理后台通知:
- 在商品管理页面显示临期商品列表
- 添加临期商品数量统计徽章
- 支持按保质期排序和筛选
2. 邮件/短信提醒:
```java
public void sendExpiryAlertsToMerchant() {
List alerts = expiryAlertRepository.findUnhandledAlerts();
// 按商家分组提醒
Map> alertsByMerchant = alerts.stream()
.collect(Collectors.groupingBy(alert ->
productRepository.findById(alert.getProductId()).get().getMerchantId()));
alertsByMerchant.forEach((merchantId, merchantAlerts) -> {
String message = buildAlertMessage(merchantAlerts);
// 发送邮件或短信
notificationService.sendToMerchant(merchantId, "临期商品提醒", message);
});
}
```
顾客端提醒
1. 商品详情页显示:
```html
已过期
3天内到期
7天内到期
```
2. 购物车提醒:
```javascript
function checkCartExpiry() {
const nearExpiryThreshold = 3; // 3天内到期
const now = new Date();
cart.items.forEach(item => {
const expiryDate = new Date(item.expiryDate);
const diffTime = expiryDate - now;
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
if (diffDays <= nearExpiryThreshold && diffDays >= 0) {
showExpiryWarning(item, diffDays);
}
});
}
```
4. 商家处理流程
1. 商家在管理后台查看临期商品列表
2. 可选择的处理方式:
- 立即下架
- 设置折扣促销
- 标记为已处理(停止提醒)
```java
@PostMapping("/alerts/{id}/handle")
public ResponseEntity<?> handleAlert(@PathVariable Long id, @RequestParam String action) {
ExpiryAlert alert = expiryAlertRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Alert not found"));
switch (action) {
case "discount":
// 设置商品折扣
productService.applyDiscount(alert.getProductId(), 0.7); // 7折
break;
case "off_shelf":
// 下架商品
productService.deactivateProduct(alert.getProductId());
break;
}
alert.setHandled(true);
expiryAlertRepository.save(alert);
return ResponseEntity.ok().build();
}
```
技术考虑
1. 性能优化:
- 对保质期字段建立索引
- 增量扫描而非全表扫描(记录上次扫描时间)
- 使用缓存存储频繁访问的商品保质期信息
2. 时区处理:
- 统一使用UTC时间存储,显示时转换为本地时区
3. 批量处理:
- 对于大量临期商品,采用分批处理避免系统负载过高
4. 数据安全:
- 临期商品数据访问权限控制
- 操作日志记录
扩展功能
1. 智能促销建议:
- 根据历史销售数据和临期时间,建议最佳折扣率
2. 库存预测:
- 结合临期商品数据优化采购计划
3. 多级提醒:
- 支持自定义提醒阈值(如生鲜商品设置更短的提醒周期)
4. 移动端推送:
- 通过APP推送临期商品提醒
通过以上实现方案,小象买菜系统可以有效管理临期商品,减少损耗,同时提升顾客购物体验和商家运营效率。