押金不是必填

This commit is contained in:
wangxiaoxian 2026-03-04 15:09:52 +08:00
parent 79deb628b4
commit 95063cad70
7 changed files with 552 additions and 328 deletions

View File

@ -91,32 +91,19 @@ const getAllRentals = async (req, res) => {
// 先检查并更新租房状态
await checkAndUpdateRentalStatus();
const { roomNumber, tenantName, status, page = 1, pageSize = 10 } = req.query;
const { roomId, tenantName, status, page = 1, pageSize = 10 } = req.query;
// 构建查询条件
const where = { isDeleted: 0 };
if (status) {
where.status = status;
}
if (roomId) {
where.roomId = roomId;
}
// 构建包含关系
// 构建包含关系 - 关联租客信息和合同信息
const include = [
{
model: Room,
where: roomNumber ? { roomNumber: { [Op.like]: `%${roomNumber}%` }, isDeleted: 0 } : { isDeleted: 0 },
include: [
{
model: Apartment,
where: { isDeleted: 0 },
include: [
{
model: Region,
where: { isDeleted: 0 }
}
]
}
]
},
{
model: Tenant,
where: tenantName ? { name: { [Op.like]: `%${tenantName}%` }, isDeleted: 0 } : { isDeleted: 0 }
@ -135,7 +122,8 @@ const getAllRentals = async (req, res) => {
where,
include,
limit: parseInt(pageSize),
offset: parseInt(offset)
offset: parseInt(offset),
order: [['createTime', 'DESC']] // 按创建时间倒序排序
});
// 格式化数据
@ -216,12 +204,6 @@ const createRental = async (req, res) => {
if (!body.tenantName) {
return res.status(400).json({ error: '缺少租客姓名' });
}
if (!body.tenantPhone) {
return res.status(400).json({ error: '缺少租客电话' });
}
if (!body.tenantIdCard) {
return res.status(400).json({ error: '缺少身份证号' });
}
if (!body.startDate) {
return res.status(400).json({ error: '缺少开始日期' });
}
@ -239,7 +221,10 @@ const createRental = async (req, res) => {
}
// 先查找或创建租客
let tenant = await Tenant.findOne({ where: { idCard: body.tenantIdCard } });
let tenant;
if (body.tenantIdCard) {
tenant = await Tenant.findOne({ where: { idCard: body.tenantIdCard } });
}
if (!tenant) {
console.log('创建新租客:', { name: body.tenantName, phone: body.tenantPhone, idCard: body.tenantIdCard });
tenant = await Tenant.create({
@ -255,6 +240,9 @@ const createRental = async (req, res) => {
return res.status(500).json({ error: '创建租客失败' });
}
// 处理押金为空时设置为0
const deposit = body.deposit || 0;
// 创建合同
console.log('创建合同:', {
roomId: parsedRoomId,
@ -262,7 +250,7 @@ const createRental = async (req, res) => {
startDate: body.startDate,
endDate: body.endDate,
rent: body.rent,
deposit: body.deposit,
deposit: deposit,
status: body.status || 'active'
});
const contract = await Contract.create({
@ -271,7 +259,7 @@ const createRental = async (req, res) => {
startDate: body.startDate,
endDate: body.endDate,
rent: body.rent,
deposit: body.deposit,
deposit: deposit,
status: body.status || 'active'
});
console.log('合同信息:', contract);
@ -289,7 +277,7 @@ const createRental = async (req, res) => {
startDate: body.startDate,
endDate: body.endDate,
rent: body.rent,
deposit: body.deposit,
deposit: deposit,
status: body.status || 'active'
});
@ -301,7 +289,7 @@ const createRental = async (req, res) => {
startDate: body.startDate,
endDate: body.endDate,
rent: body.rent,
deposit: body.deposit,
deposit: deposit,
status: body.status || 'active'
});
@ -328,7 +316,9 @@ const updateRental = async (req, res) => {
if (!rental) {
return res.status(404).json({ error: '租房记录不存在' });
}
await rental.update({ roomId, tenantId, contractId, startDate, endDate, rent, deposit, status });
// 处理押金为空时设置为0
const updateDeposit = deposit || 0;
await rental.update({ roomId, tenantId, contractId, startDate, endDate, rent, deposit: updateDeposit, status });
res.status(200).json(rental);
} catch (error) {
res.status(500).json({ error: error.message });

View File

@ -64,7 +64,8 @@ const getAllWaterBills = async (req, res) => {
}
],
limit: parseInt(pageSize),
offset: parseInt(offset)
offset: parseInt(offset),
order: [['createTime', 'DESC']] // 按创建时间倒序排序
});
// 格式化数据

View File

@ -39,7 +39,7 @@ const Contract = sequelize.define('Contract', {
},
deposit: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
allowNull: true
},
status: {
type: DataTypes.ENUM('active', 'expired'),

View File

@ -48,7 +48,7 @@ const Rental = sequelize.define('Rental', {
},
deposit: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
allowNull: true
},
status: {
type: DataTypes.ENUM('active', 'expired'),

View File

@ -13,11 +13,11 @@ const Tenant = sequelize.define('Tenant', {
},
phone: {
type: DataTypes.STRING(20),
allowNull: false
allowNull: true
},
idCard: {
type: DataTypes.STRING(20),
allowNull: false,
allowNull: true,
unique: true
},
createTime: {

711
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -12,7 +12,7 @@
"dependencies": {
"cors": "^2.8.6",
"express": "^4.17.1",
"mysql2": "^3.18.2",
"mysql2": "^2.3.3",
"sequelize": "^6.37.7"
}
}