水费修改

This commit is contained in:
wangxiaoxian 2026-03-07 15:23:18 +08:00
parent 2e8bddab4e
commit b7a7549aa0
6 changed files with 85 additions and 86 deletions

View File

@ -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',

View File

@ -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({

View File

@ -51,7 +51,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({

View File

@ -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 });

View File

@ -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,

View File

@ -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'
},