You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

121 lines
3.0 KiB

{
"properties" : { },
"id" : "5e6f7a8490b1c2d3e4f5a678903",
"script" : null,
"groupId" : "f8e3d2c1b0a94e5f8a7b6c5d4e3f2a1",
"name" : "活牛鲜肉价格趋势",
"createTime" : 1780877200000,
"updateTime" : 1781372000000,
"lock" : null,
"createBy" : "admin",
"updateBy" : "admin",
"path" : "/price-trend",
"method" : "GET",
"parameters" : [ ],
"options" : [ ],
"requestBody" : "",
"headers" : [ ],
"paths" : [ ],
"responseBody" : null,
"description" : "活牛/鲜肉价格趋势:近8个月采集均价。数据源 gov_count_price,活牛=price_key hx,鲜肉=mnr/mnt/mnp 月均,直接使用 value 与 unit,不做重量折算。",
"requestBodyDefinition" : null,
"responseBodyDefinition" : null
}
================================
// gov_count_price 价格指标 price_key:
// hx 活畜(活牛)
// mnr/mnt/mnp 鲜肉(牦牛肉/腿/排)
// 按 year + month 对各区域采集价取 AVG(value);无数据月份返回 null
var monthBucketSql = """
SELECT
EXTRACT(YEAR FROM d.month_start)::int AS year,
EXTRACT(MONTH FROM d.month_start)::int AS month,
d.month_start,
d.offs,
TO_CHAR(d.month_start, 'FMMM') || '月' AS label
FROM (
SELECT
(date_trunc('month', CURRENT_DATE) - (offs || ' months')::interval)::date AS month_start,
offs
FROM generate_series(7, 0, -1) AS offs
) d
ORDER BY d.offs DESC
"""
var livePriceSql = """
SELECT
year,
month,
ROUND(AVG(value)::numeric, 2) AS live_cattle_price
FROM gov_count_price
WHERE price_key = 'hx'
GROUP BY year, month
"""
var beefPriceSql = """
SELECT
year,
month,
ROUND(AVG(value)::numeric, 2) AS beef_price
FROM gov_count_price
WHERE price_key IN ('mnr', 'mnt', 'mnp')
GROUP BY year, month
"""
var unitSql = """
SELECT
MAX(CASE WHEN price_key = 'hx' THEN unit END) AS live_unit,
MAX(CASE WHEN price_key IN ('mnr', 'mnt', 'mnp') THEN unit END) AS beef_unit
FROM gov_count_price
WHERE price_key IN ('hx', 'mnr', 'mnt', 'mnp')
"""
var buckets = db.select(monthBucketSql)
var liveRows = db.select(livePriceSql)
var beefRows = db.select(beefPriceSql)
var unitRows = db.select(unitSql)
var liveMap = {}
var beefMap = {}
for (row in liveRows) {
var key = row.year + '-' + row.month
liveMap[key] = row.liveCattlePrice
}
for (row in beefRows) {
var key = row.year + '-' + row.month
beefMap[key] = row.beefPrice
}
var liveUnit = '元/kg'
var beefUnit = '元/kg'
if (unitRows && unitRows.length > 0) {
if (unitRows[0].liveUnit) {
liveUnit = unitRows[0].liveUnit
}
if (unitRows[0].beefUnit) {
beefUnit = unitRows[0].beefUnit
}
}
var labels = []
var liveCattlePrice = []
var beefPrice = []
for (bucket in buckets) {
var monthKey = bucket.year + '-' + bucket.month
labels.push(bucket.label)
liveCattlePrice.push(liveMap[monthKey] != null ? liveMap[monthKey] : null)
beefPrice.push(beefMap[monthKey] != null ? beefMap[monthKey] : null)
}
return {
labels: labels,
liveCattlePrice: liveCattlePrice,
beefPrice: beefPrice,
unit: liveUnit,
liveUnit: liveUnit,
beefUnit: beefUnit
}