/** * 贷款合同模型 * @file LoanContract.js * @description 银行系统贷款合同数据模型 */ const { DataTypes } = require('sequelize'); const BaseModel = require('./BaseModel'); class LoanContract extends BaseModel { /** * 获取合同状态文本 * @returns {String} 状态文本 */ getStatusText() { const statusMap = { active: '已放款', pending: '待放款', completed: '已完成', defaulted: '违约', cancelled: '已取消' }; return statusMap[this.status] || this.status; } /** * 获取合同类型文本 * @returns {String} 类型文本 */ getTypeText() { const typeMap = { livestock_collateral: '畜禽活体抵押', farmer_loan: '惠农贷', business_loan: '商业贷款', personal_loan: '个人贷款' }; return typeMap[this.type] || this.type; } /** * 格式化合同金额 * @returns {String} 格式化后的金额 */ getFormattedAmount() { return `${this.amount.toFixed(2)}元`; } /** * 计算剩余还款金额 * @returns {Number} 剩余金额 */ getRemainingAmount() { return this.amount - (this.paidAmount || 0); } /** * 计算还款进度百分比 * @returns {Number} 进度百分比 */ getRepaymentProgress() { if (this.amount <= 0) return 0; return Math.round(((this.paidAmount || 0) / this.amount) * 100); } } // 初始化LoanContract模型 LoanContract.init({ id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, contractNumber: { type: DataTypes.STRING(50), allowNull: false, unique: true, comment: '合同编号' }, applicationNumber: { type: DataTypes.STRING(50), allowNull: false, comment: '申请单号' }, productName: { type: DataTypes.STRING(200), allowNull: false, comment: '贷款产品名称' }, farmerName: { type: DataTypes.STRING(100), allowNull: false, comment: '申请养殖户姓名' }, borrowerName: { type: DataTypes.STRING(100), allowNull: false, comment: '贷款人姓名' }, borrowerIdNumber: { type: DataTypes.STRING(20), allowNull: false, comment: '贷款人身份证号' }, assetType: { type: DataTypes.STRING(50), allowNull: false, comment: '生资种类' }, applicationQuantity: { type: DataTypes.STRING(100), allowNull: false, comment: '申请数量' }, amount: { type: DataTypes.DECIMAL(15, 2), allowNull: false, comment: '合同金额' }, paidAmount: { type: DataTypes.DECIMAL(15, 2), allowNull: false, defaultValue: 0, comment: '已还款金额' }, status: { type: DataTypes.ENUM( 'active', 'pending', 'completed', 'defaulted', 'cancelled' ), allowNull: false, defaultValue: 'pending', comment: '合同状态' }, type: { type: DataTypes.ENUM( 'livestock_collateral', 'farmer_loan', 'business_loan', 'personal_loan' ), allowNull: false, comment: '合同类型' }, term: { type: DataTypes.INTEGER, allowNull: false, comment: '合同期限(月)' }, interestRate: { type: DataTypes.DECIMAL(5, 2), allowNull: false, comment: '利率' }, phone: { type: DataTypes.STRING(20), allowNull: false, comment: '联系电话' }, purpose: { type: DataTypes.TEXT, allowNull: true, comment: '贷款用途' }, remark: { type: DataTypes.TEXT, allowNull: true, comment: '备注' }, contractTime: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW, comment: '合同签订时间' }, disbursementTime: { type: DataTypes.DATE, allowNull: true, comment: '放款时间' }, maturityTime: { type: DataTypes.DATE, allowNull: true, comment: '到期时间' }, completedTime: { type: DataTypes.DATE, allowNull: true, comment: '完成时间' }, createdBy: { type: DataTypes.INTEGER, allowNull: true, comment: '创建人ID', references: { model: 'bank_users', key: 'id' } }, updatedBy: { type: DataTypes.INTEGER, allowNull: true, comment: '更新人ID', references: { model: 'bank_users', key: 'id' } } }, { sequelize: require('../config/database').sequelize, modelName: 'LoanContract', tableName: 'bank_loan_contracts', timestamps: true, createdAt: 'createdAt', updatedAt: 'updatedAt', hooks: { beforeCreate: (contract) => { // 生成合同编号 if (!contract.contractNumber) { const now = new Date(); const timestamp = now.getFullYear().toString() + (now.getMonth() + 1).toString().padStart(2, '0') + now.getDate().toString().padStart(2, '0') + now.getHours().toString().padStart(2, '0') + now.getMinutes().toString().padStart(2, '0') + now.getSeconds().toString().padStart(2, '0'); const random = Math.floor(Math.random() * 1000).toString().padStart(3, '0'); contract.contractNumber = 'HT' + timestamp + random; } } } }); module.exports = LoanContract;