rentease-backend-new/models/index.js

193 lines
7.3 KiB
JavaScript
Raw Normal View History

2026-04-20 06:43:09 +00:00
const Tenant = require('./Tenant');
const Apartment = require('./Apartment');
const Room = require('./Room');
const Rental = require('./Rental');
const Renter = require('./Renter');
const MeterReading = require('./MeterReading');
const User = require('./User');
const Role = require('./Role');
const Menu = require('./Menu');
const RoleMenu = require('./RoleMenu');
const OperationLog = require('./OperationLog');
const LoginLog = require('./LoginLog');
const Bill = require('./Bill');
const Transaction = require('./Transaction');
const SubscriptionPlan = require('./SubscriptionPlan');
const PricingConfig = require('./PricingConfig');
const Order = require('./Order');
const Payment = require('./Payment');
const TenantResourceUsage = require('./TenantResourceUsage');
const TenantBillingDetail = require('./TenantBillingDetail');
const Coupon = require('./Coupon');
const TenantCouponUsage = require('./TenantCouponUsage');
const BillingLog = require('./BillingLog');
const PaymentSetting = require('./PaymentSetting');
const Setting = require('./Setting');
const Category = require('./Category');
const BillPayment = require('./BillPayment');
// 关联关系
// 租户关联
Tenant.hasMany(User, { foreignKey: 'tenantId', as: 'users' });
User.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
Tenant.hasMany(Apartment, { foreignKey: 'tenantId', as: 'apartments' });
Apartment.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
Tenant.hasMany(Room, { foreignKey: 'tenantId', as: 'rooms' });
Room.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
Tenant.hasMany(Renter, { foreignKey: 'tenantId', as: 'renters' });
Renter.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
Tenant.hasMany(Role, { foreignKey: 'tenantId', as: 'roles' });
Role.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
// 用户与角色关联
User.belongsTo(Role, { foreignKey: 'roleId', as: 'role' });
Role.hasMany(User, { foreignKey: 'roleId', as: 'users' });
// 菜单自关联(父子菜单)
Menu.belongsTo(Menu, { foreignKey: 'parentId', as: 'parent' });
Menu.hasMany(Menu, { foreignKey: 'parentId', as: 'children' });
// 角色与菜单多对多关联
Role.belongsToMany(Menu, { through: RoleMenu, foreignKey: 'roleId', otherKey: 'menuId', as: 'menus' });
Menu.belongsToMany(Role, { through: RoleMenu, foreignKey: 'menuId', otherKey: 'roleId', as: 'roles' });
// ============================================
// 房源租赁关联关系
// ============================================
// 公寓与房间关联
Apartment.hasMany(Room, { foreignKey: 'apartmentId', as: 'rooms' });
Room.belongsTo(Apartment, { foreignKey: 'apartmentId', as: 'apartment' });
// 房间与租赁关联
Room.hasMany(Rental, { foreignKey: 'roomId', as: 'rentals' });
Rental.belongsTo(Room, { foreignKey: 'roomId', as: 'room' });
// 租客与租赁关联
Renter.hasMany(Rental, { foreignKey: 'renterId', as: 'rentals' });
Rental.belongsTo(Renter, { foreignKey: 'renterId', as: 'renter' });
// ============================================
// 账单与抄表关联关系
// ============================================
// 账单关联
Tenant.hasMany(Bill, { foreignKey: 'tenantId', as: 'bills' });
Bill.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
Room.hasMany(Bill, { foreignKey: 'roomId', as: 'bills' });
Bill.belongsTo(Room, { foreignKey: 'roomId', as: 'room' });
Renter.hasMany(Bill, { foreignKey: 'renterId', as: 'bills' });
Bill.belongsTo(Renter, { foreignKey: 'renterId', as: 'renter' });
Rental.hasMany(Bill, { foreignKey: 'rentalId', as: 'bills' });
Bill.belongsTo(Rental, { foreignKey: 'rentalId', as: 'rental' });
// 抄表记录关联
Tenant.hasMany(MeterReading, { foreignKey: 'tenantId', as: 'meterReadings' });
MeterReading.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
Room.hasMany(MeterReading, { foreignKey: 'roomId', as: 'meterReadings' });
MeterReading.belongsTo(Room, { foreignKey: 'roomId', as: 'room' });
Renter.hasMany(MeterReading, { foreignKey: 'renterId', as: 'meterReadings' });
MeterReading.belongsTo(Renter, { foreignKey: 'renterId', as: 'renter' });
Rental.hasMany(MeterReading, { foreignKey: 'rentalId', as: 'meterReadings' });
MeterReading.belongsTo(Rental, { foreignKey: 'rentalId', as: 'rental' });
// 抄表记录与账单关联
Bill.hasOne(MeterReading, { foreignKey: 'billId', as: 'meterReading' });
MeterReading.belongsTo(Bill, { foreignKey: 'billId', as: 'bill' });
// 账单与支付流水关联
Bill.hasMany(BillPayment, { foreignKey: 'billId', as: 'billPayments' });
BillPayment.belongsTo(Bill, { foreignKey: 'billId', as: 'bill' });
// ============================================
// 计费系统关联关系
// ============================================
// 租户与套餐关联
Tenant.belongsTo(SubscriptionPlan, { foreignKey: 'planId', as: 'subscriptionPlan' });
SubscriptionPlan.hasMany(Tenant, { foreignKey: 'planId', as: 'tenants' });
// 租户与订单关联
Tenant.hasMany(Order, { foreignKey: 'tenantId', as: 'orders' });
Order.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
// 订单与套餐关联
Order.belongsTo(SubscriptionPlan, { foreignKey: 'planId', as: 'subscriptionPlan' });
SubscriptionPlan.hasMany(Order, { foreignKey: 'planId', as: 'orders' });
// 订单与支付关联
Order.hasMany(Payment, { foreignKey: 'orderId', as: 'payments' });
Payment.belongsTo(Order, { foreignKey: 'orderId', as: 'order' });
// 租户与支付关联
Tenant.hasMany(Payment, { foreignKey: 'tenantId', as: 'payments' });
Payment.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
// 租户与资源使用记录关联
Tenant.hasMany(TenantResourceUsage, { foreignKey: 'tenantId', as: 'resourceUsages' });
TenantResourceUsage.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
// 租户与计费明细关联
Tenant.hasMany(TenantBillingDetail, { foreignKey: 'tenantId', as: 'billingDetails' });
TenantBillingDetail.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
// 计费明细与订单关联
TenantBillingDetail.belongsTo(Order, { foreignKey: 'orderId', as: 'order' });
Order.hasMany(TenantBillingDetail, { foreignKey: 'orderId', as: 'billingDetails' });
// 优惠券与订单多对多关联(通过使用记录)
Coupon.belongsToMany(Tenant, { through: TenantCouponUsage, foreignKey: 'couponId', otherKey: 'tenantId', as: 'tenants' });
Tenant.belongsToMany(Coupon, { through: TenantCouponUsage, foreignKey: 'tenantId', otherKey: 'couponId', as: 'coupons' });
// 租户优惠券使用记录关联
TenantCouponUsage.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
TenantCouponUsage.belongsTo(Coupon, { foreignKey: 'couponId', as: 'coupon' });
TenantCouponUsage.belongsTo(Order, { foreignKey: 'orderId', as: 'order' });
// 计费日志关联
Tenant.hasMany(BillingLog, { foreignKey: 'tenantId', as: 'billingLogs' });
BillingLog.belongsTo(Tenant, { foreignKey: 'tenantId', as: 'tenant' });
BillingLog.belongsTo(Order, { foreignKey: 'orderId', as: 'order' });
BillingLog.belongsTo(Payment, { foreignKey: 'paymentId', as: 'payment' });
module.exports = {
Tenant,
Apartment,
Room,
Rental,
Renter,
MeterReading,
User,
Role,
Menu,
RoleMenu,
OperationLog,
LoginLog,
Bill,
Transaction,
SubscriptionPlan,
PricingConfig,
Order,
Payment,
TenantResourceUsage,
TenantBillingDetail,
Coupon,
TenantCouponUsage,
BillingLog,
PaymentSetting,
Setting,
Category,
BillPayment
};