一、促销活动时间设置核心需求
1. 灵活的时间配置:支持按天、小时、分钟设置促销时段
2. 多活动并行管理:不同商品/品类可设置不同促销时段
3. 时区自适应:支持多地区商城按当地时间执行
4. 预热与缓冲期:支持活动开始前的预热展示和结束后的缓冲期
二、万象源码部署下的精准控制实现
1. 数据库设计优化
```sql
CREATE TABLE promotion_activities (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
start_time DATETIME NOT NULL,
end_time DATETIME NOT NULL,
time_zone VARCHAR(50) DEFAULT UTC+8,
status TINYINT DEFAULT 0 COMMENT 0-未开始 1-进行中 2-已结束,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE promotion_rules (
id INT AUTO_INCREMENT PRIMARY KEY,
activity_id INT NOT NULL,
product_id INT,
category_id INT,
discount_type ENUM(percentage,fixed) NOT NULL,
discount_value DECIMAL(10,2) NOT NULL,
daily_start_time TIME,
daily_end_time TIME,
weekdays_available VARCHAR(7) COMMENT 如1111100表示周一到周五,
FOREIGN KEY (activity_id) REFERENCES promotion_activities(id)
);
```
2. 核心控制逻辑实现
```php
// 万象源码核心控制类示例
class PromotionController {
// 检查商品是否在促销期内
public function isProductOnPromotion($productId, $currentTime = null) {
$currentTime = $currentTime ?: new DateTime();
$currentZone = $this->getUserTimeZone(); // 获取用户时区
// 查询所有有效活动
$activeActivities = $this->getActiveActivities($currentTime, $currentZone);
foreach ($activeActivities as $activity) {
// 检查商品是否参与该活动
$rules = $this->getPromotionRules($activity[id], $productId);
foreach ($rules as $rule) {
// 检查每日时段
if ($rule[daily_start_time] && $rule[daily_end_time]) {
$dailyStart = DateTime::createFromFormat(H:i, $rule[daily_start_time]);
$dailyEnd = DateTime::createFromFormat(H:i, $rule[daily_end_time]);
$now = clone $currentTime;
$now->setTime($dailyStart->format(H), $dailyStart->format(i));
$dailyStart = $now;
$now = clone $currentTime;
$now->setTime($dailyEnd->format(H), $dailyEnd->format(i));
$dailyEnd = $now;
if ($currentTime < $dailyStart || $currentTime > $dailyEnd) {
continue; // 不在每日时段内
}
}
// 检查星期几
if ($rule[weekdays_available]) {
$weekday = $currentTime->format(N); // 1(周一)到7(周日)
$available = substr($rule[weekdays_available], $weekday-1, 1) == 1;
if (!$available) {
continue;
}
}
return [
activity_id => $activity[id],
discount_type => $rule[discount_type],
discount_value => $rule[discount_value],
activity_name => $activity[name]
];
}
}
return false;
}
private function getActiveActivities($currentTime, $timeZone) {
// 实现从数据库获取当前时区下有效的活动
// 考虑时区转换和活动状态
}
}
```
3. 前端展示控制
```javascript
// 前端促销标签显示逻辑
function showPromotionTag(product) {
// 调用API检查当前是否在促销期
fetch(`/api/check-promotion/${product.id}`)
.then(response => response.json())
.then(data => {
if (data.is_on_promotion) {
const tag = document.createElement(div);
tag.className = promotion-tag;
// 显示剩余时间
if (data.end_time) {
const endTime = new Date(data.end_time);
updateCountdown(tag, endTime);
setInterval(() => updateCountdown(tag, endTime), 60000);
}
productElement.appendChild(tag);
}
});
}
function updateCountdown(element, endTime) {
const now = new Date();
const diff = endTime - now;
if (diff > 0) {
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
element.innerHTML = `促销中 剩余: ${days}天 ${hours}小时 ${minutes}分钟`;
} else {
element.innerHTML = 促销已结束;
}
}
```
三、部署与运维建议
1. 定时任务设置:
- 使用cron作业每天检查即将开始/结束的活动
- 提前生成静态促销页面缓存
2. 缓存策略:
```php
// 促销数据缓存示例
public function getCachedPromotions() {
$cacheKey = promotions_ . date(Ymd);
$promotions = $this->cache->get($cacheKey);
if (!$promotions) {
$promotions = $this->getAllActivePromotions();
$this->cache->put($cacheKey, $promotions, 1440); // 缓存24小时
}
return $promotions;
}
```
3. 监控与告警:
- 设置监控检查是否有活动未按时开始/结束
- 监控促销页面加载时间
4. AB测试支持:
- 在源码中预留接口支持不同时段展示不同促销策略
四、高级功能扩展
1. 智能时段推荐:
- 基于历史销售数据推荐最佳促销时段
- 按用户访问高峰自动调整展示时间
2. 动态定价集成:
- 结合库存和时段实现动态折扣
- 临近保质期商品自动加大折扣
3. 社交裂变支持:
- 特定时段分享得额外优惠
- 限时拼团功能
通过以上方案,水果商城系统可以实现精细化的促销时间控制,在万象源码部署环境下确保促销活动按时启动和结束,同时提供良好的用户体验和系统性能。