2026-03-02 12:36:41 +00:00
|
|
|
|
const { Tenant } = require('../models');
|
2026-03-04 07:39:06 +00:00
|
|
|
|
const { Op } = require('sequelize');
|
2026-03-02 12:36:41 +00:00
|
|
|
|
|
|
|
|
|
|
// 格式化时间(考虑时区,转换为北京时间)
|
|
|
|
|
|
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 formatTenantData = (tenant) => {
|
|
|
|
|
|
return {
|
|
|
|
|
|
...tenant.toJSON(),
|
2026-03-03 15:36:48 +00:00
|
|
|
|
createTime: formatDate(tenant.createTime),
|
|
|
|
|
|
updateTime: formatDate(tenant.updateTime)
|
2026-03-02 12:36:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取所有租客
|
|
|
|
|
|
const getAllTenants = async (req, res) => {
|
|
|
|
|
|
try {
|
2026-03-03 15:36:48 +00:00
|
|
|
|
const tenants = await Tenant.findAll({
|
|
|
|
|
|
where: { isDeleted: 0 }
|
|
|
|
|
|
});
|
2026-03-02 12:36:41 +00:00
|
|
|
|
const formattedTenants = tenants.map(formatTenantData);
|
|
|
|
|
|
res.status(200).json(formattedTenants);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取单个租客
|
|
|
|
|
|
const getTenantById = async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { id } = req.params;
|
2026-03-03 15:36:48 +00:00
|
|
|
|
const tenant = await Tenant.findOne({
|
|
|
|
|
|
where: { id, isDeleted: 0 }
|
|
|
|
|
|
});
|
2026-03-02 12:36:41 +00:00
|
|
|
|
if (!tenant) {
|
|
|
|
|
|
return res.status(404).json({ error: '租客不存在' });
|
|
|
|
|
|
}
|
|
|
|
|
|
const formattedTenant = formatTenantData(tenant);
|
|
|
|
|
|
res.status(200).json(formattedTenant);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 创建租客
|
|
|
|
|
|
const createTenant = async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { name, phone, idCard } = req.body;
|
|
|
|
|
|
const tenant = await Tenant.create({ name, phone, idCard });
|
|
|
|
|
|
res.status(201).json(tenant);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 更新租客
|
|
|
|
|
|
const updateTenant = async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
|
const { name, phone, idCard } = req.body;
|
2026-03-03 15:36:48 +00:00
|
|
|
|
const tenant = await Tenant.findOne({
|
|
|
|
|
|
where: { id, isDeleted: 0 }
|
|
|
|
|
|
});
|
2026-03-02 12:36:41 +00:00
|
|
|
|
if (!tenant) {
|
|
|
|
|
|
return res.status(404).json({ error: '租客不存在' });
|
|
|
|
|
|
}
|
|
|
|
|
|
await tenant.update({ name, phone, idCard });
|
|
|
|
|
|
res.status(200).json(tenant);
|
|
|
|
|
|
} 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 deleteTenant = async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { id } = req.params;
|
2026-03-03 15:36:48 +00:00
|
|
|
|
const tenant = await Tenant.findOne({
|
|
|
|
|
|
where: { id, isDeleted: 0 }
|
|
|
|
|
|
});
|
2026-03-02 12:36:41 +00:00
|
|
|
|
if (!tenant) {
|
|
|
|
|
|
return res.status(404).json({ error: '租客不存在' });
|
|
|
|
|
|
}
|
2026-03-03 15:36:48 +00:00
|
|
|
|
await tenant.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 });
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-04 07:39:06 +00:00
|
|
|
|
// 获取所有租客(不分页)
|
|
|
|
|
|
const listTenants = async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { name, phone, idCard } = req.query;
|
|
|
|
|
|
|
|
|
|
|
|
// 构建查询条件
|
|
|
|
|
|
const where = { isDeleted: 0 };
|
|
|
|
|
|
if (name) {
|
|
|
|
|
|
where.name = {
|
|
|
|
|
|
[Op.like]: `%${name}%`
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
if (phone) {
|
|
|
|
|
|
where.phone = {
|
|
|
|
|
|
[Op.like]: `%${phone}%`
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
if (idCard) {
|
|
|
|
|
|
where.idCard = {
|
|
|
|
|
|
[Op.like]: `%${idCard}%`
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const tenants = await Tenant.findAll({ where });
|
|
|
|
|
|
const formattedTenants = tenants.map(formatTenantData);
|
|
|
|
|
|
res.status(200).json(formattedTenants);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-02 12:36:41 +00:00
|
|
|
|
module.exports = {
|
|
|
|
|
|
getAllTenants,
|
2026-03-04 07:39:06 +00:00
|
|
|
|
listTenants,
|
2026-03-02 12:36:41 +00:00
|
|
|
|
getTenantById,
|
|
|
|
|
|
createTenant,
|
|
|
|
|
|
updateTenant,
|
|
|
|
|
|
deleteTenant
|
|
|
|
|
|
};
|