rentease-backend/controllers/electricityBillController.js

197 lines
5.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { ElectricityBill, 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 formatElectricityBillData = (bill) => {
const formattedBill = {
...bill.toJSON(),
startDate: formatDate(bill.startDate),
endDate: formatDate(bill.endDate),
createTime: formatDate(bill.createTime),
updateTime: formatDate(bill.updateTime)
};
// 格式化关联数据
if (formattedBill.Room) {
formattedBill.Room = {
...formattedBill.Room,
createTime: formatDate(formattedBill.Room.createTime),
updateTime: formatDate(formattedBill.Room.updateTime)
};
}
return formattedBill;
};
// 获取所有电费记录(支持搜索和分页)
const getAllElectricityBills = async (req, res) => {
try {
const { roomId, status, startDate, endDate, page = 1, pageSize = 10 } = req.query;
// 构建查询条件
const where = { isDeleted: 0 };
if (roomId) {
where.roomId = roomId;
}
if (status) {
where.status = status;
}
if (startDate) {
where.startDate = { [Op.gte]: new Date(startDate) };
}
if (endDate) {
where.endDate = { [Op.lte]: new Date(endDate) };
}
// 计算偏移量
const offset = (page - 1) * pageSize;
// 查询电费数据
const { count, rows } = await ElectricityBill.findAndCountAll({
where,
include: [
{
model: Room,
where: { isDeleted: 0 }
}
],
limit: parseInt(pageSize),
offset: parseInt(offset)
});
// 格式化数据
const formattedBills = rows.map(formatElectricityBillData);
// 返回结果
res.status(200).json({
data: formattedBills,
total: count,
page: parseInt(page),
pageSize: parseInt(pageSize)
});
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// 获取单个电费记录
const getElectricityBillById = async (req, res) => {
try {
const { id } = req.params;
const bill = await ElectricityBill.findOne({
where: { id, isDeleted: 0 },
include: [
{
model: Room,
where: { isDeleted: 0 }
}
]
});
if (!bill) {
return res.status(404).json({ error: '电费记录不存在' });
}
const formattedBill = formatElectricityBillData(bill);
res.status(200).json(formattedBill);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// 创建电费记录
const createElectricityBill = async (req, res) => {
try {
const { roomId, startDate, endDate, startReading, endReading, unitPrice } = req.body;
// 计算用电量和费用
const usage = parseFloat(endReading) - parseFloat(startReading);
const amount = usage * parseFloat(unitPrice);
const bill = await ElectricityBill.create({
roomId,
startDate,
endDate,
startReading,
endReading,
usage,
unitPrice,
amount
});
const formattedBill = formatElectricityBillData(bill);
res.status(201).json(formattedBill);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// 更新电费记录
const updateElectricityBill = async (req, res) => {
try {
const { id } = req.params;
const { startDate, endDate, startReading, endReading, unitPrice, status } = req.body;
const bill = await ElectricityBill.findOne({
where: { id, isDeleted: 0 }
});
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,
status
});
const formattedBill = formatElectricityBillData(bill);
res.status(200).json(formattedBill);
} catch (error) {
res.status(500).json({ error: error.message });
}
};
// 删除电费记录(软删除)
const deleteElectricityBill = async (req, res) => {
try {
const { id } = req.params;
const bill = await ElectricityBill.findOne({
where: { id, isDeleted: 0 }
});
if (!bill) {
return res.status(404).json({ error: '电费记录不存在' });
}
await bill.update({ isDeleted: 1 });
res.status(200).json({ message: '电费记录删除成功' });
} catch (error) {
res.status(500).json({ error: error.message });
}
};
module.exports = {
getAllElectricityBills,
getElectricityBillById,
createElectricityBill,
updateElectricityBill,
deleteElectricityBill
};