rentease-backend-new/database_schema.sql

600 lines
32 KiB
MySQL
Raw Permalink Normal View History

2026-04-20 06:43:09 +00:00
-- ============================================
-- RentEase 租房管理系统 - 数据库表结构
-- 生成时间: 2025-01-11
-- ============================================
-- 设置字符集
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ============================================
-- 1. 租户相关表
-- ============================================
-- 租户表 (SaaS系统租户)
DROP TABLE IF EXISTS `tenants`;
CREATE TABLE `tenants` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '租户ID',
`code` varchar(50) NOT NULL COMMENT '租户编码(唯一标识)',
`contactName` varchar(50) DEFAULT NULL COMMENT '联系人姓名',
`contactPhone` varchar(20) DEFAULT NULL COMMENT '联系人电话',
`contactEmail` varchar(100) DEFAULT NULL COMMENT '联系人邮箱',
`status` enum('active','suspended') NOT NULL DEFAULT 'active' COMMENT '租户状态active-正常suspended-暂停',
`billingStatus` enum('trial_active','trial_expired','paid_active','paid_expired','suspended') NOT NULL DEFAULT 'trial_active' COMMENT '计费状态',
`planId` int(11) DEFAULT NULL COMMENT '当前套餐ID',
`trialStartDate` datetime DEFAULT NULL COMMENT '试用期开始日期',
`trialEndDate` datetime DEFAULT NULL COMMENT '试用期结束日期',
`paidStartDate` datetime DEFAULT NULL COMMENT '付费期开始日期',
`paidEndDate` datetime DEFAULT NULL COMMENT '付费期结束日期',
`currentPeriodStart` datetime DEFAULT NULL COMMENT '当前计费周期开始',
`currentPeriodEnd` datetime DEFAULT NULL COMMENT '当前计费周期结束',
`maxUsers` int(11) NOT NULL DEFAULT '10' COMMENT '最大用户数',
`maxApartments` int(11) NOT NULL DEFAULT '5' COMMENT '最大公寓数',
`maxRooms` int(11) NOT NULL DEFAULT '50' COMMENT '最大房间数',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `code` (`code`),
KEY `idx_status` (`status`),
KEY `idx_billing_status` (`billingStatus`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='租户表';
-- ============================================
-- 2. 用户权限相关表
-- ============================================
-- 用户表
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`username` varchar(50) NOT NULL COMMENT '用户名',
`password` varchar(255) NOT NULL COMMENT '密码(加密存储)',
`nickname` varchar(50) DEFAULT NULL COMMENT '昵称',
`avatar` varchar(255) DEFAULT NULL COMMENT '头像URL',
`roleId` int(11) DEFAULT NULL COMMENT '角色ID',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`userType` enum('super_admin','tenant_admin','user') NOT NULL DEFAULT 'user' COMMENT '用户类型super_admin-系统管理员tenant_admin-租户管理员user-普通用户',
`status` enum('active','inactive') NOT NULL DEFAULT 'active' COMMENT '状态active-启用inactive-禁用',
`lastLoginTime` datetime DEFAULT NULL COMMENT '最后登录时间',
`lastLoginIp` varchar(50) DEFAULT NULL COMMENT '最后登录IP',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_role` (`roleId`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
-- 角色表
DROP TABLE IF EXISTS `roles`;
CREATE TABLE `roles` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色ID',
`name` varchar(50) NOT NULL COMMENT '角色名称',
`code` varchar(50) NOT NULL COMMENT '角色编码',
`description` varchar(255) DEFAULT NULL COMMENT '角色描述',
`permissions` json DEFAULT NULL COMMENT '权限列表JSON格式',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`status` enum('active','inactive') NOT NULL DEFAULT 'active' COMMENT '状态active-启用inactive-禁用',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_code` (`code`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色表';
-- 菜单表
DROP TABLE IF EXISTS `menus`;
CREATE TABLE `menus` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜单ID',
`parentId` int(11) DEFAULT NULL COMMENT '父菜单ID',
`name` varchar(50) NOT NULL COMMENT '菜单名称',
`code` varchar(50) NOT NULL COMMENT '菜单编码',
`type` enum('menu','button') NOT NULL DEFAULT 'menu' COMMENT '类型menu-菜单button-按钮',
`path` varchar(100) DEFAULT NULL COMMENT '路由路径',
`component` varchar(100) DEFAULT NULL COMMENT '组件路径',
`icon` varchar(50) DEFAULT NULL COMMENT '图标',
`sort` int(11) NOT NULL DEFAULT '0' COMMENT '排序号',
`visible` enum('show','hide') NOT NULL DEFAULT 'show' COMMENT '是否可见show-显示hide-隐藏',
`status` enum('active','inactive') NOT NULL DEFAULT 'active' COMMENT '状态active-启用inactive-禁用',
`isBasic` int(11) NOT NULL DEFAULT '0' COMMENT '是否基础菜单01新租户默认分配基础菜单',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_parent` (`parentId`),
KEY `idx_code` (`code`),
KEY `idx_type` (`type`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='菜单表';
-- 角色菜单关联表
DROP TABLE IF EXISTS `role_menus`;
CREATE TABLE `role_menus` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`roleId` int(11) NOT NULL COMMENT '角色ID',
`menuId` int(11) NOT NULL COMMENT '菜单ID',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_role_menu` (`roleId`,`menuId`),
KEY `idx_menu` (`menuId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色菜单关联表';
-- 操作日志表
DROP TABLE IF EXISTS `operation_logs`;
CREATE TABLE `operation_logs` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '日志ID',
`userId` int(11) DEFAULT NULL COMMENT '用户ID',
`username` varchar(50) DEFAULT NULL COMMENT '用户名',
`module` varchar(50) NOT NULL DEFAULT '' COMMENT '操作模块',
`action` varchar(50) NOT NULL DEFAULT '' COMMENT '操作类型',
`description` text COMMENT '操作描述',
`method` varchar(10) DEFAULT NULL COMMENT '请求方法',
`url` varchar(500) DEFAULT NULL COMMENT '请求URL',
`ip` varchar(50) DEFAULT NULL COMMENT 'IP地址',
`params` text COMMENT '请求参数',
`result` text COMMENT '操作结果',
`status` enum('success','fail') NOT NULL DEFAULT 'success' COMMENT '操作状态',
`duration` int(11) DEFAULT NULL COMMENT '执行时长(毫秒)',
`tenantId` int(11) DEFAULT NULL COMMENT '租户ID',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `idx_user` (`userId`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_createTime` (`createTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='操作日志表';
-- 登录日志表
DROP TABLE IF EXISTS `login_logs`;
CREATE TABLE `login_logs` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '日志ID',
`userId` int(11) DEFAULT NULL COMMENT '用户ID',
`username` varchar(50) DEFAULT NULL COMMENT '用户名',
`loginType` enum('login','logout') NOT NULL DEFAULT 'login' COMMENT '登录类型',
`ip` varchar(50) DEFAULT NULL COMMENT 'IP地址',
`userAgent` text COMMENT '浏览器信息',
`status` enum('success','fail') NOT NULL DEFAULT 'success' COMMENT '登录状态',
`message` varchar(255) DEFAULT NULL COMMENT '登录信息/失败原因',
`tenantId` int(11) DEFAULT NULL COMMENT '租户ID',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `idx_user` (`userId`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_createTime` (`createTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='登录日志表';
-- ============================================
-- 3. 房源相关表
-- ============================================
-- 公寓表
DROP TABLE IF EXISTS `apartments`;
CREATE TABLE `apartments` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '公寓ID',
`name` varchar(100) NOT NULL COMMENT '公寓名称',
`address` varchar(200) DEFAULT NULL COMMENT '地址',
`description` text COMMENT '描述',
`status` enum('active','inactive') NOT NULL DEFAULT 'active' COMMENT '状态active-启用inactive-禁用',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='公寓表';
-- 房间表
DROP TABLE IF EXISTS `rooms`;
CREATE TABLE `rooms` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '房间ID',
`apartmentId` int(11) NOT NULL COMMENT '所属公寓ID',
`roomNumber` varchar(50) NOT NULL COMMENT '房间号',
`floor` int(11) DEFAULT NULL COMMENT '楼层',
`roomType` varchar(50) DEFAULT NULL COMMENT '户型(如一室一厅、两室一厅等)',
`area` decimal(10,2) DEFAULT NULL COMMENT '面积(平方米)',
`orientation` varchar(20) DEFAULT NULL COMMENT '朝向',
`monthlyPrice` decimal(10,2) DEFAULT NULL COMMENT '月租金',
`yearlyPrice` decimal(10,2) DEFAULT NULL COMMENT '年租金',
`deposit` decimal(10,2) DEFAULT NULL COMMENT '押金',
`description` text COMMENT '描述',
`sortOrder` int(11) NOT NULL DEFAULT '0' COMMENT '排序字段',
`status` enum('empty','reserved','rented') NOT NULL DEFAULT 'empty' COMMENT '状态empty-空置reserved-预定rented-已租',
`rentalStatus` enum('normal','soon_expire','expired') DEFAULT 'normal' COMMENT '租约状态',
`tenantId` int(11) NOT NULL COMMENT '所属租户ID',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_apartment` (`apartmentId`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_status` (`status`),
KEY `idx_rental_status` (`rentalStatus`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='房间表';
-- ============================================
-- 4. 租赁相关表
-- ============================================
-- 租赁表
DROP TABLE IF EXISTS `rentals`;
CREATE TABLE `rentals` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '租赁ID',
`roomId` int(11) NOT NULL COMMENT '房间ID',
`renterId` int(11) NOT NULL COMMENT '租客ID',
`startDate` date NOT NULL COMMENT '开始日期',
`endDate` date NOT NULL COMMENT '结束日期',
`rent` decimal(10,2) NOT NULL COMMENT '租金',
`deposit` decimal(10,2) DEFAULT NULL COMMENT '押金',
`paymentType` enum('monthly','quarterly','half_year','yearly') NOT NULL DEFAULT 'monthly' COMMENT '付租方式',
`status` enum('active','expired','terminated') NOT NULL DEFAULT 'active' COMMENT '状态active-有效expired-到期terminated-提前终止',
`operator` varchar(50) DEFAULT NULL COMMENT '经办人',
`remark` text COMMENT '备注',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_room` (`roomId`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_status` (`status`),
KEY `idx_dates` (`startDate`,`endDate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='租赁表';
-- 租客表
DROP TABLE IF EXISTS `renters`;
CREATE TABLE `renters` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '租客ID',
`name` varchar(50) NOT NULL COMMENT '姓名',
`phone` varchar(20) DEFAULT NULL COMMENT '电话',
`idCard` varchar(18) DEFAULT NULL COMMENT '身份证号',
`status` enum('active','inactive') NOT NULL DEFAULT 'active' COMMENT '状态active-正常inactive-已退房',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_phone` (`phone`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='租客表';
-- ============================================
-- 5. 财务相关表
-- ============================================
-- 账单表(统一账单管理)
DROP TABLE IF EXISTS `bills`;
CREATE TABLE `bills` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '账单ID',
`billNo` varchar(50) NOT NULL COMMENT '账单编号',
`roomId` int(11) DEFAULT NULL COMMENT '房间ID可选',
`renterId` int(11) DEFAULT NULL COMMENT '租客ID可选',
`rentalId` int(11) DEFAULT NULL COMMENT '租赁ID可选',
`type` enum('income','expense') NOT NULL COMMENT '账单类型income-收入expense-支出',
`category` varchar(50) NOT NULL COMMENT '分类rent-租金, water-水费, electricity-电费, gas-燃气费, deposit-押金, maintenance-维修费, property_fee-物业费, agency_fee-中介费, penalty-违约金, other_income-其他收入, other_expense-其他支出',
`receivableAmount` decimal(10,2) NOT NULL COMMENT '应收金额',
`receivedAmount` decimal(10,2) DEFAULT '0.00' COMMENT '已收金额',
`status` enum('unpaid','partial','paid','cancelled') NOT NULL DEFAULT 'unpaid' COMMENT '状态unpaid-未收, partial-部分收款, paid-已收清, cancelled-已取消',
`billMonth` varchar(7) DEFAULT NULL COMMENT '账单月份格式YYYY-MM',
`billDate` date NOT NULL COMMENT '账单日期',
`sourceType` varchar(50) DEFAULT NULL COMMENT '来源类型meter_reading-抄表记录',
`sourceId` int(11) DEFAULT NULL COMMENT '来源ID',
`paymentMethod` varchar(50) DEFAULT NULL COMMENT '支付方式cash-现金, bank_transfer-银行转账, alipay-支付宝, wechat-微信支付',
`remark` text COMMENT '备注',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `billNo` (`billNo`),
KEY `idx_room` (`roomId`),
KEY `idx_renter` (`renterId`),
KEY `idx_rental` (`rentalId`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_status` (`status`),
KEY `idx_type` (`type`),
KEY `idx_category` (`category`),
KEY `idx_bill_month` (`billMonth`),
KEY `idx_source` (`sourceType`,`sourceId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='统一账单表';
-- 账单支付流水表
DROP TABLE IF EXISTS `bill_payments`;
CREATE TABLE `bill_payments` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '支付流水ID',
`billId` int(11) NOT NULL COMMENT '账单ID',
`amount` decimal(10,2) NOT NULL COMMENT '本次支付金额',
`paymentMethod` enum('cash','bank_transfer','alipay','wechat','other') NOT NULL COMMENT '支付方式cash-现金, bank_transfer-银行转账, alipay-支付宝, wechat-微信支付, other-其他',
`paymentTime` datetime NOT NULL COMMENT '支付时间',
`transactionNo` varchar(100) DEFAULT NULL COMMENT '交易流水号/凭证号',
`remark` text COMMENT '备注',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `idx_bill` (`billId`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_payment_time` (`paymentTime`),
KEY `idx_payment_method` (`paymentMethod`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='账单支付流水表';
-- 抄表记录表(水电气)
DROP TABLE IF EXISTS `meter_readings`;
CREATE TABLE `meter_readings` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '记录ID',
`roomId` int(11) NOT NULL COMMENT '房间ID',
`renterId` int(11) DEFAULT NULL COMMENT '租客ID可选',
`rentalId` int(11) DEFAULT NULL COMMENT '租赁ID可选',
`meterType` enum('water','electricity','gas') NOT NULL COMMENT '表类型water-水表, electricity-电表, gas-燃气表',
`previousReading` decimal(10,2) NOT NULL COMMENT '上期读数',
`currentReading` decimal(10,2) NOT NULL COMMENT '本期读数',
`usage` decimal(10,2) NOT NULL COMMENT '用量',
`unitPrice` decimal(10,2) NOT NULL COMMENT '单价',
`amount` decimal(10,2) NOT NULL COMMENT '金额',
`billMonth` varchar(7) NOT NULL COMMENT '账单月份格式YYYY-MM',
`readingDate` date NOT NULL COMMENT '抄表日期',
`billId` int(11) DEFAULT NULL COMMENT '关联账单ID',
`remark` text COMMENT '备注',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_room` (`roomId`),
KEY `idx_renter` (`renterId`),
KEY `idx_rental` (`rentalId`),
KEY `idx_meter_type` (`meterType`),
KEY `idx_bill_month` (`billMonth`),
KEY `idx_bill` (`billId`),
KEY `idx_tenant` (`tenantId`),
UNIQUE KEY `uk_room_type_month` (`roomId`, `meterType`, `billMonth`, `tenantId`, `isDeleted`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='抄表记录表(水电气)';
-- 交易记录表
DROP TABLE IF EXISTS `transactions`;
CREATE TABLE `transactions` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '交易ID',
`billId` int(11) DEFAULT NULL COMMENT '账单ID',
`roomId` int(11) DEFAULT NULL COMMENT '房间ID',
`renterId` int(11) DEFAULT NULL COMMENT '租客ID',
`type` enum('income','expense') NOT NULL COMMENT '类型income-收入expense-支出',
`category` varchar(50) NOT NULL COMMENT '分类',
`amount` decimal(10,2) NOT NULL COMMENT '金额',
`paymentMethod` enum('cash','bank_transfer','alipay','wechat') DEFAULT NULL COMMENT '支付方式',
`transactionDate` date NOT NULL COMMENT '交易日期',
`remark` text COMMENT '备注',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_bill` (`billId`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_type` (`type`),
KEY `idx_date` (`transactionDate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='交易记录表';
-- ============================================
-- 6. 订阅计费相关表
-- ============================================
-- 订阅套餐表
DROP TABLE IF EXISTS `subscription_plans`;
CREATE TABLE `subscription_plans` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '套餐ID',
`code` varchar(50) NOT NULL COMMENT '套餐编码',
`name` varchar(100) NOT NULL COMMENT '套餐名称',
`description` text COMMENT '套餐描述',
`maxApartments` int(11) NOT NULL DEFAULT '0' COMMENT '最大公寓数0表示无限制',
`maxRooms` int(11) NOT NULL DEFAULT '0' COMMENT '最大房间数0表示无限制',
`maxUsers` int(11) NOT NULL DEFAULT '0' COMMENT '最大用户数0表示无限制',
`monthlyPrice` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '月价格',
`yearlyPrice` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '年价格',
`isDefault` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否默认套餐01',
`status` enum('active','inactive') NOT NULL DEFAULT 'active' COMMENT '状态',
`sort` int(11) NOT NULL DEFAULT '0' COMMENT '排序',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `code` (`code`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订阅套餐表';
-- 租户资源使用表
DROP TABLE IF EXISTS `tenant_resource_usage`;
CREATE TABLE `tenant_resource_usage` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`currentApartments` int(11) NOT NULL DEFAULT '0' COMMENT '当前公寓数',
`currentRooms` int(11) NOT NULL DEFAULT '0' COMMENT '当前房间数',
`currentUsers` int(11) NOT NULL DEFAULT '0' COMMENT '当前用户数',
`recordDate` date NOT NULL COMMENT '记录日期',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_tenant_date` (`tenantId`,`recordDate`),
KEY `idx_tenant` (`tenantId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='租户资源使用表';
-- 租户计费明细表
DROP TABLE IF EXISTS `tenant_billing_details`;
CREATE TABLE `tenant_billing_details` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`billingPeriod` varchar(7) NOT NULL COMMENT '计费周期格式YYYY-MM',
`planId` int(11) DEFAULT NULL COMMENT '套餐ID',
`baseAmount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '基础费用',
`extraAmount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '超额费用',
`discountAmount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '优惠金额',
`totalAmount` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '总计金额',
`status` enum('pending','paid','overdue') NOT NULL DEFAULT 'pending' COMMENT '状态',
`paidTime` datetime DEFAULT NULL COMMENT '支付时间',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_tenant_period` (`tenantId`,`billingPeriod`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='租户计费明细表';
-- 订单表
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单ID',
`orderNo` varchar(50) NOT NULL COMMENT '订单编号',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`planId` int(11) NOT NULL COMMENT '套餐ID',
`planName` varchar(100) NOT NULL COMMENT '套餐名称',
`months` int(11) NOT NULL DEFAULT '1' COMMENT '购买月数',
`amount` decimal(10,2) NOT NULL COMMENT '订单金额',
`discountAmount` decimal(10,2) DEFAULT '0.00' COMMENT '优惠金额',
`actualAmount` decimal(10,2) NOT NULL COMMENT '实付金额',
`status` enum('pending','paid','cancelled') NOT NULL DEFAULT 'pending' COMMENT '状态pending-待支付paid-已支付cancelled-已取消',
`paidTime` datetime DEFAULT NULL COMMENT '支付时间',
`expireTime` datetime NOT NULL COMMENT '过期时间',
`remark` varchar(255) DEFAULT NULL COMMENT '备注',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `orderNo` (`orderNo`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表';
-- 支付记录表
DROP TABLE IF EXISTS `payments`;
CREATE TABLE `payments` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '支付ID',
`orderId` int(11) NOT NULL COMMENT '订单ID',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`amount` decimal(10,2) NOT NULL COMMENT '支付金额',
`paymentMethod` enum('alipay','wechat','bank','other') DEFAULT 'other' COMMENT '支付方式alipay-支付宝wechat-微信支付bank-银行转账other-其他(线下转账)',
`transactionId` varchar(100) DEFAULT NULL COMMENT '第三方交易号',
`status` enum('pending','success','failed') NOT NULL DEFAULT 'pending' COMMENT '状态pending-处理中success-成功failed-失败',
`paidAt` datetime DEFAULT NULL COMMENT '支付时间',
`remark` varchar(255) DEFAULT NULL COMMENT '备注',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_order` (`orderId`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='支付记录表';
-- 定价配置表
DROP TABLE IF EXISTS `pricing_configs`;
CREATE TABLE `pricing_configs` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`overageApartmentPrice` decimal(10,2) NOT NULL DEFAULT '10.00' COMMENT '超额公寓单价(每栋/月)',
`overageRoomPrice` decimal(10,2) NOT NULL DEFAULT '2.00' COMMENT '超额房间单价(每间/月)',
`overageUserPrice` decimal(10,2) NOT NULL DEFAULT '5.00' COMMENT '超额用户单价(每人/月)',
`currency` varchar(10) NOT NULL DEFAULT 'CNY' COMMENT '货币',
`effectiveDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '生效日期',
`isActive` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否生效',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_is_active` (`isActive`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='定价配置表';
-- 支付设置表
DROP TABLE IF EXISTS `payment_settings`;
CREATE TABLE `payment_settings` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`alipayAccount` varchar(100) DEFAULT NULL COMMENT '支付宝收款账号',
`alipayName` varchar(50) DEFAULT '支付宝' COMMENT '支付宝显示名称',
`wechatId` varchar(50) DEFAULT NULL COMMENT '客服微信号',
`wechatText` varchar(100) DEFAULT '请添加客服微信' COMMENT '微信显示文字',
`bankName` varchar(100) DEFAULT NULL COMMENT '开户行',
`bankAccount` varchar(50) DEFAULT NULL COMMENT '银行账号',
`bankHolder` varchar(100) DEFAULT NULL COMMENT '户名',
`servicePhone` varchar(20) DEFAULT NULL COMMENT '客服电话',
`serviceTime` varchar(50) DEFAULT '9:00-18:00' COMMENT '工作时间',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='支付设置表';
SET FOREIGN_KEY_CHECKS = 1;
-- ============================================
-- 7. 参数设置相关表
-- ============================================
-- 系统设置表
DROP TABLE IF EXISTS `settings`;
CREATE TABLE `settings` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '设置ID',
`key` varchar(100) NOT NULL COMMENT '设置键名',
`value` text COMMENT '设置值',
`description` varchar(255) DEFAULT NULL COMMENT '设置描述',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_key_tenant` (`key`, `tenantId`),
KEY `idx_tenant` (`tenantId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统设置表';
-- 收支类目表
DROP TABLE IF EXISTS `categories`;
CREATE TABLE `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '类目ID',
`name` varchar(50) NOT NULL COMMENT '类目名称',
`code` varchar(50) NOT NULL COMMENT '类目编码',
`type` enum('income','expense') NOT NULL COMMENT '类型income-收入expense-支出',
`sort` int(11) NOT NULL DEFAULT '0' COMMENT '排序号',
`status` enum('active','inactive') NOT NULL DEFAULT 'active' COMMENT '状态active-启用inactive-禁用',
`isDefault` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否默认类目01',
`tenantId` int(11) NOT NULL COMMENT '租户ID',
`createBy` int(11) DEFAULT NULL COMMENT '创建人ID',
`updateBy` int(11) DEFAULT NULL COMMENT '修改人ID',
`isDeleted` int(11) NOT NULL DEFAULT '0' COMMENT '删除状态0未删除1已删除',
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_tenant` (`tenantId`),
KEY `idx_type` (`type`),
KEY `idx_status` (`status`),
KEY `idx_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='收支类目表';