183 lines
5.1 KiB
JavaScript
183 lines
5.1 KiB
JavaScript
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),
|
||
createTime: formatDate(contract.createTime),
|
||
updateTime: formatDate(contract.updateTime)
|
||
};
|
||
|
||
// 格式化关联数据
|
||
if (formattedContract.Room) {
|
||
formattedContract.Room = {
|
||
...formattedContract.Room,
|
||
createTime: formatDate(formattedContract.Room.createTime),
|
||
updateTime: formatDate(formattedContract.Room.updateTime)
|
||
};
|
||
|
||
if (formattedContract.Room.Apartment) {
|
||
formattedContract.Room.Apartment = {
|
||
...formattedContract.Room.Apartment,
|
||
createTime: formatDate(formattedContract.Room.Apartment.createTime),
|
||
updateTime: formatDate(formattedContract.Room.Apartment.updateTime)
|
||
};
|
||
|
||
if (formattedContract.Room.Apartment.Region) {
|
||
formattedContract.Room.Apartment.Region = {
|
||
...formattedContract.Room.Apartment.Region,
|
||
createTime: formatDate(formattedContract.Room.Apartment.Region.createTime),
|
||
updateTime: formatDate(formattedContract.Room.Apartment.Region.updateTime)
|
||
};
|
||
}
|
||
}
|
||
}
|
||
|
||
if (formattedContract.Tenant) {
|
||
formattedContract.Tenant = {
|
||
...formattedContract.Tenant,
|
||
createTime: formatDate(formattedContract.Tenant.createTime),
|
||
updateTime: formatDate(formattedContract.Tenant.updateTime)
|
||
};
|
||
}
|
||
|
||
return formattedContract;
|
||
};
|
||
|
||
// 获取所有合同
|
||
const getAllContracts = async (req, res) => {
|
||
try {
|
||
const contracts = await Contract.findAll({
|
||
where: { isDeleted: 0 },
|
||
include: [
|
||
{
|
||
model: Room,
|
||
where: { isDeleted: 0 },
|
||
include: [
|
||
{
|
||
model: Apartment,
|
||
where: { isDeleted: 0 },
|
||
include: [
|
||
{
|
||
model: Region,
|
||
where: { isDeleted: 0 }
|
||
}
|
||
]
|
||
}
|
||
]
|
||
},
|
||
{
|
||
model: Tenant,
|
||
where: { isDeleted: 0 }
|
||
}
|
||
]
|
||
});
|
||
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;
|
||
const contract = await Contract.findOne({
|
||
where: { id, isDeleted: 0 },
|
||
include: [
|
||
{
|
||
model: Room,
|
||
where: { isDeleted: 0 },
|
||
include: [
|
||
{
|
||
model: Apartment,
|
||
where: { isDeleted: 0 },
|
||
include: [
|
||
{
|
||
model: Region,
|
||
where: { isDeleted: 0 }
|
||
}
|
||
]
|
||
}
|
||
]
|
||
},
|
||
{
|
||
model: Tenant,
|
||
where: { isDeleted: 0 }
|
||
}
|
||
]
|
||
});
|
||
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;
|
||
const contract = await Contract.findOne({
|
||
where: { id, isDeleted: 0 }
|
||
});
|
||
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 });
|
||
}
|
||
};
|
||
|
||
// 删除合同(软删除)
|
||
const deleteContract = async (req, res) => {
|
||
try {
|
||
const { id } = req.params;
|
||
const contract = await Contract.findOne({
|
||
where: { id, isDeleted: 0 }
|
||
});
|
||
if (!contract) {
|
||
return res.status(404).json({ error: '合同不存在' });
|
||
}
|
||
await contract.update({ isDeleted: 1 });
|
||
res.status(200).json({ message: '合同删除成功' });
|
||
} catch (error) {
|
||
res.status(500).json({ error: error.message });
|
||
}
|
||
};
|
||
|
||
module.exports = {
|
||
getAllContracts,
|
||
getContractById,
|
||
createContract,
|
||
updateContract,
|
||
deleteContract
|
||
}; |