{ "properties" : { }, "id" : "5e6f7a8490b1c2d3e4f5a678903", "script" : null, "groupId" : "f8e3d2c1b0a94e5f8a7b6c5d4e3f2a1", "name" : "活牛鲜肉价格趋势", "createTime" : 1780877200000, "updateTime" : null, "lock" : null, "createBy" : "admin", "updateBy" : "admin", "path" : "/price-trend", "method" : "GET", "parameters" : [ ], "options" : [ ], "requestBody" : "", "headers" : [ ], "paths" : [ ], "responseBody" : null, "description" : "活牛/鲜肉价格趋势:近8个月市场采集均价。数据源 yak_trade_market_price,活牛=LIVE_YAK,鲜肉=FRESH_MEAT,直接使用 price 字段及 unit 单位,不做重量折算。", "requestBodyDefinition" : null, "responseBodyDefinition" : null } ================================ // 只读查询,不修改任何数据 // 数据源:yak_trade_market_price(市场采集价格,已含 price + unit) // price_type: LIVE_YAK=活牛, FRESH_MEAT=鲜肉/牛肉 var monthBucketSql = """ SELECT 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 date_trunc('month', p.price_date)::date AS month_start, ROUND(AVG(p.price)::numeric, 2) AS live_cattle_price FROM yak_trade_market_price p WHERE p.price_type = 'LIVE_YAK' AND p.price_date >= date_trunc('month', CURRENT_DATE) - INTERVAL '7 months' GROUP BY date_trunc('month', p.price_date)::date """ var beefPriceSql = """ SELECT date_trunc('month', p.price_date)::date AS month_start, ROUND(AVG(p.price)::numeric, 2) AS beef_price FROM yak_trade_market_price p WHERE p.price_type = 'FRESH_MEAT' AND p.price_date >= date_trunc('month', CURRENT_DATE) - INTERVAL '7 months' GROUP BY date_trunc('month', p.price_date)::date """ var unitSql = """ SELECT MAX(CASE WHEN price_type = 'LIVE_YAK' THEN unit END) AS live_unit, MAX(CASE WHEN price_type = 'FRESH_MEAT' THEN unit END) AS beef_unit FROM yak_trade_market_price WHERE price_type IN ('LIVE_YAK', 'FRESH_MEAT') """ 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) { liveMap[row.monthStart] = row.liveCattlePrice } for (row in beefRows) { beefMap[row.monthStart] = row.beefPrice } var liveUnit = '元/公斤' var beefUnit = '元/公斤' 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.monthStart 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 }