押金不是必填
This commit is contained in:
parent
79deb628b4
commit
95063cad70
|
|
@ -91,32 +91,19 @@ const getAllRentals = async (req, res) => {
|
||||||
// 先检查并更新租房状态
|
// 先检查并更新租房状态
|
||||||
await checkAndUpdateRentalStatus();
|
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 };
|
const where = { isDeleted: 0 };
|
||||||
if (status) {
|
if (status) {
|
||||||
where.status = status;
|
where.status = status;
|
||||||
}
|
}
|
||||||
|
if (roomId) {
|
||||||
|
where.roomId = roomId;
|
||||||
|
}
|
||||||
|
|
||||||
// 构建包含关系
|
// 构建包含关系 - 关联租客信息和合同信息
|
||||||
const include = [
|
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,
|
model: Tenant,
|
||||||
where: tenantName ? { name: { [Op.like]: `%${tenantName}%` }, isDeleted: 0 } : { isDeleted: 0 }
|
where: tenantName ? { name: { [Op.like]: `%${tenantName}%` }, isDeleted: 0 } : { isDeleted: 0 }
|
||||||
|
|
@ -135,7 +122,8 @@ const getAllRentals = async (req, res) => {
|
||||||
where,
|
where,
|
||||||
include,
|
include,
|
||||||
limit: parseInt(pageSize),
|
limit: parseInt(pageSize),
|
||||||
offset: parseInt(offset)
|
offset: parseInt(offset),
|
||||||
|
order: [['createTime', 'DESC']] // 按创建时间倒序排序
|
||||||
});
|
});
|
||||||
|
|
||||||
// 格式化数据
|
// 格式化数据
|
||||||
|
|
@ -216,12 +204,6 @@ const createRental = async (req, res) => {
|
||||||
if (!body.tenantName) {
|
if (!body.tenantName) {
|
||||||
return res.status(400).json({ error: '缺少租客姓名' });
|
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) {
|
if (!body.startDate) {
|
||||||
return res.status(400).json({ error: '缺少开始日期' });
|
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) {
|
if (!tenant) {
|
||||||
console.log('创建新租客:', { name: body.tenantName, phone: body.tenantPhone, idCard: body.tenantIdCard });
|
console.log('创建新租客:', { name: body.tenantName, phone: body.tenantPhone, idCard: body.tenantIdCard });
|
||||||
tenant = await Tenant.create({
|
tenant = await Tenant.create({
|
||||||
|
|
@ -255,6 +240,9 @@ const createRental = async (req, res) => {
|
||||||
return res.status(500).json({ error: '创建租客失败' });
|
return res.status(500).json({ error: '创建租客失败' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理押金,为空时设置为0
|
||||||
|
const deposit = body.deposit || 0;
|
||||||
|
|
||||||
// 创建合同
|
// 创建合同
|
||||||
console.log('创建合同:', {
|
console.log('创建合同:', {
|
||||||
roomId: parsedRoomId,
|
roomId: parsedRoomId,
|
||||||
|
|
@ -262,7 +250,7 @@ const createRental = async (req, res) => {
|
||||||
startDate: body.startDate,
|
startDate: body.startDate,
|
||||||
endDate: body.endDate,
|
endDate: body.endDate,
|
||||||
rent: body.rent,
|
rent: body.rent,
|
||||||
deposit: body.deposit,
|
deposit: deposit,
|
||||||
status: body.status || 'active'
|
status: body.status || 'active'
|
||||||
});
|
});
|
||||||
const contract = await Contract.create({
|
const contract = await Contract.create({
|
||||||
|
|
@ -271,7 +259,7 @@ const createRental = async (req, res) => {
|
||||||
startDate: body.startDate,
|
startDate: body.startDate,
|
||||||
endDate: body.endDate,
|
endDate: body.endDate,
|
||||||
rent: body.rent,
|
rent: body.rent,
|
||||||
deposit: body.deposit,
|
deposit: deposit,
|
||||||
status: body.status || 'active'
|
status: body.status || 'active'
|
||||||
});
|
});
|
||||||
console.log('合同信息:', contract);
|
console.log('合同信息:', contract);
|
||||||
|
|
@ -289,7 +277,7 @@ const createRental = async (req, res) => {
|
||||||
startDate: body.startDate,
|
startDate: body.startDate,
|
||||||
endDate: body.endDate,
|
endDate: body.endDate,
|
||||||
rent: body.rent,
|
rent: body.rent,
|
||||||
deposit: body.deposit,
|
deposit: deposit,
|
||||||
status: body.status || 'active'
|
status: body.status || 'active'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -301,7 +289,7 @@ const createRental = async (req, res) => {
|
||||||
startDate: body.startDate,
|
startDate: body.startDate,
|
||||||
endDate: body.endDate,
|
endDate: body.endDate,
|
||||||
rent: body.rent,
|
rent: body.rent,
|
||||||
deposit: body.deposit,
|
deposit: deposit,
|
||||||
status: body.status || 'active'
|
status: body.status || 'active'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -328,7 +316,9 @@ const updateRental = async (req, res) => {
|
||||||
if (!rental) {
|
if (!rental) {
|
||||||
return res.status(404).json({ error: '租房记录不存在' });
|
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);
|
res.status(200).json(rental);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,8 @@ const getAllWaterBills = async (req, res) => {
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
limit: parseInt(pageSize),
|
limit: parseInt(pageSize),
|
||||||
offset: parseInt(offset)
|
offset: parseInt(offset),
|
||||||
|
order: [['createTime', 'DESC']] // 按创建时间倒序排序
|
||||||
});
|
});
|
||||||
|
|
||||||
// 格式化数据
|
// 格式化数据
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ const Contract = sequelize.define('Contract', {
|
||||||
},
|
},
|
||||||
deposit: {
|
deposit: {
|
||||||
type: DataTypes.DECIMAL(10, 2),
|
type: DataTypes.DECIMAL(10, 2),
|
||||||
allowNull: false
|
allowNull: true
|
||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
type: DataTypes.ENUM('active', 'expired'),
|
type: DataTypes.ENUM('active', 'expired'),
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ const Rental = sequelize.define('Rental', {
|
||||||
},
|
},
|
||||||
deposit: {
|
deposit: {
|
||||||
type: DataTypes.DECIMAL(10, 2),
|
type: DataTypes.DECIMAL(10, 2),
|
||||||
allowNull: false
|
allowNull: true
|
||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
type: DataTypes.ENUM('active', 'expired'),
|
type: DataTypes.ENUM('active', 'expired'),
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,11 @@ const Tenant = sequelize.define('Tenant', {
|
||||||
},
|
},
|
||||||
phone: {
|
phone: {
|
||||||
type: DataTypes.STRING(20),
|
type: DataTypes.STRING(20),
|
||||||
allowNull: false
|
allowNull: true
|
||||||
},
|
},
|
||||||
idCard: {
|
idCard: {
|
||||||
type: DataTypes.STRING(20),
|
type: DataTypes.STRING(20),
|
||||||
allowNull: false,
|
allowNull: true,
|
||||||
unique: true
|
unique: true
|
||||||
},
|
},
|
||||||
createTime: {
|
createTime: {
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -12,7 +12,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cors": "^2.8.6",
|
"cors": "^2.8.6",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"mysql2": "^3.18.2",
|
"mysql2": "^2.3.3",
|
||||||
"sequelize": "^6.37.7"
|
"sequelize": "^6.37.7"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue