功能概述
临期商品提示功能旨在帮助小象买菜系统自动识别即将过期的商品,并及时提醒管理人员和用户,以减少商品损耗并提升用户体验。
系统设计
1. 数据库设计
```sql
-- 商品表扩展
ALTER TABLE products ADD COLUMN production_date DATE;
ALTER TABLE products ADD COLUMN shelf_life_days INT; -- 保质期天数
ALTER TABLE products ADD COLUMN expiration_date DATE GENERATED ALWAYS AS
(DATE_ADD(production_date, INTERVAL shelf_life_days DAY)) STORED;
ALTER TABLE products ADD COLUMN warning_threshold_days INT DEFAULT 3; -- 提前多少天警告
-- 临期商品提醒记录表
CREATE TABLE expiration_warnings (
id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT NOT NULL,
warning_date DATETIME DEFAULT CURRENT_TIMESTAMP,
warning_level ENUM(warning, urgent) NOT NULL, -- 警告级别
status ENUM(unread, read, processed) DEFAULT unread,
FOREIGN KEY (product_id) REFERENCES products(id)
);
```
2. 核心功能实现
2.1 临期商品检测服务
```java
// Java示例代码
public class ExpirationWarningService {
@Autowired
private ProductRepository productRepository;
@Autowired
private ExpirationWarningRepository warningRepository;
// 每日定时任务检测临期商品
@Scheduled(cron = "0 0 8 * * ?") // 每天早上8点执行
public void checkExpiringProducts() {
LocalDate today = LocalDate.now();
// 查询所有商品
List
products = productRepository.findAll();
for (Product product : products) {
if (product.getExpirationDate() == null) continue;
long daysUntilExpiration = ChronoUnit.DAYS.between(today, product.getExpirationDate());
int threshold = product.getWarningThresholdDays() != null ?
product.getWarningThresholdDays() : 3;
if (daysUntilExpiration <= threshold && daysUntilExpiration >= 0) {
// 确定警告级别
String warningLevel;
if (daysUntilExpiration == 0) {
warningLevel = "urgent"; // 今天过期
} else {
warningLevel = "warning"; // 即将过期
}
// 创建提醒记录
ExpirationWarning warning = new ExpirationWarning();
warning.setProductId(product.getId());
warning.setWarningLevel(warningLevel);
warningRepository.save(warning);
// 发送通知(可选)
sendNotification(product, daysUntilExpiration);
}
}
}
private void sendNotification(Product product, long daysRemaining) {
// 实现通知逻辑,如邮件、短信或系统内消息
String message = String.format(
"商品 %s (ID: %d) 将在 %d 天后过期",
product.getName(), product.getId(), daysRemaining);
// 通知逻辑...
}
}
```
3. 前端展示
3.1 管理后台临期商品列表
```javascript
// Vue.js示例
export default {
data() {
return {
expiringProducts: [],
warningLevels: {
urgent: 紧急,
warning: 警告
}
}
},
created() {
this.fetchExpiringProducts();
},
methods: {
async fetchExpiringProducts() {
const response = await axios.get(/api/expiring-products);
this.expiringProducts = response.data.map(item => ({
...item,
daysRemaining: this.calculateDaysRemaining(item.expirationDate)
}));
},
calculateDaysRemaining(expDate) {
const today = new Date();
const expiry = new Date(expDate);
const diffTime = expiry - today;
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
},
getWarningClass(level) {
return level === urgent ? text-red-500 : text-yellow-500;
}
}
}
```
3.2 用户端提示(可选)
```html
该商品即将过期,剩余{{ daysRemaining }}天
<style>
.expiring-warning {
color: ff9800;
background-color: fff3e0;
padding: 8px;
border-radius: 4px;
margin-bottom: 12px;
}
```
4. 移动端推送(可选)
```javascript
// 推送临期商品提醒
function sendExpiringNotification(userId, productName, daysRemaining) {
const message = `您购买的${productName}还有${daysRemaining}天即将过期,请及时食用`;
// 调用推送服务API
pushService.send({
userId: userId,
title: 商品临期提醒,
body: message,
data: {
productId: productId,
type: expiration_warning
}
}).then(response => {
console.log(推送发送成功, response);
}).catch(error => {
console.error(推送发送失败, error);
});
}
```
实施步骤
1. 数据库改造:为商品表添加生产日期、保质期和过期日期字段
2. 定时任务开发:创建每日运行的定时任务检测临期商品
3. 提醒记录系统:开发提醒记录存储和查询功能
4. 管理后台集成:在管理后台添加临期商品管理页面
5. 用户端集成(可选):在用户端展示临期商品提示
6. 通知系统集成:集成邮件、短信或推送通知功能
7. 测试验证:进行全面测试确保功能正确性
高级功能扩展
1. 批量处理功能:允许管理员批量处理临期商品(如打折、下架)
2. 智能推荐:根据用户购买历史推荐临期商品
3. 多级预警:设置不同级别的预警阈值(如7天、3天、1天)
4. 报表生成:生成临期商品统计报表
5. 自动化策略:自动将临期商品加入促销活动
注意事项
1. 时区处理:确保所有日期计算考虑系统时区
2. 性能优化:对于商品量大的系统,考虑分批处理
3. 通知频率:避免过度通知造成用户困扰
4. 数据安全:确保临期商品数据的安全存储和传输
5. 用户体验:临期提示应明显但不突兀
通过实现上述功能,小象买菜系统可以有效管理临期商品,减少损耗,同时提升用户满意度和信任度。