fix(energy): 氢能站点名补全 tab_import_hydrogen_order,单价改为按量加权
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

站点名 fallback 链新增第三档:
  内部站表 → 外部站表 → tab_import_hydrogen_order(by bill_code) → 「未关联/未知 #ID」
经此关联,原来 140 条「未知站点」补全为 9 个真实站名(洛阳新红山、佛山新城等)

单价之前用 AVG(cost_price) 简单平均,混合价组会算出 34.0334... 这种
意外小数(看起来像被「重新计算」)。改为按量加权效率价:
  ROUND(SUM(cost_expense) / NULLIF(SUM(hydrogen_quantity), 0), 2)
单一价格组自动等于原价,混合价组得到真实付款单价。

Top5 与每日聚合两处 SQL 同步修改。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
kkfluous
2026-04-29 19:46:56 +08:00
parent 0d30ee2df5
commit d0a644cf18

View File

@@ -85,7 +85,9 @@ app.get('/hydrogen/overview', async (c) => {
// Top5 加氢站(本年) // Top5 加氢站(本年)
const [top5Rows] = await pool.query<RowDataPacket[]>( const [top5Rows] = await pool.query<RowDataPacket[]>(
`SELECT b.hydrogen_station_id AS id, `SELECT b.hydrogen_station_id AS id,
COALESCE(s.short_name, s.name, os.fixed_station_name, os.station_name, COALESCE(MAX(s.short_name), MAX(s.name),
MAX(os.fixed_station_name), MAX(os.station_name),
MAX(i.hydrogen_station_name),
CASE WHEN b.hydrogen_station_id IS NULL THEN '未关联站点' CASE WHEN b.hydrogen_station_id IS NULL THEN '未关联站点'
ELSE CONCAT('未知站点 #', b.hydrogen_station_id) END) AS name, ELSE CONCAT('未知站点 #', b.hydrogen_station_id) END) AS name,
SUM(b.hydrogen_quantity) AS kg, SUM(b.hydrogen_quantity) AS kg,
@@ -93,6 +95,7 @@ app.get('/hydrogen/overview', async (c) => {
FROM tab_energy_hydrogen_bill b FROM tab_energy_hydrogen_bill b
LEFT JOIN tab_hydrogen_site s ON s.id = b.hydrogen_station_id LEFT JOIN tab_hydrogen_site s ON s.id = b.hydrogen_station_id
LEFT JOIN tab_outside_hydrogen_site os ON os.inner_site_id = b.hydrogen_station_id LEFT JOIN tab_outside_hydrogen_site os ON os.inner_site_id = b.hydrogen_station_id
LEFT JOIN tab_import_hydrogen_order i ON i.bill_code = b.bill_code
WHERE b.is_deleted = 0 WHERE b.is_deleted = 0
AND b.hydrogen_time >= ? AND b.hydrogen_time >= ?
AND YEAR(${HYDROGEN_LOCAL}) = YEAR(CURDATE()) AND YEAR(${HYDROGEN_LOCAL}) = YEAR(CURDATE())
@@ -161,17 +164,22 @@ app.get('/hydrogen/daily', async (c) => {
].join(' AND '); ].join(' AND ');
// 站点级聚合(每日 × 每站)。前端组装成 day → stations // 站点级聚合(每日 × 每站)。前端组装成 day → stations
// 站点名 fallback 顺序:内部站表 → 外部站表 → 导入订单表tab_import_hydrogen_order.hydrogen_station_name按 bill_code 关联)
// 单价用「按量加权」的实际效率价SUM(费用)/SUM(量),同价组自动等于原价(不再产生意外小数)
const [stationRows] = await pool.query<RowDataPacket[]>( const [stationRows] = await pool.query<RowDataPacket[]>(
`SELECT DATE_FORMAT(${HYDROGEN_LOCAL}, '%Y-%m-%d') AS d, `SELECT DATE_FORMAT(${HYDROGEN_LOCAL}, '%Y-%m-%d') AS d,
b.hydrogen_station_id AS stationId, b.hydrogen_station_id AS stationId,
COALESCE(s.short_name, s.name, os.fixed_station_name, os.station_name, COALESCE(MAX(s.short_name), MAX(s.name),
MAX(os.fixed_station_name), MAX(os.station_name),
MAX(i.hydrogen_station_name),
CASE WHEN b.hydrogen_station_id IS NULL THEN '未关联站点' CASE WHEN b.hydrogen_station_id IS NULL THEN '未关联站点'
ELSE CONCAT('未知站点 #', b.hydrogen_station_id) END) AS stationName, ELSE CONCAT('未知站点 #', b.hydrogen_station_id) END) AS stationName,
SUM(b.hydrogen_quantity) AS kg, ROUND(SUM(b.hydrogen_quantity), 2) AS kg,
AVG(b.cost_price) AS pricePerKg ROUND(SUM(b.cost_expense) / NULLIF(SUM(b.hydrogen_quantity), 0), 2) AS pricePerKg
FROM tab_energy_hydrogen_bill b FROM tab_energy_hydrogen_bill b
LEFT JOIN tab_hydrogen_site s ON s.id = b.hydrogen_station_id LEFT JOIN tab_hydrogen_site s ON s.id = b.hydrogen_station_id
LEFT JOIN tab_outside_hydrogen_site os ON os.inner_site_id = b.hydrogen_station_id LEFT JOIN tab_outside_hydrogen_site os ON os.inner_site_id = b.hydrogen_station_id
LEFT JOIN tab_import_hydrogen_order i ON i.bill_code = b.bill_code
WHERE ${where} WHERE ${where}
GROUP BY d, b.hydrogen_station_id GROUP BY d, b.hydrogen_station_id
ORDER BY d DESC, kg DESC`, ORDER BY d DESC, kg DESC`,