rentease-backend/controllers/contractController.js

183 lines
5.1 KiB
JavaScript
Raw Normal View History

2026-03-02 12:36:41 +00:00
const { Contract, Room, Tenant, Apartment, Region } = require('../models');
// 格式化时间(考虑时区,转换为北京时间)
const formatDate = (date) => {
if (!date) return null;
// 创建一个新的Date对象加上8小时的时区偏移
const beijingDate = new Date(date.getTime() + 8 * 60 * 60 * 1000);
return beijingDate.toISOString().split('T')[0];
};
// 格式化合同数据
const formatContractData = (contract) => {
const formattedContract = {
...contract.toJSON(),
startDate: formatDate(contract.startDate),
endDate: formatDate(contract.endDate),
2026-03-03 15:36:48 +00:00
createTime: formatDate(contract.createTime),
updateTime: formatDate(contract.updateTime)
2026-03-02 12:36:41 +00:00
};
// 格式化关联数据
if (formattedContract.Room) {
formattedContract.Room = {
...formattedContract.Room,
2026-03-03 15:36:48 +00:00
createTime: formatDate(formattedContract.Room.createTime),
updateTime: formatDate(formattedContract.Room.updateTime)
2026-03-02 12:36:41 +00:00
};
if (formattedContract.Room.Apartment) {
formattedContract.Room.Apartment = {
...formattedContract.Room.Apartment,
2026-03-03 15:36:48 +00:00
createTime: formatDate(formattedContract.Room.Apartment.createTime),
updateTime: formatDate(formattedContract.Room.Apartment.updateTime)
2026-03-02 12:36:41 +00:00
};
if (formattedContract.Room.Apartment.Region) {
formattedContract.Room.Apartment.Region = {
...formattedContract.Room.Apartment.Region,
2026-03-03 15:36:48 +00:00
createTime: formatDate(formattedContract.Room.Apartment.Region.createTime),
updateTime: formatDate(formattedContract.Room.Apartment.Region.updateTime)
2026-03-02 12:36:41 +00:00
};
}
}
}
if (formattedContract.Tenant) {
formattedContract.Tenant = {
...formattedContract.Tenant,
2026-03-03 15:36:48 +00:00
createTime: formatDate(formattedContract.Tenant.createTime),
updateTime: formatDate(formattedContract.Tenant.updateTime)
2026-03-02 12:36:41 +00:00
};
}
return formattedContract;
};
// 获取所有合同
const getAllContracts = async (req, res) => {
try {
const contracts = await Contract.findAll({
2026-03-03 15:36:48 +00:00
where: { isDeleted: 0 },
2026-03-02 12:36:41 +00:00
include: [
{
model: Room,
2026-03-03 15:36:48 +00:00
where: { isDeleted: 0 },
2026-03-02 12:36:41 +00:00
include: [
{
model: Apartment,
2026-03-03 15:36:48 +00:00
where: { isDeleted: 0 },
include: [
{
model: Region,
where: { isDeleted: 0 }
}
]
2026-03-02 12:36:41 +00:00
}
]
},
2026-03-03 15:36:48 +00:00
{
model: Tenant,
where: { isDeleted: 0 }
}
2026-03-02 12:36:41 +00:00
]
});
const formattedContracts = contracts.map(formatContractData);
res.status(200).json(formattedContracts);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// 获取单个合同
const getContractById = async (req, res) => {
try {
const { id } = req.params;
2026-03-03 15:36:48 +00:00
const contract = await Contract.findOne({
where: { id, isDeleted: 0 },
2026-03-02 12:36:41 +00:00
include: [
{
model: Room,
2026-03-03 15:36:48 +00:00
where: { isDeleted: 0 },
2026-03-02 12:36:41 +00:00
include: [
{
model: Apartment,
2026-03-03 15:36:48 +00:00
where: { isDeleted: 0 },
include: [
{
model: Region,
where: { isDeleted: 0 }
}
]
2026-03-02 12:36:41 +00:00
}
]
},
2026-03-03 15:36:48 +00:00
{
model: Tenant,
where: { isDeleted: 0 }
}
2026-03-02 12:36:41 +00:00
]
});
if (!contract) {
return res.status(404).json({ error: '合同不存在' });
}
const formattedContract = formatContractData(contract);
res.status(200).json(formattedContract);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// 创建合同
const createContract = async (req, res) => {
try {
const { roomId, tenantId, startDate, endDate, rent, deposit, status } = req.body;
const contract = await Contract.create({ roomId, tenantId, startDate, endDate, rent, deposit, status });
res.status(201).json(contract);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// 更新合同
const updateContract = async (req, res) => {
try {
const { id } = req.params;
const { roomId, tenantId, startDate, endDate, rent, deposit, status } = req.body;
2026-03-03 15:36:48 +00:00
const contract = await Contract.findOne({
where: { id, isDeleted: 0 }
});
2026-03-02 12:36:41 +00:00
if (!contract) {
return res.status(404).json({ error: '合同不存在' });
}
await contract.update({ roomId, tenantId, startDate, endDate, rent, deposit, status });
res.status(200).json(contract);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
2026-03-03 15:36:48 +00:00
// 删除合同(软删除)
2026-03-02 12:36:41 +00:00
const deleteContract = async (req, res) => {
try {
const { id } = req.params;
2026-03-03 15:36:48 +00:00
const contract = await Contract.findOne({
where: { id, isDeleted: 0 }
});
2026-03-02 12:36:41 +00:00
if (!contract) {
return res.status(404).json({ error: '合同不存在' });
}
2026-03-03 15:36:48 +00:00
await contract.update({ isDeleted: 1 });
2026-03-02 12:36:41 +00:00
res.status(200).json({ message: '合同删除成功' });
} catch (error) {
res.status(500).json({ error: error.message });
}
};
module.exports = {
getAllContracts,
getContractById,
createContract,
updateContract,
deleteContract
};