const sequelize = require('./db'); const { Region, Apartment, Room, Tenant, Contract, Rental } = require('../models'); // 同步数据库表结构 const syncDatabase = async () => { try { console.log('正在同步数据库表结构...'); await sequelize.sync({ force: true }); console.log('数据库表结构同步成功'); // 插入初始数据 await insertInitialData(); } catch (error) { console.error('数据库表结构同步失败:', error); } finally { await sequelize.close(); } }; // 插入初始数据 const insertInitialData = async () => { try { // 插入区域数据 const regions = await Region.bulkCreate([ { id: 1, name: '大商汇', description: '大商汇区域', createTime: '2023-01-06' }, { id: 2, name: '丰源市场', description: '丰源市场区域', createTime: '2023-01-07' } ]); // 插入公寓数据 const apartments = await Apartment.bulkCreate([ { id: 1, regionId: 1, name: '爱奇艺公寓', address: '大商汇区域', createTime: '2023-01-18' }, { id: 2, regionId: 2, name: '碧云公寓', address: '丰源市场区域', createTime: '2023-01-19' } ]); // 插入房间数据 const rooms = await Room.bulkCreate([ // 大商汇 - 爱奇艺公寓 { id: 1, apartmentId: 1, roomNumber: '401', area: 40, price: 2500, status: 'empty', createTime: '2023-01-18' }, { id: 2, apartmentId: 1, roomNumber: '402', area: 40, price: 2500, status: 'empty', createTime: '2023-01-18' }, { id: 3, apartmentId: 1, roomNumber: '403', area: 45, price: 2800, status: 'rented', createTime: '2023-01-18' }, { id: 4, apartmentId: 1, roomNumber: '404', area: 45, price: 2800, status: 'soon_expire', createTime: '2023-01-18' }, { id: 5, apartmentId: 1, roomNumber: '405', area: 50, price: 3000, status: 'empty', createTime: '2023-01-18' }, { id: 6, apartmentId: 1, roomNumber: '406', area: 50, price: 3000, status: 'expired', createTime: '2023-01-18' }, { id: 7, apartmentId: 1, roomNumber: '407', area: 55, price: 3200, status: 'cleaning', createTime: '2023-01-18' }, // 丰源市场 - 碧云公寓 { id: 8, apartmentId: 2, roomNumber: '201', area: 35, price: 2200, status: 'empty', createTime: '2023-01-19' }, { id: 9, apartmentId: 2, roomNumber: '202', area: 35, price: 2200, status: 'rented', createTime: '2023-01-19' }, { id: 10, apartmentId: 2, roomNumber: '203', area: 40, price: 2400, status: 'empty', createTime: '2023-01-19' }, { id: 11, apartmentId: 2, roomNumber: '205', area: 40, price: 2400, status: 'maintenance', createTime: '2023-01-19' }, { id: 12, apartmentId: 2, roomNumber: '206', area: 45, price: 2600, status: 'rented', createTime: '2023-01-19' }, { id: 13, apartmentId: 2, roomNumber: '208', area: 45, price: 2600, status: 'empty', createTime: '2023-01-19' }, { id: 14, apartmentId: 2, roomNumber: '209', area: 50, price: 2800, status: 'soon_expire', createTime: '2023-01-19' } ]); // 插入租客数据 const tenants = await Tenant.bulkCreate([ { id: 1, name: '张三', phone: '13800138001', idCard: '110101199001011234', createTime: '2023-02-01' }, { id: 2, name: '李四', phone: '13800138002', idCard: '110101199001011235', createTime: '2023-02-02' }, { id: 3, name: '王五', phone: '13800138003', idCard: '110101199001011236', createTime: '2023-02-03' }, { id: 4, name: '赵六', phone: '13800138004', idCard: '110101199001011237', createTime: '2023-02-04' }, { id: 5, name: '钱七', phone: '13800138005', idCard: '110101199001011238', createTime: '2023-02-05' } ]); // 插入合同数据 const contracts = await Contract.bulkCreate([ { id: 1, roomId: 3, tenantId: 1, startDate: '2023-03-01', endDate: '2024-03-01', rent: 2800, deposit: 5600, status: 'active', createTime: '2023-02-28' }, { id: 2, roomId: 9, tenantId: 2, startDate: '2023-04-01', endDate: '2024-04-01', rent: 2200, deposit: 4400, status: 'active', createTime: '2023-03-31' }, { id: 3, roomId: 12, tenantId: 3, startDate: '2023-05-01', endDate: '2024-05-01', rent: 2600, deposit: 5200, status: 'active', createTime: '2023-04-30' }, { id: 4, roomId: 4, tenantId: 4, startDate: '2022-03-01', endDate: '2023-03-01', rent: 2800, deposit: 5600, status: 'expired', createTime: '2022-02-28' }, { id: 5, roomId: 6, tenantId: 5, startDate: '2022-04-01', endDate: '2023-04-01', rent: 3000, deposit: 6000, status: 'expired', createTime: '2022-03-31' } ]); // 插入租房数据 const rentals = await Rental.bulkCreate([ { id: 1, roomId: 3, tenantId: 1, contractId: 1, startDate: '2023-03-01', endDate: '2024-03-01', rent: 2800, deposit: 5600, status: 'active', createTime: '2023-02-28' }, { id: 2, roomId: 9, tenantId: 2, contractId: 2, startDate: '2023-04-01', endDate: '2024-04-01', rent: 2200, deposit: 4400, status: 'active', createTime: '2023-03-31' }, { id: 3, roomId: 12, tenantId: 3, contractId: 3, startDate: '2023-05-01', endDate: '2024-05-01', rent: 2600, deposit: 5200, status: 'active', createTime: '2023-04-30' }, { id: 4, roomId: 4, tenantId: 4, contractId: 4, startDate: '2022-03-01', endDate: '2023-03-01', rent: 2800, deposit: 5600, status: 'expired', createTime: '2022-02-28' }, { id: 5, roomId: 6, tenantId: 5, contractId: 5, startDate: '2022-04-01', endDate: '2023-04-01', rent: 3000, deposit: 6000, status: 'expired', createTime: '2022-03-31' } ]); console.log('初始数据插入成功'); } catch (error) { console.error('初始数据插入失败:', error); } }; syncDatabase();