水费修改
This commit is contained in:
parent
2e8bddab4e
commit
b7a7549aa0
|
|
@ -2,10 +2,16 @@ const { Sequelize } = require('sequelize');
|
|||
const mysql = require('mysql2/promise');
|
||||
|
||||
// 环境配置
|
||||
const NODE_ENV = 'production';
|
||||
const NODE_ENV = process.env.NODE_ENV || 'local';
|
||||
|
||||
// 数据库配置
|
||||
const dbConfig = {
|
||||
local: {
|
||||
host: 'localhost',
|
||||
user: 'root',
|
||||
password: 'root',
|
||||
database: 'rentease'
|
||||
},
|
||||
development: {
|
||||
host: 'localhost',
|
||||
user: 'root',
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ const checkAndUpdateRentalStatus = async () => {
|
|||
const currentDate = new Date();
|
||||
// 计算10天后的日期
|
||||
const tenDaysLater = new Date();
|
||||
tenDaysLater.setDate(currentDate.getDate() + 30);
|
||||
tenDaysLater.setDate(currentDate.getDate() + 7);
|
||||
|
||||
// 查找所有活跃的租房记录
|
||||
const rentals = await Rental.findAll({
|
||||
|
|
@ -80,17 +80,17 @@ const getAllRentals = async (req, res) => {
|
|||
// 先检查并更新租房状态
|
||||
await checkAndUpdateRentalStatus();
|
||||
|
||||
const {
|
||||
apartmentId,
|
||||
roomId,
|
||||
tenantName,
|
||||
status,
|
||||
startDateFrom,
|
||||
startDateTo,
|
||||
endDateFrom,
|
||||
endDateTo,
|
||||
page = 1,
|
||||
pageSize = 10
|
||||
const {
|
||||
apartmentId,
|
||||
roomId,
|
||||
tenantName,
|
||||
status,
|
||||
startDateFrom,
|
||||
startDateTo,
|
||||
endDateFrom,
|
||||
endDateTo,
|
||||
page = 1,
|
||||
pageSize = 10
|
||||
} = req.query;
|
||||
|
||||
// 构建查询条件
|
||||
|
|
@ -115,7 +115,7 @@ const getAllRentals = async (req, res) => {
|
|||
const include = [
|
||||
{
|
||||
model: Room,
|
||||
where: {
|
||||
where: {
|
||||
isDeleted: 0,
|
||||
...(apartmentId ? { apartmentId } : {})
|
||||
},
|
||||
|
|
@ -311,15 +311,15 @@ const listRentals = async (req, res) => {
|
|||
// 先检查并更新租房状态
|
||||
await checkAndUpdateRentalStatus();
|
||||
|
||||
const {
|
||||
apartmentId,
|
||||
roomId,
|
||||
tenantName,
|
||||
status,
|
||||
startDateFrom,
|
||||
startDateTo,
|
||||
endDateFrom,
|
||||
endDateTo
|
||||
const {
|
||||
apartmentId,
|
||||
roomId,
|
||||
tenantName,
|
||||
status,
|
||||
startDateFrom,
|
||||
startDateTo,
|
||||
endDateFrom,
|
||||
endDateTo
|
||||
} = req.query;
|
||||
|
||||
// 构建查询条件
|
||||
|
|
@ -344,7 +344,7 @@ const listRentals = async (req, res) => {
|
|||
const include = [
|
||||
{
|
||||
model: Room,
|
||||
where: {
|
||||
where: {
|
||||
isDeleted: 0,
|
||||
...(apartmentId ? { apartmentId } : {})
|
||||
},
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ const formatRoomData = (room) => {
|
|||
createTime: formatDate(room.createTime),
|
||||
updateTime: formatDate(room.updateTime)
|
||||
};
|
||||
|
||||
|
||||
// 格式化关联数据
|
||||
if (formattedRoom.Apartment) {
|
||||
formattedRoom.Apartment = {
|
||||
|
|
@ -25,7 +25,7 @@ const formatRoomData = (room) => {
|
|||
updateTime: formatDate(formattedRoom.Apartment.updateTime)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// 格式化租房信息
|
||||
if (formattedRoom.Rentals && formattedRoom.Rentals.length > 0) {
|
||||
formattedRoom.Rentals = formattedRoom.Rentals.map(rental => {
|
||||
|
|
@ -36,11 +36,11 @@ const formatRoomData = (room) => {
|
|||
createTime: formatDate(rental.createTime),
|
||||
updateTime: formatDate(rental.updateTime)
|
||||
};
|
||||
|
||||
|
||||
return formattedRental;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return formattedRoom;
|
||||
};
|
||||
|
||||
|
|
@ -51,17 +51,17 @@ const checkAndUpdateRentalStatus = async () => {
|
|||
const currentDate = new Date();
|
||||
// 计算10天后的日期
|
||||
const tenDaysLater = new Date();
|
||||
tenDaysLater.setDate(currentDate.getDate() + 30);
|
||||
|
||||
tenDaysLater.setDate(currentDate.getDate() + 7);
|
||||
|
||||
// 查找所有在租的租房记录
|
||||
const rentals = await Rental.findAll({
|
||||
where: { status: 'active', isDeleted: 0 }
|
||||
});
|
||||
|
||||
|
||||
// 检查每个租房记录的状态
|
||||
for (const rental of rentals) {
|
||||
const endDate = new Date(rental.endDate);
|
||||
|
||||
|
||||
// 检查是否已到期
|
||||
if (endDate < currentDate) {
|
||||
// 更新房间附属状态为已到期
|
||||
|
|
@ -83,7 +83,7 @@ const checkAndUpdateRentalStatus = async () => {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
console.log('租房状态检查和更新完成');
|
||||
} catch (error) {
|
||||
console.error('检查和更新租房状态时出错:', error);
|
||||
|
|
@ -95,9 +95,9 @@ const getAllRooms = async (req, res) => {
|
|||
try {
|
||||
// 先检查并更新租房状态
|
||||
await checkAndUpdateRentalStatus();
|
||||
|
||||
|
||||
const { apartmentId, roomNumber, status, otherStatus, subStatus, page = 1, pageSize = 10 } = req.query;
|
||||
|
||||
|
||||
// 构建查询条件
|
||||
const where = { isDeleted: 0 };
|
||||
if (apartmentId) {
|
||||
|
|
@ -117,10 +117,10 @@ const getAllRooms = async (req, res) => {
|
|||
if (subStatus) {
|
||||
where.subStatus = subStatus;
|
||||
}
|
||||
|
||||
|
||||
// 计算偏移量
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
|
||||
// 查询房间数据
|
||||
const { count, rows } = await Room.findAndCountAll({
|
||||
where,
|
||||
|
|
@ -130,11 +130,11 @@ const getAllRooms = async (req, res) => {
|
|||
limit: parseInt(pageSize),
|
||||
offset: parseInt(offset)
|
||||
});
|
||||
|
||||
|
||||
// 为每个房间获取非过期的租房信息
|
||||
const formattedRooms = await Promise.all(rows.map(async (room) => {
|
||||
const formattedRoom = formatRoomData(room);
|
||||
|
||||
|
||||
// 查询非过期的租房信息
|
||||
const rentals = await Rental.findAll({
|
||||
where: {
|
||||
|
|
@ -142,7 +142,7 @@ const getAllRooms = async (req, res) => {
|
|||
status: { [Op.ne]: 'expired' }
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// 格式化租房信息
|
||||
if (rentals.length > 0) {
|
||||
formattedRoom.Rentals = rentals.map(rental => {
|
||||
|
|
@ -152,18 +152,18 @@ const getAllRooms = async (req, res) => {
|
|||
endDate: formatDate(rental.endDate),
|
||||
createTime: formatDate(rental.createTime)
|
||||
};
|
||||
|
||||
|
||||
return formattedRental;
|
||||
});
|
||||
} else {
|
||||
formattedRoom.Rentals = [];
|
||||
}
|
||||
|
||||
|
||||
return formattedRoom;
|
||||
}));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 返回结果
|
||||
res.status(200).json({
|
||||
data: formattedRooms,
|
||||
|
|
@ -181,7 +181,7 @@ const getRoomById = async (req, res) => {
|
|||
try {
|
||||
// 先检查并更新租房状态
|
||||
await checkAndUpdateRentalStatus();
|
||||
|
||||
|
||||
const { id } = req.params;
|
||||
const room = await Room.findOne({
|
||||
where: { id, isDeleted: 0 },
|
||||
|
|
@ -190,10 +190,10 @@ const getRoomById = async (req, res) => {
|
|||
if (!room) {
|
||||
return res.status(404).json({ error: '房间不存在' });
|
||||
}
|
||||
|
||||
|
||||
// 格式化房间数据
|
||||
const formattedRoom = formatRoomData(room);
|
||||
|
||||
|
||||
// 查询非过期的租房信息
|
||||
const rentals = await Rental.findAll({
|
||||
where: {
|
||||
|
|
@ -201,7 +201,7 @@ const getRoomById = async (req, res) => {
|
|||
status: { [Op.ne]: 'expired' }
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// 格式化租房信息
|
||||
if (rentals.length > 0) {
|
||||
formattedRoom.Rentals = rentals.map(rental => {
|
||||
|
|
@ -211,7 +211,7 @@ const getRoomById = async (req, res) => {
|
|||
endDate: formatDate(rental.endDate),
|
||||
createTime: formatDate(rental.createTime)
|
||||
};
|
||||
|
||||
|
||||
return formattedRental;
|
||||
});
|
||||
} else {
|
||||
|
|
@ -296,9 +296,9 @@ const listRooms = async (req, res) => {
|
|||
try {
|
||||
// 先检查并更新租房状态
|
||||
await checkAndUpdateRentalStatus();
|
||||
|
||||
|
||||
const { apartmentId, roomNumber, status, otherStatus, subStatus } = req.query;
|
||||
|
||||
|
||||
// 构建查询条件
|
||||
const where = { isDeleted: 0 };
|
||||
if (apartmentId) {
|
||||
|
|
@ -318,7 +318,7 @@ const listRooms = async (req, res) => {
|
|||
if (subStatus) {
|
||||
where.subStatus = subStatus;
|
||||
}
|
||||
|
||||
|
||||
// 查询房间数据
|
||||
const rooms = await Room.findAll({
|
||||
where,
|
||||
|
|
@ -326,10 +326,10 @@ const listRooms = async (req, res) => {
|
|||
Apartment
|
||||
]
|
||||
});
|
||||
|
||||
|
||||
// 格式化房间数据
|
||||
const formattedRooms = rooms.map(formatRoomData);
|
||||
|
||||
|
||||
// 返回结果
|
||||
res.status(200).json(formattedRooms);
|
||||
} catch (error) {
|
||||
|
|
@ -344,4 +344,4 @@ module.exports = {
|
|||
createRoom,
|
||||
updateRoom,
|
||||
deleteRoom
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -44,7 +44,6 @@ const getAllWaterBills = async (req, res) => {
|
|||
pageSize = 10
|
||||
} = req.query;
|
||||
|
||||
// 构建查询条件
|
||||
const where = { isDeleted: 0 };
|
||||
if (roomId) {
|
||||
where.roomId = roomId;
|
||||
|
|
@ -57,7 +56,6 @@ const getAllWaterBills = async (req, res) => {
|
|||
where.endDate = { [Op.lte]: new Date(endDateTo) };
|
||||
}
|
||||
|
||||
// 构建包含关系
|
||||
const include = [
|
||||
{
|
||||
model: Room,
|
||||
|
|
@ -68,22 +66,18 @@ const getAllWaterBills = async (req, res) => {
|
|||
}
|
||||
];
|
||||
|
||||
// 计算偏移量
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
// 查询水费数据
|
||||
const { count, rows } = await WaterBill.findAndCountAll({
|
||||
where,
|
||||
include,
|
||||
limit: parseInt(pageSize),
|
||||
offset: parseInt(offset),
|
||||
order: [['createTime', 'DESC']] // 按创建时间倒序排序
|
||||
order: [['createTime', 'DESC']]
|
||||
});
|
||||
|
||||
// 格式化数据
|
||||
const formattedBills = rows.map(formatWaterBillData);
|
||||
|
||||
// 返回结果
|
||||
res.status(200).json({
|
||||
data: formattedBills,
|
||||
total: count,
|
||||
|
|
@ -121,11 +115,15 @@ const getWaterBillById = async (req, res) => {
|
|||
// 创建水费记录
|
||||
const createWaterBill = async (req, res) => {
|
||||
try {
|
||||
const { roomId, startDate, endDate, startReading, endReading, unitPrice } = req.body;
|
||||
const { roomId, startDate, endDate, startReading, endReading, unitPrice, status } = req.body;
|
||||
|
||||
// 计算用水量和费用
|
||||
const usage = parseFloat(endReading) - parseFloat(startReading);
|
||||
const amount = usage * parseFloat(unitPrice);
|
||||
let usage = null;
|
||||
let amount = null;
|
||||
|
||||
if (startReading && endReading && unitPrice) {
|
||||
usage = parseFloat(endReading) - parseFloat(startReading);
|
||||
amount = usage * parseFloat(unitPrice);
|
||||
}
|
||||
|
||||
const bill = await WaterBill.create({
|
||||
roomId,
|
||||
|
|
@ -135,7 +133,8 @@ const createWaterBill = async (req, res) => {
|
|||
endReading,
|
||||
usage,
|
||||
unitPrice,
|
||||
amount
|
||||
amount,
|
||||
status: status || 'unpaid'
|
||||
});
|
||||
|
||||
const formattedBill = formatWaterBillData(bill);
|
||||
|
|
@ -158,7 +157,6 @@ const updateWaterBill = async (req, res) => {
|
|||
return res.status(404).json({ error: '水费记录不存在' });
|
||||
}
|
||||
|
||||
// 计算用水量和费用
|
||||
let usage = bill.usage;
|
||||
let amount = bill.amount;
|
||||
if (startReading && endReading && unitPrice) {
|
||||
|
|
@ -174,7 +172,7 @@ const updateWaterBill = async (req, res) => {
|
|||
usage,
|
||||
unitPrice,
|
||||
amount,
|
||||
status
|
||||
status: status !== undefined ? status : bill.status
|
||||
});
|
||||
|
||||
const formattedBill = formatWaterBillData(bill);
|
||||
|
|
@ -212,7 +210,6 @@ const listWaterBills = async (req, res) => {
|
|||
endDateTo
|
||||
} = req.query;
|
||||
|
||||
// 构建查询条件
|
||||
const where = { isDeleted: 0 };
|
||||
if (roomId) {
|
||||
where.roomId = roomId;
|
||||
|
|
@ -225,7 +222,6 @@ const listWaterBills = async (req, res) => {
|
|||
where.endDate = { [Op.lte]: new Date(endDateTo) };
|
||||
}
|
||||
|
||||
// 构建包含关系
|
||||
const include = [
|
||||
{
|
||||
model: Room,
|
||||
|
|
@ -236,17 +232,14 @@ const listWaterBills = async (req, res) => {
|
|||
}
|
||||
];
|
||||
|
||||
// 查询水费数据
|
||||
const bills = await WaterBill.findAll({
|
||||
where,
|
||||
include,
|
||||
order: [['createTime', 'DESC']] // 按创建时间倒序排序
|
||||
order: [['createTime', 'DESC']]
|
||||
});
|
||||
|
||||
// 格式化数据
|
||||
const formattedBills = bills.map(formatWaterBillData);
|
||||
|
||||
// 返回结果
|
||||
res.status(200).json(formattedBills);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ const WaterBill = sequelize.define('WaterBill', {
|
|||
},
|
||||
endDate: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false,
|
||||
allowNull: true,
|
||||
comment: '结束日期'
|
||||
},
|
||||
startReading: {
|
||||
|
|
@ -35,12 +35,12 @@ const WaterBill = sequelize.define('WaterBill', {
|
|||
},
|
||||
endReading: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
allowNull: false,
|
||||
allowNull: true,
|
||||
comment: '结束读数'
|
||||
},
|
||||
usage: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
allowNull: false,
|
||||
allowNull: true,
|
||||
comment: '用水量'
|
||||
},
|
||||
unitPrice: {
|
||||
|
|
@ -50,14 +50,14 @@ const WaterBill = sequelize.define('WaterBill', {
|
|||
},
|
||||
amount: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
allowNull: false,
|
||||
allowNull: true,
|
||||
comment: '金额'
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.ENUM('unpaid', 'paid'),
|
||||
type: DataTypes.ENUM('unbilled', 'unpaid', 'paid'),
|
||||
allowNull: false,
|
||||
defaultValue: 'unpaid',
|
||||
comment: '状态(unpaid:未支付,paid:已支付)'
|
||||
comment: '状态(unbilled:未出账,unpaid:未支付,paid:已支付)'
|
||||
},
|
||||
createTime: {
|
||||
type: DataTypes.DATE,
|
||||
|
|
|
|||
10
sync-db.js
10
sync-db.js
|
|
@ -22,7 +22,7 @@ const WaterBill = sequelize.define('WaterBill', {
|
|||
},
|
||||
endDate: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false
|
||||
allowNull: true
|
||||
},
|
||||
startReading: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
|
|
@ -30,11 +30,11 @@ const WaterBill = sequelize.define('WaterBill', {
|
|||
},
|
||||
endReading: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
allowNull: false
|
||||
allowNull: true
|
||||
},
|
||||
usage: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
allowNull: false
|
||||
allowNull: true
|
||||
},
|
||||
unitPrice: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
|
|
@ -42,10 +42,10 @@ const WaterBill = sequelize.define('WaterBill', {
|
|||
},
|
||||
amount: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
allowNull: false
|
||||
allowNull: true
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.ENUM('unpaid', 'paid'),
|
||||
type: DataTypes.ENUM('unbilled', 'unpaid', 'paid'),
|
||||
allowNull: false,
|
||||
defaultValue: 'unpaid'
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue