136 lines
3.8 KiB
JavaScript
136 lines
3.8 KiB
JavaScript
/**
|
||
* 迁移现有价格套餐数据到数据库
|
||
* 将 config.js 中硬编码的 4 个套餐数据导入到 pricing_plans 表
|
||
*/
|
||
|
||
const Framework = require('../framework/node-core-framework.js');
|
||
const frameworkConfig = require('../config/framework.config.js');
|
||
|
||
async function migratePricingPlans() {
|
||
console.log('🔄 开始迁移价格套餐数据...\n');
|
||
|
||
try {
|
||
// 初始化框架
|
||
console.log('正在初始化框架...');
|
||
const framework = await Framework.init(frameworkConfig);
|
||
const models = Framework.getModels();
|
||
|
||
if (!models || !models.pricing_plans) {
|
||
throw new Error('无法获取 pricing_plans 模型');
|
||
}
|
||
|
||
const { pricing_plans } = models;
|
||
|
||
// 检查是否已有数据
|
||
const existingCount = await pricing_plans.count({ where: { is_delete: 0 } });
|
||
if (existingCount > 0) {
|
||
console.log(`⚠️ 已存在 ${existingCount} 条套餐数据,跳过迁移\n`);
|
||
return;
|
||
}
|
||
|
||
// 现有的4个套餐数据(来自 api/controller_front/config.js)
|
||
const plans = [
|
||
{
|
||
name: '体验套餐',
|
||
duration: '7天',
|
||
days: 7,
|
||
price: 28.00,
|
||
original_price: 28.00,
|
||
unit: '元',
|
||
discount: null,
|
||
features: JSON.stringify(['7天使用权限', '全功能体验', '技术支持']),
|
||
featured: 0,
|
||
is_active: 1,
|
||
sort_order: 1,
|
||
is_delete: 0,
|
||
create_time: new Date(),
|
||
last_modify_time: new Date()
|
||
},
|
||
{
|
||
name: '月度套餐',
|
||
duration: '30天',
|
||
days: 30,
|
||
price: 99.00,
|
||
original_price: 120.00,
|
||
unit: '元',
|
||
discount: '约8.3折',
|
||
features: JSON.stringify(['30天使用权限', '全功能使用', '优先技术支持', '性价比最高']),
|
||
featured: 1,
|
||
is_active: 1,
|
||
sort_order: 2,
|
||
is_delete: 0,
|
||
create_time: new Date(),
|
||
last_modify_time: new Date()
|
||
},
|
||
{
|
||
name: '季度套餐',
|
||
duration: '90天',
|
||
days: 90,
|
||
price: 269.00,
|
||
original_price: 360.00,
|
||
unit: '元',
|
||
discount: '7.5折',
|
||
features: JSON.stringify(['90天使用权限', '全功能使用', '优先技术支持', '更优惠价格']),
|
||
featured: 0,
|
||
is_active: 1,
|
||
sort_order: 3,
|
||
is_delete: 0,
|
||
create_time: new Date(),
|
||
last_modify_time: new Date()
|
||
},
|
||
{
|
||
name: '终生套餐',
|
||
duration: '永久',
|
||
days: -1,
|
||
price: 888.00,
|
||
original_price: null,
|
||
unit: '元',
|
||
discount: '超值',
|
||
features: JSON.stringify(['永久使用权限', '全功能使用', '终身技术支持', '一次购买,终身使用', '最划算选择']),
|
||
featured: 0,
|
||
is_active: 1,
|
||
sort_order: 4,
|
||
is_delete: 0,
|
||
create_time: new Date(),
|
||
last_modify_time: new Date()
|
||
}
|
||
];
|
||
|
||
// 批量插入
|
||
await pricing_plans.bulkCreate(plans);
|
||
console.log('✅ 成功迁移 4 条价格套餐数据!\n');
|
||
|
||
// 验证插入结果
|
||
const result = await pricing_plans.findAll({
|
||
where: { is_delete: 0 },
|
||
order: [['sort_order', 'ASC']]
|
||
});
|
||
|
||
console.log('📋 已迁移的套餐:');
|
||
result.forEach(plan => {
|
||
const planData = plan.toJSON();
|
||
console.log(` ${planData.id}. ${planData.name} - ${planData.duration} - ¥${planData.price}`);
|
||
if (planData.featured === 1) {
|
||
console.log(` [推荐套餐]`);
|
||
}
|
||
});
|
||
console.log('');
|
||
|
||
} catch (error) {
|
||
console.error('❌ 迁移失败:', error.message);
|
||
console.error('\n详细错误:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
// 执行迁移
|
||
migratePricingPlans()
|
||
.then(() => {
|
||
console.log('✨ 迁移完成!');
|
||
process.exit(0);
|
||
})
|
||
.catch(error => {
|
||
console.error('\n💥 执行失败:', error);
|
||
process.exit(1);
|
||
});
|