2026-03-02 12:36:41 +00:00
|
|
|
|
const { WaterBill, Room } = require('../models');
|
|
|
|
|
|
const { Op } = require('sequelize');
|
|
|
|
|
|
|
|
|
|
|
|
// 格式化时间(考虑时区,转换为北京时间)
|
|
|
|
|
|
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 formatWaterBillData = (bill) => {
|
|
|
|
|
|
const formattedBill = {
|
|
|
|
|
|
...bill.toJSON(),
|
|
|
|
|
|
startDate: formatDate(bill.startDate),
|
|
|
|
|
|
endDate: formatDate(bill.endDate),
|
2026-03-03 15:36:48 +00:00
|
|
|
|
createTime: formatDate(bill.createTime),
|
|
|
|
|
|
updateTime: formatDate(bill.updateTime)
|
2026-03-02 12:36:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 格式化关联数据
|
|
|
|
|
|
if (formattedBill.Room) {
|
|
|
|
|
|
formattedBill.Room = {
|
|
|
|
|
|
...formattedBill.Room,
|
2026-03-03 15:36:48 +00:00
|
|
|
|
createTime: formatDate(formattedBill.Room.createTime),
|
|
|
|
|
|
updateTime: formatDate(formattedBill.Room.updateTime)
|
2026-03-02 12:36:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return formattedBill;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取所有水费记录(支持搜索和分页)
|
|
|
|
|
|
const getAllWaterBills = async (req, res) => {
|
|
|
|
|
|
try {
|
2026-03-05 16:05:51 +00:00
|
|
|
|
const {
|
|
|
|
|
|
apartmentId,
|
|
|
|
|
|
roomId,
|
|
|
|
|
|
status,
|
|
|
|
|
|
startDateFrom,
|
|
|
|
|
|
endDateTo,
|
|
|
|
|
|
page = 1,
|
|
|
|
|
|
pageSize = 10
|
|
|
|
|
|
} = req.query;
|
2026-03-02 12:36:41 +00:00
|
|
|
|
|
2026-03-03 15:36:48 +00:00
|
|
|
|
const where = { isDeleted: 0 };
|
2026-03-02 12:36:41 +00:00
|
|
|
|
if (roomId) {
|
|
|
|
|
|
where.roomId = roomId;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status) {
|
|
|
|
|
|
where.status = status;
|
|
|
|
|
|
}
|
2026-03-05 16:05:51 +00:00
|
|
|
|
if (startDateFrom && endDateTo) {
|
|
|
|
|
|
where.startDate = { [Op.gte]: new Date(startDateFrom) };
|
|
|
|
|
|
where.endDate = { [Op.lte]: new Date(endDateTo) };
|
2026-03-02 12:36:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-05 16:05:51 +00:00
|
|
|
|
const include = [
|
|
|
|
|
|
{
|
|
|
|
|
|
model: Room,
|
|
|
|
|
|
where: {
|
|
|
|
|
|
isDeleted: 0,
|
|
|
|
|
|
...(apartmentId ? { apartmentId } : {})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
];
|
|
|
|
|
|
|
2026-03-02 12:36:41 +00:00
|
|
|
|
const offset = (page - 1) * pageSize;
|
|
|
|
|
|
|
|
|
|
|
|
const { count, rows } = await WaterBill.findAndCountAll({
|
|
|
|
|
|
where,
|
2026-03-05 16:05:51 +00:00
|
|
|
|
include,
|
2026-03-02 12:36:41 +00:00
|
|
|
|
limit: parseInt(pageSize),
|
2026-03-04 07:09:52 +00:00
|
|
|
|
offset: parseInt(offset),
|
2026-03-07 07:23:18 +00:00
|
|
|
|
order: [['createTime', 'DESC']]
|
2026-03-02 12:36:41 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const formattedBills = rows.map(formatWaterBillData);
|
|
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
|
data: formattedBills,
|
|
|
|
|
|
total: count,
|
|
|
|
|
|
page: parseInt(page),
|
|
|
|
|
|
pageSize: parseInt(pageSize)
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取单个水费记录
|
|
|
|
|
|
const getWaterBillById = async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { id } = req.params;
|
2026-03-03 15:36:48 +00:00
|
|
|
|
const bill = await WaterBill.findOne({
|
|
|
|
|
|
where: { id, isDeleted: 0 },
|
|
|
|
|
|
include: [
|
|
|
|
|
|
{
|
|
|
|
|
|
model: Room,
|
|
|
|
|
|
where: { isDeleted: 0 }
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
2026-03-02 12:36:41 +00:00
|
|
|
|
});
|
|
|
|
|
|
if (!bill) {
|
|
|
|
|
|
return res.status(404).json({ error: '水费记录不存在' });
|
|
|
|
|
|
}
|
|
|
|
|
|
const formattedBill = formatWaterBillData(bill);
|
|
|
|
|
|
res.status(200).json(formattedBill);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 创建水费记录
|
|
|
|
|
|
const createWaterBill = async (req, res) => {
|
|
|
|
|
|
try {
|
2026-03-07 07:23:18 +00:00
|
|
|
|
const { roomId, startDate, endDate, startReading, endReading, unitPrice, status } = req.body;
|
2026-03-02 12:36:41 +00:00
|
|
|
|
|
2026-03-07 07:23:18 +00:00
|
|
|
|
let usage = null;
|
|
|
|
|
|
let amount = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (startReading && endReading && unitPrice) {
|
|
|
|
|
|
usage = parseFloat(endReading) - parseFloat(startReading);
|
|
|
|
|
|
amount = usage * parseFloat(unitPrice);
|
|
|
|
|
|
}
|
2026-03-02 12:36:41 +00:00
|
|
|
|
|
|
|
|
|
|
const bill = await WaterBill.create({
|
|
|
|
|
|
roomId,
|
|
|
|
|
|
startDate,
|
|
|
|
|
|
endDate,
|
|
|
|
|
|
startReading,
|
|
|
|
|
|
endReading,
|
|
|
|
|
|
usage,
|
|
|
|
|
|
unitPrice,
|
2026-03-07 07:23:18 +00:00
|
|
|
|
amount,
|
|
|
|
|
|
status: status || 'unpaid'
|
2026-03-02 12:36:41 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const formattedBill = formatWaterBillData(bill);
|
|
|
|
|
|
res.status(201).json(formattedBill);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 更新水费记录
|
|
|
|
|
|
const updateWaterBill = async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
|
const { startDate, endDate, startReading, endReading, unitPrice, status } = req.body;
|
|
|
|
|
|
|
2026-03-03 15:36:48 +00:00
|
|
|
|
const bill = await WaterBill.findOne({
|
|
|
|
|
|
where: { id, isDeleted: 0 }
|
|
|
|
|
|
});
|
2026-03-02 12:36:41 +00:00
|
|
|
|
if (!bill) {
|
|
|
|
|
|
return res.status(404).json({ error: '水费记录不存在' });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let usage = bill.usage;
|
|
|
|
|
|
let amount = bill.amount;
|
|
|
|
|
|
if (startReading && endReading && unitPrice) {
|
|
|
|
|
|
usage = parseFloat(endReading) - parseFloat(startReading);
|
|
|
|
|
|
amount = usage * parseFloat(unitPrice);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await bill.update({
|
|
|
|
|
|
startDate,
|
|
|
|
|
|
endDate,
|
|
|
|
|
|
startReading,
|
|
|
|
|
|
endReading,
|
|
|
|
|
|
usage,
|
|
|
|
|
|
unitPrice,
|
|
|
|
|
|
amount,
|
2026-03-07 07:23:18 +00:00
|
|
|
|
status: status !== undefined ? status : bill.status
|
2026-03-02 12:36:41 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const formattedBill = formatWaterBillData(bill);
|
|
|
|
|
|
res.status(200).json(formattedBill);
|
|
|
|
|
|
} 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 deleteWaterBill = async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { id } = req.params;
|
2026-03-03 15:36:48 +00:00
|
|
|
|
const bill = await WaterBill.findOne({
|
|
|
|
|
|
where: { id, isDeleted: 0 }
|
|
|
|
|
|
});
|
2026-03-02 12:36:41 +00:00
|
|
|
|
if (!bill) {
|
|
|
|
|
|
return res.status(404).json({ error: '水费记录不存在' });
|
|
|
|
|
|
}
|
2026-03-03 15:36:48 +00:00
|
|
|
|
await bill.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 listWaterBills = async (req, res) => {
|
|
|
|
|
|
try {
|
2026-03-05 16:05:51 +00:00
|
|
|
|
const {
|
|
|
|
|
|
apartmentId,
|
|
|
|
|
|
roomId,
|
|
|
|
|
|
status,
|
|
|
|
|
|
startDateFrom,
|
|
|
|
|
|
endDateTo
|
|
|
|
|
|
} = req.query;
|
2026-03-04 07:39:06 +00:00
|
|
|
|
|
|
|
|
|
|
const where = { isDeleted: 0 };
|
|
|
|
|
|
if (roomId) {
|
|
|
|
|
|
where.roomId = roomId;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status) {
|
|
|
|
|
|
where.status = status;
|
|
|
|
|
|
}
|
2026-03-05 16:05:51 +00:00
|
|
|
|
if (startDateFrom && endDateTo) {
|
|
|
|
|
|
where.startDate = { [Op.gte]: new Date(startDateFrom) };
|
|
|
|
|
|
where.endDate = { [Op.lte]: new Date(endDateTo) };
|
2026-03-04 07:39:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-05 16:05:51 +00:00
|
|
|
|
const include = [
|
|
|
|
|
|
{
|
|
|
|
|
|
model: Room,
|
|
|
|
|
|
where: {
|
|
|
|
|
|
isDeleted: 0,
|
|
|
|
|
|
...(apartmentId ? { apartmentId } : {})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
];
|
|
|
|
|
|
|
2026-03-04 07:39:06 +00:00
|
|
|
|
const bills = await WaterBill.findAll({
|
|
|
|
|
|
where,
|
2026-03-05 16:05:51 +00:00
|
|
|
|
include,
|
2026-03-07 07:23:18 +00:00
|
|
|
|
order: [['createTime', 'DESC']]
|
2026-03-04 07:39:06 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const formattedBills = bills.map(formatWaterBillData);
|
|
|
|
|
|
|
|
|
|
|
|
res.status(200).json(formattedBills);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
res.status(500).json({ error: error.message });
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-02 12:36:41 +00:00
|
|
|
|
module.exports = {
|
|
|
|
|
|
getAllWaterBills,
|
2026-03-04 07:39:06 +00:00
|
|
|
|
listWaterBills,
|
2026-03-02 12:36:41 +00:00
|
|
|
|
getWaterBillById,
|
|
|
|
|
|
createWaterBill,
|
|
|
|
|
|
updateWaterBill,
|
|
|
|
|
|
deleteWaterBill
|
|
|
|
|
|
};
|