功能概述
临期商品提示功能旨在帮助小象买菜系统识别即将过期的商品,及时提醒管理人员和顾客,减少商品损耗并提升用户体验。
系统设计
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; -- 计算得出的过期日期
-- 临期商品提醒表
CREATE TABLE near_expiry_alerts (
id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT NOT NULL,
alert_level ENUM(warning, urgent) NOT NULL, -- 提醒级别
alert_date DATETIME DEFAULT CURRENT_TIMESTAMP,
is_handled BOOLEAN DEFAULT FALSE,
FOREIGN KEY (product_id) REFERENCES products(id)
);
```
2. 核心功能模块
2.1 临期商品计算服务
```java
public class ExpirationAlertService {
@Autowired
private ProductRepository productRepository;
@Autowired
private AlertRepository alertRepository;
// 每日定时任务计算临期商品
@Scheduled(cron = "0 0 2 * * ?") // 每天凌晨2点执行
public void calculateNearExpiryProducts() {
LocalDate today = LocalDate.now();
// 查询所有商品
List
products = productRepository.findAll();
for (Product product : products) {
if (product.getExpirationDate() == null) {
calculateExpirationDate(product); // 如果未计算过期日期,先计算
}
long daysToExpire = ChronoUnit.DAYS.between(today, product.getExpirationDate());
// 判断临期级别
if (daysToExpire <= 3 && daysToExpire >= 0) { // 3天内过期
createAlert(product, daysToExpire <= 1 ? "urgent" : "warning");
}
}
}
private void calculateExpirationDate(Product product) {
if (product.getProductionDate() != null && product.getShelfLifeDays() != null) {
product.setExpirationDate(product.getProductionDate().plusDays(product.getShelfLifeDays()));
productRepository.save(product);
}
}
private void createAlert(Product product, String alertLevel) {
NearExpiryAlert existingAlert = alertRepository.findByProductIdAndIsHandled(product.getId(), false);
if (existingAlert == null) {
NearExpiryAlert alert = new NearExpiryAlert();
alert.setProductId(product.getId());
alert.setAlertLevel(alertLevel);
alertRepository.save(alert);
}
}
}
```
2.2 提醒方式实现
管理员提醒:
```java
public class AdminAlertNotifier {
@Autowired
private AlertRepository alertRepository;
@Autowired
private EmailService emailService;
@Autowired
private SmsService smsService;
public void notifyAdminsAboutNearExpiry() {
List urgentAlerts = alertRepository.findByAlertLevelAndIsHandled("urgent", false);
if (!urgentAlerts.isEmpty()) {
String subject = "紧急:有商品即将过期!";
String body = generateAlertMessage(urgentAlerts);
// 发送邮件给管理员
emailService.sendToAdmins(subject, body);
// 发送短信提醒
smsService.sendToAdmins("系统检测到紧急临期商品,请立即处理!");
}
}
private String generateAlertMessage(List alerts) {
StringBuilder sb = new StringBuilder();
sb.append("以下商品即将过期,请及时处理:\n\n");
for (NearExpiryAlert alert : alerts) {
Product product = productRepository.findById(alert.getProductId()).orElse(null);
if (product != null) {
sb.append(String.format("商品: %s, 过期日期: %s, 剩余天数: %d\n",
product.getName(),
product.getExpirationDate(),
ChronoUnit.DAYS.between(LocalDate.now(), product.getExpirationDate())));
}
}
return sb.toString();
}
}
```
顾客端提醒:
```javascript
// 前端展示临期商品标签
function displayExpiryTags() {
// 假设从API获取商品列表
fetch(/api/products)
.then(response => response.json())
.then(products => {
products.forEach(product => {
const expiryDate = new Date(product.expirationDate);
const today = new Date();
const timeDiff = expiryDate.getTime() - today.getTime();
const daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24));
const productElement = document.getElementById(`product-${product.id}`);
if (productElement) {
if (daysDiff <= 3) {
const tag = document.createElement(span);
tag.className = expiry-tag;
tag.textContent = daysDiff <= 1 ? 明日过期 : `剩余${daysDiff}天`;
tag.style.backgroundColor = daysDiff <= 1 ? ff4d4f : faad14;
productElement.appendChild(tag);
}
}
});
});
});
```
3. 用户界面设计
3.1 管理员界面
- 临期商品列表页面,显示所有临期商品及其状态
- 可按临期级别、商品类别等筛选
- 提供处理按钮(如折扣销售、下架等)
3.2 顾客界面
- 商品列表页显示临期标签
- 搜索结果中突出显示临期商品
- 购物车中提醒已选临期商品
技术实现要点
1. 定时任务:使用Spring的@Scheduled或Quartz实现每日临期商品计算
2. 缓存优化:对频繁查询的临期商品数据进行缓存
3. 消息队列:使用RabbitMQ或Kafka处理高并发时的提醒通知
4. API设计:
```
GET /api/products/near-expiry?days=3 // 获取3天内过期的商品
POST /api/alerts/{id}/handle // 处理临期提醒
```
测试方案
1. 单元测试:测试临期计算逻辑是否正确
2. 集成测试:测试定时任务是否按预期执行
3. 压力测试:模拟大量商品数据下的系统性能
4. 用户验收测试:验证提醒是否及时准确显示
部署与监控
1. 日志记录:记录临期商品处理过程
2. 监控指标:监控临期商品数量变化趋势
3. 告警机制:当临期商品数量超过阈值时触发告警
扩展功能
1. 智能促销:根据临期商品自动生成折扣方案
2. 预测分析:基于历史数据预测临期商品高峰期
3. 供应链联动:与供应商系统对接,优化补货策略
通过以上实现,小象买菜系统可以有效管理临期商品,减少损耗,同时提升顾客购物体验。