You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
218 lines
5.2 KiB
218 lines
5.2 KiB
// 更新商品审核状态脚本 - 将商品从pending_review改为reviewed
|
|
const { Sequelize, DataTypes, Model } = require('sequelize');
|
|
require('dotenv').config();
|
|
const readline = require('readline');
|
|
|
|
// 创建读取用户输入的接口
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout
|
|
});
|
|
|
|
// MySQL数据库连接配置
|
|
const sequelize = new Sequelize(
|
|
process.env.DB_DATABASE || 'wechat_app',
|
|
process.env.DB_USER || 'root',
|
|
process.env.DB_PASSWORD === undefined ? null : process.env.DB_PASSWORD,
|
|
{
|
|
host: process.env.DB_HOST || 'localhost',
|
|
port: process.env.DB_PORT || 3306,
|
|
dialect: 'mysql',
|
|
pool: {
|
|
max: 10,
|
|
min: 0,
|
|
acquire: 30000,
|
|
idle: 10000
|
|
},
|
|
timezone: '+08:00' // 设置时区为UTC+8
|
|
}
|
|
);
|
|
|
|
// 定义Product模型
|
|
class Product extends Model { }
|
|
Product.init({
|
|
id: {
|
|
type: DataTypes.INTEGER,
|
|
autoIncrement: true,
|
|
primaryKey: true
|
|
},
|
|
productId: {
|
|
type: DataTypes.STRING(100),
|
|
allowNull: false,
|
|
unique: true
|
|
},
|
|
sellerId: {
|
|
type: DataTypes.STRING(100),
|
|
allowNull: false
|
|
},
|
|
productName: {
|
|
type: DataTypes.STRING(255),
|
|
allowNull: false
|
|
},
|
|
status: {
|
|
type: DataTypes.STRING(20),
|
|
defaultValue: 'pending_review',
|
|
validate: {
|
|
isIn: [['pending_review', 'reviewed', 'published', 'sold_out', 'rejected', 'hidden']]
|
|
}
|
|
},
|
|
created_at: {
|
|
type: DataTypes.DATE,
|
|
defaultValue: Sequelize.NOW
|
|
},
|
|
updated_at: {
|
|
type: DataTypes.DATE,
|
|
defaultValue: Sequelize.NOW,
|
|
onUpdate: Sequelize.NOW
|
|
}
|
|
}, {
|
|
sequelize,
|
|
modelName: 'Product',
|
|
tableName: 'products',
|
|
timestamps: false
|
|
});
|
|
|
|
// 测试数据库连接
|
|
async function testDbConnection() {
|
|
try {
|
|
await sequelize.authenticate();
|
|
console.log('数据库连接成功');
|
|
} catch (error) {
|
|
console.error('数据库连接失败:', error);
|
|
console.error('请检查.env文件中的数据库配置是否正确');
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// 获取所有待审核商品
|
|
async function getPendingReviewProducts() {
|
|
try {
|
|
const products = await Product.findAll({
|
|
where: {
|
|
status: 'pending_review'
|
|
},
|
|
attributes: ['id', 'productId', 'productName', 'created_at']
|
|
});
|
|
return products;
|
|
} catch (error) {
|
|
console.error('获取待审核商品失败:', error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
// 更新单个商品状态
|
|
async function updateSingleProduct(productId) {
|
|
try {
|
|
const result = await Product.update(
|
|
{
|
|
status: 'reviewed',
|
|
updated_at: new Date()
|
|
},
|
|
{
|
|
where: {
|
|
productId: productId
|
|
}
|
|
}
|
|
);
|
|
|
|
if (result[0] > 0) {
|
|
console.log(`成功更新商品状态: ${productId}`);
|
|
return true;
|
|
} else {
|
|
console.log(`未找到商品: ${productId} 或该商品状态不是待审核`);
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
console.error(`更新商品状态失败: ${productId}`, error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 更新所有待审核商品状态
|
|
async function updateAllProducts() {
|
|
try {
|
|
const result = await Product.update(
|
|
{
|
|
status: 'reviewed',
|
|
updated_at: new Date()
|
|
},
|
|
{
|
|
where: {
|
|
status: 'pending_review'
|
|
}
|
|
}
|
|
);
|
|
|
|
console.log(`成功更新 ${result[0]} 个商品的状态`);
|
|
return result[0];
|
|
} catch (error) {
|
|
console.error('批量更新商品状态失败:', error);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
// 主函数
|
|
async function main() {
|
|
try {
|
|
await testDbConnection();
|
|
|
|
// 获取待审核商品列表
|
|
const pendingProducts = await getPendingReviewProducts();
|
|
|
|
if (pendingProducts.length === 0) {
|
|
console.log('当前没有待审核的商品');
|
|
rl.close();
|
|
process.exit(0);
|
|
}
|
|
|
|
console.log(`\n找到 ${pendingProducts.length} 个待审核的商品:`);
|
|
pendingProducts.forEach((product, index) => {
|
|
console.log(`${index + 1}. ID: ${product.productId}, 名称: ${product.productName}, 创建时间: ${product.created_at.toLocaleString()}`);
|
|
});
|
|
|
|
// 询问用户要更新单个还是所有商品
|
|
rl.question('\n请选择操作 (1: 更新单个商品, 2: 更新所有商品, 0: 退出): ', async (choice) => {
|
|
switch (choice) {
|
|
case '1':
|
|
rl.question('请输入要更新的商品ID: ', async (productId) => {
|
|
await updateSingleProduct(productId);
|
|
rl.close();
|
|
});
|
|
break;
|
|
|
|
case '2':
|
|
rl.question('确定要更新所有待审核商品的状态吗? (y/n): ', async (confirm) => {
|
|
if (confirm.toLowerCase() === 'y') {
|
|
await updateAllProducts();
|
|
} else {
|
|
console.log('已取消操作');
|
|
}
|
|
rl.close();
|
|
});
|
|
break;
|
|
|
|
case '0':
|
|
console.log('已退出');
|
|
rl.close();
|
|
break;
|
|
|
|
default:
|
|
console.log('无效的选择');
|
|
rl.close();
|
|
}
|
|
});
|
|
|
|
rl.on('close', () => {
|
|
console.log('\n操作已完成,小程序中刷新后即可看到已上架的货源');
|
|
process.exit(0);
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('程序执行出错:', error);
|
|
rl.close();
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// 启动程序
|
|
main();
|