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.
 
 

152 lines
3.7 KiB

{
"properties" : { },
"id" : "4d5e6f7a8490b1c2d3e4f5a678902",
"script" : null,
"groupId" : "f8e3d2c1b0a94e5f8a7b6c5d4e3f2a1",
"name" : "综合销售统计",
"createTime" : 1780877100000,
"updateTime" : 1781368000000,
"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" : "综合销售统计:数据源 gov_count_order_total(只读)。销售按 origin_place,采购地区按 buyer_place,头数 yak_number。",
"requestBodyDefinition" : null,
"responseBodyDefinition" : null
}
================================
// gov_count_order_total
// 本月/总体销售:origin_place + SUM(yak_number)
// 采购地区:buyer_place + SUM(yak_number)(库表字段 buyer_place)
var mapOriginRows = (rows) => {
var total = 0
var result = []
for (row in rows) {
var qty = row.yakNumber != null ? row.yakNumber : 0
total = total + qty
}
for (row in rows) {
var qty = row.yakNumber != null ? row.yakNumber : 0
var percent = 0
if (total > 0) {
percent = (qty * 100) / total
}
result.push({
originPlace: row.originPlace ? row.originPlace : '未知地区',
yakNumber: qty,
percent: percent
})
}
return result
}
var mapBuyerRows = (rows) => {
var total = 0
var result = []
for (row in rows) {
var qty = row.yakNumber != null ? row.yakNumber : 0
total = total + qty
}
for (row in rows) {
var qty = row.yakNumber != null ? row.yakNumber : 0
var percent = 0
if (total > 0) {
percent = (qty * 100) / total
}
result.push({
buyerPlace: row.buyerPlace ? row.buyerPlace : '未知地区',
yakNumber: qty,
percent: percent
})
}
return result
}
var monthlySql = """
WITH anchor AS (
SELECT year, month
FROM gov_count_order_total
WHERE year IS NOT NULL AND month IS NOT NULL
ORDER BY year DESC, month DESC
LIMIT 1
)
SELECT
TRIM(o.origin_place) AS origin_place,
COALESCE(SUM(o.yak_number), 0) AS yak_number
FROM gov_count_order_total o
CROSS JOIN anchor a
WHERE o.year = a.year
AND o.month = a.month
AND o.origin_place IS NOT NULL
AND TRIM(o.origin_place) <> ''
GROUP BY TRIM(o.origin_place)
ORDER BY yak_number DESC, origin_place
LIMIT 5
"""
var overallSql = """
SELECT
TRIM(origin_place) AS origin_place,
COALESCE(SUM(yak_number), 0) AS yak_number
FROM gov_count_order_total
WHERE origin_place IS NOT NULL
AND TRIM(origin_place) <> ''
GROUP BY TRIM(origin_place)
ORDER BY yak_number DESC, origin_place
LIMIT 5
"""
var purchaseSql = """
SELECT
TRIM(buyer_place) AS buyer_place,
COALESCE(SUM(yak_number), 0) AS yak_number
FROM gov_count_order_total
WHERE buyer_place IS NOT NULL
AND TRIM(buyer_place) <> ''
GROUP BY TRIM(buyer_place)
ORDER BY yak_number DESC, buyer_place
LIMIT 5
"""
var monthlyRows = db.select(monthlySql)
var overallRows = db.select(overallSql)
var purchaseRows = db.select(purchaseSql)
var result = {
monthlySales: mapOriginRows(monthlyRows),
overallSalesDistribution: mapOriginRows(overallRows),
purchaseRegionDistribution: mapBuyerRows(purchaseRows)
}
var t = type
if (t && result[t]) {
return result[t]
}
return result