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.
 
 

157 lines
3.9 KiB

{
"properties" : { },
"id" : "4d5e6f7a8490b1c2d3e4f5a678902",
"script" : null,
"groupId" : "f8e3d2c1b0a94e5f8a7b6c5d4e3f2a1",
"name" : "综合销售统计",
"createTime" : 1780877100000,
"updateTime" : null,
"lock" : null,
"createBy" : "admin",
"updateBy" : "admin",
"path" : "/comprehensive-sales-stats",
"method" : "GET",
"parameters" : [ {
"name" : "type",
"value" : null,
"description" : "统计类型:monthlySales(本月销售)/overallSalesDistribution(总体销售)/purchaseRegionDistribution(采购地区),不传则返回全部",
"required" : false,
"dataType" : "String",
"type" : null,
"defaultValue" : null,
"validateType" : null,
"error" : null,
"expression" : null,
"children" : null
} ],
"options" : [ ],
"requestBody" : "",
"headers" : [ ],
"paths" : [ ],
"responseBody" : null,
"description" : "综合销售统计:本月销售/总体销售/采购地区三维饼图数据,数据源 yak_sn_order 已完成订单(只读)",
"requestBodyDefinition" : null,
"responseBodyDefinition" : null
}
================================
// 数据源 yak_sn_order + yak_sn_customer
// value = 牦牛交易头数 quantity
var mapRows = (rows) => {
var total = 0
var result = []
for (row in rows) {
total = total + (row.value ? row.value : 0)
}
for (row in rows) {
var value = row.value ? row.value : 0
var percent = 0
if (total > 0) {
percent = (value * 100) / total
}
result.push({
name: row.name,
value: value,
percent: percent
})
}
return result
}
var monthlySql = """
SELECT town AS name, COALESCE(SUM(quantity), 0) AS value
FROM (
SELECT
o.quantity,
COALESCE(
NULLIF((regexp_match(o.origin_place, '([^省市区县]+(?:镇|乡|街道))'))[1], ''),
NULLIF(TRIM(o.origin_place), ''),
'未知地区'
) AS town
FROM yak_sn_order o
CROSS JOIN (
SELECT CASE
WHEN EXISTS (
SELECT 1 FROM yak_sn_order x
WHERE x.del_flag = '0'
AND x.status = 'COMPLETED'
AND x.transaction_time >= date_trunc('month', CURRENT_DATE)
AND x.transaction_time < date_trunc('month', CURRENT_DATE) + INTERVAL '1 month'
) THEN date_trunc('month', CURRENT_DATE)
ELSE date_trunc('month', (
SELECT MAX(transaction_time) FROM yak_sn_order
WHERE del_flag = '0' AND status = 'COMPLETED'
))
END AS month_start
) m
WHERE o.del_flag = '0'
AND o.status = 'COMPLETED'
AND o.transaction_time >= m.month_start
AND o.transaction_time < m.month_start + INTERVAL '1 month'
) t
GROUP BY town
ORDER BY value DESC
LIMIT 5
"""
var overallSql = """
SELECT town AS name, COALESCE(SUM(quantity), 0) AS value
FROM (
SELECT
o.quantity,
COALESCE(
NULLIF((regexp_match(o.origin_place, '([^省市区县]+(?:镇|乡|街道))'))[1], ''),
NULLIF(TRIM(o.origin_place), ''),
'未知地区'
) AS town
FROM yak_sn_order o
WHERE o.del_flag = '0'
AND o.status = 'COMPLETED'
) t
GROUP BY town
ORDER BY value DESC
LIMIT 5
"""
var purchaseSql = """
SELECT region AS name, COALESCE(SUM(quantity), 0) AS value
FROM (
SELECT
o.quantity,
COALESCE(
NULLIF(TRIM(b.region_name), ''),
NULLIF((regexp_match(o.destination_place, '([^省市区县]+(?:市|州|盟|县|区))'))[1], ''),
NULLIF(TRIM(o.destination_place), ''),
'未知地区'
) AS region
FROM yak_sn_order o
LEFT JOIN yak_sn_customer b
ON b.id = o.buyer_id
AND b.del_flag = '0'
WHERE o.del_flag = '0'
AND o.status = 'COMPLETED'
) t
GROUP BY region
ORDER BY value DESC
LIMIT 5
"""
var monthlyRows = db.select(monthlySql)
var overallRows = db.select(overallSql)
var purchaseRows = db.select(purchaseSql)
var result = {
monthlySales: mapRows(monthlyRows),
overallSalesDistribution: mapRows(overallRows),
purchaseRegionDistribution: mapRows(purchaseRows)
}
var t = type
if (t && result[t]) {
return result[t]
}
return result