56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
const express = require('express');
|
|
const db = require('../db');
|
|
const { authenticate, requireAdmin } = require('../middleware/auth');
|
|
|
|
const router = express.Router();
|
|
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const { customer_id } = req.query;
|
|
let query = `
|
|
SELECT b.*,
|
|
c.name as customer_name,
|
|
u.name as manager_name
|
|
FROM budget_projects b
|
|
LEFT JOIN customers c ON b.customer_id = c.id
|
|
LEFT JOIN users u ON b.project_manager_id = u.id
|
|
`;
|
|
|
|
const params = [];
|
|
if (customer_id) {
|
|
query += ` WHERE b.customer_id = $1`;
|
|
params.push(customer_id);
|
|
}
|
|
|
|
query += ` ORDER BY b.created_at DESC`;
|
|
|
|
const result = await db.query(query, params);
|
|
|
|
const projects = result.rows.map(project => {
|
|
try {
|
|
return {
|
|
...project,
|
|
attachments: project.attachments ? JSON.parse(project.attachments) : [],
|
|
survey_photos: project.survey_photos ? JSON.parse(project.survey_photos) : [],
|
|
quotations: []
|
|
};
|
|
} catch (error) {
|
|
console.error('解析项目数据失败:', error);
|
|
return {
|
|
...project,
|
|
attachments: [],
|
|
survey_photos: [],
|
|
quotations: []
|
|
};
|
|
}
|
|
});
|
|
|
|
res.json({ success: true, data: projects, count: projects.length });
|
|
} catch (error) {
|
|
console.error('获取预算项目失败:', error);
|
|
res.status(500).json({ success: false, message: error.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|