备份:大改造前的完整版本 - 修复合同细节/付款节点/文件上传/施工管理/项目保存等BUG

This commit is contained in:
root
2026-05-15 12:02:24 +08:00
parent 1cad1e438d
commit fad28741bd
6157 changed files with 5147 additions and 877912 deletions
+55 -52
View File
@@ -1,10 +1,10 @@
const express = require('express');
const db = require('../db');
const { authenticate, requireAdmin } = require('../middleware/auth');
const LedgerService = require('../services/ledgerService');
const router = express.Router();
const express = require('express');
const db = require('../db');
const { authenticate, requireAdmin } = require('../middleware/auth');
const LedgerService = require('../services/ledgerService');
const router = express.Router();
router.get('/', async (req, res) => {
try {
const result = await db.query(`
@@ -26,22 +26,22 @@ router.get('/', async (req, res) => {
name: contact.name || '未命名',
position: contact.position || '',
phone: contact.phone || '',
is_primary: contact.is_primary === 1
is_primary: contact.is_primary === true
}));
// 获取收款信息
const paymentInfosResult = await db.query(
`SELECT * FROM supplier_payment_infos WHERE supplier_id = $1 ORDER BY is_default DESC`,
`SELECT * FROM customer_payment_infos WHERE customer_id = $1 ORDER BY is_primary DESC`,
[customer.id]
);
const paymentInfos = paymentInfosResult.rows.map(payment => ({
id: payment.id,
account_name: payment.account_name,
bank_account: payment.account_number,
bank_account: payment.bank_account,
bank_name: payment.bank_name,
qr_code: payment.qr_code,
is_primary: payment.is_default === 1
is_primary: payment.is_primary === true
}));
return {
@@ -62,11 +62,11 @@ router.get('/', async (req, res) => {
res.status(500).json({
success: false,
message: '获取客户失败',
error: error.message
error: process.env.NODE_ENV === 'development' ? error.message : '操作失败'
});
}
});
router.get('/:id', async (req, res) => {
try {
const { id } = req.params;
@@ -74,7 +74,7 @@ router.get('/:id', async (req, res) => {
// 获取客户基本信息
const customerResult = await db.query(`
SELECT * FROM customers
WHERE id = ?
WHERE id = $1
`, [id]);
if (customerResult.rows.length > 0) {
@@ -83,7 +83,7 @@ router.get('/:id', async (req, res) => {
// 获取客户的所有联系人
const contactsResult = await db.query(`
SELECT * FROM contacts
WHERE entity_id = ? AND entity_type = 'customer'
WHERE entity_id = $1 AND entity_type = 'customer'
ORDER BY is_primary DESC
`, [id]);
@@ -92,14 +92,14 @@ router.get('/:id', async (req, res) => {
name: contact.name || '未命名',
position: contact.position || '',
phone: contact.phone || '',
is_primary: contact.is_primary === 1
is_primary: contact.is_primary === true
}));
// 获取客户的所有收款信息
const paymentInfosResult = await db.query(`
SELECT * FROM supplier_payment_infos
WHERE supplier_id = ?
ORDER BY is_default DESC
SELECT * FROM customer_payment_infos
WHERE customer_id = $1
ORDER BY is_primary DESC
`, [id]);
// 转换收款信息数据结构
@@ -107,9 +107,9 @@ router.get('/:id', async (req, res) => {
id: info.id,
account_name: info.account_name || '',
bank_name: info.bank_name || '',
bank_account: info.account_number || '',
bank_account: info.bank_account || '',
qr_code: info.qr_code || '',
is_primary: info.is_default === 1
is_primary: info.is_primary === true
}));
const ledger = await LedgerService.getCustomerLedger(id);
@@ -144,26 +144,26 @@ router.get('/:id', async (req, res) => {
res.status(500).json({
success: false,
message: '获取客户详情失败',
error: error.message
error: process.env.NODE_ENV === 'development' ? error.message : '操作失败'
});
}
});
router.post('/', async (req, res) => {
try {
const { name, address, remark, contacts, payment_infos } = req.body;
// 从contacts中获取主联系人信息
const primaryContact = contacts?.find(c => c.is_primary) || contacts?.[0];
const contact = primaryContact?.name || '';
const position = primaryContact?.position || '';
const contact_person = primaryContact?.name || '';
const phone = primaryContact?.phone || '';
const email = ''; // 前端没有email字段
const result = await db.query(
`INSERT INTO customers (name, address, contact, position, phone, email, remark, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`,
[name, address, contact, position, phone, email, remark]
`INSERT INTO customers (name, address, remark, contact_person, phone, email, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
RETURNING id`,
[name, address, remark || '', contact_person, phone, email]
);
const customerId = (result.rows[0]?.id || result.rows?.[0]?.id);
@@ -173,8 +173,9 @@ router.post('/', async (req, res) => {
for (const contactItem of contacts) {
await db.query(
`INSERT INTO contacts (entity_id, entity_type, name, position, phone, is_primary, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`,
[customerId, 'customer', contactItem.name, contactItem.position, contactItem.phone, contactItem.is_primary ? 1 : 0]
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
RETURNING id`,
[customerId, 'customer', contactItem.name, contactItem.position, contactItem.phone, contactItem.is_primary ? true : false]
);
}
}
@@ -183,9 +184,10 @@ router.post('/', async (req, res) => {
if (payment_infos && payment_infos.length > 0) {
for (const paymentItem of payment_infos) {
await db.query(
`INSERT INTO supplier_payment_infos (supplier_id, account_name, bank_name, bank_account, qr_code, is_primary, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`,
[customerId, paymentItem.account_name, paymentItem.bank_name, paymentItem.bank_account, paymentItem.qr_code, paymentItem.is_primary ? 1 : 0]
`INSERT INTO customer_payment_infos (customer_id, account_name, bank_name, bank_account, qr_code, is_primary, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
RETURNING id`,
[customerId, paymentItem.account_name, paymentItem.bank_name, paymentItem.bank_account, paymentItem.qr_code, paymentItem.is_primary ? true : false]
);
}
}
@@ -212,11 +214,11 @@ router.post('/', async (req, res) => {
res.status(500).json({
success: false,
message: '创建客户失败',
error: error.message
error: process.env.NODE_ENV === 'development' ? error.message : '操作失败'
});
}
});
router.put('/:id', async (req, res) => {
try {
const { id } = req.params;
@@ -224,16 +226,15 @@ router.put('/:id', async (req, res) => {
// 从contacts中获取主联系人信息
const primaryContact = contacts?.find(c => c.is_primary) || contacts?.[0];
const contact = primaryContact?.name || '';
const position = primaryContact?.position || '';
const contact_person = primaryContact?.name || '';
const phone = primaryContact?.phone || '';
const email = ''; // 前端没有email字段
await db.query(
`UPDATE customers
SET name = ?, address = ?, contact = ?, position = ?, phone = ?, email = ?, remark = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?`,
[name, address, contact, position, phone, email, remark, id]
SET name = $1, address = $2, remark = $3, contact_person = $4, phone = $5, email = $6, updated_at = CURRENT_TIMESTAMP
WHERE id = $7`,
[name, address, remark || '', contact_person, phone, email, id]
);
// 删除旧的联系人数据
@@ -244,22 +245,24 @@ router.put('/:id', async (req, res) => {
for (const contactItem of contacts) {
await db.query(
`INSERT INTO contacts (entity_id, entity_type, name, position, phone, is_primary, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`,
[id, 'customer', contactItem.name, contactItem.position, contactItem.phone, contactItem.is_primary ? 1 : 0]
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
RETURNING id`,
[id, 'customer', contactItem.name, contactItem.position, contactItem.phone, contactItem.is_primary ? true : false]
);
}
}
// 删除旧的收款信息数据
await db.query(`DELETE FROM supplier_payment_infos WHERE supplier_id = $1`, [id]);
await db.query(`DELETE FROM customer_payment_infos WHERE customer_id = $1`, [id]);
// 插入新的收款信息数据
if (payment_infos && payment_infos.length > 0) {
for (const paymentItem of payment_infos) {
await db.query(
`INSERT INTO supplier_payment_infos (supplier_id, account_name, bank_name, bank_account, qr_code, is_primary, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`,
[id, paymentItem.account_name, paymentItem.bank_name, paymentItem.bank_account, paymentItem.qr_code, paymentItem.is_primary ? 1 : 0]
`INSERT INTO customer_payment_infos (customer_id, account_name, bank_name, bank_account, qr_code, is_primary, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
RETURNING id`,
[id, paymentItem.account_name, paymentItem.bank_name, paymentItem.bank_account, paymentItem.qr_code, paymentItem.is_primary ? true : false]
);
}
}
@@ -286,11 +289,11 @@ router.put('/:id', async (req, res) => {
res.status(500).json({
success: false,
message: '更新客户失败',
error: error.message
error: process.env.NODE_ENV === 'development' ? error.message : '操作失败'
});
}
});
router.delete('/:id', async (req, res) => {
try {
const { id } = req.params;
@@ -301,7 +304,7 @@ router.delete('/:id', async (req, res) => {
// 再删除客户数据
const result = await db.query(`DELETE FROM customers WHERE id = $1`, [id]);
if (result.changes > 0) {
if (result.rowCount > 0) {
res.json({
success: true,
message: '客户删除成功'
@@ -317,10 +320,10 @@ router.delete('/:id', async (req, res) => {
res.status(500).json({
success: false,
message: '删除客户失败',
error: error.message
error: process.env.NODE_ENV === 'development' ? error.message : '操作失败'
});
}
});
module.exports = router;