水费修改
This commit is contained in:
parent
2e8bddab4e
commit
b7a7549aa0
|
|
@ -2,10 +2,16 @@ const { Sequelize } = require('sequelize');
|
||||||
const mysql = require('mysql2/promise');
|
const mysql = require('mysql2/promise');
|
||||||
|
|
||||||
// 环境配置
|
// 环境配置
|
||||||
const NODE_ENV = 'production';
|
const NODE_ENV = process.env.NODE_ENV || 'local';
|
||||||
|
|
||||||
// 数据库配置
|
// 数据库配置
|
||||||
const dbConfig = {
|
const dbConfig = {
|
||||||
|
local: {
|
||||||
|
host: 'localhost',
|
||||||
|
user: 'root',
|
||||||
|
password: 'root',
|
||||||
|
database: 'rentease'
|
||||||
|
},
|
||||||
development: {
|
development: {
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
user: 'root',
|
user: 'root',
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ const checkAndUpdateRentalStatus = async () => {
|
||||||
const currentDate = new Date();
|
const currentDate = new Date();
|
||||||
// 计算10天后的日期
|
// 计算10天后的日期
|
||||||
const tenDaysLater = new Date();
|
const tenDaysLater = new Date();
|
||||||
tenDaysLater.setDate(currentDate.getDate() + 30);
|
tenDaysLater.setDate(currentDate.getDate() + 7);
|
||||||
|
|
||||||
// 查找所有活跃的租房记录
|
// 查找所有活跃的租房记录
|
||||||
const rentals = await Rental.findAll({
|
const rentals = await Rental.findAll({
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ const checkAndUpdateRentalStatus = async () => {
|
||||||
const currentDate = new Date();
|
const currentDate = new Date();
|
||||||
// 计算10天后的日期
|
// 计算10天后的日期
|
||||||
const tenDaysLater = new Date();
|
const tenDaysLater = new Date();
|
||||||
tenDaysLater.setDate(currentDate.getDate() + 30);
|
tenDaysLater.setDate(currentDate.getDate() + 7);
|
||||||
|
|
||||||
// 查找所有在租的租房记录
|
// 查找所有在租的租房记录
|
||||||
const rentals = await Rental.findAll({
|
const rentals = await Rental.findAll({
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,6 @@ const getAllWaterBills = async (req, res) => {
|
||||||
pageSize = 10
|
pageSize = 10
|
||||||
} = req.query;
|
} = req.query;
|
||||||
|
|
||||||
// 构建查询条件
|
|
||||||
const where = { isDeleted: 0 };
|
const where = { isDeleted: 0 };
|
||||||
if (roomId) {
|
if (roomId) {
|
||||||
where.roomId = roomId;
|
where.roomId = roomId;
|
||||||
|
|
@ -57,7 +56,6 @@ const getAllWaterBills = async (req, res) => {
|
||||||
where.endDate = { [Op.lte]: new Date(endDateTo) };
|
where.endDate = { [Op.lte]: new Date(endDateTo) };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建包含关系
|
|
||||||
const include = [
|
const include = [
|
||||||
{
|
{
|
||||||
model: Room,
|
model: Room,
|
||||||
|
|
@ -68,22 +66,18 @@ const getAllWaterBills = async (req, res) => {
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
// 计算偏移量
|
|
||||||
const offset = (page - 1) * pageSize;
|
const offset = (page - 1) * pageSize;
|
||||||
|
|
||||||
// 查询水费数据
|
|
||||||
const { count, rows } = await WaterBill.findAndCountAll({
|
const { count, rows } = await WaterBill.findAndCountAll({
|
||||||
where,
|
where,
|
||||||
include,
|
include,
|
||||||
limit: parseInt(pageSize),
|
limit: parseInt(pageSize),
|
||||||
offset: parseInt(offset),
|
offset: parseInt(offset),
|
||||||
order: [['createTime', 'DESC']] // 按创建时间倒序排序
|
order: [['createTime', 'DESC']]
|
||||||
});
|
});
|
||||||
|
|
||||||
// 格式化数据
|
|
||||||
const formattedBills = rows.map(formatWaterBillData);
|
const formattedBills = rows.map(formatWaterBillData);
|
||||||
|
|
||||||
// 返回结果
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
data: formattedBills,
|
data: formattedBills,
|
||||||
total: count,
|
total: count,
|
||||||
|
|
@ -121,11 +115,15 @@ const getWaterBillById = async (req, res) => {
|
||||||
// 创建水费记录
|
// 创建水费记录
|
||||||
const createWaterBill = async (req, res) => {
|
const createWaterBill = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { roomId, startDate, endDate, startReading, endReading, unitPrice } = req.body;
|
const { roomId, startDate, endDate, startReading, endReading, unitPrice, status } = req.body;
|
||||||
|
|
||||||
// 计算用水量和费用
|
let usage = null;
|
||||||
const usage = parseFloat(endReading) - parseFloat(startReading);
|
let amount = null;
|
||||||
const amount = usage * parseFloat(unitPrice);
|
|
||||||
|
if (startReading && endReading && unitPrice) {
|
||||||
|
usage = parseFloat(endReading) - parseFloat(startReading);
|
||||||
|
amount = usage * parseFloat(unitPrice);
|
||||||
|
}
|
||||||
|
|
||||||
const bill = await WaterBill.create({
|
const bill = await WaterBill.create({
|
||||||
roomId,
|
roomId,
|
||||||
|
|
@ -135,7 +133,8 @@ const createWaterBill = async (req, res) => {
|
||||||
endReading,
|
endReading,
|
||||||
usage,
|
usage,
|
||||||
unitPrice,
|
unitPrice,
|
||||||
amount
|
amount,
|
||||||
|
status: status || 'unpaid'
|
||||||
});
|
});
|
||||||
|
|
||||||
const formattedBill = formatWaterBillData(bill);
|
const formattedBill = formatWaterBillData(bill);
|
||||||
|
|
@ -158,7 +157,6 @@ const updateWaterBill = async (req, res) => {
|
||||||
return res.status(404).json({ error: '水费记录不存在' });
|
return res.status(404).json({ error: '水费记录不存在' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算用水量和费用
|
|
||||||
let usage = bill.usage;
|
let usage = bill.usage;
|
||||||
let amount = bill.amount;
|
let amount = bill.amount;
|
||||||
if (startReading && endReading && unitPrice) {
|
if (startReading && endReading && unitPrice) {
|
||||||
|
|
@ -174,7 +172,7 @@ const updateWaterBill = async (req, res) => {
|
||||||
usage,
|
usage,
|
||||||
unitPrice,
|
unitPrice,
|
||||||
amount,
|
amount,
|
||||||
status
|
status: status !== undefined ? status : bill.status
|
||||||
});
|
});
|
||||||
|
|
||||||
const formattedBill = formatWaterBillData(bill);
|
const formattedBill = formatWaterBillData(bill);
|
||||||
|
|
@ -212,7 +210,6 @@ const listWaterBills = async (req, res) => {
|
||||||
endDateTo
|
endDateTo
|
||||||
} = req.query;
|
} = req.query;
|
||||||
|
|
||||||
// 构建查询条件
|
|
||||||
const where = { isDeleted: 0 };
|
const where = { isDeleted: 0 };
|
||||||
if (roomId) {
|
if (roomId) {
|
||||||
where.roomId = roomId;
|
where.roomId = roomId;
|
||||||
|
|
@ -225,7 +222,6 @@ const listWaterBills = async (req, res) => {
|
||||||
where.endDate = { [Op.lte]: new Date(endDateTo) };
|
where.endDate = { [Op.lte]: new Date(endDateTo) };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建包含关系
|
|
||||||
const include = [
|
const include = [
|
||||||
{
|
{
|
||||||
model: Room,
|
model: Room,
|
||||||
|
|
@ -236,17 +232,14 @@ const listWaterBills = async (req, res) => {
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
// 查询水费数据
|
|
||||||
const bills = await WaterBill.findAll({
|
const bills = await WaterBill.findAll({
|
||||||
where,
|
where,
|
||||||
include,
|
include,
|
||||||
order: [['createTime', 'DESC']] // 按创建时间倒序排序
|
order: [['createTime', 'DESC']]
|
||||||
});
|
});
|
||||||
|
|
||||||
// 格式化数据
|
|
||||||
const formattedBills = bills.map(formatWaterBillData);
|
const formattedBills = bills.map(formatWaterBillData);
|
||||||
|
|
||||||
// 返回结果
|
|
||||||
res.status(200).json(formattedBills);
|
res.status(200).json(formattedBills);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ const WaterBill = sequelize.define('WaterBill', {
|
||||||
},
|
},
|
||||||
endDate: {
|
endDate: {
|
||||||
type: DataTypes.DATE,
|
type: DataTypes.DATE,
|
||||||
allowNull: false,
|
allowNull: true,
|
||||||
comment: '结束日期'
|
comment: '结束日期'
|
||||||
},
|
},
|
||||||
startReading: {
|
startReading: {
|
||||||
|
|
@ -35,12 +35,12 @@ const WaterBill = sequelize.define('WaterBill', {
|
||||||
},
|
},
|
||||||
endReading: {
|
endReading: {
|
||||||
type: DataTypes.DECIMAL(10, 2),
|
type: DataTypes.DECIMAL(10, 2),
|
||||||
allowNull: false,
|
allowNull: true,
|
||||||
comment: '结束读数'
|
comment: '结束读数'
|
||||||
},
|
},
|
||||||
usage: {
|
usage: {
|
||||||
type: DataTypes.DECIMAL(10, 2),
|
type: DataTypes.DECIMAL(10, 2),
|
||||||
allowNull: false,
|
allowNull: true,
|
||||||
comment: '用水量'
|
comment: '用水量'
|
||||||
},
|
},
|
||||||
unitPrice: {
|
unitPrice: {
|
||||||
|
|
@ -50,14 +50,14 @@ const WaterBill = sequelize.define('WaterBill', {
|
||||||
},
|
},
|
||||||
amount: {
|
amount: {
|
||||||
type: DataTypes.DECIMAL(10, 2),
|
type: DataTypes.DECIMAL(10, 2),
|
||||||
allowNull: false,
|
allowNull: true,
|
||||||
comment: '金额'
|
comment: '金额'
|
||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
type: DataTypes.ENUM('unpaid', 'paid'),
|
type: DataTypes.ENUM('unbilled', 'unpaid', 'paid'),
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
defaultValue: 'unpaid',
|
defaultValue: 'unpaid',
|
||||||
comment: '状态(unpaid:未支付,paid:已支付)'
|
comment: '状态(unbilled:未出账,unpaid:未支付,paid:已支付)'
|
||||||
},
|
},
|
||||||
createTime: {
|
createTime: {
|
||||||
type: DataTypes.DATE,
|
type: DataTypes.DATE,
|
||||||
|
|
|
||||||
10
sync-db.js
10
sync-db.js
|
|
@ -22,7 +22,7 @@ const WaterBill = sequelize.define('WaterBill', {
|
||||||
},
|
},
|
||||||
endDate: {
|
endDate: {
|
||||||
type: DataTypes.DATE,
|
type: DataTypes.DATE,
|
||||||
allowNull: false
|
allowNull: true
|
||||||
},
|
},
|
||||||
startReading: {
|
startReading: {
|
||||||
type: DataTypes.DECIMAL(10, 2),
|
type: DataTypes.DECIMAL(10, 2),
|
||||||
|
|
@ -30,11 +30,11 @@ const WaterBill = sequelize.define('WaterBill', {
|
||||||
},
|
},
|
||||||
endReading: {
|
endReading: {
|
||||||
type: DataTypes.DECIMAL(10, 2),
|
type: DataTypes.DECIMAL(10, 2),
|
||||||
allowNull: false
|
allowNull: true
|
||||||
},
|
},
|
||||||
usage: {
|
usage: {
|
||||||
type: DataTypes.DECIMAL(10, 2),
|
type: DataTypes.DECIMAL(10, 2),
|
||||||
allowNull: false
|
allowNull: true
|
||||||
},
|
},
|
||||||
unitPrice: {
|
unitPrice: {
|
||||||
type: DataTypes.DECIMAL(10, 2),
|
type: DataTypes.DECIMAL(10, 2),
|
||||||
|
|
@ -42,10 +42,10 @@ const WaterBill = sequelize.define('WaterBill', {
|
||||||
},
|
},
|
||||||
amount: {
|
amount: {
|
||||||
type: DataTypes.DECIMAL(10, 2),
|
type: DataTypes.DECIMAL(10, 2),
|
||||||
allowNull: false
|
allowNull: true
|
||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
type: DataTypes.ENUM('unpaid', 'paid'),
|
type: DataTypes.ENUM('unbilled', 'unpaid', 'paid'),
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
defaultValue: 'unpaid'
|
defaultValue: 'unpaid'
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue