对比图完成
This commit is contained in:
+12
-15
@@ -22,30 +22,27 @@ async function handler(req, res) {
|
||||
const params = [];
|
||||
const conditions = [];
|
||||
|
||||
if (startDate) {
|
||||
conditions.push('date >= ?');
|
||||
params.push(startDate);
|
||||
}
|
||||
|
||||
if (endDate) {
|
||||
conditions.push('date <= ?');
|
||||
params.push(endDate);
|
||||
if (startDate && endDate) {
|
||||
conditions.push('date BETWEEN ? AND ?');
|
||||
params.push(startDate, endDate);
|
||||
}
|
||||
|
||||
if (conditions.length > 0) {
|
||||
query += ' WHERE ' + conditions.join(' AND ');
|
||||
}
|
||||
|
||||
query += ' ORDER BY date ASC';
|
||||
|
||||
if (limit) {
|
||||
query += ' LIMIT ?';
|
||||
params.push(parseInt(limit));
|
||||
}
|
||||
query += ' ORDER BY date DESC';
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
|
||||
const data = result.map(row => {
|
||||
// 限制返回数量,默认为30
|
||||
const limitValue = limit ? parseInt(limit) : 30;
|
||||
const limitedResult = result.slice(0, limitValue);
|
||||
|
||||
// 反转顺序,使日期从早到晚
|
||||
const sortedResult = limitedResult.reverse();
|
||||
|
||||
const data = sortedResult.map(row => {
|
||||
let dateStr;
|
||||
if (row.date instanceof Date) {
|
||||
dateStr = row.date.toISOString().split('T')[0];
|
||||
|
||||
Reference in New Issue
Block a user