rentease-backend/scripts/add-rooms.js

71 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { Region, Apartment, Room } = require('../models');
// 房间列表
const roomNumbers = [
'402', '403', '404', '405', '406', '407', '408', '409', '410', '411', '412', '413',
'501', '502', '503', '504', '505', '506', '507', '508', '509', '510', '511', '512', '513'
];
async function addRooms() {
try {
console.log('开始添加房间...');
// 查找或创建爱奇艺公寓
let apartment = await Apartment.findOne({ where: { name: '爱奇艺公寓' } });
if (!apartment) {
console.log('爱奇艺公寓不存在,创建中...');
// 查找或创建大商汇区域
let region = await Region.findOne({ where: { name: '大商汇' } });
if (!region) {
region = await Region.create({ name: '大商汇', description: '大商汇区域' });
console.log('创建了大商汇区域');
}
// 创建爱奇艺公寓
apartment = await Apartment.create({
regionId: region.id,
name: '爱奇艺公寓',
address: '大商汇区域'
});
console.log('创建了爱奇艺公寓');
} else {
console.log('找到爱奇艺公寓ID:', apartment.id);
}
// 添加房间
console.log('开始添加房间...');
for (const roomNumber of roomNumbers) {
// 检查房间是否已存在
const existingRoom = await Room.findOne({
where: {
apartmentId: apartment.id,
roomNumber: roomNumber
}
});
if (!existingRoom) {
await Room.create({
apartmentId: apartment.id,
roomNumber: roomNumber,
area: 25, // 默认面积
price: 2000, // 默认价格
status: 'empty' // 空房状态
});
console.log(`添加了房间: ${roomNumber}`);
} else {
console.log(`房间 ${roomNumber} 已存在,跳过`);
}
}
console.log('房间添加完成!');
process.exit(0);
} catch (error) {
console.error('添加房间时出错:', error);
process.exit(1);
}
}
// 执行脚本
addRooms();