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.
 
 

217 lines
6.8 KiB

{
"properties" : { },
"id" : "9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4",
"script" : null,
"groupId" : "f8e3d2c1b0a94e5f8a7b6c5d4e3f2a1",
"name" : "地图迁徙数据",
"createTime" : 1780882000000,
"updateTime" : 1781360000000,
"lock" : null,
"createBy" : "admin",
"updateBy" : "admin",
"path" : "/map-trading-network",
"method" : "GET",
"parameters" : [ ],
"options" : [ ],
"requestBody" : "",
"headers" : [ ],
"paths" : [ ],
"responseBody" : null,
"description" : "中央地图迁徙数据:销售网络/源地供应/红原本地出栏,数据源 yak_sn_order + yak_sn_customer(只读)。枢纽名称需与系统配置 mapHub.name 一致。",
"requestBodyDefinition" : null,
"responseBodyDefinition" : null
}
================================
// 只读统计已完成订单;hubName 需与系统配置 mapHub.name 保持一致
var hubName = '红原县'
var getRowText = (row, camelKey, snakeKey) => {
if (row[camelKey]) {
return '' + row[camelKey]
}
if (row[snakeKey]) {
return '' + row[snakeKey]
}
return ''
}
var getRowNumber = (row, key) => {
var v = row[key]
if (v == null || v === '') {
return 0
}
return v
}
var normalizeCityKey = (name) => {
var text = '' + (name == null ? '' : name)
if (text.trim() == '') {
return '未知'
}
if (text.indexOf('北京') >= 0) { return '北京' }
if (text.indexOf('上海') >= 0) { return '上海' }
if (text.indexOf('天津') >= 0) { return '天津' }
if (text.indexOf('重庆') >= 0) { return '重庆' }
if (text.indexOf('成都') >= 0) { return '成都' }
if (text.indexOf('拉萨') >= 0) { return '拉萨' }
if (text.indexOf('西宁') >= 0) { return '西宁' }
if (text.indexOf('兰州') >= 0) { return '兰州' }
if (text.indexOf('西安') >= 0) { return '西安' }
if (text.indexOf('昆明') >= 0) { return '昆明' }
if (text.indexOf('贵阳') >= 0) { return '贵阳' }
if (text.indexOf('广州') >= 0) { return '广州' }
if (text.indexOf('深圳') >= 0) { return '深圳' }
if (text.indexOf('康定') >= 0) { return '康定' }
if (text.indexOf('香格里拉') >= 0) { return '香格里拉' }
if (text.indexOf('合作') >= 0) { return '合作' }
if (text.indexOf('甘南') >= 0) { return '甘南藏族自治州' }
if (text.indexOf('玉树') >= 0) { return '玉树' }
if (text.indexOf('甘孜') >= 0) { return '甘孜藏族自治州' }
if (text.indexOf('阿坝') >= 0) { return '阿坝县' }
if (text.indexOf('马尔康') >= 0) { return '马尔康' }
if (text.indexOf('理塘') >= 0) { return '理塘' }
if (text.indexOf('那曲') >= 0) { return '那曲' }
if (text.indexOf('果洛') >= 0) { return '果洛' }
if (text.indexOf('红原') >= 0) { return '红原县' }
return text
}
var buildDirectionalFlows = (rows, direction) => {
var flows = []
var valueMap = {}
var descMap = {}
if (!rows) {
return flows
}
for (row in rows) {
var rawName = getRowText(row, 'placeName', 'place_name')
var value = getRowNumber(row, 'value')
var sample = getRowText(row, 'samplePlace', 'sample_place')
if (sample == '') {
sample = rawName
}
if (value > 0) {
var cityKey = direction == 'local' ? rawName : normalizeCityKey(rawName)
if (cityKey != '未知') {
if (valueMap[cityKey] == null) {
valueMap[cityKey] = value
descMap[cityKey] = sample
if (direction == 'outflow') {
flows.push({
from: hubName,
to: cityKey,
value: value,
description: sample
})
} else {
flows.push({
from: cityKey,
to: hubName,
value: value,
description: sample
})
}
} else {
valueMap[cityKey] = valueMap[cityKey] + value
var descText = '' + descMap[cityKey]
if (descText.indexOf(sample) < 0 && sample != '') {
descMap[cityKey] = descText + ';' + sample
}
for (flow in flows) {
var flowCity = direction == 'outflow' ? flow.to : flow.from
if (flowCity == cityKey) {
flow.value = valueMap[cityKey]
flow.description = descMap[cityKey]
}
}
}
}
}
}
return flows
}
var outflowSql = """
SELECT
COALESCE(NULLIF(TRIM(c.region_name), ''), NULLIF(TRIM(o.destination_place), ''), '未知') AS place_name,
COALESCE(SUM(o.quantity), 0) AS value,
MAX(COALESCE(NULLIF(TRIM(c.region_name), ''), NULLIF(TRIM(o.destination_place), ''), '未知')) AS sample_place
FROM yak_sn_order o
LEFT JOIN yak_sn_customer c ON c.id = o.buyer_id AND c.del_flag = '0'
WHERE o.del_flag = '0'
AND o.status = 'COMPLETED'
GROUP BY COALESCE(NULLIF(TRIM(c.region_name), ''), NULLIF(TRIM(o.destination_place), ''), '未知')
HAVING COALESCE(SUM(o.quantity), 0) > 0
ORDER BY value DESC, place_name
"""
var inflowSql = """
SELECT
COALESCE(NULLIF(TRIM(c.region_name), ''), NULLIF(TRIM(o.origin_place), ''), '未知') AS place_name,
COALESCE(SUM(o.quantity), 0) AS value,
MAX(COALESCE(NULLIF(TRIM(c.region_name), ''), NULLIF(TRIM(o.origin_place), ''), '未知')) AS sample_place
FROM yak_sn_order o
LEFT JOIN yak_sn_customer c ON c.id = o.seller_id AND c.del_flag = '0'
WHERE o.del_flag = '0'
AND o.status = 'COMPLETED'
GROUP BY COALESCE(NULLIF(TRIM(c.region_name), ''), NULLIF(TRIM(o.origin_place), ''), '未知')
HAVING COALESCE(SUM(o.quantity), 0) > 0
ORDER BY value DESC, place_name
"""
var localSql = """
SELECT
COALESCE(
NULLIF((regexp_match(o.origin_place, '([^省市区县]+(?:镇|乡|街道))'))[1], ''),
NULLIF(TRIM(o.origin_place), ''),
'未知'
) AS place_name,
COALESCE(SUM(o.quantity), 0) AS value,
MAX(o.origin_place) AS sample_place
FROM yak_sn_order o
WHERE o.del_flag = '0'
AND o.status = 'COMPLETED'
AND position('红原' in o.origin_place) > 0
GROUP BY COALESCE(
NULLIF((regexp_match(o.origin_place, '([^省市区县]+(?:镇|乡|街道))'))[1], ''),
NULLIF(TRIM(o.origin_place), ''),
'未知'
)
HAVING COALESCE(SUM(o.quantity), 0) > 0
ORDER BY value DESC, place_name
"""
var outflowRows = db.select(outflowSql)
var inflowRows = db.select(inflowSql)
var localRows = db.select(localSql)
var outflowFlows = buildDirectionalFlows(outflowRows, 'outflow')
var inflowFlows = buildDirectionalFlows(inflowRows, 'inflow')
var localFlows = buildDirectionalFlows(localRows, 'local')
return {
tradingModes: {
outflow: {
title: '销售网络分布图',
description: '从' + hubName + '向全国各地输出牦牛的流向分布',
flows: outflowFlows
},
inflow: {
title: '源地供应分布图',
description: '全国各地向' + hubName + '供应牦牛的来源分布',
flows: inflowFlows
},
local: {
title: '红原出栏分布图',
description: hubName + '各乡镇牦牛出栏分布情况',
flows: localFlows
}
}
}