rentease-backend/scripts/add-rooms-m沐航.js

72 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 = [
'201', '202', '203', '205', '206',
'301', '302', '303', '305', '306',
'401', '402', '403', '405', '406'
];
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();